From ea038ab8470aaee47b823ce2953493f59d520a0d Mon Sep 17 00:00:00 2001 From: Vijayendra Bhamidipati Date: Thu, 2 Aug 2012 22:44:21 -0400 Subject: [PATCH] CS-15764: InterVlan - VMWare - Failed to create Static NAT Rule to a User VM in a VPC - SSH execution of command /root/firewall.sh -A -l 10.223.139.28 -r 192.168.11.186 -d 0:0 -G has an error status code in return. Description: Putting in code to set static nat rules on VPC VR. --- .../vmware/resource/VmwareResource.java | 6372 +++++++++-------- 1 file changed, 3206 insertions(+), 3166 deletions(-) diff --git a/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java b/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java index bb3b86d4b05..2377725cee9 100755 --- a/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java +++ b/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java @@ -10,8 +10,8 @@ // limitations under the License. // // Automatically generated by addcopyright.py at 04/03/2012 -package com.cloud.hypervisor.vmware.resource; - +package com.cloud.hypervisor.vmware.resource; + import java.io.File; import java.io.IOException; import java.net.ConnectException; @@ -29,7 +29,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; -import java.util.Set; import java.util.TimeZone; import java.util.UUID; @@ -248,183 +247,180 @@ import com.vmware.vim25.VirtualMachineGuestOsIdentifier; import com.vmware.vim25.VirtualMachinePowerState; import com.vmware.vim25.VirtualMachineRuntimeInfo; import com.vmware.vim25.VirtualSCSISharing; -import com.xensource.xenapi.Connection; -import com.xensource.xenapi.VIF; -import com.xensource.xenapi.VM; - -public class VmwareResource implements StoragePoolResource, ServerResource, VmwareHostService { - private static final Logger s_logger = Logger.getLogger(VmwareResource.class); - - protected String _name; - + +public class VmwareResource implements StoragePoolResource, ServerResource, VmwareHostService { + private static final Logger s_logger = Logger.getLogger(VmwareResource.class); + + protected String _name; + protected final long _ops_timeout = 900000; // 15 minutes time out to time protected final int _shutdown_waitMs = 300000; // wait up to 5 minutes for shutdown - - // out an operation - protected final int _retry = 24; - protected final int _sleep = 10000; - protected final int DEFAULT_DOMR_SSHPORT = 3922; - protected final int MAX_CMD_MBEAN = 100; - - protected String _url; - protected String _dcId; - protected String _pod; - protected String _cluster; - protected String _username; - protected String _password; - protected String _guid; - protected String _vCenterAddress; - - protected String _privateNetworkVSwitchName; - protected String _publicNetworkVSwitchName; + + // out an operation + protected final int _retry = 24; + protected final int _sleep = 10000; + protected final int DEFAULT_DOMR_SSHPORT = 3922; + protected final int MAX_CMD_MBEAN = 100; + + protected String _url; + protected String _dcId; + protected String _pod; + protected String _cluster; + protected String _username; + protected String _password; + protected String _guid; + protected String _vCenterAddress; + + protected String _privateNetworkVSwitchName; + protected String _publicNetworkVSwitchName; protected String _guestNetworkVSwitchName; protected VirtualSwitchType _vSwitchType = VirtualSwitchType.StandardVirtualSwitch; protected boolean _nexusVSwitch = false; - + protected float _cpuOverprovisioningFactor = 1; protected boolean _reserveCpu = false; - + protected float _memOverprovisioningFactor = 1; protected boolean _reserveMem = false; protected boolean _recycleHungWorker = false; protected DiskControllerType _rootDiskController = DiskControllerType.ide; - protected ManagedObjectReference _morHyperHost; - protected VmwareContext _serviceContext; - protected String _hostName; - - protected HashMap _vms = new HashMap(71); - protected List _cmdMBeans = new ArrayList(); - - protected Gson _gson; - - protected volatile long _cmdSequence = 1; - - protected static HashMap s_statesTable; - static { - s_statesTable = new HashMap(); - s_statesTable.put(VirtualMachinePowerState.poweredOn, State.Running); - s_statesTable.put(VirtualMachinePowerState.poweredOff, State.Stopped); - s_statesTable.put(VirtualMachinePowerState.suspended, State.Stopped); - } - - public VmwareResource() { - _gson = GsonHelper.getGsonLogger(); - } - - @Override - public Answer executeRequest(Command cmd) { - if(s_logger.isTraceEnabled()) - s_logger.trace("Begin executeRequest(), cmd: " + cmd.getClass().getSimpleName()); - - Answer answer = null; - NDC.push(_hostName != null ? _hostName : _guid + "(" + ComponentLocator.class.getPackage().getImplementationVersion() + ")"); - try { - long cmdSequence = _cmdSequence++; - Date startTime = DateUtil.currentGMTTime(); - PropertyMapDynamicBean mbean = new PropertyMapDynamicBean(); - mbean.addProp("StartTime", DateUtil.getDateDisplayString(TimeZone.getDefault(), startTime)); - mbean.addProp("Command", _gson.toJson(cmd)); - mbean.addProp("Sequence", String.valueOf(cmdSequence)); - mbean.addProp("Name", cmd.getClass().getSimpleName()); + protected ManagedObjectReference _morHyperHost; + protected VmwareContext _serviceContext; + protected String _hostName; - Class clz = cmd.getClass(); - if (clz == CreateCommand.class) { - answer = execute((CreateCommand) cmd); - } else if (clz == SetPortForwardingRulesCommand.class) { - answer = execute((SetPortForwardingRulesCommand) cmd); - } else if (clz == SetStaticNatRulesCommand.class) { - answer = execute((SetStaticNatRulesCommand) cmd); - } else if( clz == LoadBalancerConfigCommand.class) { - answer = execute((LoadBalancerConfigCommand) cmd); - } else if (clz == IpAssocCommand.class) { - answer = execute((IpAssocCommand) cmd); - } else if (clz == SavePasswordCommand.class) { - answer = execute((SavePasswordCommand) cmd); - } else if (clz == DhcpEntryCommand.class) { - answer = execute((DhcpEntryCommand) cmd); - } else if (clz == VmDataCommand.class) { - answer = execute((VmDataCommand) cmd); - } else if (clz == ReadyCommand.class) { - answer = execute((ReadyCommand) cmd); - } else if (clz == GetHostStatsCommand.class) { - answer = execute((GetHostStatsCommand) cmd); - } else if (clz == GetVmStatsCommand.class) { - answer = execute((GetVmStatsCommand) cmd); - } else if (clz == CheckHealthCommand.class) { - answer = execute((CheckHealthCommand) cmd); - } else if (clz == StopCommand.class) { - answer = execute((StopCommand) cmd); - } else if (clz == RebootRouterCommand.class) { - answer = execute((RebootRouterCommand) cmd); - } else if (clz == RebootCommand.class) { - answer = execute((RebootCommand) cmd); - } else if (clz == CheckVirtualMachineCommand.class) { - answer = execute((CheckVirtualMachineCommand) cmd); - } else if (clz == PrepareForMigrationCommand.class) { - answer = execute((PrepareForMigrationCommand) cmd); - } else if (clz == MigrateCommand.class) { - answer = execute((MigrateCommand) cmd); - } else if (clz == DestroyCommand.class) { - answer = execute((DestroyCommand) cmd); - } else if (clz == CreateStoragePoolCommand.class) { - return execute((CreateStoragePoolCommand) cmd); - } else if (clz == ModifyStoragePoolCommand.class) { - answer = execute((ModifyStoragePoolCommand) cmd); - } else if (clz == DeleteStoragePoolCommand.class) { - answer = execute((DeleteStoragePoolCommand) cmd); - } else if (clz == CopyVolumeCommand.class) { - answer = execute((CopyVolumeCommand) cmd); - } else if (clz == AttachVolumeCommand.class) { - answer = execute((AttachVolumeCommand) cmd); - } else if (clz == AttachIsoCommand.class) { - answer = execute((AttachIsoCommand) cmd); - } else if (clz == ValidateSnapshotCommand.class) { - answer = execute((ValidateSnapshotCommand) cmd); - } else if (clz == ManageSnapshotCommand.class) { - answer = execute((ManageSnapshotCommand) cmd); - } else if (clz == BackupSnapshotCommand.class) { - answer = execute((BackupSnapshotCommand) cmd); - } else if (clz == CreateVolumeFromSnapshotCommand.class) { - answer = execute((CreateVolumeFromSnapshotCommand) cmd); - } else if (clz == CreatePrivateTemplateFromVolumeCommand.class) { - answer = execute((CreatePrivateTemplateFromVolumeCommand) cmd); - } else if (clz == CreatePrivateTemplateFromSnapshotCommand.class) { - answer = execute((CreatePrivateTemplateFromSnapshotCommand) cmd); - } else if (clz == UpgradeSnapshotCommand.class) { - answer = execute((UpgradeSnapshotCommand) cmd); - } else if (clz == GetStorageStatsCommand.class) { - answer = execute((GetStorageStatsCommand) cmd); - } else if (clz == PrimaryStorageDownloadCommand.class) { - answer = execute((PrimaryStorageDownloadCommand) cmd); - } else if (clz == GetVncPortCommand.class) { - answer = execute((GetVncPortCommand) cmd); - } else if (clz == SetupCommand.class) { - answer = execute((SetupCommand) cmd); - } else if (clz == MaintainCommand.class) { - answer = execute((MaintainCommand) cmd); - } else if (clz == PingTestCommand.class) { - answer = execute((PingTestCommand) cmd); - } else if (clz == CheckOnHostCommand.class) { - answer = execute((CheckOnHostCommand) cmd); - } else if (clz == ModifySshKeysCommand.class) { - answer = execute((ModifySshKeysCommand) cmd); - } else if (clz == PoolEjectCommand.class) { - answer = execute((PoolEjectCommand) cmd); - } else if (clz == NetworkUsageCommand.class) { - answer = execute((NetworkUsageCommand) cmd); - } else if (clz == StartCommand.class) { - answer = execute((StartCommand) cmd); - } else if (clz == RemoteAccessVpnCfgCommand.class) { - answer = execute((RemoteAccessVpnCfgCommand) cmd); - } else if (clz == VpnUsersCfgCommand.class) { - answer = execute((VpnUsersCfgCommand) cmd); - } else if (clz == CheckSshCommand.class) { - answer = execute((CheckSshCommand) cmd); - } else if (clz == CheckRouterCommand.class) { - answer = execute((CheckRouterCommand) cmd); + protected HashMap _vms = new HashMap(71); + protected List _cmdMBeans = new ArrayList(); + + protected Gson _gson; + + protected volatile long _cmdSequence = 1; + + protected static HashMap s_statesTable; + static { + s_statesTable = new HashMap(); + s_statesTable.put(VirtualMachinePowerState.poweredOn, State.Running); + s_statesTable.put(VirtualMachinePowerState.poweredOff, State.Stopped); + s_statesTable.put(VirtualMachinePowerState.suspended, State.Stopped); + } + + public VmwareResource() { + _gson = GsonHelper.getGsonLogger(); + } + + @Override + public Answer executeRequest(Command cmd) { + if(s_logger.isTraceEnabled()) + s_logger.trace("Begin executeRequest(), cmd: " + cmd.getClass().getSimpleName()); + + Answer answer = null; + NDC.push(_hostName != null ? _hostName : _guid + "(" + ComponentLocator.class.getPackage().getImplementationVersion() + ")"); + try { + long cmdSequence = _cmdSequence++; + Date startTime = DateUtil.currentGMTTime(); + PropertyMapDynamicBean mbean = new PropertyMapDynamicBean(); + mbean.addProp("StartTime", DateUtil.getDateDisplayString(TimeZone.getDefault(), startTime)); + mbean.addProp("Command", _gson.toJson(cmd)); + mbean.addProp("Sequence", String.valueOf(cmdSequence)); + mbean.addProp("Name", cmd.getClass().getSimpleName()); + + Class clz = cmd.getClass(); + if (clz == CreateCommand.class) { + answer = execute((CreateCommand) cmd); + } else if (clz == SetPortForwardingRulesCommand.class) { + answer = execute((SetPortForwardingRulesCommand) cmd); + } else if (clz == SetStaticNatRulesCommand.class) { + answer = execute((SetStaticNatRulesCommand) cmd); + } else if( clz == LoadBalancerConfigCommand.class) { + answer = execute((LoadBalancerConfigCommand) cmd); + } else if (clz == IpAssocCommand.class) { + answer = execute((IpAssocCommand) cmd); + } else if (clz == SavePasswordCommand.class) { + answer = execute((SavePasswordCommand) cmd); + } else if (clz == DhcpEntryCommand.class) { + answer = execute((DhcpEntryCommand) cmd); + } else if (clz == VmDataCommand.class) { + answer = execute((VmDataCommand) cmd); + } else if (clz == ReadyCommand.class) { + answer = execute((ReadyCommand) cmd); + } else if (clz == GetHostStatsCommand.class) { + answer = execute((GetHostStatsCommand) cmd); + } else if (clz == GetVmStatsCommand.class) { + answer = execute((GetVmStatsCommand) cmd); + } else if (clz == CheckHealthCommand.class) { + answer = execute((CheckHealthCommand) cmd); + } else if (clz == StopCommand.class) { + answer = execute((StopCommand) cmd); + } else if (clz == RebootRouterCommand.class) { + answer = execute((RebootRouterCommand) cmd); + } else if (clz == RebootCommand.class) { + answer = execute((RebootCommand) cmd); + } else if (clz == CheckVirtualMachineCommand.class) { + answer = execute((CheckVirtualMachineCommand) cmd); + } else if (clz == PrepareForMigrationCommand.class) { + answer = execute((PrepareForMigrationCommand) cmd); + } else if (clz == MigrateCommand.class) { + answer = execute((MigrateCommand) cmd); + } else if (clz == DestroyCommand.class) { + answer = execute((DestroyCommand) cmd); + } else if (clz == CreateStoragePoolCommand.class) { + return execute((CreateStoragePoolCommand) cmd); + } else if (clz == ModifyStoragePoolCommand.class) { + answer = execute((ModifyStoragePoolCommand) cmd); + } else if (clz == DeleteStoragePoolCommand.class) { + answer = execute((DeleteStoragePoolCommand) cmd); + } else if (clz == CopyVolumeCommand.class) { + answer = execute((CopyVolumeCommand) cmd); + } else if (clz == AttachVolumeCommand.class) { + answer = execute((AttachVolumeCommand) cmd); + } else if (clz == AttachIsoCommand.class) { + answer = execute((AttachIsoCommand) cmd); + } else if (clz == ValidateSnapshotCommand.class) { + answer = execute((ValidateSnapshotCommand) cmd); + } else if (clz == ManageSnapshotCommand.class) { + answer = execute((ManageSnapshotCommand) cmd); + } else if (clz == BackupSnapshotCommand.class) { + answer = execute((BackupSnapshotCommand) cmd); + } else if (clz == CreateVolumeFromSnapshotCommand.class) { + answer = execute((CreateVolumeFromSnapshotCommand) cmd); + } else if (clz == CreatePrivateTemplateFromVolumeCommand.class) { + answer = execute((CreatePrivateTemplateFromVolumeCommand) cmd); + } else if (clz == CreatePrivateTemplateFromSnapshotCommand.class) { + answer = execute((CreatePrivateTemplateFromSnapshotCommand) cmd); + } else if (clz == UpgradeSnapshotCommand.class) { + answer = execute((UpgradeSnapshotCommand) cmd); + } else if (clz == GetStorageStatsCommand.class) { + answer = execute((GetStorageStatsCommand) cmd); + } else if (clz == PrimaryStorageDownloadCommand.class) { + answer = execute((PrimaryStorageDownloadCommand) cmd); + } else if (clz == GetVncPortCommand.class) { + answer = execute((GetVncPortCommand) cmd); + } else if (clz == SetupCommand.class) { + answer = execute((SetupCommand) cmd); + } else if (clz == MaintainCommand.class) { + answer = execute((MaintainCommand) cmd); + } else if (clz == PingTestCommand.class) { + answer = execute((PingTestCommand) cmd); + } else if (clz == CheckOnHostCommand.class) { + answer = execute((CheckOnHostCommand) cmd); + } else if (clz == ModifySshKeysCommand.class) { + answer = execute((ModifySshKeysCommand) cmd); + } else if (clz == PoolEjectCommand.class) { + answer = execute((PoolEjectCommand) cmd); + } else if (clz == NetworkUsageCommand.class) { + answer = execute((NetworkUsageCommand) cmd); + } else if (clz == StartCommand.class) { + answer = execute((StartCommand) cmd); + } else if (clz == RemoteAccessVpnCfgCommand.class) { + answer = execute((RemoteAccessVpnCfgCommand) cmd); + } else if (clz == VpnUsersCfgCommand.class) { + answer = execute((VpnUsersCfgCommand) cmd); + } else if (clz == CheckSshCommand.class) { + answer = execute((CheckSshCommand) cmd); + } else if (clz == CheckRouterCommand.class) { + answer = execute((CheckRouterCommand) cmd); } else if (clz == SetFirewallRulesCommand.class) { - answer = execute((SetFirewallRulesCommand)cmd); + answer = execute((SetFirewallRulesCommand)cmd); } else if (clz == BumpUpPriorityCommand.class) { answer = execute((BumpUpPriorityCommand)cmd); } else if (clz == GetDomRVersionCmd.class) { @@ -451,45 +447,45 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa answer = execute((CheckS2SVpnConnectionsCommand) cmd); } else if (clz == SetStaticRouteCommand.class) { answer = execute((SetStaticRouteCommand) cmd); - } else { - answer = Answer.createUnsupportedCommandAnswer(cmd); + } else { + answer = Answer.createUnsupportedCommandAnswer(cmd); } if(cmd.getContextParam("checkpoint") != null) { answer.setContextParam("checkpoint", cmd.getContextParam("checkpoint")); } - - Date doneTime = DateUtil.currentGMTTime(); - mbean.addProp("DoneTime", DateUtil.getDateDisplayString(TimeZone.getDefault(), doneTime)); - mbean.addProp("Answer", _gson.toJson(answer)); - - synchronized (this) { - try { - JmxUtil.registerMBean("VMware " + _morHyperHost.get_value(), "Command " + cmdSequence + "-" + cmd.getClass().getSimpleName(), mbean); - _cmdMBeans.add(mbean); - - if (_cmdMBeans.size() >= MAX_CMD_MBEAN) { - PropertyMapDynamicBean mbeanToRemove = _cmdMBeans.get(0); - _cmdMBeans.remove(0); - - JmxUtil.unregisterMBean("VMware " + _morHyperHost.get_value(), "Command " + mbeanToRemove.getProp("Sequence") + "-" + mbeanToRemove.getProp("Name")); - } - } catch (Exception e) { - if(s_logger.isTraceEnabled()) - s_logger.trace("Unable to register JMX monitoring due to exception " + ExceptionUtil.toString(e)); - } - } - - } finally { - NDC.pop(); - } - if(s_logger.isTraceEnabled()) - s_logger.trace("End executeRequest(), cmd: " + cmd.getClass().getSimpleName()); - - return answer; + Date doneTime = DateUtil.currentGMTTime(); + mbean.addProp("DoneTime", DateUtil.getDateDisplayString(TimeZone.getDefault(), doneTime)); + mbean.addProp("Answer", _gson.toJson(answer)); + + synchronized (this) { + try { + JmxUtil.registerMBean("VMware " + _morHyperHost.get_value(), "Command " + cmdSequence + "-" + cmd.getClass().getSimpleName(), mbean); + _cmdMBeans.add(mbean); + + if (_cmdMBeans.size() >= MAX_CMD_MBEAN) { + PropertyMapDynamicBean mbeanToRemove = _cmdMBeans.get(0); + _cmdMBeans.remove(0); + + JmxUtil.unregisterMBean("VMware " + _morHyperHost.get_value(), "Command " + mbeanToRemove.getProp("Sequence") + "-" + mbeanToRemove.getProp("Name")); + } + } catch (Exception e) { + if(s_logger.isTraceEnabled()) + s_logger.trace("Unable to register JMX monitoring due to exception " + ExceptionUtil.toString(e)); + } + } + + } finally { + NDC.pop(); + } + + if(s_logger.isTraceEnabled()) + s_logger.trace("End executeRequest(), cmd: " + cmd.getClass().getSimpleName()); + + return answer; } - + protected Answer execute(CheckNetworkCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource CheckNetworkCommand " + _gson.toJson(cmd)); @@ -497,22 +493,22 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa // TODO setup portgroup for private network needs to be done here now return new CheckNetworkAnswer(cmd, true , "Network Setup check by names is done"); - } - - protected Answer execute(NetworkUsageCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource NetworkUsageCommand " + _gson.toJson(cmd)); - } + } + + protected Answer execute(NetworkUsageCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource NetworkUsageCommand " + _gson.toJson(cmd)); + } if(cmd.getOption()!=null && cmd.getOption().equals("create") ){ String result = networkUsage(cmd.getPrivateIP(), "create", null); NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, result, 0L, 0L); return answer; - } - long[] stats = getNetworkStats(cmd.getPrivateIP()); - - NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, "", stats[0], stats[1]); - return answer; - } + } + long[] stats = getNetworkStats(cmd.getPrivateIP()); + + NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, "", stats[0], stats[1]); + return answer; + } private SetStaticRouteAnswer execute(SetStaticRouteCommand cmd) { if (s_logger.isInfoEnabled()) { @@ -564,197 +560,241 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa return new SetStaticRouteAnswer(cmd, endResult, results); } - - protected Answer execute(SetPortForwardingRulesCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource SetPortForwardingRulesCommand: " + _gson.toJson(cmd)); - } - - String controlIp = getRouterSshControlIp(cmd); - String args = ""; - String[] results = new String[cmd.getRules().length]; + + protected Answer execute(SetPortForwardingRulesCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource SetPortForwardingRulesCommand: " + _gson.toJson(cmd)); + } + + String controlIp = getRouterSshControlIp(cmd); + String args = ""; + String[] results = new String[cmd.getRules().length]; int i = 0; - - boolean endResult = true; - for (PortForwardingRuleTO rule : cmd.getRules()) { - args += rule.revoked() ? " -D " : " -A "; - args += " -P " + rule.getProtocol().toLowerCase(); - args += " -l " + rule.getSrcIp(); - args += " -p " + rule.getStringSrcPortRange(); - args += " -r " + rule.getDstIp(); + + boolean endResult = true; + for (PortForwardingRuleTO rule : cmd.getRules()) { + args += rule.revoked() ? " -D " : " -A "; + args += " -P " + rule.getProtocol().toLowerCase(); + args += " -l " + rule.getSrcIp(); + args += " -p " + rule.getStringSrcPortRange(); + args += " -r " + rule.getDstIp(); args += " -d " + rule.getStringDstPortRange(); - - try { - VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/root/firewall.sh " + args); - - if (s_logger.isDebugEnabled()) - s_logger.debug("Executing script on domain router " + controlIp + ": /root/firewall.sh " + args); - - if (!result.first()) { - s_logger.error("SetPortForwardingRulesCommand failure on setting one rule. args: " + args); + + try { + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/root/firewall.sh " + args); + + if (s_logger.isDebugEnabled()) + s_logger.debug("Executing script on domain router " + controlIp + ": /root/firewall.sh " + args); + + if (!result.first()) { + s_logger.error("SetPortForwardingRulesCommand failure on setting one rule. args: " + args); results[i++] = "Failed"; - endResult = false; - } else { - results[i++] = null; - } - } catch (Throwable e) { - s_logger.error("SetPortForwardingRulesCommand(args: " + args + ") failed on setting one rule due to " + VmwareHelper.getExceptionMessage(e), e); + endResult = false; + } else { + results[i++] = null; + } + } catch (Throwable e) { + s_logger.error("SetPortForwardingRulesCommand(args: " + args + ") failed on setting one rule due to " + VmwareHelper.getExceptionMessage(e), e); results[i++] = "Failed"; - endResult = false; - } - } - - return new SetPortForwardingRulesAnswer(cmd, results, endResult); + endResult = false; + } + } + + return new SetPortForwardingRulesAnswer(cmd, results, endResult); } - + protected SetFirewallRulesAnswer execute(SetFirewallRulesCommand cmd) { - String controlIp = getRouterSshControlIp(cmd); - String[] results = new String[cmd.getRules().length]; + String controlIp = getRouterSshControlIp(cmd); + String[] results = new String[cmd.getRules().length]; - String[][] rules = cmd.generateFwRules(); - String args = ""; - args += " -F "; - StringBuilder sb = new StringBuilder(); - String[] fwRules = rules[0]; - if (fwRules.length > 0) { - for (int i = 0; i < fwRules.length; i++) { - sb.append(fwRules[i]).append(','); - } - args += " -a " + sb.toString(); - } + String[][] rules = cmd.generateFwRules(); + String args = ""; + args += " -F "; + StringBuilder sb = new StringBuilder(); + String[] fwRules = rules[0]; + if (fwRules.length > 0) { + for (int i = 0; i < fwRules.length; i++) { + sb.append(fwRules[i]).append(','); + } + args += " -a " + sb.toString(); + } - try { - VmwareManager mgr = getServiceContext().getStockObject( - VmwareManager.CONTEXT_STOCK_NAME); - Pair result = SshHelper.sshExecute(controlIp, - DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), - null, "/root/firewall_rule.sh " + args); + try { + VmwareManager mgr = getServiceContext().getStockObject( + VmwareManager.CONTEXT_STOCK_NAME); + Pair result = SshHelper.sshExecute(controlIp, + DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), + null, "/root/firewall_rule.sh " + args); - if (s_logger.isDebugEnabled()) - s_logger.debug("Executing script on domain router " + controlIp - + ": /root/firewall_rule.sh " + args); + if (s_logger.isDebugEnabled()) + s_logger.debug("Executing script on domain router " + controlIp + + ": /root/firewall_rule.sh " + args); - if (!result.first()) { - s_logger.error("SetFirewallRulesCommand failure on setting one rule. args: " - + args); - //FIXME - in the future we have to process each rule separately; now we temporarily set every rule to be false if single rule fails - for (int i=0; i < results.length; i++) { - results[i] = "Failed"; - } - - return new SetFirewallRulesAnswer(cmd, false, results); - } - } catch (Throwable e) { - s_logger.error("SetFirewallRulesCommand(args: " + args - + ") failed on setting one rule due to " - + VmwareHelper.getExceptionMessage(e), e); - //FIXME - in the future we have to process each rule separately; now we temporarily set every rule to be false if single rule fails + if (!result.first()) { + s_logger.error("SetFirewallRulesCommand failure on setting one rule. args: " + + args); + //FIXME - in the future we have to process each rule separately; now we temporarily set every rule to be false if single rule fails + for (int i=0; i < results.length; i++) { + results[i] = "Failed"; + } + + return new SetFirewallRulesAnswer(cmd, false, results); + } + } catch (Throwable e) { + s_logger.error("SetFirewallRulesCommand(args: " + args + + ") failed on setting one rule due to " + + VmwareHelper.getExceptionMessage(e), e); + //FIXME - in the future we have to process each rule separately; now we temporarily set every rule to be false if single rule fails for (int i=0; i < results.length; i++) { results[i] = "Failed"; } - return new SetFirewallRulesAnswer(cmd, false, results); - } + return new SetFirewallRulesAnswer(cmd, false, results); + } - return new SetFirewallRulesAnswer(cmd, true, results); + return new SetFirewallRulesAnswer(cmd, true, results); } - - protected Answer execute(SetStaticNatRulesCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource SetFirewallRuleCommand: " + _gson.toJson(cmd)); - } - - String args = null; - String[] results = new String[cmd.getRules().length]; + + protected SetStaticNatRulesAnswer SetVPCStaticNatRules(SetStaticNatRulesCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource SetVPCStaticNatRulesCommand: " + _gson.toJson(cmd)); + } + + String[] results = new String[cmd.getRules().length]; + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + String controlIp = getRouterSshControlIp(cmd); + int i = 0; - boolean endResult = true; - for (StaticNatRuleTO rule : cmd.getRules()) { - // 1:1 NAT needs instanceip;publicip;domrip;op - args = rule.revoked() ? " -D " : " -A "; - - args += " -l " + rule.getSrcIp(); + boolean endResult = true; + for (StaticNatRuleTO rule : cmd.getRules()) { + // Prepare command to be send to VPC VR + String args = ""; + args += rule.revoked() ? " -D" : " -A"; + args += " -l " + rule.getSrcIp(); args += " -r " + rule.getDstIp(); - + + // Invoke command on VPC VR. + try { + Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpc_staticnat.sh " + args); + + if (s_logger.isDebugEnabled()) + s_logger.debug("Executing script on domain router " + controlIp + ": /opt/cloud/bin/vpc_staticnat.sh " + args); + + if (!result.first()) { + s_logger.error("SetVPCStaticNatRulesCommand failure on setting one rule. args: " + args); + results[i++] = "Failed"; + endResult = false; + } else { + results[i++] = null; + } + } catch (Throwable e) { + s_logger.error("SetVPCStaticNatRulesCommand (args: " + args + ") failed on setting one rule due to " + VmwareHelper.getExceptionMessage(e), e); + results[i++] = "Failed"; + endResult = false; + } + } + return new SetStaticNatRulesAnswer(cmd, results, endResult); + } + + protected Answer execute(SetStaticNatRulesCommand cmd) { + if ( cmd.getVpcId() != null ) { + return SetVPCStaticNatRules(cmd); + } + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource SetFirewallRuleCommand: " + _gson.toJson(cmd)); + } + + String args = null; + String[] results = new String[cmd.getRules().length]; + int i = 0; + boolean endResult = true; + for (StaticNatRuleTO rule : cmd.getRules()) { + // 1:1 NAT needs instanceip;publicip;domrip;op + args = rule.revoked() ? " -D " : " -A "; + + args += " -l " + rule.getSrcIp(); + args += " -r " + rule.getDstIp(); + if (rule.getProtocol() != null) { args += " -P " + rule.getProtocol().toLowerCase(); - } - - args += " -d " + rule.getStringSrcPortRange(); - args += " -G "; - - try { - VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - String controlIp = getRouterSshControlIp(cmd); - Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/root/firewall.sh " + args); - - if (s_logger.isDebugEnabled()) - s_logger.debug("Executing script on domain router " + controlIp + ": /root/firewall.sh " + args); - - if (!result.first()) { - s_logger.error("SetStaticNatRulesCommand failure on setting one rule. args: " + args); + } + + args += " -d " + rule.getStringSrcPortRange(); + args += " -G "; + + try { + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + String controlIp = getRouterSshControlIp(cmd); + Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/root/firewall.sh " + args); + + if (s_logger.isDebugEnabled()) + s_logger.debug("Executing script on domain router " + controlIp + ": /root/firewall.sh " + args); + + if (!result.first()) { + s_logger.error("SetStaticNatRulesCommand failure on setting one rule. args: " + args); results[i++] = "Failed"; - endResult = false; - } else { - results[i++] = null; - } - } catch (Throwable e) { - s_logger.error("SetStaticNatRulesCommand (args: " + args + ") failed on setting one rule due to " + VmwareHelper.getExceptionMessage(e), e); + endResult = false; + } else { + results[i++] = null; + } + } catch (Throwable e) { + s_logger.error("SetStaticNatRulesCommand (args: " + args + ") failed on setting one rule due to " + VmwareHelper.getExceptionMessage(e), e); results[i++] = "Failed"; - endResult = false; - } - } - return new SetStaticNatRulesAnswer(cmd, results, endResult); - } - - protected Answer execute(final LoadBalancerConfigCommand cmd) { - VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - File keyFile = mgr.getSystemVMKeyFile(); - - String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); - String controlIp = getRouterSshControlIp(cmd); - - assert(controlIp != null); - + endResult = false; + } + } + return new SetStaticNatRulesAnswer(cmd, results, endResult); + } + + protected Answer execute(final LoadBalancerConfigCommand cmd) { + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + File keyFile = mgr.getSystemVMKeyFile(); + + String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); + String controlIp = getRouterSshControlIp(cmd); + + assert(controlIp != null); + LoadBalancerConfigurator cfgtr = new HAProxyConfigurator(); - String[] config = cfgtr.generateConfiguration(cmd); - - String[][] rules = cfgtr.generateFwRules(cmd); - String tmpCfgFilePath = "/tmp/" + routerIp.replace('.', '_') + ".cfg"; - String tmpCfgFileContents = ""; - for (int i = 0; i < config.length; i++) { - tmpCfgFileContents += config[i]; - tmpCfgFileContents += "\n"; - } - - try { - SshHelper.scpTo(controlIp, DEFAULT_DOMR_SSHPORT, "root", keyFile, null, "/tmp/", tmpCfgFileContents.getBytes(), routerIp.replace('.', '_') + ".cfg", null); - - try { - String[] addRules = rules[LoadBalancerConfigurator.ADD]; + String[] config = cfgtr.generateConfiguration(cmd); + + String[][] rules = cfgtr.generateFwRules(cmd); + String tmpCfgFilePath = "/tmp/" + routerIp.replace('.', '_') + ".cfg"; + String tmpCfgFileContents = ""; + for (int i = 0; i < config.length; i++) { + tmpCfgFileContents += config[i]; + tmpCfgFileContents += "\n"; + } + + try { + SshHelper.scpTo(controlIp, DEFAULT_DOMR_SSHPORT, "root", keyFile, null, "/tmp/", tmpCfgFileContents.getBytes(), routerIp.replace('.', '_') + ".cfg", null); + + try { + String[] addRules = rules[LoadBalancerConfigurator.ADD]; String[] removeRules = rules[LoadBalancerConfigurator.REMOVE]; - String[] statRules = rules[LoadBalancerConfigurator.STATS]; - - String args = ""; - args += "-i " + routerIp; - args += " -f " + tmpCfgFilePath; - - StringBuilder sb = new StringBuilder(); - if (addRules.length > 0) { - for (int i = 0; i < addRules.length; i++) { - sb.append(addRules[i]).append(','); - } - - args += " -a " + sb.toString(); - } - - sb = new StringBuilder(); - if (removeRules.length > 0) { - for (int i = 0; i < removeRules.length; i++) { - sb.append(removeRules[i]).append(','); - } - - args += " -d " + sb.toString(); - } + String[] statRules = rules[LoadBalancerConfigurator.STATS]; + + String args = ""; + args += "-i " + routerIp; + args += " -f " + tmpCfgFilePath; + + StringBuilder sb = new StringBuilder(); + if (addRules.length > 0) { + for (int i = 0; i < addRules.length; i++) { + sb.append(addRules[i]).append(','); + } + + args += " -a " + sb.toString(); + } + + sb = new StringBuilder(); + if (removeRules.length > 0) { + for (int i = 0; i < removeRules.length; i++) { + sb.append(removeRules[i]).append(','); + } + + args += " -d " + sb.toString(); + } sb = new StringBuilder(); if (statRules.length > 0) { @@ -764,51 +804,51 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa args += " -s " + sb.toString(); } - - Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "scp " + tmpCfgFilePath + " /etc/haproxy/haproxy.cfg.new"); - - if (!result.first()) { - s_logger.error("Unable to copy haproxy configuration file"); - return new Answer(cmd, false, "LoadBalancerConfigCommand failed due to uanble to copy haproxy configuration file"); - } - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Run command on domain router " + routerIp + ", /root/loadbalancer.sh " + args); - } - - result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/root/loadbalancer.sh " + args); - - if (!result.first()) { - String msg = "LoadBalancerConfigCommand on domain router " + routerIp + " failed. message: " + result.second(); - s_logger.error(msg); - - return new Answer(cmd, false, msg); - } - - if (s_logger.isInfoEnabled()) { - s_logger.info("LoadBalancerConfigCommand on domain router " + routerIp + " completed"); - } - } finally { - SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "rm " + tmpCfgFilePath); - } - - return new Answer(cmd); - } catch (Throwable e) { - s_logger.error("Unexpected exception: " + e.toString(), e); - return new Answer(cmd, false, "LoadBalancerConfigCommand failed due to " + VmwareHelper.getExceptionMessage(e)); - } + + Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "scp " + tmpCfgFilePath + " /etc/haproxy/haproxy.cfg.new"); + + if (!result.first()) { + s_logger.error("Unable to copy haproxy configuration file"); + return new Answer(cmd, false, "LoadBalancerConfigCommand failed due to uanble to copy haproxy configuration file"); + } + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Run command on domain router " + routerIp + ", /root/loadbalancer.sh " + args); + } + + result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/root/loadbalancer.sh " + args); + + if (!result.first()) { + String msg = "LoadBalancerConfigCommand on domain router " + routerIp + " failed. message: " + result.second(); + s_logger.error(msg); + + return new Answer(cmd, false, msg); + } + + if (s_logger.isInfoEnabled()) { + s_logger.info("LoadBalancerConfigCommand on domain router " + routerIp + " completed"); + } + } finally { + SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "rm " + tmpCfgFilePath); + } + + return new Answer(cmd); + } catch (Throwable e) { + s_logger.error("Unexpected exception: " + e.toString(), e); + return new Answer(cmd, false, "LoadBalancerConfigCommand failed due to " + VmwareHelper.getExceptionMessage(e)); + } } - + // // list available ethx devices // ls /proc/sys/net/ipv4/conf // private int allocRouterEthDeviceIndex(String domrName, String routerIp) throws Exception { VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - + Pair result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "ls /proc/sys/net/ipv4/conf"); - + if(result.first()) { String[] tokens = result.second().split("\\s+"); HashMap deviceNames = new HashMap(); @@ -817,13 +857,13 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa deviceNames.put(token, token); } } - + for(int i = 1; ; i++) { if(!deviceNames.containsKey("eth" + i)) return i; } } - + return -1; } @@ -842,11 +882,11 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); s_logger.info("findRouterEthDeviceIndex. mac: " + mac); - + // TODO : this is a temporary very inefficient solution, will refactor it later Pair result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, - "ls /proc/sys/net/ipv4/conf"); - + "ls /proc/sys/net/ipv4/conf"); + // when we dynamically plug in a new NIC into virtual router, it may take time to show up in guest OS // we use a waiting loop here as a workaround to synchronize activities in systems long startTick = System.currentTimeMillis(); @@ -856,7 +896,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa for(String token : tokens) { if(!("all".equalsIgnoreCase(token) || "default".equalsIgnoreCase(token) || "lo".equalsIgnoreCase(token))) { String cmd = String.format("ip address show %s | grep link/ether | sed -e 's/^[ \t]*//' | cut -d' ' -f2", token); - + if(s_logger.isDebugEnabled()) s_logger.debug("Run domr script " + cmd); Pair result2 = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, @@ -864,26 +904,26 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa cmd); if(s_logger.isDebugEnabled()) s_logger.debug("result: " + result2.first() + ", output: " + result2.second()); - + if(result2.first() && result2.second().trim().equalsIgnoreCase(mac.trim())) return Integer.parseInt(token.substring(3)); } } } - + s_logger.warn("can not find intereface associated with mac: " + mac + ", guest OS may still at loading state, retry..."); - + try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { } } - + return -1; } - + private VirtualDevice findVirtualNicDevice(VirtualMachineMO vmMo, String mac) throws Exception { - + VirtualDevice[] nics = vmMo.getNicDevices(); for(VirtualDevice nic : nics) { if(nic instanceof VirtualEthernetCard) { @@ -893,15 +933,15 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } return null; } - + private SetupGuestNetworkAnswer execute(SetupGuestNetworkCommand cmd) { - + s_logger.info("Executing resource SetupGuestNetworkCommand " + _gson.toJson(cmd)); - + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - + NicTO nic = cmd.getNic(); - + String routerIp = getRouterSshControlIp(cmd); String domrGIP = cmd.getAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP); String domrName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); @@ -917,11 +957,11 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa dns += "," + dns2; } } - + try { int ethDeviceNum = findRouterEthDeviceIndex(domrName, routerIp, nic.getMac()); s_logger.info("find interface index. routerIp: " + routerIp + ", mac: " + nic.getMac() + ", index: " + ethDeviceNum); - + String args = "-C "; String dev = "eth" + ethDeviceNum; args += " -d " + dev; @@ -935,9 +975,9 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if ( domainName != null && !domainName.isEmpty() ) { args += " -e " + domainName; } - + Pair result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, - "/opt/cloud/bin/vpc_guestnw.sh " + args); + "/opt/cloud/bin/vpc_guestnw.sh " + args); if (!result.first()) { String msg = "SetupGuestNetworkCommand on domain router " + routerIp + " failed. message: " + result.second(); @@ -949,7 +989,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if (s_logger.isInfoEnabled()) { s_logger.info("SetupGuestNetworkCommand on domain router " + routerIp + " completed"); } - + return new SetupGuestNetworkAnswer(cmd, true, "success"); } catch (Exception e) { String msg = "SetupGuestNetwork failed due to " + e.toString(); @@ -962,12 +1002,12 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource IpAssocVpcCommand " + _gson.toJson(cmd)); } - + String[] results = new String[cmd.getIpAddresses().length]; int i = 0; String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); String routerIp = getRouterSshControlIp(cmd); - + try { IpAddressTO[] ips = cmd.getIpAddresses(); for (IpAddressTO ip : ips) { @@ -989,7 +1029,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - + String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); String routerIp = getRouterSshControlIp(cmd); IpAddressTO pubIp = cmd.getIpAddress(); @@ -1012,7 +1052,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa return new SetSourceNatAnswer(cmd, false, msg); } - + return new SetSourceNatAnswer(cmd, true, "success"); } catch (Exception e) { String msg = "Ip SNAT failure due to " + e.toString(); @@ -1029,7 +1069,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); String routerIp = getRouterSshControlIp(cmd); - + String[] results = new String[cmd.getRules().length]; try { String [][] rules = cmd.generateFwRules(); @@ -1038,11 +1078,11 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if (aclRules.length == 0) { return new SetNetworkACLAnswer(cmd, true, results); } - + for (int i = 0; i < aclRules.length; i++) { sb.append(aclRules[i]).append(','); } - + NicTO nic = cmd.getNic(); int ethDeviceNum = findRouterEthDeviceIndex(routerName, routerIp, nic.getMac()); String args = ""; @@ -1050,7 +1090,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa args += " -i " + nic.getIp(); args += " -m " + Long.toString(NetUtils.getCidrSize(nic.getNetmask())); args += " -a " + sb.toString(); - + Pair result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpc_acl.sh " + args); @@ -1060,7 +1100,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa return new SetNetworkACLAnswer(cmd, false, results); } - + return new SetNetworkACLAnswer(cmd, true, results); } catch (Exception e) { String msg = "SetNetworkACL failed due to " + e.toString(); @@ -1073,7 +1113,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource SetPortForwardingRulesVpcCommand " + _gson.toJson(cmd)); } - + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); String routerIp = getRouterSshControlIp(cmd); @@ -1094,7 +1134,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa try { Pair sshResult = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpc_portforwarding.sh " + args); - + if (!sshResult.first()) { results[i++] = "Failed"; endResult = false; @@ -1108,12 +1148,12 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } return new SetPortForwardingRulesAnswer(cmd, results, endResult); } - + protected Answer execute(Site2SiteVpnCfgCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource Site2SiteVpnCfgCommand " + _gson.toJson(cmd)); } - + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); String routerIp = getRouterSshControlIp(cmd); @@ -1122,23 +1162,23 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if (cmd.isCreate()) { args += " -A"; args += " -l "; - args += cmd.getLocalPublicIp(); + args += cmd.getLocalPublicIp(); args += " -n "; - args += cmd.getLocalGuestCidr(); + args += cmd.getLocalGuestCidr(); args += " -g "; - args += cmd.getLocalPublicGateway(); + args += cmd.getLocalPublicGateway(); args += " -r "; - args += cmd.getPeerGatewayIp(); + args += cmd.getPeerGatewayIp(); args += " -N "; - args += cmd.getPeerGuestCidrList(); + args += cmd.getPeerGuestCidrList(); args += " -e "; - args += "\"" + cmd.getEspPolicy() + "\""; + args += "\"" + cmd.getEspPolicy() + "\""; args += " -i "; - args += "\"" + cmd.getIkePolicy() + "\""; + args += "\"" + cmd.getIkePolicy() + "\""; args += " -t "; - args += Long.toString(cmd.getLifetime()); + args += Long.toString(cmd.getLifetime()); args += " -s "; - args += "\"" + cmd.getIpsecPsk() + "\""; + args += "\"" + cmd.getIpsecPsk() + "\""; } else { args += " -D"; args += " -r "; @@ -1146,7 +1186,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa args += " -N "; args += cmd.getPeerGuestCidrList(); } - + Pair result; try { result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, @@ -1168,12 +1208,12 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } return new Answer(cmd, true, result.second()); } - + private PlugNicAnswer execute(PlugNicCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource PlugNicCommand " + _gson.toJson(cmd)); } - + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); VmwareContext context = getServiceContext(); try { @@ -1185,7 +1225,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if(vmMo == null) { if(hyperHost instanceof HostMO) { ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), - ((HostMO)hyperHost).getParentMor()); + ((HostMO)hyperHost).getParentMor()); vmMo = clusterMo.findVmOnHyperHost(vmName); } } @@ -1195,10 +1235,10 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa s_logger.error(msg); throw new Exception(msg); } - + // TODO need a way to specify the control of NIC device type VirtualEthernetCardType nicDeviceType = VirtualEthernetCardType.E1000; - + // find a usable device number in VMware environment VirtualDevice[] nicDevices = vmMo.getNicDevices(); int deviceNumber = -1; @@ -1207,7 +1247,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa deviceNumber = device.getUnitNumber(); } deviceNumber++; - + NicTO nicTo = cmd.getNic(); VirtualDevice nic; Pair networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo); @@ -1219,36 +1259,36 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa dvSwitchUuid = dataCenterMo.getDvSwitchUuid(dvsMor); s_logger.info("Preparing NIC device on dvSwitch : " + dvSwitchUuid); nic = VmwareHelper.prepareDvNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), - dvSwitchUuid, nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true); + dvSwitchUuid, nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true); } else { s_logger.info("Preparing NIC device on network " + networkInfo.second()); nic = VmwareHelper.prepareNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), nicTo.getMac(), - deviceNumber, deviceNumber + 1, true, true); + deviceNumber, deviceNumber + 1, true, true); } - + VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1]; deviceConfigSpecArray[0] = new VirtualDeviceConfigSpec(); deviceConfigSpecArray[0].setDevice(nic); deviceConfigSpecArray[0].setOperation(VirtualDeviceConfigSpecOperation.add); - + vmConfigSpec.setDeviceChange(deviceConfigSpecArray); if(!vmMo.configureVm(vmConfigSpec)) { throw new Exception("Failed to configure devices when running PlugNicCommand"); } - + return new PlugNicAnswer(cmd, true, "success"); } catch(Exception e) { s_logger.error("Unexpected exception: ", e); return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + e.toString()); } } - + private UnPlugNicAnswer execute(UnPlugNicCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource UnPlugNicCommand " + _gson.toJson(cmd)); } - + VmwareContext context = getServiceContext(); try { VmwareHypervisorHost hyperHost = getHyperHost(context); @@ -1259,7 +1299,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if(vmMo == null) { if(hyperHost instanceof HostMO) { ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), - ((HostMO)hyperHost).getParentMor()); + ((HostMO)hyperHost).getParentMor()); vmMo = clusterMo.findVmOnHyperHost(vmName); } } @@ -1271,29 +1311,29 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } VirtualDevice nic = findVirtualNicDevice(vmMo, cmd.getNic().getMac()); - + VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1]; deviceConfigSpecArray[0] = new VirtualDeviceConfigSpec(); deviceConfigSpecArray[0].setDevice(nic); deviceConfigSpecArray[0].setOperation(VirtualDeviceConfigSpecOperation.remove); - + vmConfigSpec.setDeviceChange(deviceConfigSpecArray); if(!vmMo.configureVm(vmConfigSpec)) { throw new Exception("Failed to configure devices when running unplugNicCommand"); } - + return new UnPlugNicAnswer(cmd, true, "success"); } catch(Exception e) { s_logger.error("Unexpected exception: ", e); return new UnPlugNicAnswer(cmd, false, "Unable to execute unPlugNicCommand due to " + e.toString()); } } - + protected void assignVPCPublicIpAddress(String domrName, String routerIp, IpAddressTO ip) throws Exception { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource assignVPCPublicIpAddress. domrName: " + domrName + ", routerIp: " + routerIp - + ", ip: " + _gson.toJson(ip)); + + ", ip: " + _gson.toJson(ip)); } VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); @@ -1302,7 +1342,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if (ethDeviceNum < 0) { throw new InternalErrorException("Failed to find DomR VIF to associate/disassociate IP with."); } - + String args = ""; if (ip.isAdd()) { args += " -A "; @@ -1315,126 +1355,126 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa args += " -c "; args += "eth" + ethDeviceNum; - + args += " -g "; args += ip.getVlanGateway(); - + args += " -m "; args += Long.toString(NetUtils.getCidrSize(ip.getVlanNetmask())); - + args += " -n "; args += NetUtils.getSubNet(ip.getPublicIp(), ip.getVlanNetmask()); Pair result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpc_ipassoc.sh " + args); - + if (!result.first()) { throw new InternalErrorException("Unable to assign public IP address"); } } - protected void assignPublicIpAddress(VirtualMachineMO vmMo, final String vmName, final String privateIpAddress, final String publicIpAddress, final boolean add, final boolean firstIP, - final boolean sourceNat, final String vlanId, final String vlanGateway, final String vlanNetmask, final String vifMacAddress, String guestIp) throws Exception { - - String publicNeworkName = HypervisorHostHelper.getPublicNetworkNamePrefix(vlanId); - Pair publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName); - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Find public NIC index, public network name: " + publicNeworkName + ", index: " + publicNicInfo.first()); - } - - boolean addVif = false; - boolean removeVif = false; - if (add && publicNicInfo.first().intValue() == -1) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Plug new NIC to associate" + privateIpAddress + " to " + publicIpAddress); - } - - addVif = true; - } else if (!add && firstIP) { - removeVif = true; - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Unplug NIC " + publicNicInfo.first()); - } - } - - if (addVif) { - plugPublicNic(vmMo, vlanId, vifMacAddress); - publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName); - if (publicNicInfo.first().intValue() >= 0) { - networkUsage(privateIpAddress, "addVif", "eth" + publicNicInfo.first()); - } - } - - if (publicNicInfo.first().intValue() < 0) { - String msg = "Failed to find DomR VIF to associate/disassociate IP with."; - s_logger.error(msg); - throw new InternalErrorException(msg); - } - - String args = null; - - if (add) { - args = " -A "; - } else { - args = " -D "; - } - if (sourceNat) { - args += " -s "; - } - if (firstIP) { - args += " -f "; + protected void assignPublicIpAddress(VirtualMachineMO vmMo, final String vmName, final String privateIpAddress, final String publicIpAddress, final boolean add, final boolean firstIP, + final boolean sourceNat, final String vlanId, final String vlanGateway, final String vlanNetmask, final String vifMacAddress, String guestIp) throws Exception { + + String publicNeworkName = HypervisorHostHelper.getPublicNetworkNamePrefix(vlanId); + Pair publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName); + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Find public NIC index, public network name: " + publicNeworkName + ", index: " + publicNicInfo.first()); } - String cidrSize = Long.toString(NetUtils.getCidrSize(vlanNetmask)); - args += " -l "; - args += publicIpAddress + "/" + cidrSize; - args += " -c "; + boolean addVif = false; + boolean removeVif = false; + if (add && publicNicInfo.first().intValue() == -1) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Plug new NIC to associate" + privateIpAddress + " to " + publicIpAddress); + } + + addVif = true; + } else if (!add && firstIP) { + removeVif = true; + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Unplug NIC " + publicNicInfo.first()); + } + } + + if (addVif) { + plugPublicNic(vmMo, vlanId, vifMacAddress); + publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName); + if (publicNicInfo.first().intValue() >= 0) { + networkUsage(privateIpAddress, "addVif", "eth" + publicNicInfo.first()); + } + } + + if (publicNicInfo.first().intValue() < 0) { + String msg = "Failed to find DomR VIF to associate/disassociate IP with."; + s_logger.error(msg); + throw new InternalErrorException(msg); + } + + String args = null; + + if (add) { + args = " -A "; + } else { + args = " -D "; + } + if (sourceNat) { + args += " -s "; + } + if (firstIP) { + args += " -f "; + } + + String cidrSize = Long.toString(NetUtils.getCidrSize(vlanNetmask)); + args += " -l "; + args += publicIpAddress + "/" + cidrSize; + args += " -c "; args += "eth" + publicNicInfo.first(); - + args += " -g "; - args += vlanGateway; - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Run command on domain router " + privateIpAddress + ", /opt/cloud/bin/ipassoc.sh " + args); - } - - VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - Pair result = SshHelper.sshExecute(privateIpAddress, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/ipassoc.sh " + args); - - if (!result.first()) { - s_logger.error("ipassoc command on domain router " + privateIpAddress + " failed. message: " + result.second()); - throw new Exception("ipassoc failed due to " + result.second()); - } - + args += vlanGateway; + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Run command on domain router " + privateIpAddress + ", /opt/cloud/bin/ipassoc.sh " + args); + } + + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + Pair result = SshHelper.sshExecute(privateIpAddress, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/ipassoc.sh " + args); + + if (!result.first()) { + s_logger.error("ipassoc command on domain router " + privateIpAddress + " failed. message: " + result.second()); + throw new Exception("ipassoc failed due to " + result.second()); + } + if (removeVif) { - - String nicMasksStr = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK); - int nicMasks = Integer.parseInt(nicMasksStr); - nicMasks &= ~(1 << publicNicInfo.first().intValue()); - vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, String.valueOf(nicMasks)); - - HostMO hostMo = vmMo.getRunningHost(); - List networks = vmMo.getNetworksWithDetails(); - for (NetworkDetails netDetails : networks) { - if (netDetails.getGCTag() != null && netDetails.getGCTag().equalsIgnoreCase("true")) { - if (netDetails.getVMMorsOnNetwork() == null || netDetails.getVMMorsOnNetwork().length == 1) { - cleanupNetwork(hostMo, netDetails); - } - } - } - } - - if (s_logger.isInfoEnabled()) { - s_logger.info("ipassoc command on domain router " + privateIpAddress + " completed"); - } - } - - private void plugPublicNic(VirtualMachineMO vmMo, final String vlanId, final String vifMacAddress) throws Exception { + + String nicMasksStr = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK); + int nicMasks = Integer.parseInt(nicMasksStr); + nicMasks &= ~(1 << publicNicInfo.first().intValue()); + vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, String.valueOf(nicMasks)); + + HostMO hostMo = vmMo.getRunningHost(); + List networks = vmMo.getNetworksWithDetails(); + for (NetworkDetails netDetails : networks) { + if (netDetails.getGCTag() != null && netDetails.getGCTag().equalsIgnoreCase("true")) { + if (netDetails.getVMMorsOnNetwork() == null || netDetails.getVMMorsOnNetwork().length == 1) { + cleanupNetwork(hostMo, netDetails); + } + } + } + } + + if (s_logger.isInfoEnabled()) { + s_logger.info("ipassoc command on domain router " + privateIpAddress + " completed"); + } + } + + private void plugPublicNic(VirtualMachineMO vmMo, final String vlanId, final String vifMacAddress) throws Exception { // TODO : probably need to set traffic shaping Pair networkInfo = null; - + if (!_nexusVSwitch) { networkInfo = HypervisorHostHelper.prepareNetwork(this._publicNetworkVSwitchName, "cloud.public", vmMo.getRunningHost(), vlanId, null, null, this._ops_timeout, true); @@ -1466,148 +1506,148 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa deviceConfigSpecArray[0] = new VirtualDeviceConfigSpec(); deviceConfigSpecArray[0].setDevice(device); deviceConfigSpecArray[0].setOperation(VirtualDeviceConfigSpecOperation.edit); - + vmConfigSpec.setDeviceChange(deviceConfigSpecArray); if(!vmMo.configureVm(vmConfigSpec)) { throw new Exception("Failed to configure devices when plugPublicNic"); } } catch(Exception e) { - - // restore allocation mask in case of exceptions - String nicMasksStr = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK); - int nicMasks = Integer.parseInt(nicMasksStr); - nicMasks &= ~(1 << nicIndex); - vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, String.valueOf(nicMasks)); - - throw e; + + // restore allocation mask in case of exceptions + String nicMasksStr = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK); + int nicMasks = Integer.parseInt(nicMasksStr); + nicMasks &= ~(1 << nicIndex); + vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, String.valueOf(nicMasks)); + + throw e; } } - + private int allocPublicNicIndex(VirtualMachineMO vmMo) throws Exception { - String nicMasksStr = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK); - if(nicMasksStr == null || nicMasksStr.isEmpty()) { - throw new Exception("Could not find NIC allocation info"); - } - - int nicMasks = Integer.parseInt(nicMasksStr); - VirtualDevice[] nicDevices = vmMo.getNicDevices(); - for(int i = 3; i < nicDevices.length; i++) { - if((nicMasks & (1 << i)) == 0) { - nicMasks |= (1 << i); - vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, String.valueOf(nicMasks)); - return i; - } - } - - throw new Exception("Could not allocate a free public NIC"); + String nicMasksStr = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK); + if(nicMasksStr == null || nicMasksStr.isEmpty()) { + throw new Exception("Could not find NIC allocation info"); + } + + int nicMasks = Integer.parseInt(nicMasksStr); + VirtualDevice[] nicDevices = vmMo.getNicDevices(); + for(int i = 3; i < nicDevices.length; i++) { + if((nicMasks & (1 << i)) == 0) { + nicMasks |= (1 << i); + vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, String.valueOf(nicMasks)); + return i; + } + } + + throw new Exception("Could not allocate a free public NIC"); } - - protected Answer execute(IpAssocCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource IPAssocCommand: " + _gson.toJson(cmd)); - } - - int i = 0; - String[] results = new String[cmd.getIpAddresses().length]; - - VmwareContext context = getServiceContext(); - try { - VmwareHypervisorHost hyperHost = getHyperHost(context); - - IpAddressTO[] ips = cmd.getIpAddresses(); - String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); - String controlIp = VmwareResource.getRouterSshControlIp(cmd); - - VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(routerName); + + protected Answer execute(IpAssocCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource IPAssocCommand: " + _gson.toJson(cmd)); + } + + int i = 0; + String[] results = new String[cmd.getIpAddresses().length]; + + VmwareContext context = getServiceContext(); + try { + VmwareHypervisorHost hyperHost = getHyperHost(context); + + IpAddressTO[] ips = cmd.getIpAddresses(); + String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); + String controlIp = VmwareResource.getRouterSshControlIp(cmd); + + VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(routerName); // command may sometimes be redirect to a wrong host, we relax // the check and will try to find it within cluster if(vmMo == null) { if(hyperHost instanceof HostMO) { ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), - ((HostMO)hyperHost).getParentMor()); + ((HostMO)hyperHost).getParentMor()); vmMo = clusterMo.findVmOnHyperHost(routerName); } } - if (vmMo == null) { - String msg = "Router " + routerName + " no longer exists to execute IPAssoc command"; - s_logger.error(msg); - throw new Exception(msg); - } - - for (IpAddressTO ip : ips) { - assignPublicIpAddress(vmMo, routerName, controlIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(), ip.isSourceNat(), ip.getVlanId(), ip.getVlanGateway(), ip.getVlanNetmask(), - ip.getVifMacAddress(), ip.getGuestIp()); - results[i++] = ip.getPublicIp() + " - success"; - } - } catch (Throwable e) { - s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e); - - for (; i < cmd.getIpAddresses().length; i++) { - results[i++] = IpAssocAnswer.errorResult; - } - } - - return new IpAssocAnswer(cmd, results); - } - - protected Answer execute(SavePasswordCommand cmd) { - if (s_logger.isInfoEnabled()) { + if (vmMo == null) { + String msg = "Router " + routerName + " no longer exists to execute IPAssoc command"; + s_logger.error(msg); + throw new Exception(msg); + } + + for (IpAddressTO ip : ips) { + assignPublicIpAddress(vmMo, routerName, controlIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(), ip.isSourceNat(), ip.getVlanId(), ip.getVlanGateway(), ip.getVlanNetmask(), + ip.getVifMacAddress(), ip.getGuestIp()); + results[i++] = ip.getPublicIp() + " - success"; + } + } catch (Throwable e) { + s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e); + + for (; i < cmd.getIpAddresses().length; i++) { + results[i++] = IpAssocAnswer.errorResult; + } + } + + return new IpAssocAnswer(cmd, results); + } + + protected Answer execute(SavePasswordCommand cmd) { + if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource SavePasswordCommand. vmName: " + cmd.getVmName() + ", vmIp: " + cmd.getVmIpAddress() + ", password: " - + StringUtils.getMaskedPasswordForDisplay(cmd.getPassword())); - } - + + StringUtils.getMaskedPasswordForDisplay(cmd.getPassword())); + } + String controlIp = getRouterSshControlIp(cmd); - final String password = cmd.getPassword(); - final String vmIpAddress = cmd.getVmIpAddress(); - - // Run save_password_to_domr.sh + final String password = cmd.getPassword(); + final String vmIpAddress = cmd.getVmIpAddress(); + + // Run save_password_to_domr.sh String args = " -v " + vmIpAddress; if (s_logger.isDebugEnabled()) { s_logger.debug("Run command on domain router " + controlIp + ", /root/savepassword.sh " + args + " -p " + StringUtils.getMaskedPasswordForDisplay(cmd.getPassword())); } - - args += " -p " + password; - - - try { - VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/root/savepassword.sh " + args); - - if (!result.first()) { - s_logger.error("savepassword command on domain router " + controlIp + " failed, message: " + result.second()); - - return new Answer(cmd, false, "SavePassword failed due to " + result.second()); - } - - if (s_logger.isInfoEnabled()) { - s_logger.info("savepassword command on domain router " + controlIp + " completed"); - } - - } catch (Throwable e) { - String msg = "SavePasswordCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new Answer(cmd, false, msg); - } - return new Answer(cmd); - } - - protected Answer execute(DhcpEntryCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource DhcpEntryCommand: " + _gson.toJson(cmd)); - } - + + args += " -p " + password; + + + try { + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/root/savepassword.sh " + args); + + if (!result.first()) { + s_logger.error("savepassword command on domain router " + controlIp + " failed, message: " + result.second()); + + return new Answer(cmd, false, "SavePassword failed due to " + result.second()); + } + + if (s_logger.isInfoEnabled()) { + s_logger.info("savepassword command on domain router " + controlIp + " completed"); + } + + } catch (Throwable e) { + String msg = "SavePasswordCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new Answer(cmd, false, msg); + } + return new Answer(cmd); + } + + protected Answer execute(DhcpEntryCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource DhcpEntryCommand: " + _gson.toJson(cmd)); + } + // ssh -p 3922 -o StrictHostKeyChecking=no -i $cert root@$domr "/root/edithosts.sh $mac $ip $vm $dfltrt $ns $staticrt" >/dev/null - String args = " " + cmd.getVmMac(); - args += " " + cmd.getVmIpAddress(); + String args = " " + cmd.getVmMac(); + args += " " + cmd.getVmIpAddress(); args += " " + cmd.getVmName(); - + if (cmd.getDefaultRouter() != null) { args += " " + cmd.getDefaultRouter(); } - + if (cmd.getDefaultDns() != null) { args += " " + cmd.getDefaultDns(); } @@ -1615,36 +1655,36 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if (cmd.getStaticRoutes() != null) { args += " " + cmd.getStaticRoutes(); } - + if (s_logger.isDebugEnabled()) { - s_logger.debug("Run command on domR " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + ", /root/edithosts.sh " + args); - } - - try { - VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - String controlIp = getRouterSshControlIp(cmd); - Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, - "/root/edithosts.sh " + args); - - if (!result.first()) { - s_logger.error("dhcp_entry command on domR " + controlIp + " failed, message: " + result.second()); - - return new Answer(cmd, false, "DhcpEntry failed due to " + result.second()); - } - - if (s_logger.isInfoEnabled()) { - s_logger.info("dhcp_entry command on domain router " + controlIp + " completed"); - } - - } catch (Throwable e) { - String msg = "DhcpEntryCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new Answer(cmd, false, msg); - } - - return new Answer(cmd); + s_logger.debug("Run command on domR " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + ", /root/edithosts.sh " + args); + } + + try { + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + String controlIp = getRouterSshControlIp(cmd); + Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, + "/root/edithosts.sh " + args); + + if (!result.first()) { + s_logger.error("dhcp_entry command on domR " + controlIp + " failed, message: " + result.second()); + + return new Answer(cmd, false, "DhcpEntry failed due to " + result.second()); + } + + if (s_logger.isInfoEnabled()) { + s_logger.info("dhcp_entry command on domain router " + controlIp + " completed"); + } + + } catch (Throwable e) { + String msg = "DhcpEntryCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new Answer(cmd, false, msg); + } + + return new Answer(cmd); } - + protected CheckS2SVpnConnectionsAnswer execute(CheckS2SVpnConnectionsCommand cmd) { if (s_logger.isDebugEnabled()) { s_logger.debug("Executing resource CheckS2SVpnConnectionsCommand: " + _gson.toJson(cmd)); @@ -1703,7 +1743,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } return new CheckRouterAnswer(cmd, result.second(), true); } - + protected Answer execute(GetDomRVersionCmd cmd) { if (s_logger.isDebugEnabled()) { s_logger.debug("Executing resource GetDomRVersionCmd: " + _gson.toJson(cmd)); @@ -1737,7 +1777,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } return new GetDomRVersionAnswer(cmd, result.second(), lines[0], lines[1]); } - + protected Answer execute(BumpUpPriorityCommand cmd) { if (s_logger.isDebugEnabled()) { s_logger.debug("Executing resource BumpUpPriorityCommand: " + _gson.toJson(cmd)); @@ -1771,372 +1811,372 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa return new Answer(cmd, false, result.second()); } - protected Answer execute(VmDataCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource VmDataCommand: " + _gson.toJson(cmd)); - } - - String routerPrivateIpAddress = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); - String controlIp = getRouterSshControlIp(cmd); - - String vmIpAddress = cmd.getVmIpAddress(); - List vmData = cmd.getVmData(); - String[] vmDataArgs = new String[vmData.size() * 2 + 4]; - vmDataArgs[0] = "routerIP"; - vmDataArgs[1] = routerPrivateIpAddress; - vmDataArgs[2] = "vmIP"; - vmDataArgs[3] = vmIpAddress; - int i = 4; - for (String[] vmDataEntry : vmData) { - String folder = vmDataEntry[0]; - String file = vmDataEntry[1]; - String contents = (vmDataEntry[2] != null) ? vmDataEntry[2] : "none"; - - vmDataArgs[i] = folder + "," + file; - vmDataArgs[i + 1] = contents; - i += 2; - } - - String content = encodeDataArgs(vmDataArgs); - String tmpFileName = UUID.randomUUID().toString(); - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Run vm_data command on domain router " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + ", data: " + content); - } - - try { - VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - SshHelper.scpTo(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/tmp", content.getBytes(), tmpFileName, null); - - try { - Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, - "/root/userdata.py " + tmpFileName); - - if (!result.first()) { - s_logger.error("vm_data command on domain router " + controlIp + " failed. messge: " + result.second()); - return new Answer(cmd, false, "VmDataCommand failed due to " + result.second()); - } - } finally { - - SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "rm /tmp/" + tmpFileName); - } - - if (s_logger.isInfoEnabled()) { - s_logger.info("vm_data command on domain router " + controlIp + " completed"); - } - - } catch (Throwable e) { - String msg = "VmDataCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new Answer(cmd, false, msg); - } - return new Answer(cmd); - } - - private String encodeDataArgs(String[] dataArgs) { - StringBuilder sb = new StringBuilder(); - - for (String arg : dataArgs) { - sb.append(arg); - sb.append("\n"); - } - - return sb.toString(); - } - - protected CheckSshAnswer execute(CheckSshCommand cmd) { - String vmName = cmd.getName(); - String privateIp = cmd.getIp(); - int cmdPort = cmd.getPort(); - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Ping command port, " + privateIp + ":" + cmdPort); - } - - try { - String result = connect(cmd.getName(), privateIp, cmdPort); - if (result != null) { - s_logger.error("Can not ping System vm " + vmName + "due to:" + result); - return new CheckSshAnswer(cmd, "Can not ping System vm " + vmName + "due to:" + result); - } - } catch (Exception e) { - s_logger.error("Can not ping System vm " + vmName + "due to exception"); - return new CheckSshAnswer(cmd, e); - } - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Ping command port succeeded for vm " + vmName); - } - - if (VirtualMachineName.isValidRouterName(vmName)) { + protected Answer execute(VmDataCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource VmDataCommand: " + _gson.toJson(cmd)); + } + + String routerPrivateIpAddress = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); + String controlIp = getRouterSshControlIp(cmd); + + String vmIpAddress = cmd.getVmIpAddress(); + List vmData = cmd.getVmData(); + String[] vmDataArgs = new String[vmData.size() * 2 + 4]; + vmDataArgs[0] = "routerIP"; + vmDataArgs[1] = routerPrivateIpAddress; + vmDataArgs[2] = "vmIP"; + vmDataArgs[3] = vmIpAddress; + int i = 4; + for (String[] vmDataEntry : vmData) { + String folder = vmDataEntry[0]; + String file = vmDataEntry[1]; + String contents = (vmDataEntry[2] != null) ? vmDataEntry[2] : "none"; + + vmDataArgs[i] = folder + "," + file; + vmDataArgs[i + 1] = contents; + i += 2; + } + + String content = encodeDataArgs(vmDataArgs); + String tmpFileName = UUID.randomUUID().toString(); + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Run vm_data command on domain router " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + ", data: " + content); + } + + try { + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + SshHelper.scpTo(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/tmp", content.getBytes(), tmpFileName, null); + + try { + Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, + "/root/userdata.py " + tmpFileName); + + if (!result.first()) { + s_logger.error("vm_data command on domain router " + controlIp + " failed. messge: " + result.second()); + return new Answer(cmd, false, "VmDataCommand failed due to " + result.second()); + } + } finally { + + SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "rm /tmp/" + tmpFileName); + } + + if (s_logger.isInfoEnabled()) { + s_logger.info("vm_data command on domain router " + controlIp + " completed"); + } + + } catch (Throwable e) { + String msg = "VmDataCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new Answer(cmd, false, msg); + } + return new Answer(cmd); + } + + private String encodeDataArgs(String[] dataArgs) { + StringBuilder sb = new StringBuilder(); + + for (String arg : dataArgs) { + sb.append(arg); + sb.append("\n"); + } + + return sb.toString(); + } + + protected CheckSshAnswer execute(CheckSshCommand cmd) { + String vmName = cmd.getName(); + String privateIp = cmd.getIp(); + int cmdPort = cmd.getPort(); + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Ping command port, " + privateIp + ":" + cmdPort); + } + + try { + String result = connect(cmd.getName(), privateIp, cmdPort); + if (result != null) { + s_logger.error("Can not ping System vm " + vmName + "due to:" + result); + return new CheckSshAnswer(cmd, "Can not ping System vm " + vmName + "due to:" + result); + } + } catch (Exception e) { + s_logger.error("Can not ping System vm " + vmName + "due to exception"); + return new CheckSshAnswer(cmd, e); + } + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Ping command port succeeded for vm " + vmName); + } + + if (VirtualMachineName.isValidRouterName(vmName)) { if (s_logger.isDebugEnabled()) { s_logger.debug("Execute network usage setup command on " + vmName); - } - networkUsage(privateIp, "create", null); - } - - return new CheckSshAnswer(cmd); - } - - private VolumeTO[] validateDisks(VolumeTO[] disks) { - List validatedDisks = new ArrayList(); - - for (VolumeTO vol : disks) { - if (vol.getPoolUuid() != null && !vol.getPoolUuid().isEmpty()) { - validatedDisks.add(vol); - } else if (vol.getPoolType() == StoragePoolType.ISO && (vol.getPath() != null && !vol.getPath().isEmpty())) { - validatedDisks.add(vol); - } else { + } + networkUsage(privateIp, "create", null); + } + + return new CheckSshAnswer(cmd); + } + + private VolumeTO[] validateDisks(VolumeTO[] disks) { + List validatedDisks = new ArrayList(); + + for (VolumeTO vol : disks) { + if (vol.getPoolUuid() != null && !vol.getPoolUuid().isEmpty()) { + validatedDisks.add(vol); + } else if (vol.getPoolType() == StoragePoolType.ISO && (vol.getPath() != null && !vol.getPath().isEmpty())) { + validatedDisks.add(vol); + } else { if (s_logger.isDebugEnabled()) { s_logger.debug("Drop invalid disk option, volumeTO: " + _gson.toJson(vol)); - } - } - } - - return validatedDisks.toArray(new VolumeTO[0]); - } - - protected StartAnswer execute(StartCommand cmd) { - - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource StartCommand: " + _gson.toJson(cmd)); - } - - VirtualMachineTO vmSpec = cmd.getVirtualMachine(); + } + } + } + + return validatedDisks.toArray(new VolumeTO[0]); + } + + protected StartAnswer execute(StartCommand cmd) { + + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource StartCommand: " + _gson.toJson(cmd)); + } + + VirtualMachineTO vmSpec = cmd.getVirtualMachine(); String vmName = vmSpec.getName(); - - State state = State.Stopped; - VmwareContext context = getServiceContext(); - try { - VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - - // mark VM as starting state so that sync() can know not to report stopped too early - synchronized (_vms) { - _vms.put(vmName, State.Starting); - } + + State state = State.Stopped; + VmwareContext context = getServiceContext(); + try { + VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + + // mark VM as starting state so that sync() can know not to report stopped too early + synchronized (_vms) { + _vms.put(vmName, State.Starting); + } VirtualEthernetCardType nicDeviceType = VirtualEthernetCardType.valueOf(vmSpec.getDetails().get(VmDetailConstants.NIC_ADAPTER)); if(s_logger.isDebugEnabled()) - s_logger.debug("VM " + vmName + " will be started with NIC device type: " + nicDeviceType); - - VmwareHypervisorHost hyperHost = getHyperHost(context); - VolumeTO[] disks = validateDisks(vmSpec.getDisks()); - assert (disks.length > 0); - NicTO[] nics = vmSpec.getNics(); - - HashMap> dataStoresDetails = inferDatastoreDetailsFromDiskInfo(hyperHost, context, disks); - if ((dataStoresDetails == null) || (dataStoresDetails.isEmpty()) ){ - String msg = "Unable to locate datastore details of the volumes to be attached"; - s_logger.error(msg); - throw new Exception(msg); - } - - VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName); - if (vmMo != null) { - s_logger.info("VM " + vmName + " already exists, tear down devices for reconfiguration"); - if (getVmState(vmMo) != State.Stopped) - vmMo.safePowerOff(_shutdown_waitMs); - vmMo.tearDownDevices(new Class[] { VirtualDisk.class, VirtualEthernetCard.class }); + s_logger.debug("VM " + vmName + " will be started with NIC device type: " + nicDeviceType); + + VmwareHypervisorHost hyperHost = getHyperHost(context); + VolumeTO[] disks = validateDisks(vmSpec.getDisks()); + assert (disks.length > 0); + NicTO[] nics = vmSpec.getNics(); + + HashMap> dataStoresDetails = inferDatastoreDetailsFromDiskInfo(hyperHost, context, disks); + if ((dataStoresDetails == null) || (dataStoresDetails.isEmpty()) ){ + String msg = "Unable to locate datastore details of the volumes to be attached"; + s_logger.error(msg); + throw new Exception(msg); + } + + VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName); + if (vmMo != null) { + s_logger.info("VM " + vmName + " already exists, tear down devices for reconfiguration"); + if (getVmState(vmMo) != State.Stopped) + vmMo.safePowerOff(_shutdown_waitMs); + vmMo.tearDownDevices(new Class[] { VirtualDisk.class, VirtualEthernetCard.class }); vmMo.ensureScsiDeviceController(); - } else { - ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter(); - assert (morDc != null); - - vmMo = hyperHost.findVmOnPeerHyperHost(vmName); - if (vmMo != null) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Found vm " + vmName + " at other host, relocate to " + hyperHost.getHyperHostName()); - } - - takeVmFromOtherHyperHost(hyperHost, vmName); - - if (getVmState(vmMo) != State.Stopped) - vmMo.safePowerOff(_shutdown_waitMs); + } else { + ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter(); + assert (morDc != null); + + vmMo = hyperHost.findVmOnPeerHyperHost(vmName); + if (vmMo != null) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Found vm " + vmName + " at other host, relocate to " + hyperHost.getHyperHostName()); + } + + takeVmFromOtherHyperHost(hyperHost, vmName); + + if (getVmState(vmMo) != State.Stopped) + vmMo.safePowerOff(_shutdown_waitMs); vmMo.tearDownDevices(new Class[] { VirtualDisk.class, VirtualEthernetCard.class }); vmMo.ensureScsiDeviceController(); - } else { + } else { int ramMb = (int) (vmSpec.getMinRam() / (1024 * 1024)); Pair rootDiskDataStoreDetails = null; for (VolumeTO vol : disks) { if (vol.getType() == Volume.Type.ROOT) { - rootDiskDataStoreDetails = dataStoresDetails.get(vol.getPoolUuid()); + rootDiskDataStoreDetails = dataStoresDetails.get(vol.getPoolUuid()); } } - assert (vmSpec.getSpeed() != null) && (rootDiskDataStoreDetails != null); + assert (vmSpec.getSpeed() != null) && (rootDiskDataStoreDetails != null); if (!hyperHost.createBlankVm(vmName, vmSpec.getCpus(), vmSpec.getSpeed().intValue(), - getReserveCpuMHz(vmSpec.getSpeed().intValue()), vmSpec.getLimitCpuUse(), ramMb, getReserveMemMB(ramMb), - translateGuestOsIdentifier(vmSpec.getArch(), vmSpec.getOs()).toString(), rootDiskDataStoreDetails.first(), false)) { - throw new Exception("Failed to create VM. vmName: " + vmName); - } - } - - vmMo = hyperHost.findVmOnHyperHost(vmName); - if (vmMo == null) { - throw new Exception("Failed to find the newly create or relocated VM. vmName: " + vmName); - } - } - - int totalChangeDevices = disks.length + nics.length; - VolumeTO volIso = null; - if (vmSpec.getType() != VirtualMachine.Type.User) { - // system VM needs a patch ISO - totalChangeDevices++; - } else { - for (VolumeTO vol : disks) { - if (vol.getType() == Volume.Type.ISO) { - volIso = vol; - break; - } - } - - if (volIso == null) - totalChangeDevices++; - } - - VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); - int ramMb = (int) (vmSpec.getMinRam() / (1024 * 1024)); + getReserveCpuMHz(vmSpec.getSpeed().intValue()), vmSpec.getLimitCpuUse(), ramMb, getReserveMemMB(ramMb), + translateGuestOsIdentifier(vmSpec.getArch(), vmSpec.getOs()).toString(), rootDiskDataStoreDetails.first(), false)) { + throw new Exception("Failed to create VM. vmName: " + vmName); + } + } + + vmMo = hyperHost.findVmOnHyperHost(vmName); + if (vmMo == null) { + throw new Exception("Failed to find the newly create or relocated VM. vmName: " + vmName); + } + } + + int totalChangeDevices = disks.length + nics.length; + VolumeTO volIso = null; + if (vmSpec.getType() != VirtualMachine.Type.User) { + // system VM needs a patch ISO + totalChangeDevices++; + } else { + for (VolumeTO vol : disks) { + if (vol.getType() == Volume.Type.ISO) { + volIso = vol; + break; + } + } + + if (volIso == null) + totalChangeDevices++; + } + + VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); + int ramMb = (int) (vmSpec.getMinRam() / (1024 * 1024)); VmwareHelper.setBasicVmConfig(vmConfigSpec, vmSpec.getCpus(), vmSpec.getSpeed().intValue(), - getReserveCpuMHz(vmSpec.getSpeed().intValue()), ramMb, getReserveMemMB(ramMb), - translateGuestOsIdentifier(vmSpec.getArch(), vmSpec.getOs()).toString(), vmSpec.getLimitCpuUse()); - - VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[totalChangeDevices]; - int i = 0; - int ideControllerKey = vmMo.getIDEDeviceControllerKey(); - int scsiControllerKey = vmMo.getScsiDeviceControllerKey(); - int controllerKey; - String datastoreDiskPath; - - // prepare systemvm patch ISO - if (vmSpec.getType() != VirtualMachine.Type.User) { - // attach ISO (for patching of system VM) + getReserveCpuMHz(vmSpec.getSpeed().intValue()), ramMb, getReserveMemMB(ramMb), + translateGuestOsIdentifier(vmSpec.getArch(), vmSpec.getOs()).toString(), vmSpec.getLimitCpuUse()); + + VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[totalChangeDevices]; + int i = 0; + int ideControllerKey = vmMo.getIDEDeviceControllerKey(); + int scsiControllerKey = vmMo.getScsiDeviceControllerKey(); + int controllerKey; + String datastoreDiskPath; + + // prepare systemvm patch ISO + if (vmSpec.getType() != VirtualMachine.Type.User) { + // attach ISO (for patching of system VM) String secStoreUrl = mgr.getSecondaryStorageStoreUrl(Long.parseLong(_dcId)); if(secStoreUrl == null) { String msg = "secondary storage for dc " + _dcId + " is not ready yet?"; throw new Exception(msg); } mgr.prepareSecondaryStorageStore(secStoreUrl); - - ManagedObjectReference morSecDs = prepareSecondaryDatastoreOnHost(secStoreUrl); - if (morSecDs == null) { - String msg = "Failed to prepare secondary storage on host, secondary store url: " + secStoreUrl; - throw new Exception(msg); - } - DatastoreMO secDsMo = new DatastoreMO(hyperHost.getContext(), morSecDs); - - deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec(); - Pair isoInfo = VmwareHelper.prepareIsoDevice(vmMo, String.format("[%s] systemvm/%s", secDsMo.getName(), mgr.getSystemVMIsoFileNameOnDatastore()), - secDsMo.getMor(), true, true, i, i + 1); - deviceConfigSpecArray[i].setDevice(isoInfo.first()); - if (isoInfo.second()) { - if(s_logger.isDebugEnabled()) - s_logger.debug("Prepare ISO volume at new device " + _gson.toJson(isoInfo.first())); - deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.add); - } else { - if(s_logger.isDebugEnabled()) - s_logger.debug("Prepare ISO volume at existing device " + _gson.toJson(isoInfo.first())); - deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.edit); - } - i++; - } else { - // we will always plugin a CDROM device - if (volIso != null && volIso.getPath() != null && !volIso.getPath().isEmpty()) { - Pair isoDatastoreInfo = getIsoDatastoreInfo(hyperHost, volIso.getPath()); - assert (isoDatastoreInfo != null); - assert (isoDatastoreInfo.second() != null); - - deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec(); - Pair isoInfo = VmwareHelper.prepareIsoDevice(vmMo, isoDatastoreInfo.first(), isoDatastoreInfo.second(), true, true, i, i + 1); - deviceConfigSpecArray[i].setDevice(isoInfo.first()); - if (isoInfo.second()) { - if(s_logger.isDebugEnabled()) - s_logger.debug("Prepare ISO volume at new device " + _gson.toJson(isoInfo.first())); - deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.add); - } else { - if(s_logger.isDebugEnabled()) - s_logger.debug("Prepare ISO volume at existing device " + _gson.toJson(isoInfo.first())); - deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.edit); - } - } else { - deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec(); - Pair isoInfo = VmwareHelper.prepareIsoDevice(vmMo, null, null, true, true, i, i + 1); - deviceConfigSpecArray[i].setDevice(isoInfo.first()); - if (isoInfo.second()) { - if(s_logger.isDebugEnabled()) - s_logger.debug("Prepare ISO volume at existing device " + _gson.toJson(isoInfo.first())); - - deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.add); - } else { - if(s_logger.isDebugEnabled()) - s_logger.debug("Prepare ISO volume at existing device " + _gson.toJson(isoInfo.first())); - - deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.edit); - } - } - i++; - } - - for (VolumeTO vol : sortVolumesByDeviceId(disks)) { - deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec(); + + ManagedObjectReference morSecDs = prepareSecondaryDatastoreOnHost(secStoreUrl); + if (morSecDs == null) { + String msg = "Failed to prepare secondary storage on host, secondary store url: " + secStoreUrl; + throw new Exception(msg); + } + DatastoreMO secDsMo = new DatastoreMO(hyperHost.getContext(), morSecDs); + + deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec(); + Pair isoInfo = VmwareHelper.prepareIsoDevice(vmMo, String.format("[%s] systemvm/%s", secDsMo.getName(), mgr.getSystemVMIsoFileNameOnDatastore()), + secDsMo.getMor(), true, true, i, i + 1); + deviceConfigSpecArray[i].setDevice(isoInfo.first()); + if (isoInfo.second()) { + if(s_logger.isDebugEnabled()) + s_logger.debug("Prepare ISO volume at new device " + _gson.toJson(isoInfo.first())); + deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.add); + } else { + if(s_logger.isDebugEnabled()) + s_logger.debug("Prepare ISO volume at existing device " + _gson.toJson(isoInfo.first())); + deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.edit); + } + i++; + } else { + // we will always plugin a CDROM device + if (volIso != null && volIso.getPath() != null && !volIso.getPath().isEmpty()) { + Pair isoDatastoreInfo = getIsoDatastoreInfo(hyperHost, volIso.getPath()); + assert (isoDatastoreInfo != null); + assert (isoDatastoreInfo.second() != null); + + deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec(); + Pair isoInfo = VmwareHelper.prepareIsoDevice(vmMo, isoDatastoreInfo.first(), isoDatastoreInfo.second(), true, true, i, i + 1); + deviceConfigSpecArray[i].setDevice(isoInfo.first()); + if (isoInfo.second()) { + if(s_logger.isDebugEnabled()) + s_logger.debug("Prepare ISO volume at new device " + _gson.toJson(isoInfo.first())); + deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.add); + } else { + if(s_logger.isDebugEnabled()) + s_logger.debug("Prepare ISO volume at existing device " + _gson.toJson(isoInfo.first())); + deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.edit); + } + } else { + deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec(); + Pair isoInfo = VmwareHelper.prepareIsoDevice(vmMo, null, null, true, true, i, i + 1); + deviceConfigSpecArray[i].setDevice(isoInfo.first()); + if (isoInfo.second()) { + if(s_logger.isDebugEnabled()) + s_logger.debug("Prepare ISO volume at existing device " + _gson.toJson(isoInfo.first())); + + deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.add); + } else { + if(s_logger.isDebugEnabled()) + s_logger.debug("Prepare ISO volume at existing device " + _gson.toJson(isoInfo.first())); + + deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.edit); + } + } + i++; + } + + for (VolumeTO vol : sortVolumesByDeviceId(disks)) { + deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec(); if (vol.getType() == Volume.Type.ISO) { controllerKey = ideControllerKey; } else { if(vol.getType() == Volume.Type.ROOT) { - if(vmSpec.getDetails() != null && vmSpec.getDetails().get(VmDetailConstants.ROOK_DISK_CONTROLLER) != null) - { - if(vmSpec.getDetails().get(VmDetailConstants.ROOK_DISK_CONTROLLER).equalsIgnoreCase("scsi")) - controllerKey = scsiControllerKey; - else - controllerKey = ideControllerKey; - } else { - controllerKey = scsiControllerKey; - } + if(vmSpec.getDetails() != null && vmSpec.getDetails().get(VmDetailConstants.ROOK_DISK_CONTROLLER) != null) + { + if(vmSpec.getDetails().get(VmDetailConstants.ROOK_DISK_CONTROLLER).equalsIgnoreCase("scsi")) + controllerKey = scsiControllerKey; + else + controllerKey = ideControllerKey; + } else { + controllerKey = scsiControllerKey; + } } else { // DATA volume always use SCSI device controllerKey = scsiControllerKey; } } - - if (vol.getType() != Volume.Type.ISO) { + + if (vol.getType() != Volume.Type.ISO) { Pair volumeDsDetails = dataStoresDetails.get(vol.getPoolUuid()); assert (volumeDsDetails != null); - VirtualDevice device; - datastoreDiskPath = String.format("[%s] %s.vmdk", volumeDsDetails.second().getName(), vol.getPath()); - String chainInfo = vol.getChainInfo(); - - if (chainInfo != null && !chainInfo.isEmpty()) { - String[] diskChain = _gson.fromJson(chainInfo, String[].class); - if (diskChain == null || diskChain.length < 1) { - s_logger.warn("Empty previously-saved chain info, fall back to the original"); - device = VmwareHelper.prepareDiskDevice(vmMo, controllerKey, new String[] { datastoreDiskPath }, volumeDsDetails.first(), i, i + 1); - } else { - s_logger.info("Attach the disk with stored chain info: " + chainInfo); - for (int j = 0; j < diskChain.length; j++) { - diskChain[j] = String.format("[%s] %s", volumeDsDetails.second().getName(), diskChain[j]); - } - - device = VmwareHelper.prepareDiskDevice(vmMo, controllerKey, diskChain, volumeDsDetails.first(), i, i + 1); - } - } else { - device = VmwareHelper.prepareDiskDevice(vmMo, controllerKey, new String[] { datastoreDiskPath }, volumeDsDetails.first(), i, i + 1); - } - deviceConfigSpecArray[i].setDevice(device); - deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.add); - - if(s_logger.isDebugEnabled()) - s_logger.debug("Prepare volume at new device " + _gson.toJson(device)); - - i++; - } + VirtualDevice device; + datastoreDiskPath = String.format("[%s] %s.vmdk", volumeDsDetails.second().getName(), vol.getPath()); + String chainInfo = vol.getChainInfo(); + + if (chainInfo != null && !chainInfo.isEmpty()) { + String[] diskChain = _gson.fromJson(chainInfo, String[].class); + if (diskChain == null || diskChain.length < 1) { + s_logger.warn("Empty previously-saved chain info, fall back to the original"); + device = VmwareHelper.prepareDiskDevice(vmMo, controllerKey, new String[] { datastoreDiskPath }, volumeDsDetails.first(), i, i + 1); + } else { + s_logger.info("Attach the disk with stored chain info: " + chainInfo); + for (int j = 0; j < diskChain.length; j++) { + diskChain[j] = String.format("[%s] %s", volumeDsDetails.second().getName(), diskChain[j]); + } + + device = VmwareHelper.prepareDiskDevice(vmMo, controllerKey, diskChain, volumeDsDetails.first(), i, i + 1); + } + } else { + device = VmwareHelper.prepareDiskDevice(vmMo, controllerKey, new String[] { datastoreDiskPath }, volumeDsDetails.first(), i, i + 1); + } + deviceConfigSpecArray[i].setDevice(device); + deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.add); + + if(s_logger.isDebugEnabled()) + s_logger.debug("Prepare volume at new device " + _gson.toJson(device)); + + i++; + } } - + VirtualDevice nic; int nicMask = 0; - int nicCount = 0; - for (NicTO nicTo : sortNicsByDeviceId(nics)) { - s_logger.info("Prepare NIC device based on NicTO: " + _gson.toJson(nicTo)); - + int nicCount = 0; + for (NicTO nicTo : sortNicsByDeviceId(nics)) { + s_logger.info("Prepare NIC device based on NicTO: " + _gson.toJson(nicTo)); + Pair networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo); if (mgr.getNexusVSwitchGlobalParameter()) { String dvSwitchUuid; @@ -2150,144 +2190,144 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa s_logger.info("Preparing NIC device on network " + networkInfo.second()); nic = VmwareHelper.prepareNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), nicTo.getMac(), i, i + 1, true, true); } - - deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec(); - deviceConfigSpecArray[i].setDevice(nic); - deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.add); - - if(s_logger.isDebugEnabled()) - s_logger.debug("Prepare NIC at new device " + _gson.toJson(deviceConfigSpecArray[i])); - - // this is really a hacking for DomR, upon DomR startup, we will reset all the NIC allocation after eth3 + + deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec(); + deviceConfigSpecArray[i].setDevice(nic); + deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.add); + + if(s_logger.isDebugEnabled()) + s_logger.debug("Prepare NIC at new device " + _gson.toJson(deviceConfigSpecArray[i])); + + // this is really a hacking for DomR, upon DomR startup, we will reset all the NIC allocation after eth3 if(nicCount < 3) - nicMask |= (1 << nicCount); - + nicMask |= (1 << nicCount); + i++; nicCount++; - } - - vmConfigSpec.setDeviceChange(deviceConfigSpecArray); - + } + + vmConfigSpec.setDeviceChange(deviceConfigSpecArray); + // pass boot arguments through machine.id & perform customized options to VMX - + Map vmDetailOptions = validateVmDetails(vmSpec.getDetails()); - OptionValue[] extraOptions = new OptionValue[2 + vmDetailOptions.size()]; - extraOptions[0] = new OptionValue(); - extraOptions[0].setKey("machine.id"); + OptionValue[] extraOptions = new OptionValue[2 + vmDetailOptions.size()]; + extraOptions[0] = new OptionValue(); + extraOptions[0].setKey("machine.id"); extraOptions[0].setValue(vmSpec.getBootArgs()); - + extraOptions[1] = new OptionValue(); extraOptions[1].setKey("devices.hotplug"); extraOptions[1].setValue("true"); int j = 2; for(Map.Entry entry : vmDetailOptions.entrySet()) { - extraOptions[j] = new OptionValue(); - extraOptions[j].setKey(entry.getKey()); - extraOptions[j].setValue(entry.getValue()); - j++; + extraOptions[j] = new OptionValue(); + extraOptions[j].setKey(entry.getKey()); + extraOptions[j].setValue(entry.getValue()); + j++; } - + String keyboardLayout = null; if(vmSpec.getDetails() != null) - keyboardLayout = vmSpec.getDetails().get(VmDetailConstants.KEYBOARD); - vmConfigSpec.setExtraConfig(configureVnc(extraOptions, hyperHost, vmName, vmSpec.getVncPassword(), keyboardLayout)); - - if (!vmMo.configureVm(vmConfigSpec)) { - throw new Exception("Failed to configure VM before start. vmName: " + vmName); + keyboardLayout = vmSpec.getDetails().get(VmDetailConstants.KEYBOARD); + vmConfigSpec.setExtraConfig(configureVnc(extraOptions, hyperHost, vmName, vmSpec.getVncPassword(), keyboardLayout)); + + if (!vmMo.configureVm(vmConfigSpec)) { + throw new Exception("Failed to configure VM before start. vmName: " + vmName); } - - vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, String.valueOf(nicMask)); - - if (!vmMo.powerOn()) { - throw new Exception("Failed to start VM. vmName: " + vmName); - } - - state = State.Running; - return new StartAnswer(cmd); - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "StartCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.warn(msg, e); - return new StartAnswer(cmd, msg); - } finally { - synchronized (_vms) { - if (state != State.Stopped) { - _vms.put(vmName, state); - } else { - _vms.remove(vmName); - } - } - } + + vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, String.valueOf(nicMask)); + + if (!vmMo.powerOn()) { + throw new Exception("Failed to start VM. vmName: " + vmName); + } + + state = State.Running; + return new StartAnswer(cmd); + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "StartCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.warn(msg, e); + return new StartAnswer(cmd, msg); + } finally { + synchronized (_vms) { + if (state != State.Stopped) { + _vms.put(vmName, state); + } else { + _vms.remove(vmName); + } + } + } } - + private Map validateVmDetails(Map vmDetails) { - Map validatedDetails = new HashMap(); - - if(vmDetails != null && vmDetails.size() > 0) { - for(Map.Entry entry : vmDetails.entrySet()) { - if("machine.id".equalsIgnoreCase(entry.getKey())) - continue; - else if("devices.hotplug".equalsIgnoreCase(entry.getKey())) - continue; - else if("RemoteDisplay.vnc.enabled".equalsIgnoreCase(entry.getKey())) - continue; - else if("RemoteDisplay.vnc.password".equalsIgnoreCase(entry.getKey())) - continue; - else if("RemoteDisplay.vnc.port".equalsIgnoreCase(entry.getKey())) - continue; - else if("RemoteDisplay.vnc.keymap".equalsIgnoreCase(entry.getKey())) - continue; - else - validatedDetails.put(entry.getKey(), entry.getValue()); - } - } - return validatedDetails; - } + Map validatedDetails = new HashMap(); + + if(vmDetails != null && vmDetails.size() > 0) { + for(Map.Entry entry : vmDetails.entrySet()) { + if("machine.id".equalsIgnoreCase(entry.getKey())) + continue; + else if("devices.hotplug".equalsIgnoreCase(entry.getKey())) + continue; + else if("RemoteDisplay.vnc.enabled".equalsIgnoreCase(entry.getKey())) + continue; + else if("RemoteDisplay.vnc.password".equalsIgnoreCase(entry.getKey())) + continue; + else if("RemoteDisplay.vnc.port".equalsIgnoreCase(entry.getKey())) + continue; + else if("RemoteDisplay.vnc.keymap".equalsIgnoreCase(entry.getKey())) + continue; + else + validatedDetails.put(entry.getKey(), entry.getValue()); + } + } + return validatedDetails; + } private int getReserveCpuMHz(int cpuMHz) { - if(this._reserveCpu) { - return (int)(cpuMHz / this._cpuOverprovisioningFactor); - } - - return 0; + if(this._reserveCpu) { + return (int)(cpuMHz / this._cpuOverprovisioningFactor); + } + + return 0; } - + private int getReserveMemMB(int memMB) { - if(this._reserveMem) { - return (int)(memMB / this._memOverprovisioningFactor); - } - - return 0; + if(this._reserveMem) { + return (int)(memMB / this._memOverprovisioningFactor); + } + + return 0; } - - private NicTO[] sortNicsByDeviceId(NicTO[] nics) { - - List listForSort = new ArrayList(); - for (NicTO nic : nics) { - listForSort.add(nic); - } - Collections.sort(listForSort, new Comparator() { - - @Override - public int compare(NicTO arg0, NicTO arg1) { - if (arg0.getDeviceId() < arg1.getDeviceId()) { - return -1; - } else if (arg0.getDeviceId() == arg1.getDeviceId()) { - return 0; - } - - return 1; - } - }); - - return listForSort.toArray(new NicTO[0]); + + private NicTO[] sortNicsByDeviceId(NicTO[] nics) { + + List listForSort = new ArrayList(); + for (NicTO nic : nics) { + listForSort.add(nic); + } + Collections.sort(listForSort, new Comparator() { + + @Override + public int compare(NicTO arg0, NicTO arg1) { + if (arg0.getDeviceId() < arg1.getDeviceId()) { + return -1; + } else if (arg0.getDeviceId() == arg1.getDeviceId()) { + return 0; + } + + return 1; + } + }); + + return listForSort.toArray(new NicTO[0]); } - + private VolumeTO[] sortVolumesByDeviceId(VolumeTO[] volumes) { List listForSort = new ArrayList(); @@ -2310,11 +2350,11 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa return listForSort.toArray(new VolumeTO[0]); } - + private HashMap> inferDatastoreDetailsFromDiskInfo(VmwareHypervisorHost hyperHost, VmwareContext context, VolumeTO[] disks) throws Exception { HashMap> poolMors = new HashMap>(); - assert (hyperHost != null) && (context != null); + assert (hyperHost != null) && (context != null); for (VolumeTO vol : disks) { if (vol.getType() != Volume.Type.ISO) { String poolUuid = vol.getPoolUuid(); @@ -2327,62 +2367,62 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } poolMors.put(vol.getPoolUuid(), new Pair (morDataStore, new DatastoreMO(context, morDataStore))); } - } - } - return poolMors; - } - - private String getVlanInfo(NicTO nicTo, String defaultVlan) { - if (nicTo.getBroadcastType() == BroadcastDomainType.Native) { - return defaultVlan; - } - - if (nicTo.getBroadcastType() == BroadcastDomainType.Vlan) { - if (nicTo.getBroadcastUri() != null) { - return nicTo.getBroadcastUri().getHost(); - } else { - s_logger.warn("BroadcastType is not claimed as VLAN, but without vlan info in broadcast URI. Use vlan info from labeling: " + defaultVlan); - return defaultVlan; - } - } - - s_logger.warn("Unrecognized broadcast type in VmwareResource, type: " + nicTo.getBroadcastType().toString() + ". Use vlan info from labeling: " + defaultVlan); - return defaultVlan; - } - + } + } + return poolMors; + } + + private String getVlanInfo(NicTO nicTo, String defaultVlan) { + if (nicTo.getBroadcastType() == BroadcastDomainType.Native) { + return defaultVlan; + } + + if (nicTo.getBroadcastType() == BroadcastDomainType.Vlan) { + if (nicTo.getBroadcastUri() != null) { + return nicTo.getBroadcastUri().getHost(); + } else { + s_logger.warn("BroadcastType is not claimed as VLAN, but without vlan info in broadcast URI. Use vlan info from labeling: " + defaultVlan); + return defaultVlan; + } + } + + s_logger.warn("Unrecognized broadcast type in VmwareResource, type: " + nicTo.getBroadcastType().toString() + ". Use vlan info from labeling: " + defaultVlan); + return defaultVlan; + } + private Pair prepareNetworkFromNicInfo(HostMO hostMo, NicTO nicTo) throws Exception { - + Pair switchName = getTargetSwitch(nicTo); String namePrefix = getNetworkNamePrefix(nicTo); Pair networkInfo = null; - + s_logger.info("Prepare network on vSwitch: " + switchName.first() + " with name prefix: " + namePrefix); - + if(!_nexusVSwitch) { - networkInfo = HypervisorHostHelper.prepareNetwork(switchName.first(), namePrefix, hostMo, getVlanInfo(nicTo, switchName.second()), - nicTo.getNetworkRateMbps(), nicTo.getNetworkRateMulticastMbps(), _ops_timeout, + networkInfo = HypervisorHostHelper.prepareNetwork(switchName.first(), namePrefix, hostMo, getVlanInfo(nicTo, switchName.second()), + nicTo.getNetworkRateMbps(), nicTo.getNetworkRateMulticastMbps(), _ops_timeout, !namePrefix.startsWith("cloud.private")); } else { - networkInfo = HypervisorHostHelper.prepareNetwork(switchName.first(), namePrefix, hostMo, getVlanInfo(nicTo, switchName.second()), + networkInfo = HypervisorHostHelper.prepareNetwork(switchName.first(), namePrefix, hostMo, getVlanInfo(nicTo, switchName.second()), nicTo.getNetworkRateMbps(), nicTo.getNetworkRateMulticastMbps(), _ops_timeout); } - + return networkInfo; } - + // return Pair private Pair getTargetSwitch(NicTO nicTo) throws Exception { if(nicTo.getName() != null && !nicTo.getName().isEmpty()) { - String[] tokens = nicTo.getName().split(","); - - if(tokens.length == 2) { - return new Pair(tokens[0], tokens[1]); - } else { - return new Pair(nicTo.getName(), Vlan.UNTAGGED); - } + String[] tokens = nicTo.getName().split(","); + + if(tokens.length == 2) { + return new Pair(tokens[0], tokens[1]); + } else { + return new Pair(nicTo.getName(), Vlan.UNTAGGED); + } } - + if (nicTo.getType() == Networks.TrafficType.Guest) { return new Pair(this._guestNetworkVSwitchName, Vlan.UNTAGGED); } else if (nicTo.getType() == Networks.TrafficType.Control || nicTo.getType() == Networks.TrafficType.Management) { @@ -2397,7 +2437,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa throw new Exception("Unsupported traffic type: " + nicTo.getType().toString()); } } - + private String getNetworkNamePrefix(NicTO nicTo) throws Exception { if (nicTo.getType() == Networks.TrafficType.Guest) { return "cloud.guest"; @@ -2412,193 +2452,193 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } else { throw new Exception("Unsupported traffic type: " + nicTo.getType().toString()); } - } - - protected synchronized Answer execute(final RemoteAccessVpnCfgCommand cmd) { - String controlIp = getRouterSshControlIp(cmd); - StringBuffer argsBuf = new StringBuffer(); - if (cmd.isCreate()) { - argsBuf.append(" -r ").append(cmd.getIpRange()).append(" -p ").append(cmd.getPresharedKey()).append(" -s ").append(cmd.getVpnServerIp()).append(" -l ").append(cmd.getLocalIp()) - .append(" -c "); - - } else { - argsBuf.append(" -d ").append(" -s ").append(cmd.getVpnServerIp()); - } - - try { - VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - + } + + protected synchronized Answer execute(final RemoteAccessVpnCfgCommand cmd) { + String controlIp = getRouterSshControlIp(cmd); + StringBuffer argsBuf = new StringBuffer(); + if (cmd.isCreate()) { + argsBuf.append(" -r ").append(cmd.getIpRange()).append(" -p ").append(cmd.getPresharedKey()).append(" -s ").append(cmd.getVpnServerIp()).append(" -l ").append(cmd.getLocalIp()) + .append(" -c "); + + } else { + argsBuf.append(" -d ").append(" -s ").append(cmd.getVpnServerIp()); + } + + try { + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + if (s_logger.isDebugEnabled()) { s_logger.debug("Executing /opt/cloud/bin/vpn_lt2p.sh "); - } - - Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpn_l2tp.sh " + argsBuf.toString()); - - if (!result.first()) { - s_logger.error("RemoteAccessVpnCfg command on domR failed, message: " + result.second()); - - return new Answer(cmd, false, "RemoteAccessVpnCfg command failed due to " + result.second()); - } - - if (s_logger.isInfoEnabled()) { - s_logger.info("RemoteAccessVpnCfg command on domain router " + argsBuf.toString() + " completed"); - } - - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "RemoteAccessVpnCfg command failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new Answer(cmd, false, msg); - } - - return new Answer(cmd); - } - - protected synchronized Answer execute(final VpnUsersCfgCommand cmd) { - VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - - String controlIp = getRouterSshControlIp(cmd); - for (VpnUsersCfgCommand.UsernamePassword userpwd : cmd.getUserpwds()) { - StringBuffer argsBuf = new StringBuffer(); - if (!userpwd.isAdd()) { - argsBuf.append(" -U ").append(userpwd.getUsername()); - } else { - argsBuf.append(" -u ").append(userpwd.getUsernamePassword()); - } - - try { - + } + + Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpn_l2tp.sh " + argsBuf.toString()); + + if (!result.first()) { + s_logger.error("RemoteAccessVpnCfg command on domR failed, message: " + result.second()); + + return new Answer(cmd, false, "RemoteAccessVpnCfg command failed due to " + result.second()); + } + + if (s_logger.isInfoEnabled()) { + s_logger.info("RemoteAccessVpnCfg command on domain router " + argsBuf.toString() + " completed"); + } + + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "RemoteAccessVpnCfg command failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new Answer(cmd, false, msg); + } + + return new Answer(cmd); + } + + protected synchronized Answer execute(final VpnUsersCfgCommand cmd) { + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + + String controlIp = getRouterSshControlIp(cmd); + for (VpnUsersCfgCommand.UsernamePassword userpwd : cmd.getUserpwds()) { + StringBuffer argsBuf = new StringBuffer(); + if (!userpwd.isAdd()) { + argsBuf.append(" -U ").append(userpwd.getUsername()); + } else { + argsBuf.append(" -u ").append(userpwd.getUsernamePassword()); + } + + try { + if (s_logger.isDebugEnabled()) { s_logger.debug("Executing /opt/cloud/bin/vpn_lt2p.sh "); - } - - Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpn_l2tp.sh " + argsBuf.toString()); - - if (!result.first()) { - s_logger.error("VpnUserCfg command on domR failed, message: " + result.second()); - - return new Answer(cmd, false, "VpnUserCfg command failed due to " + result.second()); - } - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "VpnUserCfg command failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new Answer(cmd, false, msg); - } - } - - return new Answer(cmd); - } - - private VirtualMachineMO takeVmFromOtherHyperHost(VmwareHypervisorHost hyperHost, String vmName) throws Exception { - - VirtualMachineMO vmMo = hyperHost.findVmOnPeerHyperHost(vmName); - if (vmMo != null) { - ManagedObjectReference morTargetPhysicalHost = hyperHost.findMigrationTarget(vmMo); - if (morTargetPhysicalHost == null) { - String msg = "VM " + vmName + " is on other host and we have no resource available to migrate and start it here"; - s_logger.error(msg); - throw new Exception(msg); - } + } - if (!vmMo.relocate(morTargetPhysicalHost)) { - String msg = "VM " + vmName + " is on other host and we failed to relocate it here"; - s_logger.error(msg); - throw new Exception(msg); - } - - return vmMo; - } - return null; - } - - // isoUrl sample content : - // nfs://192.168.10.231/export/home/kelven/vmware-test/secondary/template/tmpl/2/200//200-2-80f7ee58-6eff-3a2d-bcb0-59663edf6d26.iso - private Pair getIsoDatastoreInfo(VmwareHypervisorHost hyperHost, String isoUrl) throws Exception { - - assert (isoUrl != null); - int isoFileNameStartPos = isoUrl.lastIndexOf("/"); - if (isoFileNameStartPos < 0) { - throw new Exception("Invalid ISO path info"); - } - - String isoFileName = isoUrl.substring(isoFileNameStartPos); - - int templateRootPos = isoUrl.indexOf("template/tmpl"); - if (templateRootPos < 0) { - throw new Exception("Invalid ISO path info"); - } - - String storeUrl = isoUrl.substring(0, templateRootPos - 1); - String isoPath = isoUrl.substring(templateRootPos, isoFileNameStartPos); - - ManagedObjectReference morDs = prepareSecondaryDatastoreOnHost(storeUrl); - DatastoreMO dsMo = new DatastoreMO(getServiceContext(), morDs); - - return new Pair(String.format("[%s] %s%s", dsMo.getName(), isoPath, isoFileName), morDs); - } - - protected Answer execute(ReadyCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource ReadyCommand: " + _gson.toJson(cmd)); + Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpn_l2tp.sh " + argsBuf.toString()); + + if (!result.first()) { + s_logger.error("VpnUserCfg command on domR failed, message: " + result.second()); + + return new Answer(cmd, false, "VpnUserCfg command failed due to " + result.second()); + } + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "VpnUserCfg command failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new Answer(cmd, false, msg); + } } - + + return new Answer(cmd); + } + + private VirtualMachineMO takeVmFromOtherHyperHost(VmwareHypervisorHost hyperHost, String vmName) throws Exception { + + VirtualMachineMO vmMo = hyperHost.findVmOnPeerHyperHost(vmName); + if (vmMo != null) { + ManagedObjectReference morTargetPhysicalHost = hyperHost.findMigrationTarget(vmMo); + if (morTargetPhysicalHost == null) { + String msg = "VM " + vmName + " is on other host and we have no resource available to migrate and start it here"; + s_logger.error(msg); + throw new Exception(msg); + } + + if (!vmMo.relocate(morTargetPhysicalHost)) { + String msg = "VM " + vmName + " is on other host and we failed to relocate it here"; + s_logger.error(msg); + throw new Exception(msg); + } + + return vmMo; + } + return null; + } + + // isoUrl sample content : + // nfs://192.168.10.231/export/home/kelven/vmware-test/secondary/template/tmpl/2/200//200-2-80f7ee58-6eff-3a2d-bcb0-59663edf6d26.iso + private Pair getIsoDatastoreInfo(VmwareHypervisorHost hyperHost, String isoUrl) throws Exception { + + assert (isoUrl != null); + int isoFileNameStartPos = isoUrl.lastIndexOf("/"); + if (isoFileNameStartPos < 0) { + throw new Exception("Invalid ISO path info"); + } + + String isoFileName = isoUrl.substring(isoFileNameStartPos); + + int templateRootPos = isoUrl.indexOf("template/tmpl"); + if (templateRootPos < 0) { + throw new Exception("Invalid ISO path info"); + } + + String storeUrl = isoUrl.substring(0, templateRootPos - 1); + String isoPath = isoUrl.substring(templateRootPos, isoFileNameStartPos); + + ManagedObjectReference morDs = prepareSecondaryDatastoreOnHost(storeUrl); + DatastoreMO dsMo = new DatastoreMO(getServiceContext(), morDs); + + return new Pair(String.format("[%s] %s%s", dsMo.getName(), isoPath, isoFileName), morDs); + } + + protected Answer execute(ReadyCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource ReadyCommand: " + _gson.toJson(cmd)); + } + try { - VmwareContext context = getServiceContext(); - VmwareHypervisorHost hyperHost = getHyperHost(context); - if(hyperHost.isHyperHostConnected()) { + VmwareContext context = getServiceContext(); + VmwareHypervisorHost hyperHost = getHyperHost(context); + if(hyperHost.isHyperHostConnected()) { return new ReadyAnswer(cmd); - } else { - return new ReadyAnswer(cmd, "Host is not in connect state"); - } + } else { + return new ReadyAnswer(cmd, "Host is not in connect state"); + } } catch(Exception e) { - s_logger.error("Unexpected exception: ", e); - return new ReadyAnswer(cmd, VmwareHelper.getExceptionMessage(e)); - } - } - - protected Answer execute(GetHostStatsCommand cmd) { - if (s_logger.isTraceEnabled()) { - s_logger.trace("Executing resource GetHostStatsCommand: " + _gson.toJson(cmd)); - } - - VmwareContext context = getServiceContext(); - VmwareHypervisorHost hyperHost = getHyperHost(context); - - HostStatsEntry hostStats = new HostStatsEntry(cmd.getHostId(), 0, 0, 0, "host", 0, 0, 0, 0); - Answer answer = new GetHostStatsAnswer(cmd, hostStats); - try { - HostStatsEntry entry = getHyperHostStats(hyperHost); - if(entry != null) { - entry.setHostId(cmd.getHostId()); - answer = new GetHostStatsAnswer(cmd, entry); - } - } catch (Exception e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "Unable to execute GetHostStatsCommand due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - } - - if (s_logger.isTraceEnabled()) { - s_logger.trace("GetHostStats Answer: " + _gson.toJson(answer)); + s_logger.error("Unexpected exception: ", e); + return new ReadyAnswer(cmd, VmwareHelper.getExceptionMessage(e)); + } + } + + protected Answer execute(GetHostStatsCommand cmd) { + if (s_logger.isTraceEnabled()) { + s_logger.trace("Executing resource GetHostStatsCommand: " + _gson.toJson(cmd)); } - return answer; - } - - protected Answer execute(GetVmStatsCommand cmd) { + VmwareContext context = getServiceContext(); + VmwareHypervisorHost hyperHost = getHyperHost(context); + + HostStatsEntry hostStats = new HostStatsEntry(cmd.getHostId(), 0, 0, 0, "host", 0, 0, 0, 0); + Answer answer = new GetHostStatsAnswer(cmd, hostStats); + try { + HostStatsEntry entry = getHyperHostStats(hyperHost); + if(entry != null) { + entry.setHostId(cmd.getHostId()); + answer = new GetHostStatsAnswer(cmd, entry); + } + } catch (Exception e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "Unable to execute GetHostStatsCommand due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + } + + if (s_logger.isTraceEnabled()) { + s_logger.trace("GetHostStats Answer: " + _gson.toJson(answer)); + } + + return answer; + } + + protected Answer execute(GetVmStatsCommand cmd) { if (s_logger.isTraceEnabled()) { s_logger.trace("Executing resource GetVmStatsCommand: " + _gson.toJson(cmd)); } @@ -2613,9 +2653,9 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if (requestedVmNames != null) { for (String vmName : requestedVmNames) { - if (newStates.get(vmName) != null) { - vmNames.add(vmName); - } + if (newStates.get(vmName) != null) { + vmNames.add(vmName); + } } } @@ -2636,125 +2676,125 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if (s_logger.isTraceEnabled()) { s_logger.trace("Report GetVmStatsAnswer: " + _gson.toJson(answer)); } - return answer; - } - - protected Answer execute(CheckHealthCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource CheckHealthCommand: " + _gson.toJson(cmd)); - } - - try { - VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - if (hyperHost.isHyperHostConnected()) { - return new CheckHealthAnswer(cmd, true); - } - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - s_logger.error("Unable to execute CheckHealthCommand due to " + VmwareHelper.getExceptionMessage(e), e); - } - return new CheckHealthAnswer(cmd, false); - } - - protected Answer execute(StopCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource StopCommand: " + _gson.toJson(cmd)); - } - - VmwareContext context = getServiceContext(); - VmwareHypervisorHost hyperHost = getHyperHost(context); - try { - VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName()); - if (vmMo != null) { - - State state = null; - synchronized (_vms) { - state = _vms.get(cmd.getVmName()); - _vms.put(cmd.getVmName(), State.Stopping); - } - + return answer; + } + + protected Answer execute(CheckHealthCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource CheckHealthCommand: " + _gson.toJson(cmd)); + } + + try { + VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); + if (hyperHost.isHyperHostConnected()) { + return new CheckHealthAnswer(cmd, true); + } + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + s_logger.error("Unable to execute CheckHealthCommand due to " + VmwareHelper.getExceptionMessage(e), e); + } + return new CheckHealthAnswer(cmd, false); + } + + protected Answer execute(StopCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource StopCommand: " + _gson.toJson(cmd)); + } + + VmwareContext context = getServiceContext(); + VmwareHypervisorHost hyperHost = getHyperHost(context); + try { + VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName()); + if (vmMo != null) { + + State state = null; + synchronized (_vms) { + state = _vms.get(cmd.getVmName()); + _vms.put(cmd.getVmName(), State.Stopping); + } + try { vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, "0"); - - if (getVmState(vmMo) != State.Stopped) { - + + if (getVmState(vmMo) != State.Stopped) { + // before we stop VM, remove all possible snapshots on the VM to let // disk chain be collapsed s_logger.info("Remove all snapshot before stopping VM " + cmd.getVmName()); - vmMo.removeAllSnapshots(); - if (vmMo.safePowerOff(_shutdown_waitMs)) { - state = State.Stopped; - return new StopAnswer(cmd, "Stop VM " + cmd.getVmName() + " Succeed", 0, true); + vmMo.removeAllSnapshots(); + if (vmMo.safePowerOff(_shutdown_waitMs)) { + state = State.Stopped; + return new StopAnswer(cmd, "Stop VM " + cmd.getVmName() + " Succeed", 0, true); } else { - String msg = "Have problem in powering off VM " + cmd.getVmName() + ", let the process continue"; - s_logger.warn(msg); + String msg = "Have problem in powering off VM " + cmd.getVmName() + ", let the process continue"; + s_logger.warn(msg); return new StopAnswer(cmd, msg, 0, true); } - } else { - state = State.Stopped; - } - - String msg = "VM " + cmd.getVmName() + " is already in stopped state"; - s_logger.info(msg); - return new StopAnswer(cmd, msg, 0, true); - } finally { - synchronized (_vms) { - _vms.put(cmd.getVmName(), state); - } - } - } else { - synchronized (_vms) { - _vms.remove(cmd.getVmName()); - } - - String msg = "VM " + cmd.getVmName() + " is no longer in vSphere"; - s_logger.info(msg); - return new StopAnswer(cmd, msg, 0, true); - } - } catch (Exception e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "StopCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg); - return new StopAnswer(cmd, msg, false); - } - } - - protected Answer execute(RebootRouterCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource RebootRouterCommand: " + _gson.toJson(cmd)); - } - - RebootAnswer answer = (RebootAnswer) execute((RebootCommand) cmd); - - if (answer.getResult()) { - String connectResult = connect(cmd.getVmName(), cmd.getPrivateIpAddress()); - networkUsage(cmd.getPrivateIpAddress(), "create", null); - if (connectResult == null) { - return answer; - } else { - return new Answer(cmd, false, connectResult); - } - } - return answer; - } - - protected Answer execute(RebootCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource RebootCommand: " + _gson.toJson(cmd)); - } - - VmwareContext context = getServiceContext(); - VmwareHypervisorHost hyperHost = getHyperHost(context); - try { - VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName()); + } else { + state = State.Stopped; + } + + String msg = "VM " + cmd.getVmName() + " is already in stopped state"; + s_logger.info(msg); + return new StopAnswer(cmd, msg, 0, true); + } finally { + synchronized (_vms) { + _vms.put(cmd.getVmName(), state); + } + } + } else { + synchronized (_vms) { + _vms.remove(cmd.getVmName()); + } + + String msg = "VM " + cmd.getVmName() + " is no longer in vSphere"; + s_logger.info(msg); + return new StopAnswer(cmd, msg, 0, true); + } + } catch (Exception e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "StopCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg); + return new StopAnswer(cmd, msg, false); + } + } + + protected Answer execute(RebootRouterCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource RebootRouterCommand: " + _gson.toJson(cmd)); + } + + RebootAnswer answer = (RebootAnswer) execute((RebootCommand) cmd); + + if (answer.getResult()) { + String connectResult = connect(cmd.getVmName(), cmd.getPrivateIpAddress()); + networkUsage(cmd.getPrivateIpAddress(), "create", null); + if (connectResult == null) { + return answer; + } else { + return new Answer(cmd, false, connectResult); + } + } + return answer; + } + + protected Answer execute(RebootCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource RebootCommand: " + _gson.toJson(cmd)); + } + + VmwareContext context = getServiceContext(); + VmwareHypervisorHost hyperHost = getHyperHost(context); + try { + VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName()); if (vmMo != null) { try { vmMo.rebootGuest(); @@ -2766,95 +2806,95 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } // continue to try with hard-reset - if (vmMo.reset()) { - return new RebootAnswer(cmd, "reboot succeeded", true); - } - - String msg = "Reboot failed in vSphere. vm: " + cmd.getVmName(); - s_logger.warn(msg); - return new RebootAnswer(cmd, msg, false); - } else { - String msg = "Unable to find the VM in vSphere to reboot. vm: " + cmd.getVmName(); - s_logger.warn(msg); - return new RebootAnswer(cmd, msg, false); - } - } catch (Exception e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "RebootCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg); - return new RebootAnswer(cmd, msg, false); - } - } - - protected Answer execute(CheckVirtualMachineCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource CheckVirtualMachineCommand: " + _gson.toJson(cmd)); - } - - final String vmName = cmd.getVmName(); - State state = State.Unknown; - Integer vncPort = null; - - VmwareContext context = getServiceContext(); - VmwareHypervisorHost hyperHost = getHyperHost(context); - - try { - VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName); - if (vmMo != null) { - state = getVmState(vmMo); - if (state == State.Running) { - synchronized (_vms) { - _vms.put(vmName, State.Running); - } - } - return new CheckVirtualMachineAnswer(cmd, state, vncPort); - } else { - s_logger.warn("Can not find vm " + vmName + " to execute CheckVirtualMachineCommand"); - return new CheckVirtualMachineAnswer(cmd, state, vncPort); - } - - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - s_logger.error("Unexpected exception: " + VmwareHelper.getExceptionMessage(e), e); - - return new CheckVirtualMachineAnswer(cmd, state, vncPort); - } - } - - protected Answer execute(PrepareForMigrationCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource PrepareForMigrationCommand: " + _gson.toJson(cmd)); - } - - VirtualMachineTO vm = cmd.getVirtualMachine(); - if (s_logger.isDebugEnabled()) { - s_logger.debug("Preparing host for migrating " + vm); - } - - final String vmName = vm.getName(); - try { - VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); + if (vmMo.reset()) { + return new RebootAnswer(cmd, "reboot succeeded", true); + } + + String msg = "Reboot failed in vSphere. vm: " + cmd.getVmName(); + s_logger.warn(msg); + return new RebootAnswer(cmd, msg, false); + } else { + String msg = "Unable to find the VM in vSphere to reboot. vm: " + cmd.getVmName(); + s_logger.warn(msg); + return new RebootAnswer(cmd, msg, false); + } + } catch (Exception e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "RebootCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg); + return new RebootAnswer(cmd, msg, false); + } + } + + protected Answer execute(CheckVirtualMachineCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource CheckVirtualMachineCommand: " + _gson.toJson(cmd)); + } + + final String vmName = cmd.getVmName(); + State state = State.Unknown; + Integer vncPort = null; + + VmwareContext context = getServiceContext(); + VmwareHypervisorHost hyperHost = getHyperHost(context); + + try { + VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName); + if (vmMo != null) { + state = getVmState(vmMo); + if (state == State.Running) { + synchronized (_vms) { + _vms.put(vmName, State.Running); + } + } + return new CheckVirtualMachineAnswer(cmd, state, vncPort); + } else { + s_logger.warn("Can not find vm " + vmName + " to execute CheckVirtualMachineCommand"); + return new CheckVirtualMachineAnswer(cmd, state, vncPort); + } + + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + s_logger.error("Unexpected exception: " + VmwareHelper.getExceptionMessage(e), e); + + return new CheckVirtualMachineAnswer(cmd, state, vncPort); + } + } + + protected Answer execute(PrepareForMigrationCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource PrepareForMigrationCommand: " + _gson.toJson(cmd)); + } + + VirtualMachineTO vm = cmd.getVirtualMachine(); + if (s_logger.isDebugEnabled()) { + s_logger.debug("Preparing host for migrating " + vm); + } + + final String vmName = vm.getName(); + try { + VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - - // find VM through datacenter (VM is not at the target host yet) - VirtualMachineMO vmMo = hyperHost.findVmOnPeerHyperHost(vmName); - if (vmMo == null) { - String msg = "VM " + vmName + " does not exist in VMware datacenter"; - s_logger.error(msg); - throw new Exception(msg); - } - - NicTO[] nics = vm.getNics(); - for (NicTO nic : nics) { - // prepare network on the host - prepareNetworkFromNicInfo(new HostMO(getServiceContext(), _morHyperHost), nic); + + // find VM through datacenter (VM is not at the target host yet) + VirtualMachineMO vmMo = hyperHost.findVmOnPeerHyperHost(vmName); + if (vmMo == null) { + String msg = "VM " + vmName + " does not exist in VMware datacenter"; + s_logger.error(msg); + throw new Exception(msg); + } + + NicTO[] nics = vm.getNics(); + for (NicTO nic : nics) { + // prepare network on the host + prepareNetworkFromNicInfo(new HostMO(getServiceContext(), _morHyperHost), nic); } String secStoreUrl = mgr.getSecondaryStorageStoreUrl(Long.parseLong(_dcId)); @@ -2863,981 +2903,981 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa throw new Exception(msg); } mgr.prepareSecondaryStorageStore(secStoreUrl); - + ManagedObjectReference morSecDs = prepareSecondaryDatastoreOnHost(secStoreUrl); if (morSecDs == null) { String msg = "Failed to prepare secondary storage on host, secondary store url: " + secStoreUrl; throw new Exception(msg); } - - synchronized (_vms) { - _vms.put(vm.getName(), State.Migrating); - } - return new PrepareForMigrationAnswer(cmd); - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "Unexcpeted exception " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new PrepareForMigrationAnswer(cmd, msg); - } - } - - protected Answer execute(MigrateCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource MigrateCommand: " + _gson.toJson(cmd)); - } - - final String vmName = cmd.getVmName(); - - State state = null; - synchronized (_vms) { - state = _vms.get(vmName); - _vms.put(vmName, State.Stopping); - } - - try { - VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter(); - - // find VM through datacenter (VM is not at the target host yet) - VirtualMachineMO vmMo = hyperHost.findVmOnPeerHyperHost(vmName); - if (vmMo == null) { - String msg = "VM " + vmName + " does not exist in VMware datacenter"; - s_logger.error(msg); - throw new Exception(msg); - } - - VmwareHypervisorHost destHyperHost = getTargetHyperHost(new DatacenterMO(hyperHost.getContext(), morDc), cmd.getDestinationIp()); - - ManagedObjectReference morTargetPhysicalHost = destHyperHost.findMigrationTarget(vmMo); - if (morTargetPhysicalHost == null) { - throw new Exception("Unable to find a target capable physical host"); - } - if (!vmMo.migrate(destHyperHost.getHyperHostOwnerResourcePool(), morTargetPhysicalHost)) { - throw new Exception("Migration failed"); - } - - state = State.Stopping; - return new MigrateAnswer(cmd, true, "migration succeeded", null); - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "MigrationCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.warn(msg, e); - return new MigrateAnswer(cmd, false, msg, null); - } finally { - synchronized (_vms) { - _vms.put(vmName, state); - } - } - } - + synchronized (_vms) { + _vms.put(vm.getName(), State.Migrating); + } + return new PrepareForMigrationAnswer(cmd); + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "Unexcpeted exception " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new PrepareForMigrationAnswer(cmd, msg); + } + } + + protected Answer execute(MigrateCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource MigrateCommand: " + _gson.toJson(cmd)); + } + + final String vmName = cmd.getVmName(); + + State state = null; + synchronized (_vms) { + state = _vms.get(vmName); + _vms.put(vmName, State.Stopping); + } + + try { + VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); + ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter(); + + // find VM through datacenter (VM is not at the target host yet) + VirtualMachineMO vmMo = hyperHost.findVmOnPeerHyperHost(vmName); + if (vmMo == null) { + String msg = "VM " + vmName + " does not exist in VMware datacenter"; + s_logger.error(msg); + throw new Exception(msg); + } + + VmwareHypervisorHost destHyperHost = getTargetHyperHost(new DatacenterMO(hyperHost.getContext(), morDc), cmd.getDestinationIp()); + + ManagedObjectReference morTargetPhysicalHost = destHyperHost.findMigrationTarget(vmMo); + if (morTargetPhysicalHost == null) { + throw new Exception("Unable to find a target capable physical host"); + } + + if (!vmMo.migrate(destHyperHost.getHyperHostOwnerResourcePool(), morTargetPhysicalHost)) { + throw new Exception("Migration failed"); + } + + state = State.Stopping; + return new MigrateAnswer(cmd, true, "migration succeeded", null); + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "MigrationCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.warn(msg, e); + return new MigrateAnswer(cmd, false, msg, null); + } finally { + synchronized (_vms) { + _vms.put(vmName, state); + } + } + } + private VmwareHypervisorHost getTargetHyperHost(DatacenterMO dcMo, String destIp) throws Exception { - + VmwareManager mgr = dcMo.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - - ObjectContent[] ocs = dcMo.getHostPropertiesOnDatacenterHostFolder(new String[] { "name", "parent" }); - if (ocs != null && ocs.length > 0) { - for (ObjectContent oc : ocs) { - HostMO hostMo = new HostMO(dcMo.getContext(), oc.getObj()); - VmwareHypervisorHostNetworkSummary netSummary = hostMo.getHyperHostNetworkSummary(mgr.getManagementPortGroupByHost(hostMo)); - if (destIp.equalsIgnoreCase(netSummary.getHostIp())) { - return new HostMO(dcMo.getContext(), oc.getObj()); - } - } - } - - throw new Exception("Unable to locate dest host by " + destIp); - } - - protected Answer execute(CreateStoragePoolCommand cmd) { - return new Answer(cmd, true, "success"); - } - - protected Answer execute(ModifyStoragePoolCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource ModifyStoragePoolCommand: " + _gson.toJson(cmd)); - } - - try { - VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - StorageFilerTO pool = cmd.getPool(); - + + ObjectContent[] ocs = dcMo.getHostPropertiesOnDatacenterHostFolder(new String[] { "name", "parent" }); + if (ocs != null && ocs.length > 0) { + for (ObjectContent oc : ocs) { + HostMO hostMo = new HostMO(dcMo.getContext(), oc.getObj()); + VmwareHypervisorHostNetworkSummary netSummary = hostMo.getHyperHostNetworkSummary(mgr.getManagementPortGroupByHost(hostMo)); + if (destIp.equalsIgnoreCase(netSummary.getHostIp())) { + return new HostMO(dcMo.getContext(), oc.getObj()); + } + } + } + + throw new Exception("Unable to locate dest host by " + destIp); + } + + protected Answer execute(CreateStoragePoolCommand cmd) { + return new Answer(cmd, true, "success"); + } + + protected Answer execute(ModifyStoragePoolCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource ModifyStoragePoolCommand: " + _gson.toJson(cmd)); + } + + try { + VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); + StorageFilerTO pool = cmd.getPool(); + if (pool.getType() != StoragePoolType.NetworkFilesystem && pool.getType() != StoragePoolType.VMFS) { throw new Exception("Unsupported storage pool type " + pool.getType()); - } - + } + ManagedObjectReference morDatastore = null; morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, pool.getUuid()); if(morDatastore == null) - morDatastore = hyperHost.mountDatastore(pool.getType() == StoragePoolType.VMFS, pool.getHost(), - pool.getPort(), pool.getPath(), pool.getUuid().replace("-", "")); - - assert (morDatastore != null); - DatastoreSummary summary = new DatastoreMO(getServiceContext(), morDatastore).getSummary(); - long capacity = summary.getCapacity(); - long available = summary.getFreeSpace(); - Map tInfo = new HashMap(); - ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(cmd, capacity, available, tInfo); - return answer; - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "ModifyStoragePoolCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new Answer(cmd, false, msg); - } - } - - protected Answer execute(DeleteStoragePoolCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource DeleteStoragePoolCommand: " + _gson.toJson(cmd)); - } - - StorageFilerTO pool = cmd.getPool(); + morDatastore = hyperHost.mountDatastore(pool.getType() == StoragePoolType.VMFS, pool.getHost(), + pool.getPort(), pool.getPath(), pool.getUuid().replace("-", "")); + + assert (morDatastore != null); + DatastoreSummary summary = new DatastoreMO(getServiceContext(), morDatastore).getSummary(); + long capacity = summary.getCapacity(); + long available = summary.getFreeSpace(); + Map tInfo = new HashMap(); + ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(cmd, capacity, available, tInfo); + return answer; + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "ModifyStoragePoolCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new Answer(cmd, false, msg); + } + } + + protected Answer execute(DeleteStoragePoolCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource DeleteStoragePoolCommand: " + _gson.toJson(cmd)); + } + + StorageFilerTO pool = cmd.getPool(); try { - // We will leave datastore cleanup management to vCenter. Since for cluster VMFS datastore, it will always - // be mounted by vCenter. - - // VmwareHypervisorHost hyperHost = this.getHyperHost(getServiceContext()); - // hyperHost.unmountDatastore(pool.getUuid()); - Answer answer = new Answer(cmd, true, "success"); - return answer; - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "DeleteStoragePoolCommand (pool: " + pool.getHost() + ", path: " + pool.getPath() + ") failed due to " + VmwareHelper.getExceptionMessage(e); - return new Answer(cmd, false, msg); - } - } - - protected Answer execute(AttachVolumeCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource AttachVolumeCommand: " + _gson.toJson(cmd)); - } - - /* - * AttachVolumeCommand { "attach":true,"vmName":"i-2-1-KY","pooltype":"NetworkFilesystem", - * "volumeFolder":"/export/home/kelven/vmware-test/primary", "volumePath":"uuid", - * "volumeName":"volume name","deviceId":1 } - */ - try { - VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName()); - if (vmMo == null) { - String msg = "Unable to find the VM to execute AttachVolumeCommand, vmName: " + cmd.getVmName(); - s_logger.error(msg); - throw new Exception(msg); - } - - ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getPoolUuid()); - if (morDs == null) { - String msg = "Unable to find the mounted datastore to execute AttachVolumeCommand, vmName: " + cmd.getVmName(); - s_logger.error(msg); - throw new Exception(msg); - } - - DatastoreMO dsMo = new DatastoreMO(getServiceContext(), morDs); - String datastoreVolumePath = String.format("[%s] %s.vmdk", dsMo.getName(), cmd.getVolumePath()); - - AttachVolumeAnswer answer = new AttachVolumeAnswer(cmd, cmd.getDeviceId()); - if (cmd.getAttach()) { - vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs); - } else { - vmMo.removeAllSnapshots(); - vmMo.detachDisk(datastoreVolumePath, false); - } - - return answer; - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "AttachVolumeCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new AttachVolumeAnswer(cmd, msg); - } - } - - protected Answer execute(AttachIsoCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource AttachIsoCommand: " + _gson.toJson(cmd)); - } - - try { - VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName()); - if (vmMo == null) { - String msg = "Unable to find VM in vSphere to execute AttachIsoCommand, vmName: " + cmd.getVmName(); - s_logger.error(msg); - throw new Exception(msg); - } - - String storeUrl = cmd.getStoreUrl(); - if (storeUrl == null) { - if (!cmd.getIsoPath().equalsIgnoreCase("vmware-tools.iso")) { - String msg = "ISO store root url is not found in AttachIsoCommand"; - s_logger.error(msg); - throw new Exception(msg); - } else { - if (cmd.isAttach()) { - vmMo.mountToolsInstaller(); - } else { - vmMo.unmountToolsInstaller(); - } + // We will leave datastore cleanup management to vCenter. Since for cluster VMFS datastore, it will always + // be mounted by vCenter. - return new Answer(cmd); - } - } - - ManagedObjectReference morSecondaryDs = prepareSecondaryDatastoreOnHost(storeUrl); - String isoPath = cmd.getIsoPath(); - if (!isoPath.startsWith(storeUrl)) { - assert (false); - String msg = "ISO path does not start with the secondary storage root"; - s_logger.error(msg); - throw new Exception(msg); - } - - int isoNameStartPos = isoPath.lastIndexOf('/'); - String isoFileName = isoPath.substring(isoNameStartPos + 1); - String isoStorePathFromRoot = isoPath.substring(storeUrl.length(), isoNameStartPos); - - // TODO, check if iso is already attached, or if there is a previous - // attachment - String isoDatastorePath = String.format("[%s] %s%s", getSecondaryDatastoreUUID(storeUrl), isoStorePathFromRoot, isoFileName); - - if (cmd.isAttach()) { - vmMo.attachIso(isoDatastorePath, morSecondaryDs, true, false); - } else { - vmMo.detachIso(isoDatastorePath); - } - - return new Answer(cmd); - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } + // VmwareHypervisorHost hyperHost = this.getHyperHost(getServiceContext()); + // hyperHost.unmountDatastore(pool.getUuid()); + Answer answer = new Answer(cmd, true, "success"); + return answer; + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } - if(cmd.isAttach()) { - String msg = "AttachIsoCommand(attach) failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new Answer(cmd, false, msg); + String msg = "DeleteStoragePoolCommand (pool: " + pool.getHost() + ", path: " + pool.getPath() + ") failed due to " + VmwareHelper.getExceptionMessage(e); + return new Answer(cmd, false, msg); + } + } + + protected Answer execute(AttachVolumeCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource AttachVolumeCommand: " + _gson.toJson(cmd)); + } + + /* + * AttachVolumeCommand { "attach":true,"vmName":"i-2-1-KY","pooltype":"NetworkFilesystem", + * "volumeFolder":"/export/home/kelven/vmware-test/primary", "volumePath":"uuid", + * "volumeName":"volume name","deviceId":1 } + */ + try { + VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); + VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName()); + if (vmMo == null) { + String msg = "Unable to find the VM to execute AttachVolumeCommand, vmName: " + cmd.getVmName(); + s_logger.error(msg); + throw new Exception(msg); + } + + ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getPoolUuid()); + if (morDs == null) { + String msg = "Unable to find the mounted datastore to execute AttachVolumeCommand, vmName: " + cmd.getVmName(); + s_logger.error(msg); + throw new Exception(msg); + } + + DatastoreMO dsMo = new DatastoreMO(getServiceContext(), morDs); + String datastoreVolumePath = String.format("[%s] %s.vmdk", dsMo.getName(), cmd.getVolumePath()); + + AttachVolumeAnswer answer = new AttachVolumeAnswer(cmd, cmd.getDeviceId()); + if (cmd.getAttach()) { + vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs); } else { - String msg = "AttachIsoCommand(detach) failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.warn(msg, e); - return new Answer(cmd, false, msg); - } - } - } - - private synchronized ManagedObjectReference prepareSecondaryDatastoreOnHost(String storeUrl) throws Exception { - String storeName = getSecondaryDatastoreUUID(storeUrl); - URI uri = new URI(storeUrl); - - VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - ManagedObjectReference morDatastore = hyperHost.mountDatastore(false, uri.getHost(), 0, uri.getPath(), storeName.replace("-", "")); - + vmMo.removeAllSnapshots(); + vmMo.detachDisk(datastoreVolumePath, false); + } + + return answer; + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "AttachVolumeCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new AttachVolumeAnswer(cmd, msg); + } + } + + protected Answer execute(AttachIsoCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource AttachIsoCommand: " + _gson.toJson(cmd)); + } + + try { + VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); + VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName()); + if (vmMo == null) { + String msg = "Unable to find VM in vSphere to execute AttachIsoCommand, vmName: " + cmd.getVmName(); + s_logger.error(msg); + throw new Exception(msg); + } + + String storeUrl = cmd.getStoreUrl(); + if (storeUrl == null) { + if (!cmd.getIsoPath().equalsIgnoreCase("vmware-tools.iso")) { + String msg = "ISO store root url is not found in AttachIsoCommand"; + s_logger.error(msg); + throw new Exception(msg); + } else { + if (cmd.isAttach()) { + vmMo.mountToolsInstaller(); + } else { + vmMo.unmountToolsInstaller(); + } + + return new Answer(cmd); + } + } + + ManagedObjectReference morSecondaryDs = prepareSecondaryDatastoreOnHost(storeUrl); + String isoPath = cmd.getIsoPath(); + if (!isoPath.startsWith(storeUrl)) { + assert (false); + String msg = "ISO path does not start with the secondary storage root"; + s_logger.error(msg); + throw new Exception(msg); + } + + int isoNameStartPos = isoPath.lastIndexOf('/'); + String isoFileName = isoPath.substring(isoNameStartPos + 1); + String isoStorePathFromRoot = isoPath.substring(storeUrl.length(), isoNameStartPos); + + // TODO, check if iso is already attached, or if there is a previous + // attachment + String isoDatastorePath = String.format("[%s] %s%s", getSecondaryDatastoreUUID(storeUrl), isoStorePathFromRoot, isoFileName); + + if (cmd.isAttach()) { + vmMo.attachIso(isoDatastorePath, morSecondaryDs, true, false); + } else { + vmMo.detachIso(isoDatastorePath); + } + + return new Answer(cmd); + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + if(cmd.isAttach()) { + String msg = "AttachIsoCommand(attach) failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new Answer(cmd, false, msg); + } else { + String msg = "AttachIsoCommand(detach) failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.warn(msg, e); + return new Answer(cmd, false, msg); + } + } + } + + private synchronized ManagedObjectReference prepareSecondaryDatastoreOnHost(String storeUrl) throws Exception { + String storeName = getSecondaryDatastoreUUID(storeUrl); + URI uri = new URI(storeUrl); + + VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); + ManagedObjectReference morDatastore = hyperHost.mountDatastore(false, uri.getHost(), 0, uri.getPath(), storeName.replace("-", "")); + if (morDatastore == null) - throw new Exception("Unable to mount secondary storage on host. storeUrl: " + storeUrl); - - return morDatastore; - } - - private static String getSecondaryDatastoreUUID(String storeUrl) { - return UUID.nameUUIDFromBytes(storeUrl.getBytes()).toString(); - } - - protected Answer execute(ValidateSnapshotCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource ValidateSnapshotCommand: " + _gson.toJson(cmd)); - } - - // the command is no longer available - String expectedSnapshotBackupUuid = null; - String actualSnapshotBackupUuid = null; - String actualSnapshotUuid = null; - return new ValidateSnapshotAnswer(cmd, false, "ValidateSnapshotCommand is not supported for vmware yet", expectedSnapshotBackupUuid, actualSnapshotBackupUuid, actualSnapshotUuid); - } - - protected Answer execute(ManageSnapshotCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource ManageSnapshotCommand: " + _gson.toJson(cmd)); - } - - long snapshotId = cmd.getSnapshotId(); - - /* - * "ManageSnapshotCommand", - * "{\"_commandSwitch\":\"-c\",\"_volumePath\":\"i-2-3-KY-ROOT\",\"_snapshotName\":\"i-2-3-KY_i-2-3-KY-ROOT_20101102203827\",\"_snapshotId\":1,\"_vmName\":\"i-2-3-KY\"}" - */ - boolean success = false; - String cmdSwitch = cmd.getCommandSwitch(); - String snapshotOp = "Unsupported snapshot command." + cmdSwitch; - if (cmdSwitch.equals(ManageSnapshotCommand.CREATE_SNAPSHOT)) { - snapshotOp = "create"; - } else if (cmdSwitch.equals(ManageSnapshotCommand.DESTROY_SNAPSHOT)) { - snapshotOp = "destroy"; - } - - String details = "ManageSnapshotCommand operation: " + snapshotOp + " Failed for snapshotId: " + snapshotId; - String snapshotUUID = null; + throw new Exception("Unable to mount secondary storage on host. storeUrl: " + storeUrl); + + return morDatastore; + } + + private static String getSecondaryDatastoreUUID(String storeUrl) { + return UUID.nameUUIDFromBytes(storeUrl.getBytes()).toString(); + } + + protected Answer execute(ValidateSnapshotCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource ValidateSnapshotCommand: " + _gson.toJson(cmd)); + } + + // the command is no longer available + String expectedSnapshotBackupUuid = null; + String actualSnapshotBackupUuid = null; + String actualSnapshotUuid = null; + return new ValidateSnapshotAnswer(cmd, false, "ValidateSnapshotCommand is not supported for vmware yet", expectedSnapshotBackupUuid, actualSnapshotBackupUuid, actualSnapshotUuid); + } + + protected Answer execute(ManageSnapshotCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource ManageSnapshotCommand: " + _gson.toJson(cmd)); + } + + long snapshotId = cmd.getSnapshotId(); + + /* + * "ManageSnapshotCommand", + * "{\"_commandSwitch\":\"-c\",\"_volumePath\":\"i-2-3-KY-ROOT\",\"_snapshotName\":\"i-2-3-KY_i-2-3-KY-ROOT_20101102203827\",\"_snapshotId\":1,\"_vmName\":\"i-2-3-KY\"}" + */ + boolean success = false; + String cmdSwitch = cmd.getCommandSwitch(); + String snapshotOp = "Unsupported snapshot command." + cmdSwitch; + if (cmdSwitch.equals(ManageSnapshotCommand.CREATE_SNAPSHOT)) { + snapshotOp = "create"; + } else if (cmdSwitch.equals(ManageSnapshotCommand.DESTROY_SNAPSHOT)) { + snapshotOp = "destroy"; + } + + String details = "ManageSnapshotCommand operation: " + snapshotOp + " Failed for snapshotId: " + snapshotId; + String snapshotUUID = null; // snapshot operation (create or destroy) is handled inside BackupSnapshotCommand(), we just fake // a success return here snapshotUUID = UUID.randomUUID().toString(); success = true; details = null; - - return new ManageSnapshotAnswer(cmd, snapshotId, snapshotUUID, success, details); - } - - protected Answer execute(BackupSnapshotCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource BackupSnapshotCommand: " + _gson.toJson(cmd)); - } - - try { - VmwareContext context = getServiceContext(); - VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - - return mgr.getStorageManager().execute(this, cmd); - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String details = "BackupSnapshotCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(details, e); - return new BackupSnapshotAnswer(cmd, false, details, null, true); - } - } - - - protected Answer execute(CreateVolumeFromSnapshotCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource CreateVolumeFromSnapshotCommand: " + _gson.toJson(cmd)); - } - - String details = null; - boolean success = false; - String newVolumeName = UUID.randomUUID().toString(); - - try { - VmwareContext context = getServiceContext(); - VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - return mgr.getStorageManager().execute(this, cmd); - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - details = "CreateVolumeFromSnapshotCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(details, e); - } - - return new CreateVolumeFromSnapshotAnswer(cmd, success, details, newVolumeName); - } - - protected Answer execute(CreatePrivateTemplateFromVolumeCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource CreatePrivateTemplateFromVolumeCommand: " + _gson.toJson(cmd)); - } - - try { - VmwareContext context = getServiceContext(); - VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - - return mgr.getStorageManager().execute(this, cmd); - - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String details = "CreatePrivateTemplateFromVolumeCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(details, e); - return new CreatePrivateTemplateAnswer(cmd, false, details); - } - } - - protected Answer execute(final UpgradeSnapshotCommand cmd) { - return new Answer(cmd, true, "success"); - } - - protected Answer execute(CreatePrivateTemplateFromSnapshotCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource CreatePrivateTemplateFromSnapshotCommand: " + _gson.toJson(cmd)); - } - - try { - VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - return mgr.getStorageManager().execute(this, cmd); - - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String details = "CreatePrivateTemplateFromSnapshotCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(details, e); - return new CreatePrivateTemplateAnswer(cmd, false, details); - } - } - - protected Answer execute(GetStorageStatsCommand cmd) { - if (s_logger.isTraceEnabled()) { - s_logger.trace("Executing resource GetStorageStatsCommand: " + _gson.toJson(cmd)); - } - - try { - VmwareContext context = getServiceContext(); - VmwareHypervisorHost hyperHost = getHyperHost(context); - ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getStorageId()); - - if (morDs != null) { - DatastoreMO datastoreMo = new DatastoreMO(context, morDs); - DatastoreSummary summary = datastoreMo.getSummary(); - assert (summary != null); - - long capacity = summary.getCapacity(); - long free = summary.getFreeSpace(); - long used = capacity - free; - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Datastore summary info, storageId: " + cmd.getStorageId() + ", localPath: " + cmd.getLocalPath() + ", poolType: " + cmd.getPooltype() + ", capacity: " + capacity - + ", free: " + free + ", used: " + used); - } - - if (summary.getCapacity() <= 0) { - s_logger.warn("Something is wrong with vSphere NFS datastore, rebooting ESX(ESXi) host should help"); - } - - return new GetStorageStatsAnswer(cmd, capacity, used); - } else { - String msg = "Could not find datastore for GetStorageStatsCommand storageId : " + cmd.getStorageId() + ", localPath: " + cmd.getLocalPath() + ", poolType: " + cmd.getPooltype(); - - s_logger.error(msg); - return new GetStorageStatsAnswer(cmd, msg); - } - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "Unable to execute GetStorageStatsCommand(storageId : " + cmd.getStorageId() + ", localPath: " + cmd.getLocalPath() + ", poolType: " + cmd.getPooltype() + ") due to " - + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new GetStorageStatsAnswer(cmd, msg); - } - } - - protected Answer execute(GetVncPortCommand cmd) { - if (s_logger.isTraceEnabled()) { - s_logger.trace("Executing resource GetVncPortCommand: " + _gson.toJson(cmd)); - } - - try { - VmwareContext context = getServiceContext(); - VmwareHypervisorHost hyperHost = getHyperHost(context); - assert(hyperHost instanceof HostMO); + + return new ManageSnapshotAnswer(cmd, snapshotId, snapshotUUID, success, details); + } + + protected Answer execute(BackupSnapshotCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource BackupSnapshotCommand: " + _gson.toJson(cmd)); + } + + try { + VmwareContext context = getServiceContext(); VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getName()); - if (vmMo == null) { + return mgr.getStorageManager().execute(this, cmd); + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String details = "BackupSnapshotCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(details, e); + return new BackupSnapshotAnswer(cmd, false, details, null, true); + } + } + + + protected Answer execute(CreateVolumeFromSnapshotCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource CreateVolumeFromSnapshotCommand: " + _gson.toJson(cmd)); + } + + String details = null; + boolean success = false; + String newVolumeName = UUID.randomUUID().toString(); + + try { + VmwareContext context = getServiceContext(); + VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + return mgr.getStorageManager().execute(this, cmd); + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + details = "CreateVolumeFromSnapshotCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(details, e); + } + + return new CreateVolumeFromSnapshotAnswer(cmd, success, details, newVolumeName); + } + + protected Answer execute(CreatePrivateTemplateFromVolumeCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource CreatePrivateTemplateFromVolumeCommand: " + _gson.toJson(cmd)); + } + + try { + VmwareContext context = getServiceContext(); + VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + + return mgr.getStorageManager().execute(this, cmd); + + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String details = "CreatePrivateTemplateFromVolumeCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(details, e); + return new CreatePrivateTemplateAnswer(cmd, false, details); + } + } + + protected Answer execute(final UpgradeSnapshotCommand cmd) { + return new Answer(cmd, true, "success"); + } + + protected Answer execute(CreatePrivateTemplateFromSnapshotCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource CreatePrivateTemplateFromSnapshotCommand: " + _gson.toJson(cmd)); + } + + try { + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + return mgr.getStorageManager().execute(this, cmd); + + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String details = "CreatePrivateTemplateFromSnapshotCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(details, e); + return new CreatePrivateTemplateAnswer(cmd, false, details); + } + } + + protected Answer execute(GetStorageStatsCommand cmd) { + if (s_logger.isTraceEnabled()) { + s_logger.trace("Executing resource GetStorageStatsCommand: " + _gson.toJson(cmd)); + } + + try { + VmwareContext context = getServiceContext(); + VmwareHypervisorHost hyperHost = getHyperHost(context); + ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getStorageId()); + + if (morDs != null) { + DatastoreMO datastoreMo = new DatastoreMO(context, morDs); + DatastoreSummary summary = datastoreMo.getSummary(); + assert (summary != null); + + long capacity = summary.getCapacity(); + long free = summary.getFreeSpace(); + long used = capacity - free; + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Datastore summary info, storageId: " + cmd.getStorageId() + ", localPath: " + cmd.getLocalPath() + ", poolType: " + cmd.getPooltype() + ", capacity: " + capacity + + ", free: " + free + ", used: " + used); + } + + if (summary.getCapacity() <= 0) { + s_logger.warn("Something is wrong with vSphere NFS datastore, rebooting ESX(ESXi) host should help"); + } + + return new GetStorageStatsAnswer(cmd, capacity, used); + } else { + String msg = "Could not find datastore for GetStorageStatsCommand storageId : " + cmd.getStorageId() + ", localPath: " + cmd.getLocalPath() + ", poolType: " + cmd.getPooltype(); + + s_logger.error(msg); + return new GetStorageStatsAnswer(cmd, msg); + } + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "Unable to execute GetStorageStatsCommand(storageId : " + cmd.getStorageId() + ", localPath: " + cmd.getLocalPath() + ", poolType: " + cmd.getPooltype() + ") due to " + + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new GetStorageStatsAnswer(cmd, msg); + } + } + + protected Answer execute(GetVncPortCommand cmd) { + if (s_logger.isTraceEnabled()) { + s_logger.trace("Executing resource GetVncPortCommand: " + _gson.toJson(cmd)); + } + + try { + VmwareContext context = getServiceContext(); + VmwareHypervisorHost hyperHost = getHyperHost(context); + assert(hyperHost instanceof HostMO); + VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + + VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getName()); + if (vmMo == null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Unable to find the owner VM for GetVncPortCommand on host " + hyperHost.getHyperHostName() + ", try within datacenter"); - } - - vmMo = hyperHost.findVmOnPeerHyperHost(cmd.getName()); - + } + + vmMo = hyperHost.findVmOnPeerHyperHost(cmd.getName()); + if (vmMo == null) { throw new Exception("Unable to find VM in vSphere, vm: " + cmd.getName()); - } + } } - - Pair portInfo = vmMo.getVncPort(mgr.getManagementPortGroupByHost((HostMO)hyperHost)); - - if (s_logger.isTraceEnabled()) { - s_logger.trace("Found vnc port info. vm: " + cmd.getName() + " host: " + portInfo.first() + ", vnc port: " + portInfo.second()); - } - return new GetVncPortAnswer(cmd, portInfo.first(), portInfo.second()); - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "GetVncPortCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new GetVncPortAnswer(cmd, msg); - } - } - - protected Answer execute(SetupCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource SetupCommand: " + _gson.toJson(cmd)); - } - - return new SetupAnswer(cmd, false); - } - - protected Answer execute(MaintainCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource MaintainCommand: " + _gson.toJson(cmd)); - } - - return new MaintainAnswer(cmd, "Put host in maintaince"); - } - - protected Answer execute(PingTestCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource PingTestCommand: " + _gson.toJson(cmd)); - } - - return new Answer(cmd); - } - - protected Answer execute(CheckOnHostCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource CheckOnHostCommand: " + _gson.toJson(cmd)); - } - - return new CheckOnHostAnswer(cmd, null, "Not Implmeneted"); - } - + + Pair portInfo = vmMo.getVncPort(mgr.getManagementPortGroupByHost((HostMO)hyperHost)); + + if (s_logger.isTraceEnabled()) { + s_logger.trace("Found vnc port info. vm: " + cmd.getName() + " host: " + portInfo.first() + ", vnc port: " + portInfo.second()); + } + return new GetVncPortAnswer(cmd, portInfo.first(), portInfo.second()); + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "GetVncPortCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new GetVncPortAnswer(cmd, msg); + } + } + + protected Answer execute(SetupCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource SetupCommand: " + _gson.toJson(cmd)); + } + + return new SetupAnswer(cmd, false); + } + + protected Answer execute(MaintainCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource MaintainCommand: " + _gson.toJson(cmd)); + } + + return new MaintainAnswer(cmd, "Put host in maintaince"); + } + + protected Answer execute(PingTestCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource PingTestCommand: " + _gson.toJson(cmd)); + } + + return new Answer(cmd); + } + + protected Answer execute(CheckOnHostCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource CheckOnHostCommand: " + _gson.toJson(cmd)); + } + + return new CheckOnHostAnswer(cmd, null, "Not Implmeneted"); + } + protected Answer execute(ModifySshKeysCommand cmd) { - //do not log the command contents for this command. do NOT log the ssh keys - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource ModifySshKeysCommand."); - } - - return new Answer(cmd); - } - - protected Answer execute(PoolEjectCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource PoolEjectCommand: " + _gson.toJson(cmd)); - } - - return new Answer(cmd, false, "PoolEjectCommand is not available for vmware"); - } - - @Override - public PrimaryStorageDownloadAnswer execute(PrimaryStorageDownloadCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource PrimaryStorageDownloadCommand: " + _gson.toJson(cmd)); - } - - try { - VmwareContext context = getServiceContext(); - VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - return (PrimaryStorageDownloadAnswer) mgr.getStorageManager().execute(this, cmd); - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "PrimaryStorageDownloadCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new PrimaryStorageDownloadAnswer(msg); - } - } - - @Override - public Answer execute(DestroyCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource DestroyCommand: " + _gson.toJson(cmd)); - } - - /* - * DestroyCommand content example - * - * {"volume": {"id":5,"name":"Volume1", "mountPoint":"/export/home/kelven/vmware-test/primary", - * "path":"6bb8762f-c34c-453c-8e03-26cc246ceec4", "size":0,"type":"DATADISK","resourceType": - * "STORAGE_POOL","storagePoolType":"NetworkFilesystem", "poolId":0,"deviceId":0 } } - * - * {"volume": {"id":1, "name":"i-2-1-KY-ROOT", "mountPoint":"/export/home/kelven/vmware-test/primary", - * "path":"i-2-1-KY-ROOT","size":0,"type":"ROOT", "resourceType":"STORAGE_POOL", "storagePoolType":"NetworkFilesystem", - * "poolId":0,"deviceId":0 } } - */ - - try { - VmwareContext context = getServiceContext(); - VmwareHypervisorHost hyperHost = getHyperHost(context); - VolumeTO vol = cmd.getVolume(); - - ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, vol.getPoolUuid()); - if (morDs == null) { - String msg = "Unable to find datastore based on volume mount point " + cmd.getVolume().getMountPoint(); - s_logger.error(msg); - throw new Exception(msg); - } - + //do not log the command contents for this command. do NOT log the ssh keys + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource ModifySshKeysCommand."); + } + + return new Answer(cmd); + } + + protected Answer execute(PoolEjectCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource PoolEjectCommand: " + _gson.toJson(cmd)); + } + + return new Answer(cmd, false, "PoolEjectCommand is not available for vmware"); + } + + @Override + public PrimaryStorageDownloadAnswer execute(PrimaryStorageDownloadCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource PrimaryStorageDownloadCommand: " + _gson.toJson(cmd)); + } + + try { + VmwareContext context = getServiceContext(); + VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + return (PrimaryStorageDownloadAnswer) mgr.getStorageManager().execute(this, cmd); + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "PrimaryStorageDownloadCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new PrimaryStorageDownloadAnswer(msg); + } + } + + @Override + public Answer execute(DestroyCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource DestroyCommand: " + _gson.toJson(cmd)); + } + + /* + * DestroyCommand content example + * + * {"volume": {"id":5,"name":"Volume1", "mountPoint":"/export/home/kelven/vmware-test/primary", + * "path":"6bb8762f-c34c-453c-8e03-26cc246ceec4", "size":0,"type":"DATADISK","resourceType": + * "STORAGE_POOL","storagePoolType":"NetworkFilesystem", "poolId":0,"deviceId":0 } } + * + * {"volume": {"id":1, "name":"i-2-1-KY-ROOT", "mountPoint":"/export/home/kelven/vmware-test/primary", + * "path":"i-2-1-KY-ROOT","size":0,"type":"ROOT", "resourceType":"STORAGE_POOL", "storagePoolType":"NetworkFilesystem", + * "poolId":0,"deviceId":0 } } + */ + + try { + VmwareContext context = getServiceContext(); + VmwareHypervisorHost hyperHost = getHyperHost(context); + VolumeTO vol = cmd.getVolume(); + + ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, vol.getPoolUuid()); + if (morDs == null) { + String msg = "Unable to find datastore based on volume mount point " + cmd.getVolume().getMountPoint(); + s_logger.error(msg); + throw new Exception(msg); + } + DatastoreMO dsMo = new DatastoreMO(context, morDs); - - ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter(); + + ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter(); ManagedObjectReference morCluster = hyperHost.getHyperHostCluster(); ClusterMO clusterMo = new ClusterMO(context, morCluster); - - if (cmd.getVolume().getType() == Volume.Type.ROOT) { - String vmName = cmd.getVmName(); - if (vmName != null) { - VirtualMachineMO vmMo = clusterMo.findVmOnHyperHost(vmName); - if (vmMo != null) { + + if (cmd.getVolume().getType() == Volume.Type.ROOT) { + String vmName = cmd.getVmName(); + if (vmName != null) { + VirtualMachineMO vmMo = clusterMo.findVmOnHyperHost(vmName); + if (vmMo != null) { if (s_logger.isInfoEnabled()) { s_logger.info("Destroy root volume and VM itself. vmName " + vmName); - } - - HostMO hostMo = vmMo.getRunningHost(); - List networks = vmMo.getNetworksWithDetails(); - + } + + HostMO hostMo = vmMo.getRunningHost(); + List networks = vmMo.getNetworksWithDetails(); + // tear down all devices first before we destroy the VM to avoid accidently delete disk backing files if (getVmState(vmMo) != State.Stopped) - vmMo.safePowerOff(_shutdown_waitMs); - vmMo.tearDownDevices(new Class[] { VirtualDisk.class, VirtualEthernetCard.class }); - vmMo.destroy(); - - for (NetworkDetails netDetails : networks) { - if (netDetails.getGCTag() != null && netDetails.getGCTag().equalsIgnoreCase("true")) { - if (netDetails.getVMMorsOnNetwork() == null || netDetails.getVMMorsOnNetwork().length == 1) { - cleanupNetwork(hostMo, netDetails); - } - } - } - } - - if (s_logger.isInfoEnabled()) - s_logger.info("Destroy volume by original name: " + cmd.getVolume().getPath() + ".vmdk"); + vmMo.safePowerOff(_shutdown_waitMs); + vmMo.tearDownDevices(new Class[] { VirtualDisk.class, VirtualEthernetCard.class }); + vmMo.destroy(); + + for (NetworkDetails netDetails : networks) { + if (netDetails.getGCTag() != null && netDetails.getGCTag().equalsIgnoreCase("true")) { + if (netDetails.getVMMorsOnNetwork() == null || netDetails.getVMMorsOnNetwork().length == 1) { + cleanupNetwork(hostMo, netDetails); + } + } + } + } + + if (s_logger.isInfoEnabled()) + s_logger.info("Destroy volume by original name: " + cmd.getVolume().getPath() + ".vmdk"); dsMo.deleteFile(cmd.getVolume().getPath() + ".vmdk", morDc, true); - + // root volume may be created via linked-clone, delete the delta disk as well if (s_logger.isInfoEnabled()) - s_logger.info("Destroy volume by derived name: " + cmd.getVolume().getPath() + "-delta.vmdk"); + s_logger.info("Destroy volume by derived name: " + cmd.getVolume().getPath() + "-delta.vmdk"); dsMo.deleteFile(cmd.getVolume().getPath() + "-delta.vmdk", morDc, true); - return new Answer(cmd, true, "Success"); - } - + return new Answer(cmd, true, "Success"); + } + if (s_logger.isInfoEnabled()) { s_logger.info("Destroy root volume directly from datastore"); - } + } } else { - // evitTemplate will be converted into DestroyCommand, test if we are running in this case + // evitTemplate will be converted into DestroyCommand, test if we are running in this case VirtualMachineMO vmMo = clusterMo.findVmOnHyperHost(cmd.getVolume().getPath()); if (vmMo != null) { if (s_logger.isInfoEnabled()) s_logger.info("Destroy template volume " + cmd.getVolume().getPath()); - - vmMo.destroy(); + + vmMo.destroy(); return new Answer(cmd, true, "Success"); } - } - - String chainInfo = cmd.getVolume().getChainInfo(); - if (chainInfo != null && !chainInfo.isEmpty()) { - s_logger.info("Destroy volume by chain info: " + chainInfo); - String[] diskChain = _gson.fromJson(chainInfo, String[].class); - - if (diskChain != null && diskChain.length > 0) { - for (String backingName : diskChain) { + } + + String chainInfo = cmd.getVolume().getChainInfo(); + if (chainInfo != null && !chainInfo.isEmpty()) { + s_logger.info("Destroy volume by chain info: " + chainInfo); + String[] diskChain = _gson.fromJson(chainInfo, String[].class); + + if (diskChain != null && diskChain.length > 0) { + for (String backingName : diskChain) { if (s_logger.isInfoEnabled()) { s_logger.info("Delete volume backing file: " + backingName); - } - dsMo.deleteFile(backingName, morDc, true); - } - } else { + } + dsMo.deleteFile(backingName, morDc, true); + } + } else { if (s_logger.isInfoEnabled()) { s_logger.info("Empty disk chain info, fall back to try to delete by original backing file name"); - } + } dsMo.deleteFile(cmd.getVolume().getPath() + ".vmdk", morDc, true); - + if (s_logger.isInfoEnabled()) { - s_logger.info("Destroy volume by derived name: " + cmd.getVolume().getPath() + "-flat.vmdk"); + s_logger.info("Destroy volume by derived name: " + cmd.getVolume().getPath() + "-flat.vmdk"); } dsMo.deleteFile(cmd.getVolume().getPath() + "-flat.vmdk", morDc, true); - } - } else { + } + } else { if (s_logger.isInfoEnabled()) { s_logger.info("Destroy volume by original name: " + cmd.getVolume().getPath() + ".vmdk"); - } + } dsMo.deleteFile(cmd.getVolume().getPath() + ".vmdk", morDc, true); - + if (s_logger.isInfoEnabled()) { - s_logger.info("Destroy volume by derived name: " + cmd.getVolume().getPath() + "-flat.vmdk"); + s_logger.info("Destroy volume by derived name: " + cmd.getVolume().getPath() + "-flat.vmdk"); } dsMo.deleteFile(cmd.getVolume().getPath() + "-flat.vmdk", morDc, true); - } - - return new Answer(cmd, true, "Success"); - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "DestroyCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new Answer(cmd, false, msg); - } - } - - private void cleanupNetwork(HostMO hostMo, NetworkDetails netDetails) { - // we will no longer cleanup VLAN networks in order to support native VMware HA - /* - * assert(netDetails.getName() != null); try { synchronized(this) { NetworkMO networkMo = new - * NetworkMO(hostMo.getContext(), netDetails.getNetworkMor()); ManagedObjectReference[] vms = - * networkMo.getVMsOnNetwork(); if(vms == null || vms.length == 0) { if(s_logger.isInfoEnabled()) { - * s_logger.info("Cleanup network as it is currently not in use: " + netDetails.getName()); } - * - * hostMo.deletePortGroup(netDetails.getName()); } } } catch(Throwable e) { - * s_logger.warn("Unable to cleanup network due to exception, skip for next time"); } - */ - } - - @Override - public CopyVolumeAnswer execute(CopyVolumeCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource CopyVolumeCommand: " + _gson.toJson(cmd)); - } - - try { - VmwareContext context = getServiceContext(); - VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - return (CopyVolumeAnswer) mgr.getStorageManager().execute(this, cmd); - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "CopyVolumeCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new CopyVolumeAnswer(cmd, false, msg, null, null); - } - } - - @Override - public synchronized CreateAnswer execute(CreateCommand cmd) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Executing resource CreateCommand: " + _gson.toJson(cmd)); - } - - StorageFilerTO pool = cmd.getPool(); + } + + return new Answer(cmd, true, "Success"); + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "DestroyCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new Answer(cmd, false, msg); + } + } + + private void cleanupNetwork(HostMO hostMo, NetworkDetails netDetails) { + // we will no longer cleanup VLAN networks in order to support native VMware HA + /* + * assert(netDetails.getName() != null); try { synchronized(this) { NetworkMO networkMo = new + * NetworkMO(hostMo.getContext(), netDetails.getNetworkMor()); ManagedObjectReference[] vms = + * networkMo.getVMsOnNetwork(); if(vms == null || vms.length == 0) { if(s_logger.isInfoEnabled()) { + * s_logger.info("Cleanup network as it is currently not in use: " + netDetails.getName()); } + * + * hostMo.deletePortGroup(netDetails.getName()); } } } catch(Throwable e) { + * s_logger.warn("Unable to cleanup network due to exception, skip for next time"); } + */ + } + + @Override + public CopyVolumeAnswer execute(CopyVolumeCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource CopyVolumeCommand: " + _gson.toJson(cmd)); + } + + try { + VmwareContext context = getServiceContext(); + VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + return (CopyVolumeAnswer) mgr.getStorageManager().execute(this, cmd); + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "CopyVolumeCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new CopyVolumeAnswer(cmd, false, msg, null, null); + } + } + + @Override + public synchronized CreateAnswer execute(CreateCommand cmd) { + if (s_logger.isInfoEnabled()) { + s_logger.info("Executing resource CreateCommand: " + _gson.toJson(cmd)); + } + + StorageFilerTO pool = cmd.getPool(); DiskProfile dskch = cmd.getDiskCharacteristics(); - try { - VmwareContext context = getServiceContext(); - VmwareHypervisorHost hyperHost = getHyperHost(context); - DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter()); + try { + VmwareContext context = getServiceContext(); + VmwareHypervisorHost hyperHost = getHyperHost(context); + DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter()); - ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, pool.getUuid()); + ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, pool.getUuid()); if (morDatastore == null) - throw new Exception("Unable to find datastore in vSphere"); - - DatastoreMO dsMo = new DatastoreMO(context, morDatastore); - - if (cmd.getDiskCharacteristics().getType() == Volume.Type.ROOT) { - if (cmd.getTemplateUrl() == null) { - // create a root volume for blank VM - String dummyVmName = getWorkerName(context, cmd, 0); + throw new Exception("Unable to find datastore in vSphere"); + + DatastoreMO dsMo = new DatastoreMO(context, morDatastore); + + if (cmd.getDiskCharacteristics().getType() == Volume.Type.ROOT) { + if (cmd.getTemplateUrl() == null) { + // create a root volume for blank VM + String dummyVmName = getWorkerName(context, cmd, 0); VirtualMachineMO vmMo = null; - try { - vmMo = prepareVolumeHostDummyVm(hyperHost, dsMo, dummyVmName); - if (vmMo == null) { - throw new Exception("Unable to create a dummy VM for volume creation"); - } - - String volumeDatastorePath = String.format("[%s] %s.vmdk", dsMo.getName(), cmd.getDiskCharacteristics().getName()); - synchronized (this) { - s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath); - VmwareHelper.deleteVolumeVmdkFiles(dsMo, cmd.getDiskCharacteristics().getName(), dcMo); - vmMo.createDisk(volumeDatastorePath, (int) (cmd.getDiskCharacteristics().getSize() / (1024L * 1024L)), morDatastore, -1); - vmMo.detachDisk(volumeDatastorePath, false); - } - - VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), cmd.getDiskCharacteristics().getName(), pool.getPath(), cmd - .getDiskCharacteristics().getName(), cmd.getDiskCharacteristics().getSize(), null); - return new CreateAnswer(cmd, vol); - } finally { - vmMo.detachAllDisks(); - - s_logger.info("Destroy dummy VM after volume creation"); + try { + vmMo = prepareVolumeHostDummyVm(hyperHost, dsMo, dummyVmName); + if (vmMo == null) { + throw new Exception("Unable to create a dummy VM for volume creation"); + } + + String volumeDatastorePath = String.format("[%s] %s.vmdk", dsMo.getName(), cmd.getDiskCharacteristics().getName()); + synchronized (this) { + s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath); + VmwareHelper.deleteVolumeVmdkFiles(dsMo, cmd.getDiskCharacteristics().getName(), dcMo); + vmMo.createDisk(volumeDatastorePath, (int) (cmd.getDiskCharacteristics().getSize() / (1024L * 1024L)), morDatastore, -1); + vmMo.detachDisk(volumeDatastorePath, false); + } + + VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), cmd.getDiskCharacteristics().getName(), pool.getPath(), cmd + .getDiskCharacteristics().getName(), cmd.getDiskCharacteristics().getSize(), null); + return new CreateAnswer(cmd, vol); + } finally { + vmMo.detachAllDisks(); + + s_logger.info("Destroy dummy VM after volume creation"); vmMo.destroy(); - } - } else { - VirtualMachineMO vmTemplate = VmwareHelper.pickOneVmOnRunningHost(dcMo.findVmByNameAndLabel(cmd.getTemplateUrl()), true); - if (vmTemplate == null) { + } + } else { + VirtualMachineMO vmTemplate = VmwareHelper.pickOneVmOnRunningHost(dcMo.findVmByNameAndLabel(cmd.getTemplateUrl()), true); + if (vmTemplate == null) { s_logger.warn("Template host in vSphere is not in connected state, request template reload"); return new CreateAnswer(cmd, "Template host in vSphere is not in connected state, request template reload", true); } - + ManagedObjectReference morPool = hyperHost.getHyperHostOwnerResourcePool(); - ManagedObjectReference morCluster = hyperHost.getHyperHostCluster(); - ManagedObjectReference morBaseSnapshot = vmTemplate.getSnapshotMor("cloud.template.base"); - if (morBaseSnapshot == null) { - String msg = "Unable to find template base snapshot, invalid template"; - s_logger.error(msg); - throw new Exception(msg); - } - + ManagedObjectReference morCluster = hyperHost.getHyperHostCluster(); + ManagedObjectReference morBaseSnapshot = vmTemplate.getSnapshotMor("cloud.template.base"); + if (morBaseSnapshot == null) { + String msg = "Unable to find template base snapshot, invalid template"; + s_logger.error(msg); + throw new Exception(msg); + } + String name = cmd.getDiskCharacteristics().getName(); - if(dsMo.folderExists(String.format("[%s]", dsMo.getName()), name)) - dsMo.deleteFile(String.format("[%s] %s/", dsMo.getName(), name), dcMo.getMor(), false); - - s_logger.info("create linked clone from template"); - if (!vmTemplate.createLinkedClone(name, morBaseSnapshot, dcMo.getVmFolder(), morPool, morDatastore)) { - String msg = "Unable to clone from the template"; - s_logger.error(msg); - throw new Exception(msg); - } - - VirtualMachineMO vmMo = new ClusterMO(context, morCluster).findVmOnHyperHost(name); - assert (vmMo != null); + if(dsMo.folderExists(String.format("[%s]", dsMo.getName()), name)) + dsMo.deleteFile(String.format("[%s] %s/", dsMo.getName(), name), dcMo.getMor(), false); + + s_logger.info("create linked clone from template"); + if (!vmTemplate.createLinkedClone(name, morBaseSnapshot, dcMo.getVmFolder(), morPool, morDatastore)) { + String msg = "Unable to clone from the template"; + s_logger.error(msg); + throw new Exception(msg); + } + + VirtualMachineMO vmMo = new ClusterMO(context, morCluster).findVmOnHyperHost(name); + assert (vmMo != null); // we can't rely on un-offical API (VirtualMachineMO.moveAllVmDiskFiles() any more, use hard-coded disk names that we know - // to move files + // to move files s_logger.info("Move volume out of volume-wrapper VM "); dsMo.moveDatastoreFile(String.format("[%s] %s/%s.vmdk", dsMo.getName(), name, name), - dcMo.getMor(), dsMo.getMor(), - String.format("[%s] %s.vmdk", dsMo.getName(), name), dcMo.getMor(), true); - + dcMo.getMor(), dsMo.getMor(), + String.format("[%s] %s.vmdk", dsMo.getName(), name), dcMo.getMor(), true); + dsMo.moveDatastoreFile(String.format("[%s] %s/%s-delta.vmdk", dsMo.getName(), name, name), - dcMo.getMor(), dsMo.getMor(), - String.format("[%s] %s-delta.vmdk", dsMo.getName(), name), dcMo.getMor(), true); - - s_logger.info("detach disks from volume-wrapper VM " + name); - vmMo.detachAllDisks(); - - s_logger.info("destroy volume-wrapper VM " + name); - vmMo.destroy(); - - String srcFile = String.format("[%s] %s/", dsMo.getName(), name); - dsMo.deleteFile(srcFile, dcMo.getMor(), true); - - VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), name, pool.getPath(), name, cmd.getDiskCharacteristics().getSize(), null); - return new CreateAnswer(cmd, vol); - } - } else { - // create data volume - VirtualMachineMO vmMo = null; - String volumeUuid = UUID.randomUUID().toString().replace("-", ""); - String volumeDatastorePath = String.format("[%s] %s.vmdk", dsMo.getName(), volumeUuid); - String dummyVmName = getWorkerName(context, cmd, 0); - try { - vmMo = prepareVolumeHostDummyVm(hyperHost, dsMo, dummyVmName); - if (vmMo == null) { - throw new Exception("Unable to create a dummy VM for volume creation"); - } - - synchronized (this) { - // s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath); - VmwareHelper.deleteVolumeVmdkFiles(dsMo, volumeUuid.toString(), dcMo); - - vmMo.createDisk(volumeDatastorePath, (int) (cmd.getDiskCharacteristics().getSize() / (1024L * 1024L)), morDatastore, vmMo.getScsiDeviceControllerKey()); - vmMo.detachDisk(volumeDatastorePath, false); - } - - VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), cmd.getDiskCharacteristics().getName(), pool.getPath(), volumeUuid, cmd - .getDiskCharacteristics().getSize(), null); - return new CreateAnswer(cmd, vol); - } finally { - s_logger.info("Destroy dummy VM after volume creation"); - vmMo.detachAllDisks(); - vmMo.destroy(); - } - } - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - String msg = "CreateCommand failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - return new CreateAnswer(cmd, new Exception(e)); - } - } - - protected VirtualMachineMO prepareVolumeHostDummyVm(VmwareHypervisorHost hyperHost, DatastoreMO dsMo, String vmName) throws Exception { - assert (hyperHost != null); - - VirtualMachineMO vmMo = null; - VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec(); - vmConfig.setName(vmName); - vmConfig.setMemoryMB((long) 4); // vmware request minimum of 4 MB - vmConfig.setNumCPUs(1); - vmConfig.setGuestId(VirtualMachineGuestOsIdentifier._otherGuest.toString()); - VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo(); - fileInfo.setVmPathName(String.format("[%s]", dsMo.getName())); - vmConfig.setFiles(fileInfo); - - // Scsi controller - VirtualLsiLogicController scsiController = new VirtualLsiLogicController(); - scsiController.setSharedBus(VirtualSCSISharing.noSharing); - scsiController.setBusNumber(0); - scsiController.setKey(1); - VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec(); - scsiControllerSpec.setDevice(scsiController); - scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.add); - - vmConfig.setDeviceChange(new VirtualDeviceConfigSpec[] { scsiControllerSpec }); - hyperHost.createVm(vmConfig); - vmMo = hyperHost.findVmOnHyperHost(vmName); - return vmMo; - } - - @Override - public void disconnected() { - } - - @Override - public IAgentControl getAgentControl() { - return null; - } - - @Override - public PingCommand getCurrentStatus(long id) { - HashMap newStates = sync(); - if (newStates == null) { - return null; - } - - try { - // take the chance to do left-over dummy VM cleanup from previous run + dcMo.getMor(), dsMo.getMor(), + String.format("[%s] %s-delta.vmdk", dsMo.getName(), name), dcMo.getMor(), true); + + s_logger.info("detach disks from volume-wrapper VM " + name); + vmMo.detachAllDisks(); + + s_logger.info("destroy volume-wrapper VM " + name); + vmMo.destroy(); + + String srcFile = String.format("[%s] %s/", dsMo.getName(), name); + dsMo.deleteFile(srcFile, dcMo.getMor(), true); + + VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), name, pool.getPath(), name, cmd.getDiskCharacteristics().getSize(), null); + return new CreateAnswer(cmd, vol); + } + } else { + // create data volume + VirtualMachineMO vmMo = null; + String volumeUuid = UUID.randomUUID().toString().replace("-", ""); + String volumeDatastorePath = String.format("[%s] %s.vmdk", dsMo.getName(), volumeUuid); + String dummyVmName = getWorkerName(context, cmd, 0); + try { + vmMo = prepareVolumeHostDummyVm(hyperHost, dsMo, dummyVmName); + if (vmMo == null) { + throw new Exception("Unable to create a dummy VM for volume creation"); + } + + synchronized (this) { + // s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath); + VmwareHelper.deleteVolumeVmdkFiles(dsMo, volumeUuid.toString(), dcMo); + + vmMo.createDisk(volumeDatastorePath, (int) (cmd.getDiskCharacteristics().getSize() / (1024L * 1024L)), morDatastore, vmMo.getScsiDeviceControllerKey()); + vmMo.detachDisk(volumeDatastorePath, false); + } + + VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), cmd.getDiskCharacteristics().getName(), pool.getPath(), volumeUuid, cmd + .getDiskCharacteristics().getSize(), null); + return new CreateAnswer(cmd, vol); + } finally { + s_logger.info("Destroy dummy VM after volume creation"); + vmMo.detachAllDisks(); + vmMo.destroy(); + } + } + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + String msg = "CreateCommand failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + return new CreateAnswer(cmd, new Exception(e)); + } + } + + protected VirtualMachineMO prepareVolumeHostDummyVm(VmwareHypervisorHost hyperHost, DatastoreMO dsMo, String vmName) throws Exception { + assert (hyperHost != null); + + VirtualMachineMO vmMo = null; + VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec(); + vmConfig.setName(vmName); + vmConfig.setMemoryMB((long) 4); // vmware request minimum of 4 MB + vmConfig.setNumCPUs(1); + vmConfig.setGuestId(VirtualMachineGuestOsIdentifier._otherGuest.toString()); + VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo(); + fileInfo.setVmPathName(String.format("[%s]", dsMo.getName())); + vmConfig.setFiles(fileInfo); + + // Scsi controller + VirtualLsiLogicController scsiController = new VirtualLsiLogicController(); + scsiController.setSharedBus(VirtualSCSISharing.noSharing); + scsiController.setBusNumber(0); + scsiController.setKey(1); + VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec(); + scsiControllerSpec.setDevice(scsiController); + scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.add); + + vmConfig.setDeviceChange(new VirtualDeviceConfigSpec[] { scsiControllerSpec }); + hyperHost.createVm(vmConfig); + vmMo = hyperHost.findVmOnHyperHost(vmName); + return vmMo; + } + + @Override + public void disconnected() { + } + + @Override + public IAgentControl getAgentControl() { + return null; + } + + @Override + public PingCommand getCurrentStatus(long id) { + HashMap newStates = sync(); + if (newStates == null) { + return null; + } + + try { + // take the chance to do left-over dummy VM cleanup from previous run VmwareContext context = getServiceContext(); VmwareHypervisorHost hyperHost = getHyperHost(context); VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - + if(hyperHost.isHyperHostConnected()) { mgr.gcLeftOverVMs(context); - + if(_recycleHungWorker) { s_logger.info("Scan hung worker VM to recycle"); - + // GC worker that has been running for too long ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost( new String[] {"name", "config.template", "runtime.powerState", "runtime.bootTime"}); @@ -3849,7 +3889,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa boolean template = false; VirtualMachinePowerState powerState = VirtualMachinePowerState.poweredOff; GregorianCalendar bootTime = null; - + for(DynamicProperty prop : props) { if(prop.getName().equals("name")) name = prop.getVal().toString(); @@ -3860,19 +3900,19 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa else if(prop.getName().equals("runtime.bootTime")) bootTime = (GregorianCalendar)prop.getVal(); } - + if(!template && name.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) { boolean recycle = false; - + // recycle stopped worker VM and VM that has been running for too long (hard-coded 10 hours for now) if(powerState == VirtualMachinePowerState.poweredOff) recycle = true; else if(bootTime != null && (new Date().getTime() - bootTime.getTimeInMillis() > 10*3600*1000)) recycle = true; - + if(recycle) { s_logger.info("Recycle pending worker VM: " + name); - + VirtualMachineMO vmMo = new VirtualMachineMO(hyperHost.getContext(), oc.getObj()); vmMo.powerOff(); vmMo.destroy(); @@ -3884,411 +3924,411 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } } else { s_logger.error("Host is no longer connected."); - return null; + return null; } - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - return null; - } - } - - return new PingRoutingCommand(getType(), id, newStates); - } - - @Override - public Type getType() { - return com.cloud.host.Host.Type.Routing; - } - - @Override + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + return null; + } + } + + return new PingRoutingCommand(getType(), id, newStates); + } + + @Override + public Type getType() { + return com.cloud.host.Host.Type.Routing; + } + + @Override public StartupCommand[] initialize() { String hostApiVersion = "4.1"; - VmwareContext context = getServiceContext(); + VmwareContext context = getServiceContext(); try { VmwareHypervisorHost hyperHost = getHyperHost(context); assert(hyperHost instanceof HostMO); if(!((HostMO)hyperHost).isHyperHostConnected()) { - s_logger.info("Host " + hyperHost.getHyperHostName() + " is not in connected state"); - return null; + s_logger.info("Host " + hyperHost.getHyperHostName() + " is not in connected state"); + return null; } - + AboutInfo aboutInfo = ((HostMO)hyperHost).getHostAboutInfo(); hostApiVersion = aboutInfo.getApiVersion(); - + } catch (Exception e) { String msg = "VmwareResource intialize() failed due to : " + VmwareHelper.getExceptionMessage(e); s_logger.error(msg); invalidateServiceContext(); return null; } - - StartupRoutingCommand cmd = new StartupRoutingCommand(); - fillHostInfo(cmd); - - Map changes = null; - synchronized (_vms) { - _vms.clear(); - changes = sync(); - } - - cmd.setHypervisorType(HypervisorType.VMware); - cmd.setStateChanges(changes); + + StartupRoutingCommand cmd = new StartupRoutingCommand(); + fillHostInfo(cmd); + + Map changes = null; + synchronized (_vms) { + _vms.clear(); + changes = sync(); + } + + cmd.setHypervisorType(HypervisorType.VMware); + cmd.setStateChanges(changes); cmd.setCluster(_cluster); - cmd.setVersion(hostApiVersion); - - List storageCmds = initializeLocalStorage(); - StartupCommand[] answerCmds = new StartupCommand[1 + storageCmds.size()]; - answerCmds[0] = cmd; - for (int i = 0; i < storageCmds.size(); i++) { - answerCmds[i + 1] = storageCmds.get(i); - } - - return answerCmds; - } - - private List initializeLocalStorage() { - List storageCmds = new ArrayList(); - VmwareContext context = getServiceContext(); - - try { - VmwareHypervisorHost hyperHost = getHyperHost(context); - if (hyperHost instanceof HostMO) { - HostMO hostMo = (HostMO) hyperHost; - - List> dsList = hostMo.getLocalDatastoreOnHost(); - for (Pair dsPair : dsList) { - DatastoreMO dsMo = new DatastoreMO(context, dsPair.first()); - - String poolUuid = dsMo.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID); - if (poolUuid == null || poolUuid.isEmpty()) { - poolUuid = UUID.randomUUID().toString(); - dsMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, poolUuid); - } - - DatastoreSummary dsSummary = dsMo.getSummary(); - String address = hostMo.getHostName(); - StoragePoolInfo pInfo = new StoragePoolInfo(poolUuid, address, dsMo.getMor().get_value(), "", StoragePoolType.LVM, dsSummary.getCapacity(), dsSummary.getFreeSpace()); - StartupStorageCommand cmd = new StartupStorageCommand(); - cmd.setName(poolUuid); - cmd.setPoolInfo(pInfo); - cmd.setGuid(poolUuid); // give storage host the same UUID as the local storage pool itself - cmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL); - cmd.setDataCenter(_dcId); - cmd.setPod(_pod); - cmd.setCluster(_cluster); - - s_logger.info("Add local storage startup command: " + _gson.toJson(cmd)); - storageCmds.add(cmd); - } - - } else { - s_logger.info("Cluster host does not support local storage, skip it"); - } - } catch (Exception e) { - String msg = "initializing local storage failed due to : " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg); - invalidateServiceContext(); - throw new CloudRuntimeException(msg); - } - - return storageCmds; - } - - protected void fillHostInfo(StartupRoutingCommand cmd) { - VmwareContext serviceContext = getServiceContext(); - Map details = cmd.getHostDetails(); - if (details == null) { - details = new HashMap(); - } - - try { - fillHostHardwareInfo(serviceContext, cmd); - fillHostNetworkInfo(serviceContext, cmd); - fillHostDetailsInfo(serviceContext, details); - } catch (RuntimeFault e) { - s_logger.error("RuntimeFault while retrieving host info: " + e.toString(), e); - throw new CloudRuntimeException("RuntimeFault while retrieving host info"); - } catch (RemoteException e) { - s_logger.error("RemoteException while retrieving host info: " + e.toString(), e); - invalidateServiceContext(); - throw new CloudRuntimeException("RemoteException while retrieving host info"); - } catch (Exception e) { - s_logger.error("Exception while retrieving host info: " + e.toString(), e); - invalidateServiceContext(); - throw new CloudRuntimeException("Exception while retrieving host info: " + e.toString()); - } - - cmd.setHostDetails(details); - cmd.setName(_url); - cmd.setGuid(_guid); - cmd.setDataCenter(_dcId); - cmd.setPod(_pod); - cmd.setCluster(_cluster); - cmd.setVersion(VmwareResource.class.getPackage().getImplementationVersion()); - } - - private void fillHostHardwareInfo(VmwareContext serviceContext, StartupRoutingCommand cmd) throws RuntimeFault, RemoteException, Exception { - - VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - VmwareHypervisorHostResourceSummary summary = hyperHost.getHyperHostResourceSummary(); - - if (s_logger.isInfoEnabled()) { - s_logger.info("Startup report on host hardware info. " + _gson.toJson(summary)); - } - - cmd.setCaps("hvm"); - cmd.setDom0MinMemory(0); - cmd.setSpeed(summary.getCpuSpeed()); - cmd.setCpus((int) summary.getCpuCount()); - cmd.setMemory(summary.getMemoryBytes()); - } - - private void fillHostNetworkInfo(VmwareContext serviceContext, StartupRoutingCommand cmd) throws RuntimeFault, RemoteException { - - try { + cmd.setVersion(hostApiVersion); + + List storageCmds = initializeLocalStorage(); + StartupCommand[] answerCmds = new StartupCommand[1 + storageCmds.size()]; + answerCmds[0] = cmd; + for (int i = 0; i < storageCmds.size(); i++) { + answerCmds[i + 1] = storageCmds.get(i); + } + + return answerCmds; + } + + private List initializeLocalStorage() { + List storageCmds = new ArrayList(); + VmwareContext context = getServiceContext(); + + try { + VmwareHypervisorHost hyperHost = getHyperHost(context); + if (hyperHost instanceof HostMO) { + HostMO hostMo = (HostMO) hyperHost; + + List> dsList = hostMo.getLocalDatastoreOnHost(); + for (Pair dsPair : dsList) { + DatastoreMO dsMo = new DatastoreMO(context, dsPair.first()); + + String poolUuid = dsMo.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID); + if (poolUuid == null || poolUuid.isEmpty()) { + poolUuid = UUID.randomUUID().toString(); + dsMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, poolUuid); + } + + DatastoreSummary dsSummary = dsMo.getSummary(); + String address = hostMo.getHostName(); + StoragePoolInfo pInfo = new StoragePoolInfo(poolUuid, address, dsMo.getMor().get_value(), "", StoragePoolType.LVM, dsSummary.getCapacity(), dsSummary.getFreeSpace()); + StartupStorageCommand cmd = new StartupStorageCommand(); + cmd.setName(poolUuid); + cmd.setPoolInfo(pInfo); + cmd.setGuid(poolUuid); // give storage host the same UUID as the local storage pool itself + cmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL); + cmd.setDataCenter(_dcId); + cmd.setPod(_pod); + cmd.setCluster(_cluster); + + s_logger.info("Add local storage startup command: " + _gson.toJson(cmd)); + storageCmds.add(cmd); + } + + } else { + s_logger.info("Cluster host does not support local storage, skip it"); + } + } catch (Exception e) { + String msg = "initializing local storage failed due to : " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg); + invalidateServiceContext(); + throw new CloudRuntimeException(msg); + } + + return storageCmds; + } + + protected void fillHostInfo(StartupRoutingCommand cmd) { + VmwareContext serviceContext = getServiceContext(); + Map details = cmd.getHostDetails(); + if (details == null) { + details = new HashMap(); + } + + try { + fillHostHardwareInfo(serviceContext, cmd); + fillHostNetworkInfo(serviceContext, cmd); + fillHostDetailsInfo(serviceContext, details); + } catch (RuntimeFault e) { + s_logger.error("RuntimeFault while retrieving host info: " + e.toString(), e); + throw new CloudRuntimeException("RuntimeFault while retrieving host info"); + } catch (RemoteException e) { + s_logger.error("RemoteException while retrieving host info: " + e.toString(), e); + invalidateServiceContext(); + throw new CloudRuntimeException("RemoteException while retrieving host info"); + } catch (Exception e) { + s_logger.error("Exception while retrieving host info: " + e.toString(), e); + invalidateServiceContext(); + throw new CloudRuntimeException("Exception while retrieving host info: " + e.toString()); + } + + cmd.setHostDetails(details); + cmd.setName(_url); + cmd.setGuid(_guid); + cmd.setDataCenter(_dcId); + cmd.setPod(_pod); + cmd.setCluster(_cluster); + cmd.setVersion(VmwareResource.class.getPackage().getImplementationVersion()); + } + + private void fillHostHardwareInfo(VmwareContext serviceContext, StartupRoutingCommand cmd) throws RuntimeFault, RemoteException, Exception { + + VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); + VmwareHypervisorHostResourceSummary summary = hyperHost.getHyperHostResourceSummary(); + + if (s_logger.isInfoEnabled()) { + s_logger.info("Startup report on host hardware info. " + _gson.toJson(summary)); + } + + cmd.setCaps("hvm"); + cmd.setDom0MinMemory(0); + cmd.setSpeed(summary.getCpuSpeed()); + cmd.setCpus((int) summary.getCpuCount()); + cmd.setMemory(summary.getMemoryBytes()); + } + + private void fillHostNetworkInfo(VmwareContext serviceContext, StartupRoutingCommand cmd) throws RuntimeFault, RemoteException { + + try { VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - + assert(hyperHost instanceof HostMO); VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - - VmwareHypervisorHostNetworkSummary summary = hyperHost.getHyperHostNetworkSummary(mgr.getManagementPortGroupByHost((HostMO)hyperHost)); - if (summary == null) { - throw new Exception("No ESX(i) host found"); - } - - if (s_logger.isInfoEnabled()) { - s_logger.info("Startup report on host network info. " + _gson.toJson(summary)); - } - - cmd.setPrivateIpAddress(summary.getHostIp()); - cmd.setPrivateNetmask(summary.getHostNetmask()); - cmd.setPrivateMacAddress(summary.getHostMacAddress()); - - cmd.setStorageIpAddress(summary.getHostIp()); - cmd.setStorageNetmask(summary.getHostNetmask()); - cmd.setStorageMacAddress(summary.getHostMacAddress()); - - } catch (Throwable e) { - String msg = "querying host network info failed due to " + VmwareHelper.getExceptionMessage(e); - s_logger.error(msg, e); - throw new CloudRuntimeException(msg); - } - } - - private void fillHostDetailsInfo(VmwareContext serviceContext, Map details) throws Exception { - VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - - ClusterDasConfigInfo dasConfig = hyperHost.getDasConfig(); + + VmwareHypervisorHostNetworkSummary summary = hyperHost.getHyperHostNetworkSummary(mgr.getManagementPortGroupByHost((HostMO)hyperHost)); + if (summary == null) { + throw new Exception("No ESX(i) host found"); + } + + if (s_logger.isInfoEnabled()) { + s_logger.info("Startup report on host network info. " + _gson.toJson(summary)); + } + + cmd.setPrivateIpAddress(summary.getHostIp()); + cmd.setPrivateNetmask(summary.getHostNetmask()); + cmd.setPrivateMacAddress(summary.getHostMacAddress()); + + cmd.setStorageIpAddress(summary.getHostIp()); + cmd.setStorageNetmask(summary.getHostNetmask()); + cmd.setStorageMacAddress(summary.getHostMacAddress()); + + } catch (Throwable e) { + String msg = "querying host network info failed due to " + VmwareHelper.getExceptionMessage(e); + s_logger.error(msg, e); + throw new CloudRuntimeException(msg); + } + } + + private void fillHostDetailsInfo(VmwareContext serviceContext, Map details) throws Exception { + VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); + + ClusterDasConfigInfo dasConfig = hyperHost.getDasConfig(); if (dasConfig != null && dasConfig.getEnabled() != null && dasConfig.getEnabled().booleanValue()) { details.put("NativeHA", "true"); - } - } + } + } - protected HashMap sync() { - HashMap changes = new HashMap(); - HashMap oldStates = null; - - try { - synchronized (_vms) { - HashMap newStates = getVmStates(); - oldStates = new HashMap(_vms.size()); - oldStates.putAll(_vms); - - for (final Map.Entry entry : newStates.entrySet()) { - final String vm = entry.getKey(); - - State newState = entry.getValue(); - final State oldState = oldStates.remove(vm); - - if (s_logger.isTraceEnabled()) { - s_logger.trace("VM " + vm + ": vSphere has state " + newState + " and we have state " + (oldState != null ? oldState.toString() : "null")); - } - - if (vm.startsWith("migrating")) { - s_logger.debug("Migrating detected. Skipping"); - continue; - } - - if (oldState == null) { - _vms.put(vm, newState); - s_logger.debug("Detecting a new state but couldn't find a old state so adding it to the changes: " + vm); - changes.put(vm, newState); - } else if (oldState == State.Starting) { - if (newState == State.Running) { - _vms.put(vm, newState); - } else if (newState == State.Stopped) { - s_logger.debug("Ignoring vm " + vm + " because of a lag in starting the vm."); - } - } else if (oldState == State.Migrating) { - if (newState == State.Running) { - s_logger.debug("Detected that an migrating VM is now running: " + vm); - _vms.put(vm, newState); - } - } else if (oldState == State.Stopping) { - if (newState == State.Stopped) { - _vms.put(vm, newState); - } else if (newState == State.Running) { - s_logger.debug("Ignoring vm " + vm + " because of a lag in stopping the vm. "); - } - } else if (oldState != newState) { - _vms.put(vm, newState); - if (newState == State.Stopped) { - /* - * if (_vmsKilled.remove(vm)) { s_logger.debug("VM " + vm + " has been killed for storage. "); - * newState = State.Error; } - */ - } - changes.put(vm, newState); - } - } - - for (final Map.Entry entry : oldStates.entrySet()) { - final String vm = entry.getKey(); - final State oldState = entry.getValue(); - - if (isVmInCluster(vm)) { + protected HashMap sync() { + HashMap changes = new HashMap(); + HashMap oldStates = null; + + try { + synchronized (_vms) { + HashMap newStates = getVmStates(); + oldStates = new HashMap(_vms.size()); + oldStates.putAll(_vms); + + for (final Map.Entry entry : newStates.entrySet()) { + final String vm = entry.getKey(); + + State newState = entry.getValue(); + final State oldState = oldStates.remove(vm); + + if (s_logger.isTraceEnabled()) { + s_logger.trace("VM " + vm + ": vSphere has state " + newState + " and we have state " + (oldState != null ? oldState.toString() : "null")); + } + + if (vm.startsWith("migrating")) { + s_logger.debug("Migrating detected. Skipping"); + continue; + } + + if (oldState == null) { + _vms.put(vm, newState); + s_logger.debug("Detecting a new state but couldn't find a old state so adding it to the changes: " + vm); + changes.put(vm, newState); + } else if (oldState == State.Starting) { + if (newState == State.Running) { + _vms.put(vm, newState); + } else if (newState == State.Stopped) { + s_logger.debug("Ignoring vm " + vm + " because of a lag in starting the vm."); + } + } else if (oldState == State.Migrating) { + if (newState == State.Running) { + s_logger.debug("Detected that an migrating VM is now running: " + vm); + _vms.put(vm, newState); + } + } else if (oldState == State.Stopping) { + if (newState == State.Stopped) { + _vms.put(vm, newState); + } else if (newState == State.Running) { + s_logger.debug("Ignoring vm " + vm + " because of a lag in stopping the vm. "); + } + } else if (oldState != newState) { + _vms.put(vm, newState); + if (newState == State.Stopped) { + /* + * if (_vmsKilled.remove(vm)) { s_logger.debug("VM " + vm + " has been killed for storage. "); + * newState = State.Error; } + */ + } + changes.put(vm, newState); + } + } + + for (final Map.Entry entry : oldStates.entrySet()) { + final String vm = entry.getKey(); + final State oldState = entry.getValue(); + + if (isVmInCluster(vm)) { if (s_logger.isDebugEnabled()) { s_logger.debug("VM " + vm + " is now missing from host report but we detected that it might be migrated to other host by vCenter"); } - + if(oldState != State.Starting && oldState != State.Migrating) { s_logger.debug("VM " + vm + " is now missing from host report and VM is not at starting/migrating state, remove it from host VM-sync map, oldState: " + oldState); - _vms.remove(vm); + _vms.remove(vm); } else { - s_logger.debug("VM " + vm + " is missing from host report, but we will ignore VM " + vm + " in transition state " + oldState); + s_logger.debug("VM " + vm + " is missing from host report, but we will ignore VM " + vm + " in transition state " + oldState); } - continue; - } - - if (s_logger.isDebugEnabled()) { - s_logger.debug("VM " + vm + " is now missing from host report"); - } - - if (oldState == State.Stopping) { - s_logger.debug("Ignoring VM " + vm + " in transition state stopping."); - _vms.remove(vm); - } else if (oldState == State.Starting) { - s_logger.debug("Ignoring VM " + vm + " in transition state starting."); - } else if (oldState == State.Stopped) { - _vms.remove(vm); - } else if (oldState == State.Migrating) { - s_logger.debug("Ignoring VM " + vm + " in migrating state."); - } else { - State state = State.Stopped; - changes.put(entry.getKey(), state); - } - } - } - } catch (Throwable e) { - if (e instanceof RemoteException) { - s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); - invalidateServiceContext(); - } - - s_logger.error("Unable to perform sync information collection process at this point due to " + VmwareHelper.getExceptionMessage(e), e); - return null; - } - return changes; - } - - private boolean isVmInCluster(String vmName) throws Exception { - VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - - return hyperHost.findVmOnPeerHyperHost(vmName) != null; - } - - protected OptionValue[] configureVnc(OptionValue[] optionsToMerge, VmwareHypervisorHost hyperHost, String vmName, - String vncPassword, String keyboardLayout) throws Exception { - - VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName); - - VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - if(!mgr.beginExclusiveOperation(600)) - throw new Exception("Unable to begin exclusive operation, lock time out"); - - try { - int maxVncPorts = 64; - int vncPort = 0; - Random random = new Random(); - - HostMO vmOwnerHost = vmMo.getRunningHost(); - - ManagedObjectReference morParent = vmOwnerHost.getParentMor(); - HashMap portInfo; - if(morParent.getType().equalsIgnoreCase("ClusterComputeResource")) { - ClusterMO clusterMo = new ClusterMO(vmOwnerHost.getContext(), morParent); - portInfo = clusterMo.getVmVncPortsOnCluster(); - } else { - portInfo = vmOwnerHost.getVmVncPortsOnHost(); - } - - // allocate first at 5900 - 5964 range - Collection existingPorts = portInfo.values(); - int val = random.nextInt(maxVncPorts); - int startVal = val; - do { - if (!existingPorts.contains(5900 + val)) { - vncPort = 5900 + val; - break; - } - - val = (++val) % maxVncPorts; - } while (val != startVal); - - if(vncPort == 0) { - s_logger.info("we've run out of range for ports between 5900-5964 for the cluster, we will try port range at 59000-60000"); + continue; + } - Pair additionalRange = mgr.getAddiionalVncPortRange(); - maxVncPorts = additionalRange.second(); - val = random.nextInt(maxVncPorts); - startVal = val; - do { - if (!existingPorts.contains(additionalRange.first() + val)) { - vncPort = additionalRange.first() + val; - break; - } - - val = (++val) % maxVncPorts; - } while (val != startVal); - } - - if (vncPort == 0) { - throw new Exception("Unable to find an available VNC port on host"); - } - - if (s_logger.isInfoEnabled()) { - s_logger.info("Configure VNC port for VM " + vmName + ", port: " + vncPort + ", host: " + vmOwnerHost.getHyperHostName()); - } - - return VmwareHelper.composeVncOptions(optionsToMerge, true, vncPassword, vncPort, keyboardLayout); - } finally { - try { - mgr.endExclusiveOperation(); - } catch(Throwable e) { - assert(false); - s_logger.error("Unexpected exception ", e); - } - } - } - - private VirtualMachineGuestOsIdentifier translateGuestOsIdentifier(String cpuArchitecture, String cloudGuestOs) { - if (cpuArchitecture == null) { - s_logger.warn("CPU arch is not set, default to i386. guest os: " + cloudGuestOs); - cpuArchitecture = "i386"; - } - - VirtualMachineGuestOsIdentifier identifier = VmwareGuestOsMapper.getGuestOsIdentifier(cloudGuestOs); - if (identifier != null) { - return identifier; - } - - if (cpuArchitecture.equalsIgnoreCase("x86_64")) { - return VirtualMachineGuestOsIdentifier.otherGuest64; - } - return VirtualMachineGuestOsIdentifier.otherGuest; - } + if (s_logger.isDebugEnabled()) { + s_logger.debug("VM " + vm + " is now missing from host report"); + } + + if (oldState == State.Stopping) { + s_logger.debug("Ignoring VM " + vm + " in transition state stopping."); + _vms.remove(vm); + } else if (oldState == State.Starting) { + s_logger.debug("Ignoring VM " + vm + " in transition state starting."); + } else if (oldState == State.Stopped) { + _vms.remove(vm); + } else if (oldState == State.Migrating) { + s_logger.debug("Ignoring VM " + vm + " in migrating state."); + } else { + State state = State.Stopped; + changes.put(entry.getKey(), state); + } + } + } + } catch (Throwable e) { + if (e instanceof RemoteException) { + s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); + invalidateServiceContext(); + } + + s_logger.error("Unable to perform sync information collection process at this point due to " + VmwareHelper.getExceptionMessage(e), e); + return null; + } + return changes; + } + + private boolean isVmInCluster(String vmName) throws Exception { + VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); + + return hyperHost.findVmOnPeerHyperHost(vmName) != null; + } + + protected OptionValue[] configureVnc(OptionValue[] optionsToMerge, VmwareHypervisorHost hyperHost, String vmName, + String vncPassword, String keyboardLayout) throws Exception { + + VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName); + + VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + if(!mgr.beginExclusiveOperation(600)) + throw new Exception("Unable to begin exclusive operation, lock time out"); + + try { + int maxVncPorts = 64; + int vncPort = 0; + Random random = new Random(); + + HostMO vmOwnerHost = vmMo.getRunningHost(); + + ManagedObjectReference morParent = vmOwnerHost.getParentMor(); + HashMap portInfo; + if(morParent.getType().equalsIgnoreCase("ClusterComputeResource")) { + ClusterMO clusterMo = new ClusterMO(vmOwnerHost.getContext(), morParent); + portInfo = clusterMo.getVmVncPortsOnCluster(); + } else { + portInfo = vmOwnerHost.getVmVncPortsOnHost(); + } + + // allocate first at 5900 - 5964 range + Collection existingPorts = portInfo.values(); + int val = random.nextInt(maxVncPorts); + int startVal = val; + do { + if (!existingPorts.contains(5900 + val)) { + vncPort = 5900 + val; + break; + } + + val = (++val) % maxVncPorts; + } while (val != startVal); + + if(vncPort == 0) { + s_logger.info("we've run out of range for ports between 5900-5964 for the cluster, we will try port range at 59000-60000"); + + Pair additionalRange = mgr.getAddiionalVncPortRange(); + maxVncPorts = additionalRange.second(); + val = random.nextInt(maxVncPorts); + startVal = val; + do { + if (!existingPorts.contains(additionalRange.first() + val)) { + vncPort = additionalRange.first() + val; + break; + } + + val = (++val) % maxVncPorts; + } while (val != startVal); + } + + if (vncPort == 0) { + throw new Exception("Unable to find an available VNC port on host"); + } + + if (s_logger.isInfoEnabled()) { + s_logger.info("Configure VNC port for VM " + vmName + ", port: " + vncPort + ", host: " + vmOwnerHost.getHyperHostName()); + } + + return VmwareHelper.composeVncOptions(optionsToMerge, true, vncPassword, vncPort, keyboardLayout); + } finally { + try { + mgr.endExclusiveOperation(); + } catch(Throwable e) { + assert(false); + s_logger.error("Unexpected exception ", e); + } + } + } + + private VirtualMachineGuestOsIdentifier translateGuestOsIdentifier(String cpuArchitecture, String cloudGuestOs) { + if (cpuArchitecture == null) { + s_logger.warn("CPU arch is not set, default to i386. guest os: " + cloudGuestOs); + cpuArchitecture = "i386"; + } + + VirtualMachineGuestOsIdentifier identifier = VmwareGuestOsMapper.getGuestOsIdentifier(cloudGuestOs); + if (identifier != null) { + return identifier; + } + + if (cpuArchitecture.equalsIgnoreCase("x86_64")) { + return VirtualMachineGuestOsIdentifier.otherGuest64; + } + return VirtualMachineGuestOsIdentifier.otherGuest; + } private void prepareNetworkForVmTargetHost(HostMO hostMo, VirtualMachineMO vmMo) throws Exception { assert (vmMo != null); @@ -4314,7 +4354,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa vlanId = tokens[2]; HypervisorHostHelper.prepareNetwork(this._privateNetworkVSwitchName, "cloud.private", - hostMo, vlanId, networkRateMbps, null, this._ops_timeout, false); + hostMo, vlanId, networkRateMbps, null, this._ops_timeout, false); } else { s_logger.info("Skip suspecious cloud network " + networkName); } @@ -4330,7 +4370,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa vlanId = tokens[2]; HypervisorHostHelper.prepareNetwork(this._publicNetworkVSwitchName, "cloud.public", - hostMo, vlanId, networkRateMbps, null, this._ops_timeout, false); + hostMo, vlanId, networkRateMbps, null, this._ops_timeout, false); } else { s_logger.info("Skip suspecious cloud network " + networkName); } @@ -4347,7 +4387,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa vlanId = tokens[2]; HypervisorHostHelper.prepareNetwork(this._guestNetworkVSwitchName, "cloud.guest", - hostMo, vlanId, networkRateMbps, null, this._ops_timeout, false); + hostMo, vlanId, networkRateMbps, null, this._ops_timeout, false); } else { s_logger.info("Skip suspecious cloud network " + networkName); } @@ -4356,42 +4396,42 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } } } - - private HashMap getVmStates() throws Exception { - VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", "runtime.powerState", "config.template" }); - - HashMap newStates = new HashMap(); - if (ocs != null && ocs.length > 0) { - for (ObjectContent oc : ocs) { - DynamicProperty[] objProps = oc.getPropSet(); - if (objProps != null) { - - boolean isTemplate = false; - String name = null; - VirtualMachinePowerState powerState = VirtualMachinePowerState.poweredOff; - for (DynamicProperty objProp : objProps) { - if (objProp.getName().equals("config.template")) { - if (objProp.getVal().toString().equalsIgnoreCase("true")) { - isTemplate = true; - } - } else if (objProp.getName().equals("name")) { - name = (String) objProp.getVal(); - } else if (objProp.getName().equals("runtime.powerState")) { - powerState = (VirtualMachinePowerState) objProp.getVal(); - } else { - assert (false); - } - } - - if (!isTemplate) { - newStates.put(name, convertState(powerState)); - } - } - } - } - return newStates; - } + + private HashMap getVmStates() throws Exception { + VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); + ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", "runtime.powerState", "config.template" }); + + HashMap newStates = new HashMap(); + if (ocs != null && ocs.length > 0) { + for (ObjectContent oc : ocs) { + DynamicProperty[] objProps = oc.getPropSet(); + if (objProps != null) { + + boolean isTemplate = false; + String name = null; + VirtualMachinePowerState powerState = VirtualMachinePowerState.poweredOff; + for (DynamicProperty objProp : objProps) { + if (objProp.getName().equals("config.template")) { + if (objProp.getVal().toString().equalsIgnoreCase("true")) { + isTemplate = true; + } + } else if (objProp.getName().equals("name")) { + name = (String) objProp.getVal(); + } else if (objProp.getName().equals("runtime.powerState")) { + powerState = (VirtualMachinePowerState) objProp.getVal(); + } else { + assert (false); + } + } + + if (!isTemplate) { + newStates.put(name, convertState(powerState)); + } + } + } + } + return newStates; + } private HashMap getVmStats(List vmNames) throws Exception { VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); @@ -4424,11 +4464,11 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa for (DynamicProperty objProp : objProps) { if (objProp.getName().equals("name")) { - name = objProp.getVal().toString(); + name = objProp.getVal().toString(); } else if (objProp.getName().equals("summary.config.numCpu")) { - numberCPUs = objProp.getVal().toString(); + numberCPUs = objProp.getVal().toString(); } else if (objProp.getName().equals("summary.quickStats.overallCpuUsage")) { - maxCpuUsage = objProp.getVal().toString(); + maxCpuUsage = objProp.getVal().toString(); } } @@ -4443,12 +4483,12 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa // get all the metrics from the available sample period PerfMetricId[] perfMetrics = service.queryAvailablePerfMetric(perfMgr, vmMor, null, null, null); if(perfMetrics != null) { - for(int index=0; index < perfMetrics.length; ++index) { - if ( ((rxPerfCounterInfo != null) && (perfMetrics[index].getCounterId() == rxPerfCounterInfo.getKey())) || - ((txPerfCounterInfo != null) && (perfMetrics[index].getCounterId() == txPerfCounterInfo.getKey())) ) { - vmNetworkMetrics.add(perfMetrics[index]); - } - } + for(int index=0; index < perfMetrics.length; ++index) { + if ( ((rxPerfCounterInfo != null) && (perfMetrics[index].getCounterId() == rxPerfCounterInfo.getKey())) || + ((txPerfCounterInfo != null) && (perfMetrics[index].getCounterId() == txPerfCounterInfo.getKey())) ) { + vmNetworkMetrics.add(perfMetrics[index]); + } + } } double networkReadKBs=0; @@ -4477,9 +4517,9 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa if (vals[vi].getId().getCounterId() == txPerfCounterInfo.getKey()) { networkWriteKBs = sampleDuration * perfValues[3];//get the average TX rate multiplied by sampled duration } - } + } } - } + } } vmResponseMap.put(name, new VmStatsEntry(Integer.parseInt(maxCpuUsage), networkReadKBs, networkWriteKBs, Integer.parseInt(numberCPUs), "vm")); } @@ -4487,197 +4527,197 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } return vmResponseMap; } - - protected String networkUsage(final String privateIpAddress, final String option, final String ethName) { - String args = null; - if (option.equals("get")) { - args = "-g"; - } else if (option.equals("create")) { - args = "-c"; - } else if (option.equals("reset")) { - args = "-r"; - } else if (option.equals("addVif")) { - args = "-a"; - args += ethName; - } else if (option.equals("deleteVif")) { - args = "-d"; - args += ethName; - } - - try { + + protected String networkUsage(final String privateIpAddress, final String option, final String ethName) { + String args = null; + if (option.equals("get")) { + args = "-g"; + } else if (option.equals("create")) { + args = "-c"; + } else if (option.equals("reset")) { + args = "-r"; + } else if (option.equals("addVif")) { + args = "-a"; + args += ethName; + } else if (option.equals("deleteVif")) { + args = "-d"; + args += ethName; + } + + try { if (s_logger.isTraceEnabled()) { s_logger.trace("Executing /opt/cloud/bin/netusage.sh " + args + " on DomR " + privateIpAddress); - } - - VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - - Pair result = SshHelper.sshExecute(privateIpAddress, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/netusage.sh " + args); - - if (!result.first()) { - return null; - } - - return result.second(); - } catch (Throwable e) { + } + + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + + Pair result = SshHelper.sshExecute(privateIpAddress, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/netusage.sh " + args); + + if (!result.first()) { + return null; + } + + return result.second(); + } catch (Throwable e) { s_logger.error("Unable to execute NetworkUsage command on DomR (" + privateIpAddress + "), domR may not be ready yet. failure due to " - + VmwareHelper.getExceptionMessage(e), e); - } - - return null; - } - - private long[] getNetworkStats(String privateIP) { - String result = networkUsage(privateIP, "get", null); - long[] stats = new long[2]; - if (result != null) { - try { - String[] splitResult = result.split(":"); - int i = 0; - while (i < splitResult.length - 1) { - stats[0] += (new Long(splitResult[i++])).longValue(); - stats[1] += (new Long(splitResult[i++])).longValue(); - } - } catch (Throwable e) { - s_logger.warn("Unable to parse return from script return of network usage command: " + e.toString(), e); - } - } - return stats; - } - - protected String connect(final String vmName, final String ipAddress, final int port) { - long startTick = System.currentTimeMillis(); - - // wait until we have at least been waiting for _ops_timeout time or - // at least have tried _retry times, this is to coordinate with system - // VM patching/rebooting time that may need - int retry = _retry; - while (System.currentTimeMillis() - startTick <= _ops_timeout || --retry > 0) { - SocketChannel sch = null; - try { - s_logger.info("Trying to connect to " + ipAddress); - sch = SocketChannel.open(); - sch.configureBlocking(true); - sch.socket().setSoTimeout(5000); - - InetSocketAddress addr = new InetSocketAddress(ipAddress, port); - sch.connect(addr); - return null; - } catch (IOException e) { - s_logger.info("Could not connect to " + ipAddress + " due to " + e.toString()); - if (e instanceof ConnectException) { - // if connection is refused because of VM is being started, - // we give it more sleep time - // to avoid running out of retry quota too quickly - try { - Thread.sleep(5000); - } catch (InterruptedException ex) { - } - } - } finally { - if (sch != null) { - try { - sch.close(); - } catch (IOException e) { - } - } - } - - try { - Thread.sleep(1000); - } catch (InterruptedException ex) { - } - } - - s_logger.info("Unable to logon to " + ipAddress); - - return "Unable to connect"; - } - - protected String connect(final String vmname, final String ipAddress) { - return connect(vmname, ipAddress, 3922); - } - - private static State convertState(VirtualMachinePowerState powerState) { - return s_statesTable.get(powerState); - } - - private static State getVmState(VirtualMachineMO vmMo) throws Exception { - VirtualMachineRuntimeInfo runtimeInfo = vmMo.getRuntimeInfo(); - return convertState(runtimeInfo.getPowerState()); - } - - private static HostStatsEntry getHyperHostStats(VmwareHypervisorHost hyperHost) throws Exception { + + VmwareHelper.getExceptionMessage(e), e); + } + + return null; + } + + private long[] getNetworkStats(String privateIP) { + String result = networkUsage(privateIP, "get", null); + long[] stats = new long[2]; + if (result != null) { + try { + String[] splitResult = result.split(":"); + int i = 0; + while (i < splitResult.length - 1) { + stats[0] += (new Long(splitResult[i++])).longValue(); + stats[1] += (new Long(splitResult[i++])).longValue(); + } + } catch (Throwable e) { + s_logger.warn("Unable to parse return from script return of network usage command: " + e.toString(), e); + } + } + return stats; + } + + protected String connect(final String vmName, final String ipAddress, final int port) { + long startTick = System.currentTimeMillis(); + + // wait until we have at least been waiting for _ops_timeout time or + // at least have tried _retry times, this is to coordinate with system + // VM patching/rebooting time that may need + int retry = _retry; + while (System.currentTimeMillis() - startTick <= _ops_timeout || --retry > 0) { + SocketChannel sch = null; + try { + s_logger.info("Trying to connect to " + ipAddress); + sch = SocketChannel.open(); + sch.configureBlocking(true); + sch.socket().setSoTimeout(5000); + + InetSocketAddress addr = new InetSocketAddress(ipAddress, port); + sch.connect(addr); + return null; + } catch (IOException e) { + s_logger.info("Could not connect to " + ipAddress + " due to " + e.toString()); + if (e instanceof ConnectException) { + // if connection is refused because of VM is being started, + // we give it more sleep time + // to avoid running out of retry quota too quickly + try { + Thread.sleep(5000); + } catch (InterruptedException ex) { + } + } + } finally { + if (sch != null) { + try { + sch.close(); + } catch (IOException e) { + } + } + } + + try { + Thread.sleep(1000); + } catch (InterruptedException ex) { + } + } + + s_logger.info("Unable to logon to " + ipAddress); + + return "Unable to connect"; + } + + protected String connect(final String vmname, final String ipAddress) { + return connect(vmname, ipAddress, 3922); + } + + private static State convertState(VirtualMachinePowerState powerState) { + return s_statesTable.get(powerState); + } + + private static State getVmState(VirtualMachineMO vmMo) throws Exception { + VirtualMachineRuntimeInfo runtimeInfo = vmMo.getRuntimeInfo(); + return convertState(runtimeInfo.getPowerState()); + } + + private static HostStatsEntry getHyperHostStats(VmwareHypervisorHost hyperHost) throws Exception { ComputeResourceSummary hardwareSummary = hyperHost.getHyperHostHardwareSummary(); if(hardwareSummary == null) - return null; - - HostStatsEntry entry = new HostStatsEntry(); - - entry.setEntityType("host"); - double cpuUtilization = ((double) (hardwareSummary.getTotalCpu() - hardwareSummary.getEffectiveCpu()) / (double) hardwareSummary.getTotalCpu() * 100); - entry.setCpuUtilization(cpuUtilization); - entry.setTotalMemoryKBs(hardwareSummary.getTotalMemory() / 1024); - entry.setFreeMemoryKBs(hardwareSummary.getEffectiveMemory() * 1024); - - return entry; - } - - private static String getRouterSshControlIp(NetworkElementCommand cmd) { - String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); - String routerGuestIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP); - String zoneNetworkType = cmd.getAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE); - - if(routerGuestIp != null && zoneNetworkType != null && NetworkType.valueOf(zoneNetworkType) == NetworkType.Basic) { - if(s_logger.isDebugEnabled()) - s_logger.debug("In Basic zone mode, use router's guest IP for SSH control. guest IP : " + routerGuestIp); - - return routerGuestIp; - } - - if(s_logger.isDebugEnabled()) - s_logger.debug("Use router's private IP for SSH control. IP : " + routerIp); - return routerIp; - } - - @Override - public void setAgentControl(IAgentControl agentControl) { - } - - @Override - public boolean configure(String name, Map params) throws ConfigurationException { - _name = name; - - _url = (String) params.get("url"); - _username = (String) params.get("username"); - _password = (String) params.get("password"); - _dcId = (String) params.get("zone"); - _pod = (String) params.get("pod"); + return null; + + HostStatsEntry entry = new HostStatsEntry(); + + entry.setEntityType("host"); + double cpuUtilization = ((double) (hardwareSummary.getTotalCpu() - hardwareSummary.getEffectiveCpu()) / (double) hardwareSummary.getTotalCpu() * 100); + entry.setCpuUtilization(cpuUtilization); + entry.setTotalMemoryKBs(hardwareSummary.getTotalMemory() / 1024); + entry.setFreeMemoryKBs(hardwareSummary.getEffectiveMemory() * 1024); + + return entry; + } + + private static String getRouterSshControlIp(NetworkElementCommand cmd) { + String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); + String routerGuestIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP); + String zoneNetworkType = cmd.getAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE); + + if(routerGuestIp != null && zoneNetworkType != null && NetworkType.valueOf(zoneNetworkType) == NetworkType.Basic) { + if(s_logger.isDebugEnabled()) + s_logger.debug("In Basic zone mode, use router's guest IP for SSH control. guest IP : " + routerGuestIp); + + return routerGuestIp; + } + + if(s_logger.isDebugEnabled()) + s_logger.debug("Use router's private IP for SSH control. IP : " + routerIp); + return routerIp; + } + + @Override + public void setAgentControl(IAgentControl agentControl) { + } + + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + _name = name; + + _url = (String) params.get("url"); + _username = (String) params.get("username"); + _password = (String) params.get("password"); + _dcId = (String) params.get("zone"); + _pod = (String) params.get("pod"); _cluster = (String) params.get("cluster"); - + _guid = (String) params.get("guid"); - String[] tokens = _guid.split("@"); - _vCenterAddress = tokens[1]; - _morHyperHost = new ManagedObjectReference(); - String[] hostTokens = tokens[0].split(":"); - _morHyperHost.setType(hostTokens[0]); + String[] tokens = _guid.split("@"); + _vCenterAddress = tokens[1]; + _morHyperHost = new ManagedObjectReference(); + String[] hostTokens = tokens[0].split(":"); + _morHyperHost.setType(hostTokens[0]); _morHyperHost.set_value(hostTokens[1]); - - VmwareContext context = getServiceContext(); - try { - VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); - mgr.setupResourceStartupParams(params); - - CustomFieldsManagerMO cfmMo = new CustomFieldsManagerMO(context, context.getServiceContent().getCustomFieldsManager()); + + VmwareContext context = getServiceContext(); + try { + VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + mgr.setupResourceStartupParams(params); + + CustomFieldsManagerMO cfmMo = new CustomFieldsManagerMO(context, context.getServiceContent().getCustomFieldsManager()); cfmMo.ensureCustomFieldDef("Datastore", CustomFieldConstants.CLOUD_UUID); if (mgr.getNexusVSwitchGlobalParameter()) { - cfmMo.ensureCustomFieldDef("DistributedVirtualPortgroup", CustomFieldConstants.CLOUD_GC_DVP); - } else { - cfmMo.ensureCustomFieldDef("Network", CustomFieldConstants.CLOUD_GC); - } + cfmMo.ensureCustomFieldDef("DistributedVirtualPortgroup", CustomFieldConstants.CLOUD_GC_DVP); + } else { + cfmMo.ensureCustomFieldDef("Network", CustomFieldConstants.CLOUD_GC); + } cfmMo.ensureCustomFieldDef("VirtualMachine", CustomFieldConstants.CLOUD_UUID); cfmMo.ensureCustomFieldDef("VirtualMachine", CustomFieldConstants.CLOUD_NIC_MASK); - - VmwareHypervisorHost hostMo = this.getHyperHost(context); + + VmwareHypervisorHost hostMo = this.getHyperHost(context); _hostName = hostMo.getHyperHostName(); Map vsmCredentials; @@ -4691,21 +4731,21 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa _publicNetworkVSwitchName = mgr.getPublicVSwitchName(Long.parseLong(_dcId), HypervisorType.VMware); _guestNetworkVSwitchName = mgr.getGuestVSwitchName(Long.parseLong(_dcId), HypervisorType.VMware); } - - } catch (Exception e) { - s_logger.error("Unexpected Exception ", e); - } - + + } catch (Exception e) { + s_logger.error("Unexpected Exception ", e); + } + if(_privateNetworkVSwitchName == null) { _privateNetworkVSwitchName = (String) params.get("private.network.vswitch.name"); } - if(_publicNetworkVSwitchName == null) { + if(_publicNetworkVSwitchName == null) { _publicNetworkVSwitchName = (String) params.get("public.network.vswitch.name"); - } + } if(_guestNetworkVSwitchName == null) { _guestNetworkVSwitchName = (String) params.get("guest.network.vswitch.name"); } - + String value = (String) params.get("cpu.overprovisioning.factor"); if(value != null) _cpuOverprovisioningFactor = Float.parseFloat(value); @@ -4713,7 +4753,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa value = (String) params.get("vmware.reserve.cpu"); if(value != null && value.equalsIgnoreCase("true")) _reserveCpu = true; - + value = (String) params.get("vmware.recycle.hung.wokervm"); if(value != null && value.equalsIgnoreCase("true")) _recycleHungWorker = true; @@ -4725,105 +4765,105 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa value = (String) params.get("vmware.reserve.mem"); if(value != null && value.equalsIgnoreCase("true")) _reserveMem = true; - + value = (String)params.get("vmware.root.disk.controller"); if(value != null && value.equalsIgnoreCase("scsi")) - _rootDiskController = DiskControllerType.scsi; + _rootDiskController = DiskControllerType.scsi; else - _rootDiskController = DiskControllerType.ide; + _rootDiskController = DiskControllerType.ide; value = params.get("vmware.use.nexus.vswitch").toString(); if(value != null && value.equalsIgnoreCase("true")) - _nexusVSwitch = true; + _nexusVSwitch = true; - s_logger.info("VmwareResource network configuration info. private vSwitch: " + _privateNetworkVSwitchName + ", public vSwitch: " + _publicNetworkVSwitchName + ", guest network: " - + _guestNetworkVSwitchName); - - return true; - } - - @Override - public String getName() { - return _name; - } - - @Override - public boolean start() { - return true; - } - - @Override - public boolean stop() { - return true; - } - - private VmwareContext getServiceContext() { - return getServiceContext(null); - } - - private void invalidateServiceContext() { - invalidateServiceContext(null); - } - - private VmwareHypervisorHost getHyperHost(VmwareContext context) { - return getHyperHost(context, null); - } - - @Override - public synchronized VmwareContext getServiceContext(Command cmd) { - if (_serviceContext == null) { - try { + s_logger.info("VmwareResource network configuration info. private vSwitch: " + _privateNetworkVSwitchName + ", public vSwitch: " + _publicNetworkVSwitchName + ", guest network: " + + _guestNetworkVSwitchName); + + return true; + } + + @Override + public String getName() { + return _name; + } + + @Override + public boolean start() { + return true; + } + + @Override + public boolean stop() { + return true; + } + + private VmwareContext getServiceContext() { + return getServiceContext(null); + } + + private void invalidateServiceContext() { + invalidateServiceContext(null); + } + + private VmwareHypervisorHost getHyperHost(VmwareContext context) { + return getHyperHost(context, null); + } + + @Override + public synchronized VmwareContext getServiceContext(Command cmd) { + if (_serviceContext == null) { + try { _serviceContext = VmwareContextFactory.create(_vCenterAddress, _username, _password); VmwareHypervisorHost hyperHost = getHyperHost(_serviceContext, cmd); assert(hyperHost instanceof HostMO); - + HostFirewallSystemMO firewallMo = ((HostMO)hyperHost).getHostFirewallSystemMO(); boolean bRefresh = false; if(firewallMo != null) { - HostFirewallInfo firewallInfo = firewallMo.getFirewallInfo(); - if(firewallInfo != null) { - for(HostFirewallRuleset rule : firewallInfo.getRuleset()) { - if("vncServer".equalsIgnoreCase(rule.getKey())) { - bRefresh = true; - firewallMo.enableRuleset("vncServer"); - } else if("gdbserver".equalsIgnoreCase(rule.getKey())) { - bRefresh = true; - firewallMo.enableRuleset("gdbserver"); - } - } - } - - if(bRefresh) - firewallMo.refreshFirewall(); + HostFirewallInfo firewallInfo = firewallMo.getFirewallInfo(); + if(firewallInfo != null) { + for(HostFirewallRuleset rule : firewallInfo.getRuleset()) { + if("vncServer".equalsIgnoreCase(rule.getKey())) { + bRefresh = true; + firewallMo.enableRuleset("vncServer"); + } else if("gdbserver".equalsIgnoreCase(rule.getKey())) { + bRefresh = true; + firewallMo.enableRuleset("gdbserver"); + } + } + } + + if(bRefresh) + firewallMo.refreshFirewall(); } - } catch (Exception e) { - s_logger.error("Unable to connect to vSphere server: " + _vCenterAddress, e); - throw new CloudRuntimeException("Unable to connect to vSphere server: " + _vCenterAddress); - } - } - return _serviceContext; - } - - @Override - public synchronized void invalidateServiceContext(VmwareContext context) { - if (_serviceContext != null) { - _serviceContext.close(); - } - _serviceContext = null; - } - - @Override - public VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd) { - if (_morHyperHost.getType().equalsIgnoreCase("HostSystem")) { - return new HostMO(context, _morHyperHost); - } - return new ClusterMO(context, _morHyperHost); - } - + } catch (Exception e) { + s_logger.error("Unable to connect to vSphere server: " + _vCenterAddress, e); + throw new CloudRuntimeException("Unable to connect to vSphere server: " + _vCenterAddress); + } + } + return _serviceContext; + } + @Override - @DB - public String getWorkerName(VmwareContext context, Command cmd, int workerSequence) { - VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + public synchronized void invalidateServiceContext(VmwareContext context) { + if (_serviceContext != null) { + _serviceContext.close(); + } + _serviceContext = null; + } + + @Override + public VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd) { + if (_morHyperHost.getType().equalsIgnoreCase("HostSystem")) { + return new HostMO(context, _morHyperHost); + } + return new ClusterMO(context, _morHyperHost); + } + + @Override + @DB + public String getWorkerName(VmwareContext context, Command cmd, int workerSequence) { + VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME); String vmName = mgr.composeWorkerName(); assert(cmd != null); @@ -4831,5 +4871,5 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa long checkPointId = vmwareMgr.pushCleanupCheckpoint(this._guid, vmName); cmd.setContextParam("checkpoint", String.valueOf(checkPointId)); return vmName; - } -} + } +}