use of com.cloud.agent.api.MaintainCommand in project CloudStack-archive by CloudStack-extras.
the class SimulatorManagerImpl method simulate.
@DB
@Override
public Answer simulate(Command cmd, String hostGuid) {
Transaction txn = Transaction.currentTxn();
txn.transitToUserManagedConnection(_concierge.conn());
try {
MockHost host = _mockHost.findByGuid(hostGuid);
String cmdName = cmd.toString();
int index = cmdName.lastIndexOf(".");
if (index != -1) {
cmdName = cmdName.substring(index + 1);
}
MockConfigurationVO config = _mockConfigDao.findByNameBottomUP(host.getDataCenterId(), host.getPodId(), host.getClusterId(), host.getId(), cmdName);
SimulatorInfo info = new SimulatorInfo();
info.setHostUuid(hostGuid);
if (config != null) {
Map<String, String> configParameters = config.getParameters();
for (Map.Entry<String, String> entry : configParameters.entrySet()) {
if (entry.getKey().equalsIgnoreCase("enabled")) {
info.setEnabled(Boolean.parseBoolean(entry.getValue()));
} else if (entry.getKey().equalsIgnoreCase("timeout")) {
try {
info.setTimeout(Integer.valueOf(entry.getValue()));
} catch (NumberFormatException e) {
s_logger.debug("invalid timeout parameter: " + e.toString());
}
} else if (entry.getKey().equalsIgnoreCase("wait")) {
try {
int wait = Integer.valueOf(entry.getValue());
Thread.sleep(wait * 1000);
} catch (NumberFormatException e) {
s_logger.debug("invalid timeout parameter: " + e.toString());
} catch (InterruptedException e) {
s_logger.debug("thread is interrupted: " + e.toString());
}
}
}
}
if (cmd instanceof GetHostStatsCommand) {
return _mockAgentMgr.getHostStatistic((GetHostStatsCommand) cmd);
} else if (cmd instanceof CheckHealthCommand) {
return _mockAgentMgr.checkHealth((CheckHealthCommand) cmd);
} else if (cmd instanceof PingTestCommand) {
return _mockAgentMgr.pingTest((PingTestCommand) cmd);
} else if (cmd instanceof MigrateCommand) {
return _mockVmMgr.Migrate((MigrateCommand) cmd, info);
} else if (cmd instanceof StartCommand) {
return _mockVmMgr.startVM((StartCommand) cmd, info);
} else if (cmd instanceof CheckSshCommand) {
return _mockVmMgr.checkSshCommand((CheckSshCommand) cmd);
} else if (cmd instanceof SetStaticNatRulesCommand) {
return _mockVmMgr.SetStaticNatRules((SetStaticNatRulesCommand) cmd);
} else if (cmd instanceof SetPortForwardingRulesCommand) {
return _mockVmMgr.SetPortForwardingRules((SetPortForwardingRulesCommand) cmd);
} else if (cmd instanceof NetworkUsageCommand) {
return _mockVmMgr.getNetworkUsage((NetworkUsageCommand) cmd);
} else if (cmd instanceof IpAssocCommand) {
return _mockVmMgr.IpAssoc((IpAssocCommand) cmd);
} else if (cmd instanceof LoadBalancerConfigCommand) {
return _mockVmMgr.LoadBalancerConfig((LoadBalancerConfigCommand) cmd);
} else if (cmd instanceof DhcpEntryCommand) {
return _mockVmMgr.AddDhcpEntry((DhcpEntryCommand) cmd);
} else if (cmd instanceof VmDataCommand) {
return _mockVmMgr.setVmData((VmDataCommand) cmd);
} else if (cmd instanceof CleanupNetworkRulesCmd) {
return _mockVmMgr.CleanupNetworkRules((CleanupNetworkRulesCmd) cmd, info);
} else if (cmd instanceof CheckNetworkCommand) {
return _mockAgentMgr.checkNetworkCommand((CheckNetworkCommand) cmd);
} else if (cmd instanceof StopCommand) {
return _mockVmMgr.stopVM((StopCommand) cmd);
} else if (cmd instanceof RebootCommand) {
return _mockVmMgr.rebootVM((RebootCommand) cmd);
} else if (cmd instanceof GetVncPortCommand) {
return _mockVmMgr.getVncPort((GetVncPortCommand) cmd);
} else if (cmd instanceof CheckConsoleProxyLoadCommand) {
return _mockVmMgr.CheckConsoleProxyLoad((CheckConsoleProxyLoadCommand) cmd);
} else if (cmd instanceof WatchConsoleProxyLoadCommand) {
return _mockVmMgr.WatchConsoleProxyLoad((WatchConsoleProxyLoadCommand) cmd);
} else if (cmd instanceof SecurityGroupRulesCmd) {
return _mockVmMgr.AddSecurityGroupRules((SecurityGroupRulesCmd) cmd, info);
} else if (cmd instanceof SavePasswordCommand) {
return _mockVmMgr.SavePassword((SavePasswordCommand) cmd);
} else if (cmd instanceof PrimaryStorageDownloadCommand) {
return _mockStorageMgr.primaryStorageDownload((PrimaryStorageDownloadCommand) cmd);
} else if (cmd instanceof CreateCommand) {
return _mockStorageMgr.createVolume((CreateCommand) cmd);
} else if (cmd instanceof AttachVolumeCommand) {
return _mockStorageMgr.AttachVolume((AttachVolumeCommand) cmd);
} else if (cmd instanceof AttachIsoCommand) {
return _mockStorageMgr.AttachIso((AttachIsoCommand) cmd);
} else if (cmd instanceof DeleteStoragePoolCommand) {
return _mockStorageMgr.DeleteStoragePool((DeleteStoragePoolCommand) cmd);
} else if (cmd instanceof ModifyStoragePoolCommand) {
return _mockStorageMgr.ModifyStoragePool((ModifyStoragePoolCommand) cmd);
} else if (cmd instanceof CreateStoragePoolCommand) {
return _mockStorageMgr.CreateStoragePool((CreateStoragePoolCommand) cmd);
} else if (cmd instanceof SecStorageSetupCommand) {
return _mockStorageMgr.SecStorageSetup((SecStorageSetupCommand) cmd);
} else if (cmd instanceof ListTemplateCommand) {
return _mockStorageMgr.ListTemplates((ListTemplateCommand) cmd);
} else if (cmd instanceof DestroyCommand) {
return _mockStorageMgr.Destroy((DestroyCommand) cmd);
} else if (cmd instanceof DownloadProgressCommand) {
return _mockStorageMgr.DownloadProcess((DownloadProgressCommand) cmd);
} else if (cmd instanceof DownloadCommand) {
return _mockStorageMgr.Download((DownloadCommand) cmd);
} else if (cmd instanceof GetStorageStatsCommand) {
return _mockStorageMgr.GetStorageStats((GetStorageStatsCommand) cmd);
} else if (cmd instanceof ManageSnapshotCommand) {
return _mockStorageMgr.ManageSnapshot((ManageSnapshotCommand) cmd);
} else if (cmd instanceof BackupSnapshotCommand) {
return _mockStorageMgr.BackupSnapshot((BackupSnapshotCommand) cmd, info);
} else if (cmd instanceof DeleteSnapshotBackupCommand) {
return _mockStorageMgr.DeleteSnapshotBackup((DeleteSnapshotBackupCommand) cmd);
} else if (cmd instanceof CreateVolumeFromSnapshotCommand) {
return _mockStorageMgr.CreateVolumeFromSnapshot((CreateVolumeFromSnapshotCommand) cmd);
} else if (cmd instanceof DeleteTemplateCommand) {
return _mockStorageMgr.DeleteTemplate((DeleteTemplateCommand) cmd);
} else if (cmd instanceof SecStorageVMSetupCommand) {
return _mockStorageMgr.SecStorageVMSetup((SecStorageVMSetupCommand) cmd);
} else if (cmd instanceof CreatePrivateTemplateFromSnapshotCommand) {
return _mockStorageMgr.CreatePrivateTemplateFromSnapshot((CreatePrivateTemplateFromSnapshotCommand) cmd);
} else if (cmd instanceof ComputeChecksumCommand) {
return _mockStorageMgr.ComputeChecksum((ComputeChecksumCommand) cmd);
} else if (cmd instanceof CreatePrivateTemplateFromVolumeCommand) {
return _mockStorageMgr.CreatePrivateTemplateFromVolume((CreatePrivateTemplateFromVolumeCommand) cmd);
} else if (cmd instanceof MaintainCommand) {
return _mockAgentMgr.MaintainCommand((MaintainCommand) cmd);
} else if (cmd instanceof GetVmStatsCommand) {
return _mockVmMgr.getVmStats((GetVmStatsCommand) cmd);
} else if (cmd instanceof GetDomRVersionCmd) {
return _mockVmMgr.getDomRVersion((GetDomRVersionCmd) cmd);
} else if (cmd instanceof ClusterSyncCommand) {
return new Answer(cmd);
} else if (cmd instanceof CopyVolumeCommand) {
return _mockStorageMgr.CopyVolume((CopyVolumeCommand) cmd);
} else {
return Answer.createUnsupportedCommandAnswer(cmd);
}
} catch (Exception e) {
s_logger.debug("Failed execute cmd: " + e.toString());
txn.rollback();
return new Answer(cmd, false, e.toString());
} finally {
txn.transitToAutoManagedConnection(Transaction.CLOUD_DB);
}
}
use of com.cloud.agent.api.MaintainCommand in project CloudStack-archive by CloudStack-extras.
the class Agent method processRequest.
protected void processRequest(final Request request, final Link link) {
boolean requestLogged = false;
Response response = null;
try {
final Command[] cmds = request.getCommands();
final Answer[] answers = new Answer[cmds.length];
for (int i = 0; i < cmds.length; i++) {
final Command cmd = cmds[i];
Answer answer;
try {
if (s_logger.isDebugEnabled()) {
// this is a hack to make sure we do NOT log the ssh keys
if ((cmd instanceof ModifySshKeysCommand)) {
s_logger.debug("Received the request for command: ModifySshKeysCommand");
} else {
if (// ensures request is logged only once per method call
!requestLogged) {
s_logger.debug("Request:" + request.toString());
requestLogged = true;
}
}
s_logger.debug("Processing command: " + cmd.toString());
}
if (cmd instanceof CronCommand) {
final CronCommand watch = (CronCommand) cmd;
scheduleWatch(link, request, watch.getInterval() * 1000, watch.getInterval() * 1000);
answer = new Answer(cmd, true, null);
} else if (cmd instanceof UpgradeCommand) {
final UpgradeCommand upgrade = (UpgradeCommand) cmd;
answer = upgradeAgent(upgrade.getUpgradeUrl(), upgrade);
} else if (cmd instanceof ShutdownCommand) {
ShutdownCommand shutdown = (ShutdownCommand) cmd;
s_logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
cancelTasks();
_reconnectAllowed = false;
answer = new Answer(cmd, true, null);
} else if (cmd instanceof MaintainCommand) {
s_logger.debug("Received maintainCommand");
cancelTasks();
_reconnectAllowed = false;
answer = new MaintainAnswer((MaintainCommand) cmd);
} else if (cmd instanceof AgentControlCommand) {
answer = null;
synchronized (_controlListeners) {
for (IAgentControlListener listener : _controlListeners) {
answer = listener.processControlRequest(request, (AgentControlCommand) cmd);
if (answer != null) {
break;
}
}
}
if (answer == null) {
s_logger.warn("No handler found to process cmd: " + cmd.toString());
answer = new AgentControlAnswer(cmd);
}
} else {
_inProgress.incrementAndGet();
try {
answer = _resource.executeRequest(cmd);
} finally {
_inProgress.decrementAndGet();
}
if (answer == null) {
s_logger.debug("Response: unsupported command" + cmd.toString());
answer = Answer.createUnsupportedCommandAnswer(cmd);
}
}
} catch (final Throwable th) {
s_logger.warn("Caught: ", th);
final StringWriter writer = new StringWriter();
th.printStackTrace(new PrintWriter(writer));
answer = new Answer(cmd, false, writer.toString());
}
answers[i] = answer;
if (!answer.getResult() && request.stopOnError()) {
for (i++; i < cmds.length; i++) {
answers[i] = new Answer(cmds[i], false, "Stopped by previous failure");
}
break;
}
}
response = new Response(request, answers);
} finally {
if (s_logger.isDebugEnabled()) {
s_logger.debug(response != null ? response.toString() : "response is null");
}
if (response != null) {
try {
link.send(response.toBytes());
} catch (final ClosedChannelException e) {
s_logger.warn("Unable to send response: " + response.toString());
}
}
}
}
use of com.cloud.agent.api.MaintainCommand in project cloudstack by apache.
the class ResourceManagerImpl method doUmanageHost.
private boolean doUmanageHost(final long hostId) {
final HostVO host = _hostDao.findById(hostId);
if (host == null) {
s_logger.debug("Cannot find host " + hostId + ", assuming it has been deleted, skip umanage");
return true;
}
if (host.getHypervisorType() == HypervisorType.KVM || host.getHypervisorType() == HypervisorType.LXC) {
final MaintainAnswer answer = (MaintainAnswer) _agentMgr.easySend(hostId, new MaintainCommand());
}
_agentMgr.disconnectWithoutInvestigation(hostId, Event.ShutdownRequested);
return true;
}
use of com.cloud.agent.api.MaintainCommand in project cloudstack by apache.
the class NotAValidCommand method testMaintainCommand.
@Test
public void testMaintainCommand() {
// This test needs further work.
final String uuid = "befc4dcd-f5c6-4015-8791-3c18622b7c7f";
final Connection conn = Mockito.mock(Connection.class);
final XsHost xsHost = Mockito.mock(XsHost.class);
final XmlRpcClient client = Mockito.mock(XmlRpcClient.class);
// final Host.Record hr = PowerMockito.mock(Host.Record.class);
// final Host host = PowerMockito.mock(Host.class);
final MaintainCommand maintainCommand = new MaintainCommand();
final Map<String, Object> map = new Hashtable<String, Object>();
map.put("Value", "Xen");
final Map<String, Object> spiedMap = spy(map);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(citrixResourceBase.getConnection()).thenReturn(conn);
when(citrixResourceBase.getHost()).thenReturn(xsHost);
when(xsHost.getUuid()).thenReturn(uuid);
when(conn.getSessionReference()).thenReturn("befc4dcd");
try {
final Object[] params = { Marshalling.toXMLRPC("befc4dcd"), Marshalling.toXMLRPC(uuid) };
when(client.execute("host.get_by_uuid", new Object[] { "befc4dcd", uuid })).thenReturn(spiedMap);
PowerMockito.when(conn, "dispatch", "host.get_by_uuid", params).thenReturn(spiedMap);
} catch (final Exception e) {
fail(e.getMessage());
}
// try {
// PowerMockito.mockStatic(Host.class);
// //BDDMockito.given(Host.getByUuid(conn,
// xsHost.getUuid())).willReturn(host);
// PowerMockito.when(Host.getByUuid(conn,
// xsHost.getUuid())).thenReturn(host);
// PowerMockito.verifyStatic(times(1));
// } catch (final BadServerResponse e) {
// fail(e.getMessage());
// } catch (final XenAPIException e) {
// fail(e.getMessage());
// } catch (final XmlRpcException e) {
// fail(e.getMessage());
// }
//
// PowerMockito.mockStatic(Types.class);
// PowerMockito.when(Types.toHostRecord(spiedMap)).thenReturn(hr);
// PowerMockito.verifyStatic(times(1));
//
// try {
// PowerMockito.mockStatic(Host.Record.class);
// when(host.getRecord(conn)).thenReturn(hr);
// verify(host, times(1)).getRecord(conn);
// } catch (final BadServerResponse e) {
// fail(e.getMessage());
// } catch (final XenAPIException e) {
// fail(e.getMessage());
// } catch (final XmlRpcException e) {
// fail(e.getMessage());
// }
final Answer answer = wrapper.execute(maintainCommand, citrixResourceBase);
assertFalse(answer.getResult());
}
use of com.cloud.agent.api.MaintainCommand in project cloudstack by apache.
the class Ovm3HypervisorSupportTest method MaintainCommandTest.
@Test
public void MaintainCommandTest() throws ConfigurationException {
con = prepare();
MaintainCommand cmd = new MaintainCommand();
Answer ra = hypervisor.executeRequest(cmd);
results.basicBooleanTest(ra.getResult());
}
Aggregations