Search in sources :

Example 11 with StartupCommand

use of com.cloud.legacymodel.communication.command.startup.StartupCommand in project cosmic by MissionCriticalCloud.

the class ResourceManagerImpl method markHostAsDisconnected.

private void markHostAsDisconnected(HostVO host, final StartupCommand[] cmds) {
    if (host == null) {
        // reloading the host from db
        if (cmds != null) {
            final StartupCommand firstCmd = cmds[0];
            host = findHostByGuid(firstCmd.getGuid());
            if (host == null) {
                host = findHostByGuid(firstCmd.getGuidWithoutResource());
            }
        }
    }
    if (host != null) {
        // Change agent status to Alert, so that host is considered for
        // reconnection next time
        this._agentMgr.agentStatusTransitTo(host, Event.AgentDisconnected, this._nodeId);
    }
}
Also used : StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand)

Example 12 with StartupCommand

use of com.cloud.legacymodel.communication.command.startup.StartupCommand in project cosmic by MissionCriticalCloud.

the class ResourceManagerImpl method createHostVO.

protected HostVO createHostVO(final StartupCommand[] cmds, final ServerResource resource, final Map<String, String> details, List<String> hostTags, final ResourceStateAdapter.Event stateEvent) {
    final StartupCommand startup = cmds[0];
    HostVO host = findHostByGuid(startup.getGuid());
    boolean isNew = false;
    if (host == null) {
        host = findHostByGuid(startup.getGuidWithoutResource());
    }
    if (host == null) {
        host = new HostVO(startup.getGuid());
        isNew = true;
    }
    String dataCenter = startup.getDataCenter();
    String pod = startup.getPod();
    final String cluster = startup.getCluster();
    if (pod != null && dataCenter != null && pod.equalsIgnoreCase("default") && dataCenter.equalsIgnoreCase("default")) {
        final List<HostPodVO> pods = this._podDao.listAllIncludingRemoved();
        for (final HostPodVO hpv : pods) {
            if (checkCIDR(hpv, startup.getPrivateIpAddress(), startup.getPrivateNetmask())) {
                pod = hpv.getName();
                dataCenter = this._dcDao.findById(hpv.getDataCenterId()).getName();
                break;
            }
        }
    }
    long dcId;
    DataCenterVO dc = this._dcDao.findByName(dataCenter);
    if (dc == null) {
        try {
            dcId = Long.parseLong(dataCenter);
            dc = this._dcDao.findById(dcId);
        } catch (final NumberFormatException e) {
            s_logger.debug("Cannot parse " + dataCenter + " into Long.");
        }
    }
    if (dc == null) {
        throw new IllegalArgumentException("Host " + startup.getPrivateIpAddress() + " sent incorrect data center: " + dataCenter);
    }
    dcId = dc.getId();
    HostPodVO p = this._podDao.findByName(pod, dcId);
    if (p == null) {
        try {
            final long podId = Long.parseLong(pod);
            p = this._podDao.findById(podId);
        } catch (final NumberFormatException e) {
            s_logger.debug("Cannot parse " + pod + " into Long.");
        }
    }
    /*
         * ResourceStateAdapter is responsible for throwing Exception if Pod is
         * null and non-null is required. for example, XcpServerDiscoever.
         */
    final Long podId = p == null ? null : p.getId();
    Long clusterId = null;
    if (cluster != null) {
        try {
            clusterId = Long.valueOf(cluster);
        } catch (final NumberFormatException e) {
            if (podId != null) {
                ClusterVO c = this._clusterDao.findBy(cluster, podId.longValue());
                if (c == null) {
                    c = new ClusterVO(dcId, podId.longValue(), cluster);
                    c = this._clusterDao.persist(c);
                }
                clusterId = c.getId();
            }
        }
    }
    if (startup instanceof StartupRoutingCommand) {
        final StartupRoutingCommand ssCmd = (StartupRoutingCommand) startup;
        final List<String> implicitHostTags = ssCmd.getHostTags();
        if (!implicitHostTags.isEmpty()) {
            if (hostTags == null) {
                hostTags = this._hostTagsDao.gethostTags(host.getId());
            }
            if (hostTags != null) {
                implicitHostTags.removeAll(hostTags);
                hostTags.addAll(implicitHostTags);
            } else {
                hostTags = implicitHostTags;
            }
        }
    }
    host.setDataCenterId(dc.getId());
    host.setPodId(podId);
    host.setClusterId(clusterId);
    host.setPrivateIpAddress(startup.getPrivateIpAddress());
    host.setPrivateNetmask(startup.getPrivateNetmask());
    host.setPrivateMacAddress(startup.getPrivateMacAddress());
    host.setPublicIpAddress(startup.getPublicIpAddress());
    host.setPublicMacAddress(startup.getPublicMacAddress());
    host.setPublicNetmask(startup.getPublicNetmask());
    host.setStorageIpAddress(startup.getStorageIpAddress());
    host.setStorageMacAddress(startup.getStorageMacAddress());
    host.setStorageNetmask(startup.getStorageNetmask());
    host.setVersion(startup.getVersion());
    host.setName(startup.getName());
    host.setManagementServerId(this._nodeId);
    host.setStorageUrl(startup.getIqn());
    host.setLastPinged(System.currentTimeMillis() >> 10);
    host.setHostTags(hostTags);
    host.setDetails(details);
    if (startup.getStorageIpAddressDeux() != null) {
        host.setStorageIpAddressDeux(startup.getStorageIpAddressDeux());
        host.setStorageMacAddressDeux(startup.getStorageMacAddressDeux());
        host.setStorageNetmaskDeux(startup.getStorageNetmaskDeux());
    }
    if (resource != null) {
        /* null when agent is connected agent */
        host.setResource(resource.getClass().getName());
    }
    host = (HostVO) dispatchToStateAdapters(stateEvent, true, host, cmds, resource, details, hostTags);
    if (host == null) {
        throw new CloudRuntimeException("No resource state adapter response");
    }
    if (isNew) {
        host = this._hostDao.persist(host);
    } else {
        this._hostDao.update(host.getId(), host);
    }
    try {
        resourceStateTransitTo(host, ResourceState.Event.InternalCreated, this._nodeId);
        /* Agent goes to Connecting status */
        this._agentMgr.agentStatusTransitTo(host, Event.AgentConnected, this._nodeId);
    } catch (final Exception e) {
        s_logger.debug("Cannot transmit host " + host.getId() + " to Creating state", e);
        this._agentMgr.agentStatusTransitTo(host, Event.Error, this._nodeId);
        try {
            resourceStateTransitTo(host, ResourceState.Event.Error, this._nodeId);
        } catch (final NoTransitionException e1) {
            s_logger.debug("Cannot transmit host " + host.getId() + "to Error state", e);
        }
    }
    return host;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) ClusterVO(com.cloud.dc.ClusterVO) HostPodVO(com.cloud.dc.HostPodVO) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) ResourceInUseException(com.cloud.legacymodel.exceptions.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) SshException(com.cloud.utils.ssh.SshException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.legacymodel.exceptions.UnableDeleteHostException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DiscoveryException(com.cloud.legacymodel.exceptions.DiscoveryException) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) StartupRoutingCommand(com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand)

