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;
}
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;
}
}
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());
}
}
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;
}
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;
}
}
Aggregations