Search in sources :

Example 21 with Command

use of com.cloud.agent.api.Command in project cloudstack by apache.

the class HypervDirectConnectResource method requestStartupCommand.

// TODO: Is it valid to return NULL, or should we throw on error?
// Returns StartupCommand with fields revised with values known only to the
// host
public final Command[] requestStartupCommand(final Command[] cmd) {
    // Set HTTP POST destination URI
    // Using java.net.URI, see
    // http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URI.html
    URI agentUri = null;
    try {
        final String cmdName = StartupCommand.class.getName();
        agentUri = new URI("https", null, _agentIp, _port, "/api/HypervResource/" + cmdName, null, null);
    } catch (final URISyntaxException e) {
        // TODO add proper logging
        final String errMsg = "Could not generate URI for Hyper-V agent";
        s_logger.error(errMsg, e);
        return null;
    }
    final String incomingCmd = postHttpRequest(s_gson.toJson(cmd), agentUri);
    if (incomingCmd == null) {
        return null;
    }
    Command[] result = null;
    try {
        result = s_gson.fromJson(incomingCmd, Command[].class);
    } catch (final Exception ex) {
        final String errMsg = "Failed to deserialize Command[] " + incomingCmd;
        s_logger.error(errMsg, ex);
    }
    s_logger.debug("requestStartupCommand received response " + s_gson.toJson(result));
    if (result.length > 0) {
        return result;
    }
    return null;
}
Also used : CheckRouterCommand(com.cloud.agent.api.CheckRouterCommand) PlugNicCommand(com.cloud.agent.api.PlugNicCommand) Site2SiteVpnCfgCommand(com.cloud.agent.api.routing.Site2SiteVpnCfgCommand) CheckSshCommand(com.cloud.agent.api.check.CheckSshCommand) SetupGuestNetworkCommand(com.cloud.agent.api.SetupGuestNetworkCommand) DnsMasqConfigCommand(com.cloud.agent.api.routing.DnsMasqConfigCommand) PingTestCommand(com.cloud.agent.api.PingTestCommand) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) ModifyVmNicConfigCommand(com.cloud.agent.api.ModifyVmNicConfigCommand) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) SetStaticRouteCommand(com.cloud.agent.api.routing.SetStaticRouteCommand) NetworkElementCommand(com.cloud.agent.api.routing.NetworkElementCommand) SetStaticNatRulesCommand(com.cloud.agent.api.routing.SetStaticNatRulesCommand) VmDataCommand(com.cloud.agent.api.routing.VmDataCommand) SetSourceNatCommand(com.cloud.agent.api.routing.SetSourceNatCommand) CreateIpAliasCommand(com.cloud.agent.api.routing.CreateIpAliasCommand) SetMonitorServiceCommand(com.cloud.agent.api.routing.SetMonitorServiceCommand) CheckS2SVpnConnectionsCommand(com.cloud.agent.api.CheckS2SVpnConnectionsCommand) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand) RemoteAccessVpnCfgCommand(com.cloud.agent.api.routing.RemoteAccessVpnCfgCommand) Command(com.cloud.agent.api.Command) NetworkUsageCommand(com.cloud.agent.api.NetworkUsageCommand) DeleteIpAliasCommand(com.cloud.agent.api.routing.DeleteIpAliasCommand) DhcpEntryCommand(com.cloud.agent.api.routing.DhcpEntryCommand) StartCommand(com.cloud.agent.api.StartCommand) GetVmConfigCommand(com.cloud.agent.api.GetVmConfigCommand) StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) SetPortForwardingRulesCommand(com.cloud.agent.api.routing.SetPortForwardingRulesCommand) StartupCommand(com.cloud.agent.api.StartupCommand) IpAssocCommand(com.cloud.agent.api.routing.IpAssocCommand) SavePasswordCommand(com.cloud.agent.api.routing.SavePasswordCommand) PingCommand(com.cloud.agent.api.PingCommand) PingRoutingCommand(com.cloud.agent.api.PingRoutingCommand) LoadBalancerConfigCommand(com.cloud.agent.api.routing.LoadBalancerConfigCommand) SetFirewallRulesCommand(com.cloud.agent.api.routing.SetFirewallRulesCommand) IpAssocVpcCommand(com.cloud.agent.api.routing.IpAssocVpcCommand) SetNetworkACLCommand(com.cloud.agent.api.routing.SetNetworkACLCommand) VpnUsersCfgCommand(com.cloud.agent.api.routing.VpnUsersCfgCommand) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException)

