Search in sources :

Example 1 with PreparePxeServerAnswer

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

the class BaremetalPingPxeResource method execute.

protected PreparePxeServerAnswer execute(PreparePxeServerCommand cmd) {
    com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_ip, 22);
    try {
        sshConnection.connect(null, 60000, 60000);
        if (!sshConnection.authenticateWithPassword(_username, _password)) {
            s_logger.debug("SSH Failed to authenticate");
            throw new ConfigurationException(String.format("Cannot connect to PING PXE server(IP=%1$s, username=%2$s, password=%3$s", _ip, _username, _password));
        }
        String script = String.format("python /usr/bin/prepare_tftp_bootfile.py restore %1$s %2$s %3$s %4$s %5$s %6$s %7$s %8$s %9$s %10$s %11$s", _tftpDir, cmd.getMac(), _storageServer, _share, _dir, cmd.getTemplate(), _cifsUserName, _cifsPassword, cmd.getIp(), cmd.getNetMask(), cmd.getGateWay());
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, script)) {
            return new PreparePxeServerAnswer(cmd, "prepare PING at " + _ip + " failed, command:" + script);
        }
        s_logger.debug("Prepare Ping PXE server successfully");
        return new PreparePxeServerAnswer(cmd);
    } catch (Exception e) {
        s_logger.debug("Prepare PING pxe server failed", e);
        return new PreparePxeServerAnswer(cmd, e.getMessage());
    } finally {
        if (sshConnection != null) {
            sshConnection.close();
        }
    }
}
Also used : ConfigurationException(javax.naming.ConfigurationException) PreparePxeServerAnswer(com.cloud.agent.api.baremetal.PreparePxeServerAnswer) ConfigurationException(javax.naming.ConfigurationException)

Example 2 with PreparePxeServerAnswer

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

the class BareMetalPingServiceImpl method prepare.

@Override
public boolean prepare(VirtualMachineProfile profile, NicProfile pxeNic, Network network, DeployDestination dest, ReservationContext context) {
    QueryBuilder<BaremetalPxeVO> sc = QueryBuilder.create(BaremetalPxeVO.class);
    sc.and(sc.entity().getDeviceType(), Op.EQ, BaremetalPxeType.PING.toString());
    sc.and(sc.entity().getPodId(), Op.EQ, dest.getPod().getId());
    BaremetalPxeVO pxeVo = sc.find();
    if (pxeVo == null) {
        throw new CloudRuntimeException("No PING PXE server found in pod: " + dest.getPod().getId() + ", you need to add it before starting VM");
    }
    long pxeServerId = pxeVo.getHostId();
    String mac = pxeNic.getMacAddress();
    String ip = pxeNic.getIPv4Address();
    String gateway = pxeNic.getIPv4Gateway();
    String mask = pxeNic.getIPv4Netmask();
    String dns = pxeNic.getIPv4Dns1();
    if (dns == null) {
        dns = pxeNic.getIPv4Dns2();
    }
    try {
        String tpl = profile.getTemplate().getUrl();
        assert tpl != null : "How can a null template get here!!!";
        PreparePxeServerCommand cmd = new PreparePxeServerCommand(ip, mac, mask, gateway, dns, tpl, profile.getVirtualMachine().getInstanceName(), dest.getHost().getName());
        PreparePxeServerAnswer ans = (PreparePxeServerAnswer) _agentMgr.send(pxeServerId, cmd);
        if (!ans.getResult()) {
            s_logger.warn("Unable tot program PXE server: " + pxeVo.getId() + " because " + ans.getDetails());
            return false;
        }
        IpmISetBootDevCommand bootCmd = new IpmISetBootDevCommand(BootDev.pxe);
        Answer anw = _agentMgr.send(dest.getHost().getId(), bootCmd);
        if (!anw.getResult()) {
            s_logger.warn("Unable to set host: " + dest.getHost().getId() + " to PXE boot because " + anw.getDetails());
        }
        return anw.getResult();
    } catch (Exception e) {
        s_logger.warn("Cannot prepare PXE server", e);
        return false;
    }
}
Also used : Answer(com.cloud.agent.api.Answer) PreparePxeServerAnswer(com.cloud.agent.api.baremetal.PreparePxeServerAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PreparePxeServerCommand(com.cloud.agent.api.baremetal.PreparePxeServerCommand) IpmISetBootDevCommand(com.cloud.agent.api.baremetal.IpmISetBootDevCommand) PreparePxeServerAnswer(com.cloud.agent.api.baremetal.PreparePxeServerAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BaremetalPxeVO(com.cloud.baremetal.database.BaremetalPxeVO)

Aggregations

PreparePxeServerAnswer (com.cloud.agent.api.baremetal.PreparePxeServerAnswer)2 Answer (com.cloud.agent.api.Answer)1 IpmISetBootDevCommand (com.cloud.agent.api.baremetal.IpmISetBootDevCommand)1 PreparePxeServerCommand (com.cloud.agent.api.baremetal.PreparePxeServerCommand)1 BaremetalPxeVO (com.cloud.baremetal.database.BaremetalPxeVO)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 ConfigurationException (javax.naming.ConfigurationException)1