Example 13 with StartupCommand

use of com.cloud.legacymodel.communication.command.startup.StartupCommand in project cosmic by MissionCriticalCloud.

the class ResourceManagerImpl method createHostAndAgentDeferred.

private Host createHostAndAgentDeferred(final ServerResource resource, final Map<String, String> details, final boolean old, final List<String> hostTags, final boolean forRebalance) {
    HostVO host = null;
    StartupCommand[] cmds = null;
    boolean hostExists = false;
    boolean deferAgentCreation = true;
    boolean created = false;
    try {
        cmds = resource.initialize();
        if (cmds == null) {
            s_logger.info("Unable to fully initialize the agent because no StartupCommands are returned");
            return null;
        }
        /* Generate a random version in a dev setup situation */
        if (this.getClass().getPackage().getImplementationVersion() == null) {
            for (final StartupCommand cmd : cmds) {
                if (cmd.getVersion() == null) {
                    cmd.setVersion(Long.toString(System.currentTimeMillis()));
                }
            }
        }
        if (s_logger.isDebugEnabled()) {
            new Request(-1l, -1l, cmds, true, false).logD("Startup request from directly connected host: ", true);
        }
        if (old) {
            final StartupCommand firstCmd = cmds[0];
            host = findHostByGuid(firstCmd.getGuid());
            if (host == null) {
                host = findHostByGuid(firstCmd.getGuidWithoutResource());
            }
            if (host != null && host.getRemoved() == null) {
                // host already
                // added, no
                // need to add
                // again
                s_logger.debug("Found the host " + host.getId() + " by guid: " + firstCmd.getGuid() + ", old host reconnected as new");
                // ensures that host status is left
                hostExists = true;
                // again
                return null;
            }
        }
        host = null;
        final GlobalLock addHostLock = GlobalLock.getInternLock("AddHostLock");
        try {
            if (addHostLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION)) {
                // to safely determine first host in cluster in multi-MS scenario
                try {
                    host = createHostVO(cmds, resource, details, hostTags, ResourceStateAdapter.Event.CREATE_HOST_VO_FOR_DIRECT_CONNECT);
                    if (host != null) {
                        // if first host in cluster no need to defer agent creation
                        deferAgentCreation = !isFirstHostInCluster(host);
                    }
                } finally {
                    addHostLock.unlock();
                }
            }
        } finally {
            addHostLock.releaseRef();
        }
        if (host != null) {
            if (!deferAgentCreation) {
                // if first host in cluster then
                created = this._agentMgr.handleDirectConnectAgent(host, cmds, resource, forRebalance);
                // reload
                host = this._hostDao.findById(host.getId());
            } else {
                // reload
                host = this._hostDao.findById(host.getId());
                // force host status to 'Alert' so that it is loaded for
                // connection during next scan task
                this._agentMgr.agentStatusTransitTo(host, Event.AgentDisconnected, this._nodeId);
                // reload
                host = this._hostDao.findById(host.getId());
                // so that scan task can pick it up
                host.setLastPinged(0);
                this._hostDao.update(host.getId(), host);
            }
        }
    } catch (final Exception e) {
        s_logger.warn("Unable to connect due to ", e);
    } finally {
        if (hostExists) {
            if (cmds != null) {
                resource.disconnected();
            }
        } else {
            if (!deferAgentCreation && !created) {
                if (cmds != null) {
                    resource.disconnected();
                }
                markHostAsDisconnected(host, cmds);
            }
        }
    }
    return host;
}
Also used : StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) GlobalLock(com.cloud.utils.db.GlobalLock) Request(com.cloud.common.transport.Request) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) ResourceInUseException(com.cloud.legacymodel.exceptions.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) SshException(com.cloud.utils.ssh.SshException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.legacymodel.exceptions.UnableDeleteHostException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DiscoveryException(com.cloud.legacymodel.exceptions.DiscoveryException)

Example 14 with StartupCommand

use of com.cloud.legacymodel.communication.command.startup.StartupCommand in project cosmic by MissionCriticalCloud.

the class ResourceManagerImpl method createHostAndAgent.

private Host createHostAndAgent(final ServerResource resource, final Map<String, String> details, final boolean old, final List<String> hostTags, final boolean forRebalance) {
    HostVO host = null;
    StartupCommand[] cmds = null;
    boolean hostExists = false;
    boolean created = false;
    try {
        cmds = resource.initialize();
        if (cmds == null) {
            s_logger.info("Unable to fully initialize the agent because no StartupCommands are returned");
            return null;
        }
        /* Generate a random version in a dev setup situation */
        if (this.getClass().getPackage().getImplementationVersion() == null) {
            for (final StartupCommand cmd : cmds) {
                if (cmd.getVersion() == null) {
                    cmd.setVersion(Long.toString(System.currentTimeMillis()));
                }
            }
        }
        if (s_logger.isDebugEnabled()) {
            new Request(-1l, -1l, cmds, true, false).logD("Startup request from directly connected host: ", true);
        }
        if (old) {
            final StartupCommand firstCmd = cmds[0];
            host = findHostByGuid(firstCmd.getGuid());
            if (host == null) {
                host = findHostByGuid(firstCmd.getGuidWithoutResource());
            }
            if (host != null && host.getRemoved() == null) {
                // host already added, no need to add again
                s_logger.debug("Found the host " + host.getId() + " by guid: " + firstCmd.getGuid() + ", old host reconnected as new");
                // ensures that host status is left unchanged in case of adding same one again
                hostExists = true;
                return null;
            }
        }
        host = createHostVO(cmds, resource, details, hostTags, ResourceStateAdapter.Event.CREATE_HOST_VO_FOR_DIRECT_CONNECT);
        if (host != null) {
            created = this._agentMgr.handleDirectConnectAgent(host, cmds, resource, forRebalance);
            /* reload myself from database */
            host = this._hostDao.findById(host.getId());
        }
    } catch (final Exception e) {
        s_logger.warn("Unable to connect due to ", e);
    } finally {
        if (hostExists) {
            if (cmds != null) {
                resource.disconnected();
            }
        } else {
            if (!created) {
                if (cmds != null) {
                    resource.disconnected();
                }
                markHostAsDisconnected(host, cmds);
            }
        }
    }
    return host;
}
Also used : StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) Request(com.cloud.common.transport.Request) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) ResourceInUseException(com.cloud.legacymodel.exceptions.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) SshException(com.cloud.utils.ssh.SshException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.legacymodel.exceptions.UnableDeleteHostException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DiscoveryException(com.cloud.legacymodel.exceptions.DiscoveryException)

Example 15 with StartupCommand

use of com.cloud.legacymodel.communication.command.startup.StartupCommand in project cosmic by MissionCriticalCloud.

the class UploadListener method processConnect.

@Override
public void processConnect(final Host agent, final StartupCommand[] startupCommands, final boolean forRebalance) {
    for (final StartupCommand startupCommand : startupCommands) {
        if (!(startupCommand instanceof StartupStorageCommand)) {
            return;
        }
        final long agentId = agent.getId();
        final StartupStorageCommand storage = (StartupStorageCommand) startupCommand;
        if (storage.getResourceType() == StorageResourceType.STORAGE_HOST || storage.getResourceType() == StorageResourceType.SECONDARY_STORAGE) {
            this.uploadMonitor.handleUploadSync(agentId);
        }
    }
}
Also used : StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) StartupStorageCommand(com.cloud.legacymodel.communication.command.startup.StartupStorageCommand)

Aggregations

StartupCommand (com.cloud.legacymodel.communication.command.startup.StartupCommand)27 StartupRoutingCommand (com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand)14 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)6 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)6 StartupStorageCommand (com.cloud.legacymodel.communication.command.startup.StartupStorageCommand)5 HostVO (com.cloud.host.HostVO)4 StartupSecondaryStorageCommand (com.cloud.legacymodel.communication.command.startup.StartupSecondaryStorageCommand)4 Request (com.cloud.common.transport.Request)3 ClusterVO (com.cloud.dc.ClusterVO)3 ConnectionException (com.cloud.legacymodel.exceptions.ConnectionException)3 DiscoveryException (com.cloud.legacymodel.exceptions.DiscoveryException)3 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)3 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)3 ResourceInUseException (com.cloud.legacymodel.exceptions.ResourceInUseException)3 UnableDeleteHostException (com.cloud.legacymodel.exceptions.UnableDeleteHostException)3 StoragePoolHostVO (com.cloud.storage.StoragePoolHostVO)3 SshException (com.cloud.utils.ssh.SshException)3 URISyntaxException (java.net.URISyntaxException)3 ConfigurationException (javax.naming.ConfigurationException)3 Commands (com.cloud.agent.manager.Commands)2