Example 22 with Command

use of com.cloud.agent.api.Command in project cloudstack by apache.

the class HypervDirectConnectResource method initialize.

@Override
public final StartupCommand[] initialize() {
    // assert
    if (!_configureCalled) {
        final String errMsg = this.getClass().getName() + " requires configure() be called before" + " initialize()";
        s_logger.error(errMsg);
    }
    // Create default StartupRoutingCommand, then customise
    final StartupRoutingCommand defaultStartRoutCmd = new StartupRoutingCommand(0, 0, 0, 0, null, Hypervisor.HypervisorType.Hyperv, RouterPrivateIpStrategy.HostLocal);
    // Identity within the data centre is decided by CloudStack kernel,
    // and passed via ServerResource.configure()
    defaultStartRoutCmd.setDataCenter(_zoneId);
    defaultStartRoutCmd.setPod(_podId);
    defaultStartRoutCmd.setCluster(_clusterId);
    defaultStartRoutCmd.setGuid(_guid);
    defaultStartRoutCmd.setName(_name);
    defaultStartRoutCmd.setPrivateIpAddress(_agentIp);
    defaultStartRoutCmd.setStorageIpAddress(_agentIp);
    defaultStartRoutCmd.setPool(_clusterGuid);
    s_logger.debug("Generated StartupRoutingCommand for _agentIp \"" + _agentIp + "\"");
    defaultStartRoutCmd.setVersion(this.getClass().getPackage().getImplementationVersion());
    // Specifics of the host's resource capacity and network configuration
    // comes from the host itself. CloudStack sanity checks network
    // configuration
    // and uses capacity info for resource allocation.
    final Command[] startCmds = requestStartupCommand(new Command[] { defaultStartRoutCmd });
    // TODO: may throw, is this okay?
    final StartupRoutingCommand startCmd = (StartupRoutingCommand) startCmds[0];
    // Assert that host identity is consistent with existing values.
    if (startCmd == null) {
        final String errMsg = String.format("Host %s (IP %s)" + "did not return a StartupRoutingCommand", _name, _agentIp);
        s_logger.error(errMsg);
        // TODO: valid to return null, or should we throw?
        return null;
    }
    if (!startCmd.getDataCenter().equals(defaultStartRoutCmd.getDataCenter())) {
        final String errMsg = String.format("Host %s (IP %s) changed zone/data center.  Was " + defaultStartRoutCmd.getDataCenter() + " NOW its " + startCmd.getDataCenter(), _name, _agentIp);
        s_logger.error(errMsg);
        // TODO: valid to return null, or should we throw?
        return null;
    }
    if (!startCmd.getPod().equals(defaultStartRoutCmd.getPod())) {
        final String errMsg = String.format("Host %s (IP %s) changed pod.  Was " + defaultStartRoutCmd.getPod() + " NOW its " + startCmd.getPod(), _name, _agentIp);
        s_logger.error(errMsg);
        // TODO: valid to return null, or should we throw?
        return null;
    }
    if (!startCmd.getCluster().equals(defaultStartRoutCmd.getCluster())) {
        final String errMsg = String.format("Host %s (IP %s) changed cluster.  Was " + defaultStartRoutCmd.getCluster() + " NOW its " + startCmd.getCluster(), _name, _agentIp);
        s_logger.error(errMsg);
        // TODO: valid to return null, or should we throw?
        return null;
    }
    if (!startCmd.getGuid().equals(defaultStartRoutCmd.getGuid())) {
        final String errMsg = String.format("Host %s (IP %s) changed guid.  Was " + defaultStartRoutCmd.getGuid() + " NOW its " + startCmd.getGuid(), _name, _agentIp);
        s_logger.error(errMsg);
        // TODO: valid to return null, or should we throw?
        return null;
    }
    if (!startCmd.getPrivateIpAddress().equals(defaultStartRoutCmd.getPrivateIpAddress())) {
        final String errMsg = String.format("Host %s (IP %s) IP address.  Was " + defaultStartRoutCmd.getPrivateIpAddress() + " NOW its " + startCmd.getPrivateIpAddress(), _name, _agentIp);
        s_logger.error(errMsg);
        // TODO: valid to return null, or should we throw?
        return null;
    }
    if (!startCmd.getName().equals(defaultStartRoutCmd.getName())) {
        final String errMsg = String.format("Host %s (IP %s) name.  Was " + startCmd.getName() + " NOW its " + defaultStartRoutCmd.getName(), _name, _agentIp);
        s_logger.error(errMsg);
        // TODO: valid to return null, or should we throw?
        return null;
    }
    // Host will also supply details of an existing StoragePool if it has
    // been configured with one.
    //
    // NB: if the host was configured
    // with a local storage pool, CloudStack may not be able to use it
    // unless
    // it is has service offerings configured to recognise this storage
    // type.
    StartupStorageCommand storePoolCmd = null;
    if (startCmds.length > 1) {
        storePoolCmd = (StartupStorageCommand) startCmds[1];
        // TODO: is this assertion required?
        if (storePoolCmd == null) {
            final String frmtStr = "Host %s (IP %s) sent incorrect Command, " + "second parameter should be a " + "StartupStorageCommand";
            final String errMsg = String.format(frmtStr, _name, _agentIp);
            s_logger.error(errMsg);
            // TODO: valid to return null, or should we throw?
            return null;
        }
        s_logger.info("Host " + _name + " (IP " + _agentIp + ") already configured with a storeage pool, details " + s_gson.toJson(startCmds[1]));
    } else {
        s_logger.info("Host " + _name + " (IP " + _agentIp + ") already configured with a storeage pool, details ");
    }
    return new StartupCommand[] { startCmd, storePoolCmd };
}
Also used : StartupCommand(com.cloud.agent.api.StartupCommand) CheckRouterCommand(com.cloud.agent.api.CheckRouterCommand) PlugNicCommand(com.cloud.agent.api.PlugNicCommand) Site2SiteVpnCfgCommand(com.cloud.agent.api.routing.Site2SiteVpnCfgCommand) CheckSshCommand(com.cloud.agent.api.check.CheckSshCommand) SetupGuestNetworkCommand(com.cloud.agent.api.SetupGuestNetworkCommand) DnsMasqConfigCommand(com.cloud.agent.api.routing.DnsMasqConfigCommand) PingTestCommand(com.cloud.agent.api.PingTestCommand) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) ModifyVmNicConfigCommand(com.cloud.agent.api.ModifyVmNicConfigCommand) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) SetStaticRouteCommand(com.cloud.agent.api.routing.SetStaticRouteCommand) NetworkElementCommand(com.cloud.agent.api.routing.NetworkElementCommand) SetStaticNatRulesCommand(com.cloud.agent.api.routing.SetStaticNatRulesCommand) VmDataCommand(com.cloud.agent.api.routing.VmDataCommand) SetSourceNatCommand(com.cloud.agent.api.routing.SetSourceNatCommand) CreateIpAliasCommand(com.cloud.agent.api.routing.CreateIpAliasCommand) SetMonitorServiceCommand(com.cloud.agent.api.routing.SetMonitorServiceCommand) CheckS2SVpnConnectionsCommand(com.cloud.agent.api.CheckS2SVpnConnectionsCommand) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand) RemoteAccessVpnCfgCommand(com.cloud.agent.api.routing.RemoteAccessVpnCfgCommand) Command(com.cloud.agent.api.Command) NetworkUsageCommand(com.cloud.agent.api.NetworkUsageCommand) DeleteIpAliasCommand(com.cloud.agent.api.routing.DeleteIpAliasCommand) DhcpEntryCommand(com.cloud.agent.api.routing.DhcpEntryCommand) StartCommand(com.cloud.agent.api.StartCommand) GetVmConfigCommand(com.cloud.agent.api.GetVmConfigCommand) StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) SetPortForwardingRulesCommand(com.cloud.agent.api.routing.SetPortForwardingRulesCommand) StartupCommand(com.cloud.agent.api.StartupCommand) IpAssocCommand(com.cloud.agent.api.routing.IpAssocCommand) SavePasswordCommand(com.cloud.agent.api.routing.SavePasswordCommand) PingCommand(com.cloud.agent.api.PingCommand) PingRoutingCommand(com.cloud.agent.api.PingRoutingCommand) LoadBalancerConfigCommand(com.cloud.agent.api.routing.LoadBalancerConfigCommand) SetFirewallRulesCommand(com.cloud.agent.api.routing.SetFirewallRulesCommand) IpAssocVpcCommand(com.cloud.agent.api.routing.IpAssocVpcCommand) SetNetworkACLCommand(com.cloud.agent.api.routing.SetNetworkACLCommand) VpnUsersCfgCommand(com.cloud.agent.api.routing.VpnUsersCfgCommand) StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand)

