You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ya...@apache.org on 2014/01/25 04:58:13 UTC
git commit: updated refs/heads/master to 3c955c3
Updated Branches:
refs/heads/master cb8cf1eb1 -> 3c955c365
CLOUDSTACK-5779: Make VmwareResource use VirtualRoutingResource
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/3c955c36
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/3c955c36
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/3c955c36
Branch: refs/heads/master
Commit: 3c955c3659c3e29491833a9574c96a35a5591aa8
Parents: cb8cf1e
Author: Sheng Yang <sh...@citrix.com>
Authored: Mon Jan 20 12:03:07 2014 -0800
Committer: Sheng Yang <sh...@citrix.com>
Committed: Fri Jan 24 19:57:52 2014 -0800
----------------------------------------------------------------------
.../kvm/resource/LibvirtComputingResource.java | 1 +
.../vmware/resource/VmwareResource.java | 1260 +++---------------
.../xen/resource/CitrixResourceBase.java | 1 +
3 files changed, 203 insertions(+), 1059 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3c955c36/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
index fa0d99f..ab37446 100755
--- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
@@ -351,6 +351,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
public ExecutionResult prepareCommand(NetworkElementCommand cmd) {
//Update IP used to access router
cmd.setRouterAccessIp(cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP));
+ assert cmd.getRouterAccessIp() != null;
if (cmd instanceof IpAssocVpcCommand) {
return prepareNetworkElementCommand((IpAssocVpcCommand)cmd);
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3c955c36/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java
index e5158cb..4073275 100755
--- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java
+++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java
@@ -44,7 +44,6 @@ import java.util.concurrent.TimeUnit;
import javax.naming.ConfigurationException;
-import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
@@ -115,17 +114,12 @@ import com.cloud.agent.api.AttachVolumeAnswer;
import com.cloud.agent.api.AttachVolumeCommand;
import com.cloud.agent.api.BackupSnapshotAnswer;
import com.cloud.agent.api.BackupSnapshotCommand;
-import com.cloud.agent.api.BumpUpPriorityCommand;
import com.cloud.agent.api.CheckHealthAnswer;
import com.cloud.agent.api.CheckHealthCommand;
import com.cloud.agent.api.CheckNetworkAnswer;
import com.cloud.agent.api.CheckNetworkCommand;
import com.cloud.agent.api.CheckOnHostAnswer;
import com.cloud.agent.api.CheckOnHostCommand;
-import com.cloud.agent.api.CheckRouterAnswer;
-import com.cloud.agent.api.CheckRouterCommand;
-import com.cloud.agent.api.CheckS2SVpnConnectionsAnswer;
-import com.cloud.agent.api.CheckS2SVpnConnectionsCommand;
import com.cloud.agent.api.CheckVirtualMachineAnswer;
import com.cloud.agent.api.CheckVirtualMachineCommand;
import com.cloud.agent.api.Command;
@@ -139,8 +133,6 @@ import com.cloud.agent.api.CreateVolumeFromSnapshotCommand;
import com.cloud.agent.api.DeleteStoragePoolCommand;
import com.cloud.agent.api.DeleteVMSnapshotAnswer;
import com.cloud.agent.api.DeleteVMSnapshotCommand;
-import com.cloud.agent.api.GetDomRVersionAnswer;
-import com.cloud.agent.api.GetDomRVersionCmd;
import com.cloud.agent.api.GetHostStatsAnswer;
import com.cloud.agent.api.GetHostStatsCommand;
import com.cloud.agent.api.GetStorageStatsAnswer;
@@ -186,7 +178,6 @@ import com.cloud.agent.api.ScaleVmAnswer;
import com.cloud.agent.api.ScaleVmCommand;
import com.cloud.agent.api.SetupAnswer;
import com.cloud.agent.api.SetupCommand;
-import com.cloud.agent.api.SetupGuestNetworkAnswer;
import com.cloud.agent.api.SetupGuestNetworkCommand;
import com.cloud.agent.api.StartAnswer;
import com.cloud.agent.api.StartCommand;
@@ -206,35 +197,11 @@ import com.cloud.agent.api.ValidateSnapshotCommand;
import com.cloud.agent.api.VmStatsEntry;
import com.cloud.agent.api.check.CheckSshAnswer;
import com.cloud.agent.api.check.CheckSshCommand;
-import com.cloud.agent.api.routing.CreateIpAliasCommand;
-import com.cloud.agent.api.routing.DeleteIpAliasCommand;
-import com.cloud.agent.api.routing.DhcpEntryCommand;
-import com.cloud.agent.api.routing.DnsMasqConfigCommand;
-import com.cloud.agent.api.routing.IpAliasTO;
-import com.cloud.agent.api.routing.IpAssocAnswer;
import com.cloud.agent.api.routing.IpAssocCommand;
import com.cloud.agent.api.routing.IpAssocVpcCommand;
-import com.cloud.agent.api.routing.LoadBalancerConfigCommand;
import com.cloud.agent.api.routing.NetworkElementCommand;
-import com.cloud.agent.api.routing.RemoteAccessVpnCfgCommand;
-import com.cloud.agent.api.routing.SavePasswordCommand;
-import com.cloud.agent.api.routing.SetFirewallRulesAnswer;
-import com.cloud.agent.api.routing.SetFirewallRulesCommand;
-import com.cloud.agent.api.routing.SetMonitorServiceCommand;
-import com.cloud.agent.api.routing.SetNetworkACLAnswer;
import com.cloud.agent.api.routing.SetNetworkACLCommand;
-import com.cloud.agent.api.routing.SetPortForwardingRulesAnswer;
-import com.cloud.agent.api.routing.SetPortForwardingRulesCommand;
-import com.cloud.agent.api.routing.SetPortForwardingRulesVpcCommand;
-import com.cloud.agent.api.routing.SetSourceNatAnswer;
import com.cloud.agent.api.routing.SetSourceNatCommand;
-import com.cloud.agent.api.routing.SetStaticNatRulesAnswer;
-import com.cloud.agent.api.routing.SetStaticNatRulesCommand;
-import com.cloud.agent.api.routing.SetStaticRouteAnswer;
-import com.cloud.agent.api.routing.SetStaticRouteCommand;
-import com.cloud.agent.api.routing.Site2SiteVpnCfgCommand;
-import com.cloud.agent.api.routing.VmDataCommand;
-import com.cloud.agent.api.routing.VpnUsersCfgCommand;
import com.cloud.agent.api.storage.CopyVolumeAnswer;
import com.cloud.agent.api.storage.CopyVolumeCommand;
import com.cloud.agent.api.storage.CreateAnswer;
@@ -248,17 +215,15 @@ import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;
import com.cloud.agent.api.storage.ResizeVolumeAnswer;
import com.cloud.agent.api.storage.ResizeVolumeCommand;
import com.cloud.agent.api.to.DataStoreTO;
-import com.cloud.agent.api.to.DhcpTO;
import com.cloud.agent.api.to.DiskTO;
-import com.cloud.agent.api.to.FirewallRuleTO;
import com.cloud.agent.api.to.IpAddressTO;
import com.cloud.agent.api.to.NfsTO;
import com.cloud.agent.api.to.NicTO;
-import com.cloud.agent.api.to.PortForwardingRuleTO;
-import com.cloud.agent.api.to.StaticNatRuleTO;
import com.cloud.agent.api.to.StorageFilerTO;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.agent.api.to.VolumeTO;
+import com.cloud.agent.resource.virtualnetwork.VirtualRouterDeployer;
+import com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource;
import com.cloud.configuration.Config;
import com.cloud.dc.DataCenter.NetworkType;
import com.cloud.dc.Vlan;
@@ -293,13 +258,10 @@ import com.cloud.hypervisor.vmware.mo.VmwareHypervisorHostResourceSummary;
import com.cloud.hypervisor.vmware.util.VmwareContext;
import com.cloud.hypervisor.vmware.util.VmwareGuestOsMapper;
import com.cloud.hypervisor.vmware.util.VmwareHelper;
-import com.cloud.network.HAProxyConfigurator;
-import com.cloud.network.LoadBalancerConfigurator;
import com.cloud.network.Networks;
import com.cloud.network.Networks.BroadcastDomainType;
import com.cloud.network.Networks.TrafficType;
import com.cloud.network.VmwareTrafficLabel;
-import com.cloud.network.rules.FirewallRule;
import com.cloud.resource.ServerResource;
import com.cloud.serializer.GsonHelper;
import com.cloud.storage.Storage;
@@ -331,7 +293,7 @@ import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachineName;
import com.cloud.vm.VmDetailConstants;
-public class VmwareResource implements StoragePoolResource, ServerResource, VmwareHostService {
+public class VmwareResource implements StoragePoolResource, ServerResource, VmwareHostService, VirtualRouterDeployer {
private static final Logger s_logger = Logger.getLogger(VmwareResource.class);
protected String _name;
@@ -380,6 +342,8 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
protected StorageSubsystemCommandHandler storageHandler;
+ protected VirtualRoutingResource _vrResource;
+
protected static HashMap<VirtualMachinePowerState, PowerState> s_powerStatesTable;
static {
s_powerStatesTable = new HashMap<VirtualMachinePowerState, PowerState>();
@@ -429,26 +393,8 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
Class<? extends Command> 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 == CreateIpAliasCommand.class) {
- return execute((CreateIpAliasCommand)cmd);
- } else if (clz == DnsMasqConfigCommand.class) {
- return execute((DnsMasqConfigCommand)cmd);
- } else if (clz == DeleteIpAliasCommand.class) {
- return execute((DeleteIpAliasCommand)cmd);
- } else if (clz == VmDataCommand.class) {
- answer = execute((VmDataCommand)cmd);
+ } else if (cmd instanceof NetworkElementCommand) {
+ return _vrResource.executeRequest(cmd);
} else if (clz == ReadyCommand.class) {
answer = execute((ReadyCommand)cmd);
} else if (clz == GetHostStatsCommand.class) {
@@ -525,62 +471,32 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
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);
- } else if (clz == BumpUpPriorityCommand.class) {
- answer = execute((BumpUpPriorityCommand)cmd);
- } else if (clz == GetDomRVersionCmd.class) {
- answer = execute((GetDomRVersionCmd)cmd);
} else if (clz == CheckNetworkCommand.class) {
answer = execute((CheckNetworkCommand)cmd);
- } else if (clz == SetupGuestNetworkCommand.class) {
- answer = execute((SetupGuestNetworkCommand)cmd);
- } else if (clz == IpAssocVpcCommand.class) {
- answer = execute((IpAssocVpcCommand)cmd);
} else if (clz == PlugNicCommand.class) {
answer = execute((PlugNicCommand)cmd);
} else if (clz == UnPlugNicCommand.class) {
answer = execute((UnPlugNicCommand)cmd);
- } else if (clz == SetSourceNatCommand.class) {
- answer = execute((SetSourceNatCommand)cmd);
- } else if (clz == SetNetworkACLCommand.class) {
- answer = execute((SetNetworkACLCommand)cmd);
} else if (cmd instanceof CreateVMSnapshotCommand) {
return execute((CreateVMSnapshotCommand)cmd);
} else if (cmd instanceof DeleteVMSnapshotCommand) {
return execute((DeleteVMSnapshotCommand)cmd);
} else if (cmd instanceof RevertToVMSnapshotCommand) {
return execute((RevertToVMSnapshotCommand)cmd);
- } else if (clz == SetPortForwardingRulesVpcCommand.class) {
- answer = execute((SetPortForwardingRulesVpcCommand)cmd);
- } else if (clz == Site2SiteVpnCfgCommand.class) {
- answer = execute((Site2SiteVpnCfgCommand)cmd);
- } else if (clz == CheckS2SVpnConnectionsCommand.class) {
- answer = execute((CheckS2SVpnConnectionsCommand)cmd);
} else if (clz == ResizeVolumeCommand.class) {
return execute((ResizeVolumeCommand)cmd);
} else if (clz == UnregisterVMCommand.class) {
- return execute((UnregisterVMCommand)cmd);
+ return execute((UnregisterVMCommand) cmd);
} else if (cmd instanceof StorageSubSystemCommand) {
- return storageHandler.handleStorageCommands((StorageSubSystemCommand)cmd);
+ return storageHandler.handleStorageCommands((StorageSubSystemCommand) cmd);
} else if (clz == ScaleVmCommand.class) {
return execute((ScaleVmCommand)cmd);
} else if (clz == PvlanSetupCommand.class) {
return execute((PvlanSetupCommand)cmd);
- } else if (clz == SetStaticRouteCommand.class) {
- answer = execute((SetStaticRouteCommand)cmd);
} else if (clz == UnregisterNicCommand.class) {
answer = execute((UnregisterNicCommand)cmd);
- } else if (clz == SetMonitorServiceCommand.class) {
- answer = execute((SetMonitorServiceCommand)cmd);
} else {
answer = Answer.createUnsupportedCommandAnswer(cmd);
}
@@ -696,39 +612,6 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
return answer;
}
- private SetStaticRouteAnswer execute(SetStaticRouteCommand cmd) {
- if (s_logger.isInfoEnabled()) {
- s_logger.info("Executing resource SetStaticRouteCommand: " + _gson.toJson(cmd));
- }
-
- boolean endResult = true;
-
- String controlIp = getRouterSshControlIp(cmd);
- String args = "";
- String[] results = new String[cmd.getStaticRoutes().length];
- String[][] rules = cmd.generateSRouteRules();
- int i = 0;
-
- StringBuilder sb = new StringBuilder();
- String[] srRules = rules[0];
- for (int j = 0; j < srRules.length; j++) {
- sb.append(srRules[j]).append(',');
- }
- args += " -a " + sb.toString();
-
- ExecutionResult result = executeInVR(controlIp, "vpc_staticroute.sh", args);
-
- if (!result.isSuccess()) {
- s_logger.error("SetStaticRouteCommand failure on setting one rule. args: " + args);
- while (i < results.length) {
- results[i++] = "Failed";
- }
- endResult = false;
- }
- return new SetStaticRouteAnswer(cmd, endResult, results);
-
- }
-
protected NetworkUsageAnswer VPCNetworkUsage(NetworkUsageCommand cmd) {
String privateIp = cmd.getPrivateIP();
String option = cmd.getOption();
@@ -776,180 +659,8 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
return new NetworkUsageAnswer(cmd, "success", 0L, 0L);
}
- protected Answer execute(SetMonitorServiceCommand cmd) {
- if (s_logger.isInfoEnabled()) {
- s_logger.info("Executing resource SetMonitorServiceCommand: " + _gson.toJson(cmd));
- }
-
- String controlIp = getRouterSshControlIp(cmd);
- String config = cmd.getConfiguration();
-
- String args = "";
-
- args += " -c " + config;
-
- ExecutionResult result = executeInVR(controlIp, "monitor_service.sh", args);
- if (!result.isSuccess()) {
- return new Answer(cmd, false, result.getDetails());
- }
- return new Answer(cmd);
- }
-
- 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();
- args += " -d " + rule.getStringDstPortRange();
-
- ExecutionResult result = executeInVR(controlIp, "firewall_nat.sh", args);
- if (!result.isSuccess()) {
- s_logger.error("SetPortForwardingRulesCommand failure on setting one rule. args: " + args);
- results[i++] = "Failed";
- endResult = false;
- } else {
- results[i++] = null;
- }
- }
-
- return new SetPortForwardingRulesAnswer(cmd, results, endResult);
- }
-
- protected SetFirewallRulesAnswer execute(SetFirewallRulesCommand cmd) {
- String controlIp = getRouterSshControlIp(cmd);
- String[] results = new String[cmd.getRules().length];
- FirewallRuleTO[] allrules = cmd.getRules();
- FirewallRule.TrafficType trafficType = allrules[0].getTrafficType();
- String egressDefault = cmd.getAccessDetail(NetworkElementCommand.FIREWALL_EGRESS_DEFAULT);
-
- String[][] rules = cmd.generateFwRules();
- String args = "";
- args += " -F ";
- if (trafficType == FirewallRule.TrafficType.Egress) {
- args += " -E ";
- if (egressDefault.equals("true")) {
- args += " -P 1 ";
- } else if (egressDefault.equals("System")) {
- args += " -P 2 ";
- } else {
- args += " -P 0 ";
- }
- }
-
- 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();
- }
-
- ExecutionResult result = null;
-
- if (trafficType == FirewallRule.TrafficType.Egress) {
- result = executeInVR(controlIp, "firewall_egress.sh", args);
- } else {
- result = executeInVR(controlIp, "firewall_ingress.sh", args);
- }
-
- if (!result.isSuccess()) {
- 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);
- }
-
- return new SetFirewallRulesAnswer(cmd, true, results);
- }
-
- 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];
- String controlIp = getRouterSshControlIp(cmd);
-
- int i = 0;
- boolean endResult = true;
- for (StaticNatRuleTO rule : cmd.getRules()) {
- String args = "";
- args += rule.revoked() ? " -D" : " -A";
- args += " -l " + rule.getSrcIp();
- args += " -r " + rule.getDstIp();
-
- ExecutionResult result = executeInVR(controlIp, "vpc_staticnat.sh", args);
-
- if (!result.isSuccess()) {
- s_logger.error("SetVPCStaticNatRulesCommand failure on setting one rule. args: " + args);
- results[i++] = "Failed";
- endResult = false;
- } else {
- results[i++] = null;
- }
- }
- 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 ";
-
- String controlIp = getRouterSshControlIp(cmd);
- ExecutionResult result = executeInVR(controlIp, "firewall_nat.sh", args);
-
- if (!result.isSuccess()) {
- s_logger.error("SetStaticNatRulesCommand failure on setting one rule. args: " + args);
- results[i++] = "Failed";
- endResult = false;
- } else {
- results[i++] = null;
- }
- }
- return new SetStaticNatRulesAnswer(cmd, results, endResult);
- }
-
- protected boolean createFileInVR(String routerIp, String filePath, String fileName, String content) {
+ @Override
+ public ExecutionResult createFileInVR(String routerIp, String filePath, String fileName, String content) {
VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
File keyFile = mgr.getSystemVMKeyFile();
boolean result = true;
@@ -958,89 +669,34 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
SshHelper.scpTo(routerIp, 3922, "root", keyFile, null, filePath, content.getBytes(), fileName, null);
} catch (Exception e) {
s_logger.warn("Fail to create file " + filePath + fileName + " in VR " + routerIp, e);
- result = false;
+ return new ExecutionResult(false, e.getMessage());
}
- return result;
+ return new ExecutionResult(true, null);
}
- protected Answer execute(final LoadBalancerConfigCommand cmd) {
- VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
-
- String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
- String controlIp = getRouterSshControlIp(cmd);
-
- assert (controlIp != null);
-
- LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
- String[] config = cfgtr.generateConfiguration(cmd);
-
- String tmpCfgFileContents = "";
- String tmpCfgFileName = "haproxy.cfg.new";
- String tmpCfgFilePath = "/etc/haproxy/";
- for (int i = 0; i < config.length; i++) {
- tmpCfgFileContents += config[i];
- tmpCfgFileContents += "\n";
- }
-
- if (!createFileInVR(controlIp, tmpCfgFilePath, tmpCfgFileName, tmpCfgFileContents)) {
- return new Answer(cmd, false, "Fail to create LB config file in VR");
- }
-
- String[][] rules = cfgtr.generateFwRules(cmd);
-
- String[] addRules = rules[LoadBalancerConfigurator.ADD];
- String[] removeRules = rules[LoadBalancerConfigurator.REMOVE];
- String[] statRules = rules[LoadBalancerConfigurator.STATS];
-
- String args = "";
- 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) {
- for (int i = 0; i < statRules.length; i++) {
- sb.append(statRules[i]).append(',');
- }
-
- args += " -s " + sb.toString();
- }
-
- ExecutionResult result;
- if (cmd.getVpcId() == null) {
- args = " -i " + routerIp + args;
- result = executeInVR(controlIp, "loadbalancer.sh", args);
- } else {
- args = " -i " + cmd.getNic().getIp() + args;
- result = executeInVR(controlIp, "vpc_loadbalancer.sh", args);
- }
- // Invoke the command
-
- if (!result.isSuccess()) {
- String msg = "LoadBalancerConfigCommand on domain router " + routerIp + " failed. message: " + result.getDetails();
- s_logger.error(msg);
+ @Override
+ public ExecutionResult prepareCommand(NetworkElementCommand cmd) {
+ //Update IP used to access router
+ cmd.setRouterAccessIp(getRouterSshControlIp(cmd));
+ assert cmd.getRouterAccessIp() != null;
- return new Answer(cmd, false, msg);
+ if (cmd instanceof IpAssocVpcCommand) {
+ return prepareNetworkElementCommand((IpAssocVpcCommand)cmd);
+ } else if (cmd instanceof IpAssocCommand) {
+ return prepareNetworkElementCommand((IpAssocCommand)cmd);
+ } else if (cmd instanceof SetSourceNatCommand) {
+ return prepareNetworkElementCommand((SetSourceNatCommand)cmd);
+ } else if (cmd instanceof SetupGuestNetworkCommand) {
+ return prepareNetworkElementCommand((SetupGuestNetworkCommand)cmd);
+ } else if (cmd instanceof SetNetworkACLCommand) {
+ return prepareNetworkElementCommand((SetNetworkACLCommand)cmd);
}
+ return new ExecutionResult(true, null);
+ }
- if (s_logger.isInfoEnabled()) {
- s_logger.info("LoadBalancerConfigCommand on domain router " + routerIp + " completed");
- }
- return new Answer(cmd);
+ @Override
+ public ExecutionResult cleanupCommand(NetworkElementCommand cmd) {
+ return new ExecutionResult(true, null);
}
//
@@ -1108,75 +764,26 @@ 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);
-
+ protected ExecutionResult prepareNetworkElementCommand(SetupGuestNetworkCommand cmd) {
NicTO nic = cmd.getNic();
-
String routerIp = getRouterSshControlIp(cmd);
- String domrGIP = cmd.getAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP);
- String domrName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
- String gw = cmd.getAccessDetail(NetworkElementCommand.GUEST_NETWORK_GATEWAY);
- String cidr = Long.toString(NetUtils.getCidrSize(nic.getNetmask()));
- String domainName = cmd.getNetworkDomain();
- String dns = cmd.getDefaultDns1();
- if (dns == null || dns.isEmpty()) {
- dns = cmd.getDefaultDns2();
- } else {
- String dns2 = cmd.getDefaultDns2();
- if (dns2 != null && !dns2.isEmpty()) {
- dns += "," + dns2;
- }
- }
+ String domrName =
+ cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
try {
- int ethDeviceNum = findRouterEthDeviceIndex(domrName, routerIp, nic.getMac());
- s_logger.info("find interface index. routerIp: " + routerIp + ", mac: " + nic.getMac() + ", index: " + ethDeviceNum);
-
- String args = (cmd.isAdd() ? "-C" : "-D");
- String dev = "eth" + ethDeviceNum;
- args += " -d " + dev;
- args += " -i " + domrGIP;
- args += " -g " + gw;
- args += " -m " + cidr;
- args += " -n " + NetUtils.getSubNet(domrGIP, nic.getNetmask());
- if (dns != null && !dns.isEmpty()) {
- args += " -s " + dns;
- }
- if (domainName != null && !domainName.isEmpty()) {
- args += " -e " + domainName;
- }
-
- ExecutionResult result = executeInVR(routerIp, "vpc_guestnw.sh", args);
-
- if (!result.isSuccess()) {
- String msg = "SetupGuestNetworkCommand on domain router " + routerIp + " failed. message: " + result.getDetails();
- s_logger.error(msg);
- return new SetupGuestNetworkAnswer(cmd, false, msg);
- }
-
- if (s_logger.isInfoEnabled()) {
- s_logger.info("SetupGuestNetworkCommand on domain router " + routerIp + " completed");
- }
-
- return new SetupGuestNetworkAnswer(cmd, true, "success");
+ int ethDeviceNum = findRouterEthDeviceIndex(domrName, routerIp,
+ nic.getMac());
+ nic.setDeviceId(ethDeviceNum);
} catch (Exception e) {
- String msg = "SetupGuestNetwork failed due to " + e.toString();
+ String msg = "Prepare SetupGuestNetwork failed due to " + e.toString();
s_logger.warn(msg, e);
- return new SetupGuestNetworkAnswer(cmd, false, msg);
+ return new ExecutionResult(false, msg);
}
+ return new ExecutionResult(true, null);
}
- protected IpAssocAnswer execute(IpAssocVpcCommand cmd) {
- if (s_logger.isInfoEnabled()) {
- s_logger.info("Executing resource IpAssocVpcCommand " + _gson.toJson(cmd));
- }
- String[] results = new String[cmd.getIpAddresses().length];
- int i = 0;
+ private ExecutionResult prepareNetworkElementCommand(IpAssocVpcCommand cmd) {
String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
String routerIp = getRouterSshControlIp(cmd);
@@ -1184,217 +791,58 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
IpAddressTO[] ips = cmd.getIpAddresses();
for (IpAddressTO ip : ips) {
- assignVPCPublicIpAddress(routerName, routerIp, ip);
- results[i++] = ip.getPublicIp() + " - success";
+ int ethDeviceNum = findRouterEthDeviceIndex(routerName, routerIp, ip.getVifMacAddress());
+ if (ethDeviceNum < 0) {
+ if (ip.isAdd()) {
+ throw new InternalErrorException("Failed to find DomR VIF to associate/disassociate IP with.");
+ } else {
+ s_logger.debug("VIF to deassociate IP with does not exist, return success");
+ continue;
+ }
+ }
+
+ ip.setNicDevId(ethDeviceNum);
}
} catch (Exception e) {
- s_logger.error("Ip Assoc failure on applying one ip due to exception: ", e);
- results[i++] = IpAssocAnswer.errorResult;
+ s_logger.error("Prepare Ip Assoc failure on applying one ip due to exception: ", e);
+ return new ExecutionResult(false, e.toString());
}
- return new IpAssocAnswer(cmd, results);
+ return new ExecutionResult(true, null);
}
- protected SetSourceNatAnswer execute(SetSourceNatCommand cmd) {
- if (s_logger.isInfoEnabled()) {
- s_logger.info("Executing resource SetSourceNatCommand " + _gson.toJson(cmd));
- }
-
- VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
-
+ protected ExecutionResult prepareNetworkElementCommand(SetSourceNatCommand cmd) {
String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
String routerIp = getRouterSshControlIp(cmd);
IpAddressTO pubIp = cmd.getIpAddress();
try {
int ethDeviceNum = findRouterEthDeviceIndex(routerName, routerIp, pubIp.getVifMacAddress());
- String args = "";
- args += " -A ";
- args += " -l ";
- args += pubIp.getPublicIp();
-
- args += " -c ";
- args += "eth" + ethDeviceNum;
-
- ExecutionResult result = executeInVR(routerIp, "vpc_snat.sh", args);
-
- if (!result.isSuccess()) {
- String msg = "SetupGuestNetworkCommand on domain router " + routerIp + " failed. message: " + result.getDetails();
- s_logger.error(msg);
-
- return new SetSourceNatAnswer(cmd, false, msg);
- }
-
- return new SetSourceNatAnswer(cmd, true, "success");
+ pubIp.setNicDevId(ethDeviceNum);
} catch (Exception e) {
- String msg = "Ip SNAT failure due to " + e.toString();
+ String msg = "Prepare Ip SNAT failure due to " + e.toString();
s_logger.error(msg, e);
- return new SetSourceNatAnswer(cmd, false, msg);
+ return new ExecutionResult(false, e.toString());
}
+ return new ExecutionResult(true, null);
}
- private SetNetworkACLAnswer execute(SetNetworkACLCommand cmd) {
- if (s_logger.isInfoEnabled()) {
- s_logger.info("Executing resource SetNetworkACLCommand " + _gson.toJson(cmd));
- }
-
- VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
- String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
- String privateGw = cmd.getAccessDetail(NetworkElementCommand.VPC_PRIVATE_GATEWAY);
+ private ExecutionResult prepareNetworkElementCommand(SetNetworkACLCommand cmd) {
+ NicTO nic = cmd.getNic();
+ String routerName =
+ cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
String routerIp = getRouterSshControlIp(cmd);
- String[] results = new String[cmd.getRules().length];
try {
- String[][] rules = cmd.generateFwRules();
- StringBuilder sb = new StringBuilder();
- String[] aclRules = rules[0];
- 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 = "";
- ExecutionResult result;
-
- if (privateGw != null) {
- s_logger.debug("Private gateway configuration is set");
- args += " -d " + "eth" + ethDeviceNum;
- args += " -a " + sb.toString();
- result = executeInVR(routerIp, "vpc_privategw_acl.sh", args);
-
- if (!result.isSuccess()) {
- String msg = "SetNetworkACLAnswer on domain router " + routerIp + " failed. message: " + result.getDetails();
- s_logger.error(msg);
- return new SetNetworkACLAnswer(cmd, false, results);
- }
- } else {
- args = "";
- args += " -d " + "eth" + ethDeviceNum;
- args += " -i " + nic.getIp();
- args += " -m " + Long.toString(NetUtils.getCidrSize(nic.getNetmask()));
- args += " -a " + sb.toString();
-
- result = executeInVR(routerIp, "vpc_acl.sh", args);
-
- if (!result.isSuccess()) {
- String msg = "SetNetworkACLAnswer on domain router " + routerIp + " failed. message: " + result.getDetails();
- s_logger.error(msg);
-
- return new SetNetworkACLAnswer(cmd, false, results);
- }
- }
-
- return new SetNetworkACLAnswer(cmd, true, results);
+ int ethDeviceNum = findRouterEthDeviceIndex(routerName, routerIp,
+ nic.getMac());
+ nic.setDeviceId(ethDeviceNum);
} catch (Exception e) {
- String msg = "SetNetworkACL failed due to " + e.toString();
+ String msg = "Prepare SetNetworkACL failed due to " + e.toString();
s_logger.error(msg, e);
- return new SetNetworkACLAnswer(cmd, false, results);
- }
- }
-
- protected SetPortForwardingRulesAnswer execute(SetPortForwardingRulesVpcCommand cmd) {
- 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);
-
- String[] results = new String[cmd.getRules().length];
- int i = 0;
-
- boolean endResult = true;
- for (PortForwardingRuleTO rule : cmd.getRules()) {
- String args = "";
- 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().replace(":", "-");
-
- ExecutionResult sshResult = executeInVR(routerIp, "vpc_portforwarding.sh", args);
-
- if (!sshResult.isSuccess()) {
- results[i++] = "Failed";
- endResult = false;
- } else {
- results[i++] = null;
- }
- }
- 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);
-
- String args = "";
- if (cmd.isCreate()) {
- args += " -A";
- args += " -l ";
- args += cmd.getLocalPublicIp();
- args += " -n ";
- args += cmd.getLocalGuestCidr();
- args += " -g ";
- args += cmd.getLocalPublicGateway();
- args += " -r ";
- args += cmd.getPeerGatewayIp();
- args += " -N ";
- args += cmd.getPeerGuestCidrList();
- args += " -e ";
- args += "\"" + cmd.getEspPolicy() + "\"";
- args += " -i ";
- args += "\"" + cmd.getIkePolicy() + "\"";
- args += " -t ";
- args += Long.toString(cmd.getIkeLifetime());
- args += " -T ";
- args += Long.toString(cmd.getEspLifetime());
- args += " -s ";
- args += "\"" + cmd.getIpsecPsk() + "\"";
- args += " -d ";
- if (cmd.getDpd()) {
- args += "1";
- } else {
- args += "0";
- }
- if (cmd.isPassive()) {
- args += " -p ";
- }
- } else {
- args += " -D";
- args += " -r ";
- args += cmd.getPeerGatewayIp();
- args += " -n ";
- args += cmd.getLocalGuestCidr();
- args += " -N ";
- args += cmd.getPeerGuestCidrList();
- }
-
- ExecutionResult result = executeInVR(routerIp, "ipsectunnel.sh", args);
-
- if (!result.isSuccess()) {
- s_logger.error("Setup site2site VPN " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + " failed, message: " + result.getDetails());
-
- return new Answer(cmd, false, "Setup site2site VPN falied due to " + result.getDetails());
+ return new ExecutionResult(false, msg);
}
-
- if (s_logger.isDebugEnabled()) {
- s_logger.debug("setup site 2 site vpn on router " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + " completed");
- }
-
- return new Answer(cmd, true, result.getDetails());
+ return new ExecutionResult(true, null);
}
private PlugNicAnswer execute(PlugNicCommand cmd) {
@@ -1534,176 +982,6 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
}
}
- 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));
- }
-
- VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
-
- int ethDeviceNum = findRouterEthDeviceIndex(domrName, routerIp, ip.getVifMacAddress());
- if (ethDeviceNum < 0) {
- if (ip.isAdd()) {
- throw new InternalErrorException("Failed to find DomR VIF to associate/disassociate IP with.");
- } else {
- s_logger.debug("VIF to deassociate IP with does not exist, return success");
- return;
- }
- }
-
- String args = "";
- String snatArgs = "";
-
- if (ip.isAdd()) {
- args += " -A ";
- snatArgs += " -A ";
- } else {
- args += " -D ";
- snatArgs += " -D ";
- }
-
- args += " -l ";
- args += ip.getPublicIp();
-
- 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());
-
- ExecutionResult result = executeInVR(routerIp, "vpc_ipassoc.sh", args);
-
- if (!result.isSuccess()) {
- throw new InternalErrorException("Unable to assign public IP address");
- }
-
- if (ip.isSourceNat()) {
- snatArgs += " -l ";
- snatArgs += ip.getPublicIp();
- snatArgs += " -c ";
- snatArgs += "eth" + ethDeviceNum;
-
- ExecutionResult result_gateway = executeInVR(routerIp, "vpc_privateGateway.sh", snatArgs);
-
- if (!result_gateway.isSuccess()) {
- throw new InternalErrorException("Unable to configure source NAT for 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 broadcastId, final String vlanGateway, final String vlanNetmask, final String vifMacAddress)
- throws Exception {
-
- /**
- * TODO support other networks
- */
- URI broadcastUri = BroadcastDomainType.fromString(broadcastId);
- if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
- throw new InternalErrorException("Unable to assign a public IP to a VIF on network " + broadcastId);
- }
- String vlanId = BroadcastDomainType.getValue(broadcastUri);
-
- String publicNeworkName = HypervisorHostHelper.getPublicNetworkNamePrefix(vlanId);
- Pair<Integer, VirtualDevice> 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 ";
- }
- String cidrSize = Long.toString(NetUtils.getCidrSize(vlanNetmask));
- args += " -l ";
- args += publicIpAddress + "/" + cidrSize;
-
- args += " -c ";
- args += "eth" + publicNicInfo.first();
-
- args += " -g ";
- args += vlanGateway;
-
- if (addVif) {
- args += " -n ";
- }
-
- ExecutionResult result = executeInVR(privateIpAddress, "ipassoc.sh", args);
-
- if (!result.isSuccess()) {
- s_logger.error("ipassoc command on domain router " + privateIpAddress + " failed. message: " + result.getDetails());
- throw new Exception("ipassoc failed due to " + result.getDetails());
- }
-
- 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<NetworkDetails> 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<ManagedObjectReference, String> networkInfo = null;
@@ -1786,11 +1064,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
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));
- }
-
+ private ExecutionResult prepareNetworkElementCommand(IpAssocCommand cmd) {
int i = 0;
String[] results = new String[cmd.getIpAddresses().length];
@@ -1820,243 +1094,158 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
}
for (IpAddressTO ip : ips) {
- assignPublicIpAddress(vmMo, routerName, controlIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(), ip.isSourceNat(), ip.getBroadcastUri(),
- ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress());
- 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 ExecutionResult executeInVR(String routerIP, String script, String args) {
- Pair<Boolean, String> result;
-
- if (s_logger.isDebugEnabled()) {
- s_logger.debug("Run command on VR: " + routerIP + ", script: " + script + " with args: " + args);
- }
-
- try {
- VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
- result = SshHelper.sshExecute(routerIP, DefaultDomRSshPort, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/" + script + " " + args);
- } catch (Exception e) {
- String msg = "Command failed due to " + VmwareHelper.getExceptionMessage(e);
- s_logger.error(msg);
- result = new Pair<Boolean, String>(false, msg);
- }
- if (s_logger.isDebugEnabled()) {
- s_logger.debug(script + " execution result: " + result.first().toString());
- }
- return new ExecutionResult(result.first(), result.second());
- }
-
- 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()));
- }
-
- String controlIp = getRouterSshControlIp(cmd);
- final String password = cmd.getPassword();
- final String vmIpAddress = cmd.getVmIpAddress();
-
- String args = " -v " + vmIpAddress;
-
- if (s_logger.isDebugEnabled()) {
- s_logger.debug("Run command on domain router " + controlIp + ", /opt/cloud/bin/savepassword.sh " + args + " -p " +
- StringUtils.getMaskedPasswordForDisplay(cmd.getPassword()));
- }
-
- args += " -p " + password;
-
- //TODO: Password should be masked, cannot output to log directly
- ExecutionResult result = executeInVR(controlIp, "savepassword.sh", args);
- if (!result.isSuccess()) {
- s_logger.error("savepassword command on domain router " + controlIp + " failed, message: " + result.getDetails());
- return new Answer(cmd, false, result.getDetails());
- }
-
- return new Answer(cmd);
- }
-
- protected Answer execute(DhcpEntryCommand cmd) {
- // ssh -p 3922 -o StrictHostKeyChecking=no -i $cert root@$domr "/root/edithosts.sh $mac $ip $vm $dfltrt $ns $staticrt" >/dev/null
-
- String args = " -m " + cmd.getVmMac();
- if (cmd.getVmIpAddress() != null) {
- args += " -4 " + cmd.getVmIpAddress();
- }
- args += " -h " + cmd.getVmName();
-
- if (cmd.getDefaultRouter() != null) {
- args += " -d " + cmd.getDefaultRouter();
- }
-
- if (cmd.getDefaultDns() != null) {
- args += " -n " + cmd.getDefaultDns();
- }
-
- if (cmd.getStaticRoutes() != null) {
- args += " -s " + cmd.getStaticRoutes();
- }
-
- if (cmd.getVmIp6Address() != null) {
- args += " -6 " + cmd.getVmIp6Address();
- args += " -u " + cmd.getDuid();
- }
-
- if (!cmd.isDefault()) {
- args += " -N";
- }
-
- String controlIp = getRouterSshControlIp(cmd);
- ExecutionResult result = executeInVR(controlIp, "edithosts.sh", args);
+ /**
+ * TODO support other networks
+ */
+ URI broadcastUri = BroadcastDomainType.fromString(ip.getBroadcastUri());
+ if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
+ throw new InternalErrorException("Unable to assign a public IP to a VIF on network " + ip.getBroadcastUri());
+ }
+ String vlanId = BroadcastDomainType.getValue(broadcastUri);
- if (!result.isSuccess()) {
- return new Answer(cmd, false, result.getDetails());
- }
+ String publicNeworkName = HypervisorHostHelper.getPublicNetworkNamePrefix(vlanId);
+ Pair<Integer, VirtualDevice> publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName);
- return new Answer(cmd);
- }
+ if (s_logger.isDebugEnabled()) {
+ s_logger.debug("Find public NIC index, public network name: " + publicNeworkName + ", index: " + publicNicInfo.first());
+ }
- protected Answer execute(final CreateIpAliasCommand cmd) {
- cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
- List<IpAliasTO> ipAliasTOs = cmd.getIpAliasList();
- String args = "";
- for (IpAliasTO ipaliasto : ipAliasTOs) {
- args = args + ipaliasto.getAlias_count() + ":" + ipaliasto.getRouterip() + ":" + ipaliasto.getNetmask() + "-";
- }
+ boolean addVif = false;
+ if (ip.isAdd() && publicNicInfo.first().intValue() == -1) {
+ if (s_logger.isDebugEnabled()) {
+ s_logger.debug("Plug new NIC to associate" + controlIp + " to " + ip.getPublicIp());
+ }
+ addVif = true;
+ }
- String controlIp = getRouterSshControlIp(cmd);
- ExecutionResult result = executeInVR(controlIp, "createIpAlias.sh", args);
+ if (addVif) {
+ plugPublicNic(vmMo, vlanId, ip.getVifMacAddress());
+ publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName);
+ if (publicNicInfo.first().intValue() >= 0) {
+ networkUsage(controlIp, "addVif", "eth" + publicNicInfo.first());
+ }
+ }
- if (!result.isSuccess()) {
- return new Answer(cmd, false, result.getDetails());
+ 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);
+ }
+ ip.setNicDevId(publicNicInfo.first().intValue());
+ ip.setNewNic(addVif);
+ }
+ } catch (Throwable e) {
+ s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e);
+ return new ExecutionResult(false, e.toString());
}
-
- return new Answer(cmd);
+ return new ExecutionResult(true, null);
}
- protected Answer execute(final DeleteIpAliasCommand cmd) {
- cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
- List<IpAliasTO> revokedIpAliasTOs = cmd.getDeleteIpAliasTos();
- List<IpAliasTO> activeIpAliasTOs = cmd.getCreateIpAliasTos();
- String args = "";
- for (IpAliasTO ipAliasTO : revokedIpAliasTOs) {
- args = args + ipAliasTO.getAlias_count() + ":" + ipAliasTO.getRouterip() + ":" + ipAliasTO.getNetmask() + "-";
- }
- args = args + "- ";
- for (IpAliasTO ipAliasTO : activeIpAliasTOs) {
- args = args + ipAliasTO.getAlias_count() + ":" + ipAliasTO.getRouterip() + ":" + ipAliasTO.getNetmask() + "-";
- }
-
- String controlIp = getRouterSshControlIp(cmd);
- ExecutionResult result = executeInVR(controlIp, "deleteIpAlias.sh", args);
-
- if (!result.isSuccess()) {
- return new Answer(cmd, false, result.getDetails());
- }
-
- return new Answer(cmd);
- }
+ private ExecutionResult NetworkElementCommandnup(IpAssocCommand cmd) {
+ String[] results = new String[cmd.getIpAddresses().length];
- protected Answer execute(final DnsMasqConfigCommand cmd) {
- String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
- String controlIp = getRouterSshControlIp(cmd);
+ VmwareContext context = getServiceContext();
+ try {
+ VmwareHypervisorHost hyperHost = getHyperHost(context);
- assert (controlIp != null);
+ IpAddressTO[] ips = cmd.getIpAddresses();
+ String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
+ String controlIp = VmwareResource.getRouterSshControlIp(cmd);
- List<DhcpTO> dhcpTos = cmd.getIps();
- String args = "";
- for (DhcpTO dhcpTo : dhcpTos) {
- args = args + dhcpTo.getRouterIp() + ":" + dhcpTo.getGateway() + ":" + dhcpTo.getNetmask() + ":" + dhcpTo.getStartIpOfSubnet() + "-";
- }
+ VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(routerName);
- ExecutionResult result = executeInVR(controlIp, "dnsmasq.sh", args);
+ // 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());
+ vmMo = clusterMo.findVmOnHyperHost(routerName);
+ }
+ }
- if (!result.isSuccess()) {
- return new Answer(cmd, false, result.getDetails());
- }
+ if (vmMo == null) {
+ String msg = "Router " + routerName + " no longer exists to execute IPAssoc command";
+ s_logger.error(msg);
+ throw new Exception(msg);
+ }
- return new Answer(cmd);
- }
+ for (IpAddressTO ip : ips) {
+ /**
+ * TODO support other networks
+ */
+ URI broadcastUri = BroadcastDomainType.fromString(ip.getBroadcastUri());
+ if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
+ throw new InternalErrorException("Unable to assign a public IP to a VIF on network " + ip.getBroadcastUri());
+ }
+ String vlanId = BroadcastDomainType.getValue(broadcastUri);
- protected CheckS2SVpnConnectionsAnswer execute(CheckS2SVpnConnectionsCommand cmd) {
- String controlIp = getRouterSshControlIp(cmd);
- String args = "";
- for (String ip : cmd.getVpnIps()) {
- args += ip + " ";
- }
+ String publicNeworkName = HypervisorHostHelper.getPublicNetworkNamePrefix(vlanId);
+ Pair<Integer, VirtualDevice> publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName);
- ExecutionResult result = executeInVR(controlIp, "checkbatchs2svpn.sh", args);
+ if (s_logger.isDebugEnabled()) {
+ s_logger.debug("Find public NIC index, public network name: " + publicNeworkName + ", index: " + publicNicInfo.first());
+ }
- if (!result.isSuccess()) {
- return new CheckS2SVpnConnectionsAnswer(cmd, false, result.getDetails());
- }
+ boolean removeVif = false;
+ if (!ip.isAdd() && ip.isFirstIP()) {
+ removeVif = true;
- return new CheckS2SVpnConnectionsAnswer(cmd, true, result.getDetails());
- }
+ if (s_logger.isDebugEnabled()) {
+ s_logger.debug("Unplug NIC " + publicNicInfo.first());
+ }
+ }
- protected Answer execute(CheckRouterCommand cmd) {
- String controlIp = getRouterSshControlIp(cmd);
- ExecutionResult result = executeInVR(controlIp, "checkrouter.sh", null);
+ 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);
+ }
- if (!result.isSuccess()) {
- return new CheckRouterAnswer(cmd, result.getDetails());
+ 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<NetworkDetails> 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);
+ }
+ }
+ }
+ }
+ }
+ } catch (Throwable e) {
+ s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e);
+ return new ExecutionResult(false, e.toString());
}
- return new CheckRouterAnswer(cmd, result.getDetails(), true);
+ return new ExecutionResult(true, null);
}
- protected Answer execute(GetDomRVersionCmd cmd) {
- String controlIp = getRouterSshControlIp(cmd);
- ExecutionResult result = executeInVR(controlIp, "get_template_version.sh", null);
+ @Override
+ public ExecutionResult executeInVR(String routerIP, String script, String args) {
+ Pair<Boolean, String> result;
- if (!result.isSuccess()) {
- return new GetDomRVersionAnswer(cmd, result.getDetails());
- }
- String[] lines = result.getDetails().split("&");
- if (lines.length != 2) {
- return new GetDomRVersionAnswer(cmd, result.getDetails());
+ //TODO: Password should be masked, cannot output to log directly
+ if (s_logger.isDebugEnabled()) {
+ s_logger.debug("Run command on VR: " + routerIP + ", script: " + script + " with args: " + args);
}
- return new GetDomRVersionAnswer(cmd, result.getDetails(), lines[0], lines[1]);
- }
- protected Answer execute(BumpUpPriorityCommand cmd) {
- String controlIp = getRouterSshControlIp(cmd);
- ExecutionResult result = executeInVR(controlIp, "bumpup_priority.sh", null);
-
- if (!result.isSuccess()) {
- return new Answer(cmd, false, result.getDetails());
+ try {
+ VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
+ result = SshHelper.sshExecute(routerIP, DefaultDomRSshPort, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/" + script + " " + args);
+ } catch (Exception e) {
+ String msg = "Command failed due to " + VmwareHelper.getExceptionMessage(e);
+ s_logger.error(msg);
+ result = new Pair<Boolean, String>(false, msg);
}
-
- return new Answer(cmd);
- }
-
- protected Answer execute(VmDataCommand cmd) {
- String controlIp = getRouterSshControlIp(cmd);
- Map<String, List<String[]>> data = new HashMap<String, List<String[]>>();
- data.put(cmd.getVmIpAddress(), cmd.getVmData());
-
- String json = new Gson().toJson(data);
- s_logger.debug("VM data JSON IS:" + json);
-
- json = Base64.encodeBase64String(json.getBytes());
-
- String args = "-d " + json;
-
- ExecutionResult result = executeInVR(controlIp, "vmdata.py", args);
-
- if (!result.isSuccess()) {
- return new Answer(cmd, false, result.getDetails());
+ if (s_logger.isDebugEnabled()) {
+ s_logger.debug(script + " execution result: " + result.first().toString());
}
-
- return new Answer(cmd);
+ return new ExecutionResult(result.first(), result.second());
}
protected CheckSshAnswer execute(CheckSshCommand cmd) {
@@ -3281,57 +2470,6 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
}
}
- protected 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());
- }
- argsBuf.append(" -C ").append(cmd.getLocalCidr());
- argsBuf.append(" -i ").append(cmd.getPublicInterface());
-
-
- ExecutionResult result = executeInVR(controlIp, "vpn_l2tp.sh", argsBuf.toString());
-
- if (!result.isSuccess()) {
- return new Answer(cmd, false, result.getDetails());
- }
-
- return new Answer(cmd);
- }
-
- protected Answer execute(final VpnUsersCfgCommand cmd) {
- 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());
- }
-
- ExecutionResult result = executeInVR(controlIp, "vpn_l2tp.sh", argsBuf.toString());
-
- if (!result.isSuccess()) {
- return new Answer(cmd, false, result.getDetails());
- }
- }
-
- return new Answer(cmd);
- }
-
private VirtualMachineMO takeVmFromOtherHyperHost(VmwareHypervisorHost hyperHost, String vmName) throws Exception {
VirtualMachineMO vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
@@ -6439,6 +5577,10 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
new VmwareStorageProcessor((VmwareHostService)this, _fullCloneFlag, (VmwareStorageMount)mgr, timeout, this, _shutdownWaitMs, null);
storageHandler = new VmwareStorageSubsystemCommandHandler(storageProcessor);
+ _vrResource = new VirtualRoutingResource(this);
+ if (!_vrResource.configure(name, params)) {
+ throw new ConfigurationException("Unable to configure VirtualRoutingResource");
+ }
return true;
} finally {
recycleServiceContext();
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3c955c36/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
index a817e26..200a72f 100644
--- a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
+++ b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
@@ -550,6 +550,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
public ExecutionResult prepareCommand(NetworkElementCommand cmd) {
//Update IP used to access router
cmd.setRouterAccessIp(cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP));
+ assert cmd.getRouterAccessIp() != null;
if (cmd instanceof IpAssocVpcCommand) {
return prepareNetworkElementCommand((IpAssocVpcCommand)cmd);