Search in sources :

Example 81 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class BaremetalVlanManagerImpl method prepareVlan.

@Override
public void prepareVlan(Network nw, DeployDestination destHost) {
    List<BaremetalRctVO> vos = rctDao.listAll();
    if (vos.isEmpty()) {
        throw new CloudRuntimeException("no rack configuration found, please call addBaremetalRct to add one");
    }
    BaremetalRctVO vo = vos.get(0);
    BaremetalRct rct = gson.fromJson(vo.getRct(), BaremetalRct.class);
    RackPair rp = findRack(rct, destHost.getHost().getPrivateMacAddress());
    if (rp == null) {
        throw new CloudRuntimeException(String.format("cannot find any rack contains host[mac:%s], please double check your rack configuration file, update it and call addBaremetalRct again", destHost.getHost().getPrivateMacAddress()));
    }
    int vlan = Integer.parseInt(Networks.BroadcastDomainType.getValue(nw.getBroadcastUri()));
    BaremetalSwitchBackend backend = getSwitchBackend(rp.rack.getL2Switch().getType());
    BaremetalVlanStruct struct = new BaremetalVlanStruct();
    struct.setHostMac(rp.host.getMac());
    struct.setPort(rp.host.getPort());
    struct.setSwitchIp(rp.rack.getL2Switch().getIp());
    struct.setSwitchPassword(rp.rack.getL2Switch().getPassword());
    struct.setSwitchType(rp.rack.getL2Switch().getType());
    struct.setSwitchUsername(rp.rack.getL2Switch().getUsername());
    struct.setVlan(vlan);
    backend.prepareVlan(struct);
}
Also used : BaremetalRctVO(com.cloud.baremetal.database.BaremetalRctVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BaremetalSwitchBackend(com.cloud.baremetal.networkservice.BaremetalSwitchBackend) BaremetalVlanStruct(com.cloud.baremetal.networkservice.BaremetalVlanStruct)

Example 82 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class BaremetalVlanManagerImpl method releaseVlan.

@Override
public void releaseVlan(Network nw, VirtualMachineProfile vm) {
    List<BaremetalRctVO> vos = rctDao.listAll();
    if (vos.isEmpty()) {
        throw new CloudRuntimeException("no rack configuration found, please call addBaremetalRct to add one");
    }
    BaremetalRctVO vo = vos.get(0);
    BaremetalRct rct = gson.fromJson(vo.getRct(), BaremetalRct.class);
    HostVO host = hostDao.findById(vm.getVirtualMachine().getHostId());
    RackPair rp = findRack(rct, host.getPrivateMacAddress());
    assert rp != null : String.format("where is my rack???");
    int vlan = Integer.parseInt(Networks.BroadcastDomainType.getValue(nw.getBroadcastUri()));
    BaremetalVlanStruct struct = new BaremetalVlanStruct();
    struct.setHostMac(rp.host.getMac());
    struct.setPort(rp.host.getPort());
    struct.setSwitchIp(rp.rack.getL2Switch().getIp());
    struct.setSwitchPassword(rp.rack.getL2Switch().getPassword());
    struct.setSwitchType(rp.rack.getL2Switch().getType());
    struct.setSwitchUsername(rp.rack.getL2Switch().getUsername());
    struct.setVlan(vlan);
    BaremetalSwitchBackend backend = getSwitchBackend(rp.rack.getL2Switch().getType());
    backend.removePortFromVlan(struct);
}
Also used : BaremetalRctVO(com.cloud.baremetal.database.BaremetalRctVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BaremetalSwitchBackend(com.cloud.baremetal.networkservice.BaremetalSwitchBackend) BaremetalVlanStruct(com.cloud.baremetal.networkservice.BaremetalVlanStruct) HostVO(com.cloud.host.HostVO)

Example 83 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class BaremetalVlanManagerImpl method addRct.

@Override
public BaremetalRctResponse addRct(AddBaremetalRctCmd cmd) {
    try {
        List<BaremetalRctVO> existings = rctDao.listAll();
        if (!existings.isEmpty()) {
            throw new CloudRuntimeException(String.format("there is some RCT existing. A CloudStack deployment accepts only one RCT"));
        }
        URL url = new URL(cmd.getRctUrl());
        RestTemplate rest = new RestTemplate();
        String rctStr = rest.getForObject(url.toString(), String.class);
        // validate it's right format
        BaremetalRct rct = gson.fromJson(rctStr, BaremetalRct.class);
        QueryBuilder<BaremetalRctVO> sc = QueryBuilder.create(BaremetalRctVO.class);
        sc.and(sc.entity().getUrl(), SearchCriteria.Op.EQ, cmd.getRctUrl());
        BaremetalRctVO vo = sc.find();
        if (vo == null) {
            vo = new BaremetalRctVO();
            vo.setRct(gson.toJson(rct));
            vo.setUrl(cmd.getRctUrl());
            vo = rctDao.persist(vo);
        } else {
            vo.setRct(gson.toJson(rct));
            rctDao.update(vo.getId(), vo);
        }
        BaremetalRctResponse rsp = new BaremetalRctResponse();
        rsp.setUrl(vo.getUrl());
        rsp.setId(vo.getUuid());
        rsp.setObjectName("baremetalrct");
        return rsp;
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException(String.format("%s is not a legal http url", cmd.getRctUrl()));
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) BaremetalRctVO(com.cloud.baremetal.database.BaremetalRctVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RestTemplate(org.springframework.web.client.RestTemplate) BaremetalRctResponse(com.cloud.baremetal.networkservice.BaremetalRctResponse) URL(java.net.URL)

Example 84 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException 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)

Example 85 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class BaremetalKickStartPxeResource method configure.

@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    super.configure(name, params);
    _tftpDir = (String) params.get(BaremetalPxeService.PXE_PARAM_TFTP_DIR);
    if (_tftpDir == null) {
        throw new ConfigurationException("No tftp directory specified");
    }
    com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_ip, 22);
    s_logger.debug(String.format("Trying to connect to kickstart PXE server(IP=%1$s, username=%2$s, password=%3$s", _ip, _username, "******"));
    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 kickstart PXE server(IP=%1$s, username=%2$s, password=%3$s", _ip, _username, "******"));
        }
        String cmd = String.format("[ -f /%1$s/pxelinux.0 ]", _tftpDir);
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, cmd)) {
            throw new ConfigurationException("Miss files in TFTP directory at " + _tftpDir + " check if pxelinux.0 are here");
        }
        SCPClient scp = new SCPClient(sshConnection);
        String prepareScript = "scripts/network/ping/prepare_kickstart_bootfile.py";
        String prepareScriptPath = Script.findScript("", prepareScript);
        if (prepareScriptPath == null) {
            throw new ConfigurationException("Can not find prepare_kickstart_bootfile.py at " + prepareScript);
        }
        scp.put(prepareScriptPath, "/usr/bin/", "0755");
        String cpScript = "scripts/network/ping/prepare_kickstart_kernel_initrd.py";
        String cpScriptPath = Script.findScript("", cpScript);
        if (cpScriptPath == null) {
            throw new ConfigurationException("Can not find prepare_kickstart_kernel_initrd.py at " + cpScript);
        }
        scp.put(cpScriptPath, "/usr/bin/", "0755");
        String userDataScript = "scripts/network/ping/baremetal_user_data.py";
        String userDataScriptPath = Script.findScript("", userDataScript);
        if (userDataScriptPath == null) {
            throw new ConfigurationException("Can not find baremetal_user_data.py at " + userDataScript);
        }
        scp.put(userDataScriptPath, "/usr/bin/", "0755");
        return true;
    } catch (Exception e) {
        throw new CloudRuntimeException(e);
    } finally {
        if (sshConnection != null) {
            sshConnection.close();
        }
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Aggregations

CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1279 PreparedStatement (java.sql.PreparedStatement)320 SQLException (java.sql.SQLException)320 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)236 ResultSet (java.sql.ResultSet)217 ArrayList (java.util.ArrayList)217 ConfigurationException (javax.naming.ConfigurationException)171 HashMap (java.util.HashMap)129 DB (com.cloud.utils.db.DB)118 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)115 IOException (java.io.IOException)95 HostVO (com.cloud.host.HostVO)94 Answer (com.cloud.agent.api.Answer)84 Account (com.cloud.user.Account)84 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)82 URISyntaxException (java.net.URISyntaxException)82 ActionEvent (com.cloud.event.ActionEvent)70 TransactionStatus (com.cloud.utils.db.TransactionStatus)65 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)63 InternalErrorException (com.cloud.exception.InternalErrorException)57