Example 23 with Command

use of com.cloud.agent.api.Command in project cloudstack by apache.

the class HypervDirectConnectResourceTest method testStartupCommand.

public final void testStartupCommand() {
    StartupRoutingCommand defaultStartRoutCmd = new StartupRoutingCommand(0, 0, 0, 0, null, Hypervisor.HypervisorType.Hyperv, RouterPrivateIpStrategy.HostLocal);
    // Identity within the data centre is decided by CloudStack kernel,
    // and passed via ServerResource.configure()
    defaultStartRoutCmd.setDataCenter("1");
    defaultStartRoutCmd.setPod("1");
    defaultStartRoutCmd.setCluster("1");
    defaultStartRoutCmd.setGuid("1");
    defaultStartRoutCmd.setName("1");
    defaultStartRoutCmd.setPrivateIpAddress("1");
    defaultStartRoutCmd.setStorageIpAddress("1");
    defaultStartRoutCmd.setCpus(12);
    // TODO: does version need to be hard coded.
    defaultStartRoutCmd.setVersion("4.2.0");
    StartupCommand scmd = defaultStartRoutCmd;
    Command[] cmds = { scmd };
    String cmdsStr = s_gson.toJson(cmds);
    s_logger.debug("Commands[] toJson is " + cmdsStr);
    Command[] result = s_gson.fromJson(cmdsStr, Command[].class);
    s_logger.debug("Commands[] fromJson is " + s_gson.toJson(result));
    s_logger.debug("Commands[] first element has type" + result[0].toString());
}
Also used : StartupCommand(com.cloud.agent.api.StartupCommand) DeleteStoragePoolCommand(com.cloud.agent.api.DeleteStoragePoolCommand) StartCommand(com.cloud.agent.api.StartCommand) StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) PrimaryStorageDownloadCommand(com.cloud.agent.api.storage.PrimaryStorageDownloadCommand) StartupCommand(com.cloud.agent.api.StartupCommand) GetVmStatsCommand(com.cloud.agent.api.GetVmStatsCommand) StopCommand(com.cloud.agent.api.StopCommand) CreateCommand(com.cloud.agent.api.storage.CreateCommand) DestroyCommand(com.cloud.agent.api.storage.DestroyCommand) GetStorageStatsCommand(com.cloud.agent.api.GetStorageStatsCommand) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand) CreateStoragePoolCommand(com.cloud.agent.api.CreateStoragePoolCommand) Command(com.cloud.agent.api.Command) GetHostStatsCommand(com.cloud.agent.api.GetHostStatsCommand) ModifyStoragePoolCommand(com.cloud.agent.api.ModifyStoragePoolCommand) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand)

