public ApplianceVmNicTO(VmNicInventory inv) { ip = inv.getIp(); netmask = inv.getNetmask(); gateway = inv.getGateway(); mac = inv.getMac(); }
@Override protected void scripts() { String acntUuid = Account.getAccountUuidOfResource(spec.getVmInventory().getUuid()); spec.getDestNics().forEach(nic -> { VmNicVO nvo = new VmNicVO(); nvo.setUuid(nic.getUuid()); nvo.setDeviceId(nic.getDeviceId()); nvo.setIp(nic.getIp()); nvo.setL3NetworkUuid(nic.getL3NetworkUuid()); nvo.setMac(nic.getMac()); nvo.setHypervisorType(nic.getHypervisorType()); nvo.setUsedIpUuid(nic.getUsedIpUuid()); nvo.setGateway(nic.getGateway()); nvo.setNetmask(nic.getNetmask()); nvo.setVmInstanceUuid(nic.getVmInstanceUuid()); nvo.setMetaData(nic.getMetaData()); nvo.setInternalName(nic.getInternalName()); nvo.setAccountUuid(acntUuid); nvo.setIpVersion(nic.getIpVersion()); persist(nvo); if (nic.getUsedIpUuid() != null) { SQL.New(UsedIpVO.class).eq(UsedIpVO_.uuid, nic.getUsedIpUuid()).set(UsedIpVO_.vmNicUuid, nvo.getUuid()).update(); } }); ApplianceVmVO apvm = findByUuid(spec.getVmInventory().getUuid(), ApplianceVmVO.class); apvm.setManagementNetworkUuid(mgmtNic.getL3NetworkUuid()); merge(apvm); } }.execute();
@Override public void run(MessageReply reply) { if (!reply.isSuccess()) { logger.warn(String.format("unable to remove dhcp entry[%s] from virtual router vm[uuid:%s, ip:%s], %s", struct, vr.getUuid(), vr .getManagementNic().getIp(), reply.getError())); //TODO: GC } else { VirtualRouterAsyncHttpCallReply ret = reply.castReply(); if (ret.isSuccess()) { logger.debug(String.format("successfully removed dhcp entry[%s] from virtual router vm[uuid:%s, ip:%s]", struct, vr.getUuid(), vr .getManagementNic().getIp())); } else { logger.warn(String.format("unable to remove dhcp entry[%s] from virtual router vm[uuid:%s, ip:%s], %s", struct, vr.getUuid(), vr .getManagementNic().getIp(), ret.getError())); //TODO: GC } } releaseDhcp(it, spec, completion); } });
@Override public void run(MessageReply reply) { if (!reply.isSuccess()) { logger.debug(String.format("failed to change nic firewall default action of virtual router vm[uuid:%s ip:%s], because %s", servedVm.getUuid(), servedVm.getManagementNic().getIp(), reply.getError())); trigger.next(); return; } VirtualRouterAsyncHttpCallReply re = reply.castReply(); VirtualRouterCommands.ConfigureNicFirewallDefaultActionRsp rsp = re.toResponse(VirtualRouterCommands.ConfigureNicFirewallDefaultActionRsp.class); if (rsp.isSuccess()) { logger.debug(String.format("successfully change nic firewall default action of virtual router vm[uuid:%s, ip:%s]", servedVm.getUuid(), servedVm.getManagementNic().getIp())); trigger.next(); } else { logger.debug(String.format("failed to change nic firewall default action of virtual router vm[uuid:%s ip:%s], because %s", servedVm.getUuid(), servedVm.getManagementNic().getIp(), rsp.getError())); trigger.next(); } } });
@Override public void run(MessageReply reply) { if (!reply.isSuccess()) { completion.fail(reply.getError()); return; } VirtualRouterAsyncHttpCallReply re = reply.castReply(); CreateVipRsp ret = re.toResponse(CreateVipRsp.class); if (!ret.isSuccess()) { ErrorCode err = operr("failed to sync vips[ips: %s] on virtual router[uuid:%s]" + " for attaching nic[uuid: %s, ip: %s], because %s", vips.stream().map(v -> v.getIp()).collect(Collectors.toList()), nic.getVmInstanceUuid(), nic.getUuid(), nic.getIp(), ret.getError()); completion.fail(err); } else { completion.success(); } } });
@Override public void run(MessageReply reply) { if (!reply.isSuccess()) { logger.warn(String.format("unable to program dhcp entries served by virtual router[uuid:%s, ip:%s], %s", vr.getUuid(), vr.getManagementNic().getIp(), reply.getError())); chain.fail(reply.getError()); return; } VirtualRouterAsyncHttpCallReply re = reply.castReply(); AddDhcpEntryRsp ret = re.toResponse(AddDhcpEntryRsp.class); if (!ret.isSuccess()) { ErrorCode err = operr("unable to program dhcp entries served by virtual router[uuid:%s, ip:%s], %s", vr.getUuid(), vr.getManagementNic().getIp(), ret.getError()); chain.fail(err); } else { logger.debug(String.format("successfully programmed dhcp entries served by virtual router[uuid:%s, ip:%s]", vr.getUuid(), vr.getManagementNic().getIp())); chain.next(); } } });
@Override public void run(MessageReply reply) { if (!reply.isSuccess()) { completion.fail(reply.getError()); return; } VirtualRouterAsyncHttpCallReply re = reply.castReply(); VirtualRouterCommands.ConfigureNicFirewallDefaultActionRsp rsp = re.toResponse(VirtualRouterCommands.ConfigureNicFirewallDefaultActionRsp.class); if (rsp.isSuccess()) { logger.debug(String.format("successfully change nic[ip:%s, mac:%s] firewall default action of virtual router vm[uuid:%s]", nic.getIp(), nic.getMac(), nic.getVmInstanceUuid())); completion.success(); } else { ErrorCode err = operr("failed to change nic[ip:%s, mac:%s] firewall default action of virtual router vm[uuid:%s], because %s", nic.getIp(), nic.getMac(), nic.getVmInstanceUuid(), rsp.getError()); completion.fail(err); } } });
@Override public void run(MessageReply reply) { if (!reply.isSuccess()) { trigger.fail(reply.getError()); return; } VirtualRouterAsyncHttpCallReply re = reply.castReply(); VirtualRouterCommands.RemoveNicRsp rsp = re.toResponse(VirtualRouterCommands.RemoveNicRsp.class); if (rsp.isSuccess()) { logger.debug(String.format("successfully detach nic[%s] from virtual router vm[uuid:%s, ip:%s]",info, vr.getUuid(), vr.getManagementNic() .getIp())); trigger.next(); } else { ErrorCode err = operr("unable to detach nic[%s] from virtual router vm[uuid:%s ip:%s], because %s", info, vr.getUuid(), vr.getManagementNic().getIp(), rsp.getError()); trigger.fail(err); } } });
private void checkManagementIp(VmInstanceSpec spec, boolean isNewCreated) { if (CoreGlobalProperty.UNIT_TEST_ON) { return; } if (!ApplianceVmConstant.APPLIANCE_VM_TYPE.equals(spec.getVmInventory().getType())) { return; } VmNicInventory mgmtNic; if (isNewCreated) { final ApplianceVmSpec aspec = spec.getExtensionData(ApplianceVmConstant.Params.applianceVmSpec.toString(), ApplianceVmSpec.class); mgmtNic = CollectionUtils.find(spec.getDestNics(), new Function<VmNicInventory, VmNicInventory>() { @Override public VmNicInventory call(VmNicInventory arg) { return arg.getL3NetworkUuid().equals(aspec.getManagementNic().getL3NetworkUuid()) ? arg : null; } }); } else { ApplianceVmInventory apvm = ApplianceVmInventory.valueOf(dbf.findByUuid(spec.getVmInventory().getUuid(), ApplianceVmVO.class)); mgmtNic = apvm.getManagementNic(); } DebugUtils.Assert(mgmtNic!=null, String.format("cannot find management nic of appliance vm[uuid:%s, newCreated: %s]", spec.getVmInventory().getUuid(), isNewCreated)); ShellResult ret = ShellUtils.runAndReturn(String.format("ping -c 1 -W 1 %s", mgmtNic.getIp())); if (ret.isReturnCode(0)) { throw new OperationFailureException(err(ApplianceVmErrors.MANAGEMENT_IP_OCCUPIED, "the management nic IP[%s] has been occupied by another device in the data center, we can ping it", mgmtNic.getIp() )); } }
private SnatStruct makeSnatStruct(VmInstanceSpec spec, L3NetworkInventory l3) { VmNicInventory nic = null; for (VmNicInventory inv : spec.getDestNics()) { if (VmNicHelper.getL3Uuids(inv).contains(l3.getUuid())){ nic = inv; break; } } SnatStruct struct = new SnatStruct(); struct.setL3Network(l3); struct.setGuestGateway(nic.getGateway()); struct.setGuestIp(nic.getIp()); struct.setGuestMac(nic.getMac()); struct.setGuestNetmask(nic.getNetmask()); return struct; }
@Override public VirtualRouterCommands.NicInfo call(VmNicInventory arg) { VirtualRouterCommands.NicInfo info = new VirtualRouterCommands.NicInfo(); info.setIp(arg.getIp()); info.setDefaultRoute(false); info.setGateway(arg.getGateway()); info.setMac(arg.getMac()); info.setNetmask(arg.getNetmask()); info.setFirewallDefaultAction(action); return info; } });
@Override public void run(MessageReply reply) { if (!reply.isSuccess()) { trigger.fail(reply.getError()); return; } VirtualRouterAsyncHttpCallReply re = reply.castReply(); VirtualRouterCommands.ConfigureNicRsp rsp = re.toResponse(VirtualRouterCommands.ConfigureNicRsp.class); if (rsp.isSuccess()) { logger.debug(String.format("successfully add nic[ip:%s, mac:%s] to virtual router vm[uuid:%s, ip:%s]", info.getIp(), info.getMac(), vr.getUuid(), vr.getManagementNic().getIp())); trigger.next(); } else { ErrorCode err = operr("unable to add nic[ip:%s, mac:%s] to virtual router vm[uuid:%s ip:%s], because %s", info.getIp(), info.getMac(), vr.getUuid(), vr.getManagementNic().getIp(), rsp.getError()); trigger.fail(err); } } });
struct.setRule(PortForwardingRuleInventory.valueOf(pfvo)); struct.setVip(VipInventory.valueOf(vipvo)); struct.setGuestIp(nic.getIp()); struct.setGuestMac(nic.getMac()); struct.setGuestL3Network(l3);
@Override public void run(MessageReply reply) { if (!reply.isSuccess()) { logger.warn(String.format("failed to release snat[%s] on virtual router[name:%s, uuid:%s] for vm[uuid: %s, name: %s], %s", struct, vr.getName(), vr.getUuid(), vmInstanceInventory.getUuid(), vmInstanceInventory.getName(), reply.getError())); //TODO GC } else { VirtualRouterAsyncHttpCallReply re = reply.castReply(); RemoveSNATRsp ret = re.toResponse(RemoveSNATRsp.class); if (!ret.isSuccess()) { String err = String.format( "virtual router[uuid:%s, ip:%s] failed to release snat[%s] for vm[uuid:%s, name:%s] on L3Network[uuid:%s, name:%s], because %s", vr.getUuid(), vr.getManagementNic().getIp(), JSONObjectUtil.toJsonString(info), vmInstanceInventory.getUuid(), vmInstanceInventory.getName(), struct.getL3Network().getUuid(), struct.getL3Network().getName(), ret.getError()); logger.warn(err); //TODO GC } else { String msg = String.format( "virtual router[uuid:%s, ip:%s] released snat[%s] for vm[uuid:%s, name:%s] on L3Network[uuid:%s, name:%s], because %s", vr.getUuid(), vr.getManagementNic().getIp(), JSONObjectUtil.toJsonString(info), vmInstanceInventory.getUuid(), vmInstanceInventory.getName(), struct.getL3Network().getUuid(), struct.getL3Network().getName(), ret.getError()); logger.warn(msg); } } releaseSnat(it, vmInstanceInventory, completion); } });
struct.setL3Network(l3); struct.setGuestGateway(nic.getGateway()); struct.setGuestIp(nic.getIp()); struct.setGuestMac(nic.getMac()); struct.setGuestNetmask(nic.getNetmask());
VirtualRouterCommands.RemoveNicCmd cmd = new VirtualRouterCommands.RemoveNicCmd(); VirtualRouterCommands.NicInfo info = new VirtualRouterCommands.NicInfo(); info.setIp(nicInventory.getIp()); info.setDefaultRoute(false); info.setGateway(nicInventory.getGateway());
info.setIp(nic.getIp()); info.setDefaultRoute(false); info.setGateway(nic.getGateway());
cmd.bridgeName = new BridgeNameFinder().findByL3Uuid(struct.getL3NetworkUuid()); cmd.namespaceName = FlatDhcpBackend.makeNamespaceName(cmd.bridgeName, struct.getL3NetworkUuid()); cmd.vmIp = CollectionUtils.find(struct.getVmNics(), arg -> arg.getL3NetworkUuid().equals(struct.getL3NetworkUuid()) ? arg.getIp() : null);
to.setSnatInboundTraffic(struct.isSnatInboundTraffic()); to.setVipIp(struct.getVip().getIp()); to.setGuestIp(struct.getNic().getIp()); cmd.setEip(to);
restf.asyncJsonPost(buildUrl(vr.getManagementNic().getIp(), msg.getPath()), msg.getCommand(), new JsonAsyncRESTCallback<LinkedHashMap>(msg, chain) { @Override public void fail(ErrorCode err) {