use of org.platformlayer.ops.MachineCreationRequest in project platformlayer by platformlayer.
the class CloudInstanceMapper method doOperation.
@Handler
public void doOperation() throws OpsException, IOException {
Tags instanceTags = instance.getTags();
OpenstackCloud cloud = findCloud();
if (cloud == null) {
throw new OpsException("Could not find cloud");
}
OpenstackComputeClient computeClient = openstack.getComputeClient(cloud);
getRecursionState().pushChildScope(cloud);
List<String> assignedInstanceIds = instanceTags.findAll(Tag.ASSIGNED);
if (assignedInstanceIds.isEmpty()) {
if (createInstance && !OpsContext.isDelete()) {
MachineCreationRequest request = buildMachineCreationRequest();
PlatformLayerKey instanceKey = instance.getKey();
request.tags.add(Tag.buildParentTag(instanceKey));
String serverName = buildServerName();
Server created = openstack.createInstance(cloud, serverName, request);
{
Tag instanceTag = Tag.build(Tag.ASSIGNED, created.getId());
platformLayer.addTag(instance.getKey(), instanceTag);
}
assignedInstanceIds.add(created.getId());
}
}
if (assignedInstanceIds.isEmpty() && !OpsContext.isDelete()) {
throw new OpsException("Instance not yet assigned");
}
Machine machine = null;
OpsTarget target = null;
if (!assignedInstanceIds.isEmpty()) {
if (assignedInstanceIds.size() != 1) {
log.warn("Multiple instance ids found: " + assignedInstanceIds);
}
// We just take the first instance id
String assignedInstanceId = Iterables.getFirst(assignedInstanceIds, null);
Server server = openstack.findServerById(cloud, assignedInstanceId);
if (server == null) {
if (OpsContext.isConfigure()) {
throw new OpsException("Unable to find assigned server: " + assignedInstanceId);
}
} else {
server = openstack.ensureHasPublicIp(cloud, server);
AsyncServerOperation powerOnOperation = openstack.ensurePoweredOn(cloud, server);
if (powerOnOperation != null) {
waitOperation(powerOnOperation);
}
machine = new OpenstackComputeMachine(openstack, cloud, server);
SshKey sshKey = service.getSshKey();
target = machine.getTarget(sshKey);
}
}
if (!assignedInstanceIds.isEmpty() && OpsContext.isDelete()) {
CloudBehaviours cloudBehaviours = new CloudBehaviours(cloud);
boolean supportsSecurityGroups = cloudBehaviours.supportsSecurityGroups();
for (String instanceId : assignedInstanceIds) {
Server server = openstack.findServerById(cloud, instanceId);
if (server == null) {
log.warn("Could not find assigned server: " + instanceId + ", ignoring");
continue;
}
SecurityGroup securityGroup = null;
if (supportsSecurityGroups) {
securityGroup = openstackHelpers.getMachineSecurityGroup(computeClient, server);
}
AsyncServerOperation terminateOperation = openstack.terminateInstance(cloud, instanceId);
if (securityGroup != null) {
// We need to terminate the instance before we delete the security group it uses
if (terminateOperation != null) {
waitOperation(terminateOperation);
}
try {
log.info("Deleting security group: " + securityGroup.getId());
computeClient.root().securityGroups().securityGroup(securityGroup.getId()).delete();
} catch (OpenstackNotFoundException e) {
log.info("Ignoring not-found error while deleting security group: " + securityGroup.getId());
}
}
}
}
RecursionState recursion = getRecursionState();
if (OpsContext.isDelete() && machine == null) {
recursion.setPreventRecursion(true);
} else {
recursion.pushChildScope(machine);
recursion.pushChildScope(target);
}
}
use of org.platformlayer.ops.MachineCreationRequest in project platformlayer by platformlayer.
the class PersistentInstanceMapper method buildMachineCreationRequest.
private MachineCreationRequest buildMachineCreationRequest(PersistentInstance model) throws OpsException {
MachineCreationRequest request = new MachineCreationRequest();
try {
request.sshPublicKey = OpenSshUtils.readSshPublicKey(model.sshPublicKey);
} catch (IOException e) {
throw new OpsException("Error reading sshPublicKey", e);
}
request.cloud = model.cloud;
request.hostname = model.dnsName;
request.hostPolicy = model.hostPolicy;
Tags tags = new Tags();
tags.addAll(Tag.HOST_POLICY.filter(tags));
request.tags = tags;
if (model.securityGroup != null) {
request.securityGroups = Lists.newArrayList();
request.securityGroups.add(model.securityGroup);
}
request.minimumMemoryMB = model.minimumRam;
request.recipeId = model.recipe;
request.publicPorts = model.publicPorts;
return request;
}
Aggregations