Example 24 with Command

use of com.cloud.agent.api.Command in project cloudstack by apache.

the class VMwareGuru method finalizeExpungeVolumes.

@Override
public List<Command> finalizeExpungeVolumes(VirtualMachine vm) {
    List<Command> commands = new ArrayList<Command>();
    List<VolumeVO> volumes = _volumeDao.findByInstance(vm.getId());
    if (volumes != null) {
        for (VolumeVO volume : volumes) {
            StoragePoolVO storagePool = _storagePoolDao.findById(volume.getPoolId());
            // so the volume was never assigned to a storage pool)
            if (storagePool != null && storagePool.isManaged() && volume.getVolumeType() == Volume.Type.ROOT) {
                VolumeInfo volumeInfo = _volFactory.getVolume(volume.getId());
                PrimaryDataStore primaryDataStore = (PrimaryDataStore) volumeInfo.getDataStore();
                Map<String, String> details = primaryDataStore.getDetails();
                if (details == null) {
                    details = new HashMap<String, String>();
                    primaryDataStore.setDetails(details);
                }
                details.put(DiskTO.MANAGED, Boolean.TRUE.toString());
                DeleteCommand cmd = new DeleteCommand(volumeInfo.getTO());
                commands.add(cmd);
                break;
            }
        }
    }
    return commands;
}
Also used : DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) VolumeVO(com.cloud.storage.VolumeVO) DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) BackupSnapshotCommand(com.cloud.agent.api.BackupSnapshotCommand) CreateVolumeOVACommand(com.cloud.agent.api.storage.CreateVolumeOVACommand) CreatePrivateTemplateFromSnapshotCommand(com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand) PrepareOVAPackingCommand(com.cloud.agent.api.storage.PrepareOVAPackingCommand) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) UnregisterNicCommand(com.cloud.agent.api.UnregisterNicCommand) CopyVolumeCommand(com.cloud.agent.api.storage.CopyVolumeCommand) StorageSubSystemCommand(org.apache.cloudstack.storage.command.StorageSubSystemCommand) CreateVolumeFromSnapshotCommand(com.cloud.agent.api.CreateVolumeFromSnapshotCommand) CreateEntityDownloadURLCommand(com.cloud.agent.api.storage.CreateEntityDownloadURLCommand) Command(com.cloud.agent.api.Command) CreatePrivateTemplateFromVolumeCommand(com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand) ArrayList(java.util.ArrayList) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Example 25 with Command

