Search in sources :

Example 1 with BaremetalPxeVO

use of com.cloud.baremetal.database.BaremetalPxeVO in project cloudstack by apache.

the class BaremetalPxeElement method canHandle.

private boolean canHandle(DeployDestination dest, TrafficType trafficType, GuestType networkType) {
    Pod pod = dest.getPod();
    if (pod != null && trafficType == TrafficType.Guest) {
        QueryBuilder<BaremetalPxeVO> sc = QueryBuilder.create(BaremetalPxeVO.class);
        sc.and(sc.entity().getPodId(), Op.EQ, pod.getId());
        return sc.find() != null;
    }
    return false;
}
Also used : Pod(com.cloud.dc.Pod) BaremetalPxeVO(com.cloud.baremetal.database.BaremetalPxeVO)

Example 2 with BaremetalPxeVO

use of com.cloud.baremetal.database.BaremetalPxeVO 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 3 with BaremetalPxeVO

use of com.cloud.baremetal.database.BaremetalPxeVO in project cloudstack by apache.

the class AddBaremetalPxeCmd method execute.

@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
    try {
        BaremetalPxeVO vo = pxeMgr.addPxeServer(this);
        BaremetalPxeResponse rsp = pxeMgr.getApiResponse(vo);
        rsp.setResponseName(getCommandName());
        this.setResponseObject(rsp);
    } catch (Exception e) {
        s_logger.warn("Unable to add external pxe server with url: " + getUrl(), e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    }
}
Also used : BaremetalPxeResponse(com.cloud.baremetal.networkservice.BaremetalPxeResponse) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) BaremetalPxeVO(com.cloud.baremetal.database.BaremetalPxeVO)

Example 4 with BaremetalPxeVO

use of com.cloud.baremetal.database.BaremetalPxeVO in project cloudstack by apache.

the class BaremetalKickStartServiceImpl method addPxeServer.

