package kubeiaas.iaascore.service;
import kubeiaas.common.bean.*;
import kubeiaas.common.constants.LogInjectionConstants;
import kubeiaas.common.constants.ResponseMsgConstants;
import kubeiaas.common.constants.bean.HostConstants;
import kubeiaas.common.constants.bean.VmConstants;
import kubeiaas.common.enums.config.SpecTypeEnum;
import kubeiaas.common.enums.network.IpTypeEnum;
import kubeiaas.common.enums.vm.VmBootEnum;
import kubeiaas.common.enums.vm.VmInstallationMethodEnum;
import kubeiaas.common.enums.vm.VmOperateEnum;
import kubeiaas.common.enums.vm.VmStatusEnum;
import kubeiaas.iaascore.config.AgentConfig;
import kubeiaas.iaascore.dao.TableStorage;
import kubeiaas.iaascore.exception.BaseException;
import kubeiaas.iaascore.exception.VmException;
import kubeiaas.iaascore.process.*;
import kubeiaas.iaascore.response.PageResponse;
import kubeiaas.iaascore.response.ResponseEnum;
import kubeiaas.iaascore.scheduler.DeviceScheduler;
import kubeiaas.iaascore.utils.LogUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.*;
@Slf4j
@Service
public class VmService {
@Resource
private TableStorage tableStorage;
@Resource
private NetworkProcess networkProcess;
@Resource
private VmProcess vmProcess;
@Resource
private ResourceProcess resourceProcess;
@Resource
private VolumeProcess volumeProcess;
@Resource
private VncProcess vncProcess;
@Resource
private DeviceScheduler deviceScheduler;
/**
* 创建虚拟机
*/
public Vm createVm(
String name,
int cpus,
int memory,
String imageUuid,
int ipSegmentId,
Integer publicIpSegId,
Integer diskSize,
String description,
String hostUUid) throws VmException {
log.info(String.format("createVm info -- name: %s, cpus: %d, memory: %d, imageUuid: %s, ipSegmentId: %d, publicIpSegId: %d, diskSize: %d, description: %s, hostUUid: %s",
name, cpus, memory, imageUuid, ipSegmentId, publicIpSegId, diskSize, description, hostUUid));
/* ---- 1. pre create VM ----
Generate and Set basic info of vm.
(预处理:设置基础参数,保存虚拟机信息)
*/
log.info("STEP 1: create VM in database");
Vm newVm = vmProcess.preCreateVm(name, cpus, memory, imageUuid, diskSize, description, hostUUid);
MDC.put(LogInjectionConstants.UUID_INJECTION,
LogUtils.appendUuidInjection(MDC.get(LogInjectionConstants.UUID_INJECTION), newVm.getUuid()));
/* ---- 2. Image Check ----
Check image available and set diskSize
*/
log.info("STEP 2: Image Check");
newVm = resourceProcess.updateVmImage(newVm);
/* ---- 3. Resource Operator ----
Use Resource Operator to allocate Host and check Resource available
(资源调度:分配宿主机,检查资源合法性)
*/
log.info("STEP 3: Resource Operator");
if (publicIpSegId == null || publicIpSegId <= 0) {
newVm = resourceProcess.createVmOperate(newVm, ipSegmentId, false, -1);
} else {
newVm = resourceProcess.createVmOperate(newVm, ipSegmentId, true, publicIpSegId);
}
/* ---- 4. Network ----
Get mac-info ip-info and bind in DHCP-Controller
(网络信息:分配 mac 与 ip,存储入库,dhcp 绑定)
*/
log.info("STEP 4: Get mac-info ip-info and bind in DHCP-Controller");
List<IpUsed> newIpUsedList = allocateIPs(newVm, ipSegmentId, publicIpSegId);
newVm.setIps(newIpUsedList);
/* ---- 5. Volume ----
Create system volume.
(系统盘:使用 image 创建 system volume)
*/
log.info("STEP 5: Create system volume");
volumeProcess.createVmVolume(newVm);
return newVm;
}
/**
* 删除虚拟机
*/
public String deleteVM(String vmUuid, boolean isForce) throws BaseException, VmException {
log.info(String.format("deleteVM info -- vmUuid: %s, isForce: %s", vmUuid, isForce));
// ----- check if exist -----
log.info(String.format("-> invoke DB -- vmQueryByUuid, vmUuid: %s", vmUuid));
Vm vm = tableStorage.vmQueryByUuid(vmUuid);
log.info("<- invoke DB -- done");
if (vm == null) {
log.error(String.format("ERROR: vm not found! (uuid: %s)", vmUuid));
throw new BaseException("ERR: vm not found! (uuid: " + vmUuid + ")", ResponseEnum.VM_DELETE_ERROR);
}
/* ----- judge status ----
Check the VM status
*/
if (!isForce) {
if (vm.getStatus().equals(VmStatusEnum.ACTIVE)) {
return ResponseMsgConstants.FAILED;
}
}
try {
// -- 0. set status -- Set deleting status
log.info("STEP 0: Set deleting status");
vmProcess.setVmStatus(vmUuid, VmStatusEnum.DELETING);
// -- 1. choose host -- Select the host where the VM to be deleted resides
log.info("STEP 1: Select the host where the VM to be deleted resides");
resourceProcess.selectHostByVmUuid(vmUuid);
// ┌- 2. delete VM -- Delete the VM and then delete other information -┐
log.info("STEP 2: Delete the VM and then delete other information");
vmProcess.deleteVM(vmUuid);
// ├- 3. Delete Volume -- Delete disks, including Linux files and database information
log.info("STEP 3: Delete disks, including Linux files and database information");
volumeProcess.deleteSystemVolume(vmUuid);
// ├- 4. Delete Ip -- Delete Ip information
log.info("STEP 4: Delete Ip information");
networkProcess.deleteIps(vmUuid);
// ├- 5. Delete Device -- Delete Device information (while Step.2 already detached)
log.info("STEP 5: Delete Device information");
log.info(String.format("-> invoke DB -- deviceQueryByVm, vmUuid: %s", vmUuid));
deviceScheduler.deleteDevice(vmUuid);
log.info("<- invoke DB -- done");
// └- 6. delete in database -- Delete VM records from the database -┘
log.info("STEP 6: Delete VM records from the database");
vmProcess.deleteVmInDataBase(vmUuid);
// -- 7. delete vnc -- delete vnc in token.config
log.info("STEP 7: delete vnc in token.config");
vncProcess.deleteVncToken(vmUuid);
} catch (Exception e) {
log.error(String.format("ERROR: delete failed (uuid: %s)!", vmUuid), e);
String log = (e instanceof BaseException) ? ((BaseException) e).getMsg() : "";
throw new VmException(vm,
String.format("err: delete failed (uuid: %s)! %s", vmUuid, log), ResponseEnum.VM_DELETE_ERROR);
}
return ResponseMsgConstants.SUCCESS;
}
/**
* ====== 修改系列 ======
* 支持:CPU、内存
*/
public String modifyVm(String vmUuid, Integer cpus, Integer memory, boolean isReduce) throws BaseException {
log.info(String.format("modifyVm info -- vmUuid: %s, cpus: %d, memory: %d, isReduce: %s", vmUuid, cpus, memory, isReduce));
/* -----1. choose host ----
Select the host where the VM to be modified
*/
log.info("STEP 1: Select the host where the VM to be modified");
resourceProcess.selectHostByVmUuid(vmUuid);
/* -----2. modify VM ----
Modify cpu and memory
*/
log.info("STEP 2: Modify cpu and memory");
if (isReduce) {
vmProcess.reduceVM(vmUuid, cpus, memory);
} else {
v
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
KubeIaaS 是基于 Kubernetes 的易于部署和运维、功能完备且易用的私有 IaaS 云解决方案, 旨在充分利用 Kubernetes 的强大能力,将其应用场景从容器云领域拓展到 IaaS 云领域,从而进一步提高基于 Kubernetes 的 IT 基础架构的灵活性和可扩展性。
资源推荐
资源详情
资源评论
收起资源包目录
KubeIaaS 是基于 Kubernetes 的易于部署和运维、功能完备且易用的私有 IaaS 云解决方案, (326个子文件)
kubeiaas-init.db 44KB
Dockerfile 633B
Dockerfile 554B
Dockerfile 516B
Dockerfile 398B
Dockerfile 398B
Dockerfile 392B
Dockerfile 260B
Dockerfile 250B
Dockerfile 235B
Dockerfile 88B
.gitattributes 283B
.gitignore 426B
iaas-vue.tar.gz 5.42MB
kubeiaas-1.0-release.tar.gz 3KB
kubeiaas-2.0-release.tar.gz 3KB
VmService.java 29KB
VmService.java 25KB
VmOpenAPI.java 19KB
LibvirtConfig.java 19KB
VolumeService.java 18KB
VmProcess.java 17KB
TableStorage.java 16KB
VolumeProcess.java 14KB
VolumeService.java 14KB
DbProxy.java 14KB
SQLiteDialect.java 14KB
ImageService.java 13KB
VolumeScheduler.java 13KB
IsoService.java 13KB
VolumeController.java 11KB
HostService.java 10KB
VolumeOpenAPI.java 10KB
ImageUtils.java 10KB
ResourceProcess.java 10KB
VmController.java 10KB
DeviceOpenAPI.java 9KB
ImageOpenAPI.java 9KB
MountProcess.java 9KB
IpUtils.java 9KB
RequestMappingConstants.java 9KB
TableStorage.java 9KB
VmController.java 9KB
NetworkProcess.java 9KB
VolumeController.java 8KB
DeviceScheduler.java 8KB
FileUtils.java 8KB
NetworkService.java 8KB
IpSegmentOpenAPI.java 8KB
XmlConfig.java 7KB
IsoService.java 7KB
ImageController.java 7KB
VmScheduler.java 7KB
ServiceConfig.java 7KB
DhcpService.java 7KB
DbProxy.java 7KB
VolumeController.java 6KB
PathUtils.java 5KB
HostService.java 5KB
SpecConfigOpenAPI.java 5KB
VmController.java 5KB
HostConfig.java 5KB
DeviceService.java 5KB
IpSegmentController.java 5KB
IsoOpenApi.java 4KB
ShellUtils.java 4KB
Device.java 4KB
IsoController.java 4KB
VolumeTable.java 4KB
AgentConfig.java 3KB
HostOpenAPI.java 3KB
USBUtils.java 3KB
PCIUtils.java 3KB
RequestParamConstants.java 3KB
HostController.java 3KB
IpUsedController.java 3KB
VolumeService.java 3KB
MountController.java 3KB
ImageTable.java 3KB
ServiceOpenAPI.java 3KB
OpenAPIExceptionHandler.java 3KB
SpecConfigController.java 3KB
VmTable.java 3KB
DeviceController.java 3KB
ResourceController.java 3KB
ImageOperator.java 3KB
DhcpController.java 3KB
MountPointUtils.java 3KB
DeviceController.java 3KB
IaasAgentRegister.java 3KB
ResourceService.java 3KB
IsoController.java 3KB
ImageController.java 3KB
MacUtils.java 3KB
VolumeConstants.java 2KB
ResponseEnum.java 2KB
DhcpScheduler.java 2KB
DbProxy.java 2KB
TableStorage.java 2KB
IpSegmentTable.java 2KB
共 326 条
- 1
- 2
- 3
- 4
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 7364
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功