use of com.cloud.agent.api.Command in project cloudstack by apache.

the class StorageManagerImpl method sendToPool.

@Override
public Pair<Long, Answer[]> sendToPool(StoragePool pool, long[] hostIdsToTryFirst, List<Long> hostIdsToAvoid, Commands cmds) throws StorageUnavailableException {
    List<Long> hostIds = getUpHostsInPool(pool.getId());
    Collections.shuffle(hostIds);
    if (hostIdsToTryFirst != null) {
        for (int i = hostIdsToTryFirst.length - 1; i >= 0; i--) {
            if (hostIds.remove(hostIdsToTryFirst[i])) {
                hostIds.add(0, hostIdsToTryFirst[i]);
            }
        }
    }
    if (hostIdsToAvoid != null) {
        hostIds.removeAll(hostIdsToAvoid);
    }
    if (hostIds == null || hostIds.isEmpty()) {
        throw new StorageUnavailableException("Unable to send command to the pool " + pool.getId() + " due to there is no enabled hosts up in this cluster", pool.getId());
    }
    for (Long hostId : hostIds) {
        try {
            List<Answer> answers = new ArrayList<Answer>();
            Command[] cmdArray = cmds.toCommands();
            for (Command cmd : cmdArray) {
                long targetHostId = _hvGuruMgr.getGuruProcessedCommandTargetHost(hostId, cmd);
                answers.add(_agentMgr.send(targetHostId, cmd));
            }
            return new Pair<Long, Answer[]>(hostId, answers.toArray(new Answer[answers.size()]));
        } catch (AgentUnavailableException e) {
            s_logger.debug("Unable to send storage pool command to " + pool + " via " + hostId, e);
        } catch (OperationTimedoutException e) {
            s_logger.debug("Unable to send storage pool command to " + pool + " via " + hostId, e);
        }
    }
    throw new StorageUnavailableException("Unable to send command to the pool ", pool.getId());
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) ArrayList(java.util.ArrayList) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) Answer(com.cloud.agent.api.Answer) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) Command(com.cloud.agent.api.Command) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) Pair(com.cloud.utils.Pair)

Aggregations

Command (com.cloud.agent.api.Command)33 Answer (com.cloud.agent.api.Answer)17 StartupCommand (com.cloud.agent.api.StartupCommand)17 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)13 PingCommand (com.cloud.agent.api.PingCommand)13 StartupRoutingCommand (com.cloud.agent.api.StartupRoutingCommand)11 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)10 PingRoutingCommand (com.cloud.agent.api.PingRoutingCommand)8 ReadyCommand (com.cloud.agent.api.ReadyCommand)8 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)8 MaintainCommand (com.cloud.agent.api.MaintainCommand)7 StartupAnswer (com.cloud.agent.api.StartupAnswer)7 StartupStorageCommand (com.cloud.agent.api.StartupStorageCommand)7 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)6 CronCommand (com.cloud.agent.api.CronCommand)6 MaintainAnswer (com.cloud.agent.api.MaintainAnswer)5 PlugNicCommand (com.cloud.agent.api.PlugNicCommand)5 StartCommand (com.cloud.agent.api.StartCommand)5 Request (com.cloud.agent.transport.Request)5