@Override
@DB
public BaremetalPxeVO addPxeServer(AddBaremetalPxeCmd cmd) {
    AddBaremetalKickStartPxeCmd kcmd = (AddBaremetalKickStartPxeCmd) cmd;
    PhysicalNetworkVO pNetwork = null;
    long zoneId;
    if (cmd.getPhysicalNetworkId() == null || cmd.getUrl() == null || cmd.getUsername() == null || cmd.getPassword() == null) {
        throw new IllegalArgumentException("At least one of the required parameters(physical network id, url, username, password) is null");
    }
    pNetwork = _physicalNetworkDao.findById(cmd.getPhysicalNetworkId());
    if (pNetwork == null) {
        throw new IllegalArgumentException("Could not find phyical network with ID: " + cmd.getPhysicalNetworkId());
    }
    zoneId = pNetwork.getDataCenterId();
    PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(pNetwork.getId(), BaremetalPxeManager.BAREMETAL_PXE_SERVICE_PROVIDER.getName());
    if (ntwkSvcProvider == null) {
        throw new CloudRuntimeException("Network Service Provider: " + BaremetalPxeManager.BAREMETAL_PXE_SERVICE_PROVIDER.getName() + " is not enabled in the physical network: " + cmd.getPhysicalNetworkId() + "to add this device");
    } else if (ntwkSvcProvider.getState() == PhysicalNetworkServiceProvider.State.Shutdown) {
        throw new CloudRuntimeException("Network Service Provider: " + ntwkSvcProvider.getProviderName() + " is in shutdown state in the physical network: " + cmd.getPhysicalNetworkId() + "to add this device");
    }
    List<HostVO> pxes = _resourceMgr.listAllHostsInOneZoneByType(Host.Type.BaremetalPxe, zoneId);
    if (!pxes.isEmpty()) {
        throw new IllegalArgumentException("Already had a PXE server zone: " + zoneId);
    }
    String tftpDir = kcmd.getTftpDir();
    if (tftpDir == null) {
        throw new IllegalArgumentException("No TFTP directory specified");
    }
    URI uri;
    try {
        uri = new URI(cmd.getUrl());
    } catch (Exception e) {
        s_logger.debug(e);
        throw new IllegalArgumentException(e.getMessage());
    }
    String ipAddress = uri.getHost();
    if (ipAddress == null) {
        ipAddress = cmd.getUrl();
    }
    String guid = getPxeServerGuid(Long.toString(zoneId), BaremetalPxeType.KICK_START.toString(), ipAddress);
    ServerResource resource = null;
    Map params = new HashMap<String, String>();
    params.put(BaremetalPxeService.PXE_PARAM_ZONE, Long.toString(zoneId));
    params.put(BaremetalPxeService.PXE_PARAM_IP, ipAddress);
    params.put(BaremetalPxeService.PXE_PARAM_USERNAME, cmd.getUsername());
    params.put(BaremetalPxeService.PXE_PARAM_PASSWORD, cmd.getPassword());
    params.put(BaremetalPxeService.PXE_PARAM_TFTP_DIR, tftpDir);
    params.put(BaremetalPxeService.PXE_PARAM_GUID, guid);
    resource = new BaremetalKickStartPxeResource();
    try {
        resource.configure("KickStart PXE resource", params);
    } catch (Exception e) {
        throw new CloudRuntimeException(e.getMessage(), e);
    }
    Host pxeServer = _resourceMgr.addHost(zoneId, resource, Host.Type.BaremetalPxe, params);
    if (pxeServer == null) {
        throw new CloudRuntimeException("Cannot add PXE server as a host");
    }
    BaremetalPxeVO vo = new BaremetalPxeVO();
    vo.setHostId(pxeServer.getId());
    vo.setNetworkServiceProviderId(ntwkSvcProvider.getId());
    vo.setPhysicalNetworkId(kcmd.getPhysicalNetworkId());
    vo.setDeviceType(BaremetalPxeType.KICK_START.toString());
    _pxeDao.persist(vo);
    return vo;
}
Also used : HashMap(java.util.HashMap) ServerResource(com.cloud.resource.ServerResource) Host(com.cloud.host.Host) URI(java.net.URI) HostVO(com.cloud.host.HostVO) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PhysicalNetworkServiceProviderVO(com.cloud.network.dao.PhysicalNetworkServiceProviderVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) AddBaremetalKickStartPxeCmd(org.apache.cloudstack.api.AddBaremetalKickStartPxeCmd) Map(java.util.Map) HashMap(java.util.HashMap) BaremetalPxeVO(com.cloud.baremetal.database.BaremetalPxeVO) DB(com.cloud.utils.db.DB)

Example 5 with BaremetalPxeVO

use of com.cloud.baremetal.database.BaremetalPxeVO in project cloudstack by apache.

the class BaremetalPxeManagerImpl method addUserData.

