@Transactional(readOnly = true) private void isVmNicUsedByEip(String vmNicUuid) { String sql = "select eip from EipVO eip, VmInstanceVO vm, VmNicVO nic where eip.vmNicUuid = nic.uuid" + " and nic.vmInstanceUuid = vm.uuid and vm.uuid = (select n.vmInstanceUuid from VmNicVO n where n.uuid = :nicUuid)"; TypedQuery<EipVO> q = dbf.getEntityManager().createQuery(sql, EipVO.class); q.setParameter("nicUuid", vmNicUuid); List<EipVO> eips = q.getResultList(); if (!eips.isEmpty()) { sql = "select vm from VmInstanceVO vm, VmNicVO nic where vm.uuid = nic.vmInstanceUuid and nic.uuid = :nicUuid"; TypedQuery<VmInstanceVO> vq = dbf.getEntityManager().createQuery(sql, VmInstanceVO.class); vq.setParameter("nicUuid", vmNicUuid); VmInstanceVO vm = vq.getSingleResult(); List<String> eipStr = eips.stream().map(eip -> String.format("(name:%s, ip:%s)", eip.getName(), eip.getVipIp())).collect(Collectors.toList()); throw new ApiMessageInterceptionException(operr("the vm[name:%s, uuid:%s] already has some EIPs%s attached", vm.getName(), vm.getUuid(), StringUtils.join(eipStr, ","))); } }
@Transactional(readOnly = true) private void isVmNicUsedByPortForwarding(String vmNicUuid) { String sql = "select pf from PortForwardingRuleVO pf, VmInstanceVO vm, VmNicVO nic where pf.vmNicUuid = nic.uuid" + " and nic.vmInstanceUuid = vm.uuid and vm.uuid = (select n.vmInstanceUuid from VmNicVO n where n.uuid = :nicUuid)"; TypedQuery<PortForwardingRuleVO> q = dbf.getEntityManager().createQuery(sql, PortForwardingRuleVO.class); q.setParameter("nicUuid", vmNicUuid); List<PortForwardingRuleVO> pfs = q.getResultList(); if (!pfs.isEmpty()) { sql = "select vm from VmInstanceVO vm, VmNicVO nic where vm.uuid = nic.vmInstanceUuid and nic.uuid = :nicUuid"; TypedQuery<VmInstanceVO> vq = dbf.getEntityManager().createQuery(sql, VmInstanceVO.class); vq.setParameter("nicUuid", vmNicUuid); VmInstanceVO vm = vq.getSingleResult(); List<String> pfStr = pfs.stream().map(pf -> String.format("(name:%s, ip:%s)", pf.getName(), pf.getVipIp())).collect(Collectors.toList()); throw new ApiMessageInterceptionException(operr("the vm[name:%s, uuid:%s] already has some port forwarding rules%s attached", vm.getName(), vm.getUuid(), StringUtils.join(pfStr, ","))); } }
@Transactional(readOnly = true) private void checkIfAnotherVip(String vipUuid, String vmNicUuid) { String sql = "select nic.uuid from VmNicVO nic where nic.vmInstanceUuid = (select n.vmInstanceUuid from VmNicVO n where" + " n.uuid = :nicUuid)"; TypedQuery<String> q = dbf.getEntityManager().createQuery(sql, String.class); q.setParameter("nicUuid", vmNicUuid); List<String> nicUuids = q.getResultList(); sql = "select count(*) from VmNicVO nic, PortForwardingRuleVO pf where nic.uuid = pf.vmNicUuid and pf.vipUuid != :vipUuid and nic.uuid in (:nicUuids)"; TypedQuery<Long> lq = dbf.getEntityManager().createQuery(sql, Long.class); lq.setParameter("vipUuid", vipUuid); lq.setParameter("nicUuids", nicUuids); long count = lq.getSingleResult(); if (count > 0) { sql = "select vm from VmInstanceVO vm, VmNicVO nic where vm.uuid = nic.vmInstanceUuid and nic.uuid = :nicUuid"; TypedQuery<VmInstanceVO> vq = dbf.getEntityManager().createQuery(sql, VmInstanceVO.class); vq.setParameter("nicUuid", vmNicUuid); VmInstanceVO vm = vq.getSingleResult(); throw new ApiMessageInterceptionException(operr("the VM[name:%s uuid:%s] already has port forwarding rules that have different VIPs than the one[uuid:%s]", vm.getName(), vm.getUuid(), vipUuid)); } }
@Override public void fail(ErrorCode errorCode) { extEmitter.failedToDestroyVm(inv, errorCode); logger.debug(String.format("failed to delete vm instance[name:%s, uuid:%s], because %s", self.getName(), self.getUuid(), errorCode)); completion.fail(errorCode); } });
protected VmInstanceVO refreshVO(boolean noException) { VmInstanceVO vo = self; self = dbf.findByUuid(self.getUuid(), VmInstanceVO.class); if (self == null && noException) { return null; } if (self == null) { throw new OperationFailureException(operr("vm[uuid:%s, name:%s] has been deleted", vo.getUuid(), vo.getName())); } originalCopy = ObjectUtils.newAndCopy(vo, vo.getClass()); return self; }
" where the VM[name:%s, uuid:%s] is on", isoUuid, psUuid, self.getName(), self.getUuid()));
@Override public void success(RefreshFirewallRsp ret) { if (!ret.isSuccess()) { logger.warn(String.format("failed to refresh firewall rules on appliance vm[uuid:%s, name:%s], %s", self.getUuid(), self.getName(), ret.getError())); reply.setError(operr("operation error, because:%s", ret.getError())); } bus.reply(msg, reply); completion.done(); }
@Override public void handle(Map data) { VmInstanceSpec spec = (VmInstanceSpec) data.get(VmInstanceConstant.Params.VmInstanceSpec.toString()); changeVmStateInDb(VmInstanceStateEvent.running, () -> new SQLBatch() { @Override protected void scripts() { self.setLastHostUuid(spec.getDestHost().getUuid()); self.setHostUuid(spec.getDestHost().getUuid()); self.setClusterUuid(spec.getDestHost().getClusterUuid()); self.setZoneUuid(spec.getDestHost().getZoneUuid()); self.setHypervisorType(spec.getDestHost().getHypervisorType()); self.setRootVolumeUuid(spec.getDestRootVolume().getUuid()); } }.execute()); logger.debug(String.format("appliance vm[uuid:%s, name: %s, type:%s] is running ..", self.getUuid(), self.getName(), getSelf().getApplianceVmType())); VmInstanceInventory inv = VmInstanceInventory.valueOf(self); extEmitter.afterStartNewCreatedVm(inv); InstantiateNewCreatedVmInstanceReply reply = new InstantiateNewCreatedVmInstanceReply(); reply.setVmInventory(inv); bus.reply(msg, reply); taskChain.next(); } }).error(new FlowErrorHandler(msg, taskChain) {
judger.getClass(), self.getUuid(), self.getName())); SQL.New(VmInstanceVO.class).eq(VmInstanceVO_.uuid, self.getUuid()) .set(VmInstanceVO_.state, VmInstanceState.Stopped)
@Override public void success() { extEmitter.afterDestroyVm(inv); logger.debug(String.format("successfully deleted vm instance[name:%s, uuid:%s]", self.getName(), self.getUuid())); if (deletionPolicy == VmInstanceDeletionPolicy.Direct) { changeVmStateInDb(VmInstanceStateEvent.destroyed);
if (msg.isCheckStatus() && getSelf().getState() != VmInstanceState.Running) { throw new OperationFailureException(operr("the virtual router[name:%s, uuid:%s, current state:%s] is not running," + "and cannot perform required operation. Please retry your operation later once it is running", self.getName(), self.getUuid(), self.getState()));
protected VmInstanceInventory(VmInstanceVO vo) { this.setUuid(vo.getUuid()); this.setName(vo.getName()); this.setDescription(vo.getDescription()); this.setZoneUuid(vo.getZoneUuid()); this.setClusterUuid(vo.getClusterUuid()); this.setImageUuid(vo.getImageUuid()); this.setHostUuid(vo.getHostUuid()); this.setLastHostUuid(vo.getLastHostUuid()); this.setInstanceOfferingUuid(vo.getInstanceOfferingUuid()); this.setType(vo.getType()); this.setHypervisorType(vo.getHypervisorType()); this.setCreateDate(vo.getCreateDate()); this.setLastOpDate(vo.getLastOpDate()); this.setState(vo.getState().toString()); this.setRootVolumeUuid(vo.getRootVolumeUuid()); this.setAllVolumes(VolumeInventory.valueOf(vo.getAllVolumes())); this.setVmNics(VmNicInventory.valueOf(vo.getVmNics())); this.setVmCdRoms(VmCdRomInventory.valueOf(vo.getVmCdRoms())); this.setInternalId(vo.getInternalId()); this.setDefaultL3NetworkUuid(vo.getDefaultL3NetworkUuid()); this.setCpuNum(vo.getCpuNum()); this.setCpuSpeed(vo.getCpuSpeed()); this.setMemorySize(vo.getMemorySize()); this.setAllocatorStrategy(vo.getAllocatorStrategy()); this.setPlatform(vo.getPlatform()); }