@Override
public boolean addUserData(NicProfile nic, VirtualMachineProfile profile) {
    UserVmVO vm = _vmDao.findById(profile.getVirtualMachine().getId());
    _vmDao.loadDetails(vm);
    String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId()).getDisplayText();
    String zoneName = _dcDao.findById(vm.getDataCenterId()).getName();
    NicVO nvo = _nicDao.findById(nic.getId());
    VmDataCommand cmd = new VmDataCommand(nvo.getIPv4Address(), vm.getInstanceName(), _ntwkModel.getExecuteInSeqNtwkElmtCmd());
    // if you add new metadata files, also edit systemvm/patches/debian/config/var/www/html/latest/.htaccess
    cmd.addVmData("userdata", "user-data", vm.getUserData());
    cmd.addVmData("metadata", "service-offering", StringUtils.unicodeEscape(serviceOffering));
    cmd.addVmData("metadata", "availability-zone", StringUtils.unicodeEscape(zoneName));
    cmd.addVmData("metadata", "local-ipv4", nic.getIPv4Address());
    cmd.addVmData("metadata", "local-hostname", StringUtils.unicodeEscape(vm.getInstanceName()));
    cmd.addVmData("metadata", "public-ipv4", nic.getIPv4Address());
    cmd.addVmData("metadata", "public-hostname", StringUtils.unicodeEscape(vm.getInstanceName()));
    cmd.addVmData("metadata", "instance-id", String.valueOf(vm.getUuid()));
    cmd.addVmData("metadata", "vm-id", String.valueOf(vm.getInstanceName()));
    cmd.addVmData("metadata", "public-keys", null);
    String cloudIdentifier = _configDao.getValue("cloud.identifier");
    if (cloudIdentifier == null) {
        cloudIdentifier = "";
    } else {
        cloudIdentifier = "CloudStack-{" + cloudIdentifier + "}";
    }
    cmd.addVmData("metadata", "cloud-identifier", cloudIdentifier);
    List<PhysicalNetworkVO> phys = _phynwDao.listByZone(vm.getDataCenterId());
    if (phys.isEmpty()) {
        throw new CloudRuntimeException(String.format("Cannot find physical network in zone %s", vm.getDataCenterId()));
    }
    if (phys.size() > 1) {
        throw new CloudRuntimeException(String.format("Baremetal only supports one physical network in zone, but zone %s has %s physical networks", vm.getDataCenterId(), phys.size()));
    }
    PhysicalNetworkVO phy = phys.get(0);
    QueryBuilder<BaremetalPxeVO> sc = QueryBuilder.create(BaremetalPxeVO.class);
    //TODO: handle both kickstart and PING
    //sc.addAnd(sc.getEntity().getPodId(), Op.EQ, vm.getPodIdToDeployIn());
    sc.and(sc.entity().getPhysicalNetworkId(), Op.EQ, phy.getId());
    BaremetalPxeVO pxeVo = sc.find();
    if (pxeVo == null) {
        throw new CloudRuntimeException("No PXE server found in pod: " + vm.getPodIdToDeployIn() + ", you need to add it before starting VM");
    }
    try {
        Answer ans = _agentMgr.send(pxeVo.getHostId(), cmd);
        if (!ans.getResult()) {
            s_logger.debug(String.format("Add userdata to vm:%s failed because %s", vm.getInstanceName(), ans.getDetails()));
            return false;
        } else {
            return true;
        }
    } catch (Exception e) {
        s_logger.debug(String.format("Add userdata to vm:%s failed", vm.getInstanceName()), e);
        return false;
    }
}
Also used : Answer(com.cloud.agent.api.Answer) UserVmVO(com.cloud.vm.UserVmVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VmDataCommand(com.cloud.agent.api.routing.VmDataCommand) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NicVO(com.cloud.vm.NicVO) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException) BaremetalPxeVO(com.cloud.baremetal.database.BaremetalPxeVO)

Aggregations

BaremetalPxeVO (com.cloud.baremetal.database.BaremetalPxeVO)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)4 Answer (com.cloud.agent.api.Answer)3 Host (com.cloud.host.Host)2 HostVO (com.cloud.host.HostVO)2 PhysicalNetworkServiceProviderVO (com.cloud.network.dao.PhysicalNetworkServiceProviderVO)2 ServerResource (com.cloud.resource.ServerResource)2 DB (com.cloud.utils.db.DB)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 IpmISetBootDevCommand (com.cloud.agent.api.baremetal.IpmISetBootDevCommand)1 PreparePxeServerAnswer (com.cloud.agent.api.baremetal.PreparePxeServerAnswer)1 PreparePxeServerCommand (com.cloud.agent.api.baremetal.PreparePxeServerCommand)1 VmDataCommand (com.cloud.agent.api.routing.VmDataCommand)1 BaremetalPxeResponse (com.cloud.baremetal.networkservice.BaremetalPxeResponse)1 HostPodVO (com.cloud.dc.HostPodVO)1 Pod (com.cloud.dc.Pod)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1