use of org.openstack.client.OpenstackNotFoundException in project platformlayer by platformlayer.
the class OpenstackCasStore method findArtifact.
@Override
public CasStoreObject findArtifact(Md5Hash hash) {
OpenstackStorageClient storageClient = getStorageClient();
try {
List<StorageObject> storageObjects = Lists.newArrayList(storageClient.listObjects(containerName, null, null));
String findHash = hash.toHex();
for (StorageObject storageObject : storageObjects) {
String storageObjectHash = storageObject.getHash();
if (storageObjectHash.equalsIgnoreCase(findHash)) {
return new OpenstackCasObject(storageObject);
}
}
} catch (OpenstackNotFoundException e) {
log.debug("Not found (404) returned from Openstack");
return null;
}
return null;
}
use of org.openstack.client.OpenstackNotFoundException 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.openstack.client.OpenstackNotFoundException in project platformlayer by platformlayer.
the class OpenstackCloudContext method terminateInstance.
public AsyncServerOperation terminateInstance(OpenstackCloud cloud, String instanceId) throws OpsException {
try {
OpenstackComputeClient computeClient = getComputeClient(cloud);
log.info("Terminating server: " + instanceId);
AsyncServerOperation deleteOperation = computeClient.deleteServer(instanceId);
return deleteOperation;
} catch (OpenstackNotFoundException e) {
log.info("Could not find instance to be terminated, assuming already terminated: " + instanceId);
return null;
} catch (OpenstackException e) {
throw new OpsException("Error terminating server", e);
}
}
use of org.openstack.client.OpenstackNotFoundException in project platformlayer by platformlayer.
the class OpenstackCloudContext method findServerById.
// private AmazonEC2Client buildEc2Client() throws OpsException {
// OpsContext opsContext = OpsContext.get();
//
// AmazonEC2Client amazonEC2Client = opsContext.getUserInfo().buildEc2Client();
//
// return amazonEC2Client;
// }
//
// // // sshKeyPair = cloud.generateSshKeyPair(sshKeyName);
// // public KeyPair generateSshKeyPair(String sshKeyName) throws OpsException {
// // KeyPair sshKeyPair;
// //
// // try {
// // // OpenStack EC2 api does not support import
// // // // We create a new keypair each time... we don't want the user
// // // logging in
// // // KeyPair sshKeyPair = generateKeyPair("RSA", null);
// //
// // // ImportKeyPairRequest importKeyPairRequest = new
// // // ImportKeyPairRequest();
// // // importKeyPairRequest.setPublicKeyMaterial(publicKeyMaterial);
// // // ImportKeyPairResult importKeyPairResult =
// // // computeClient.importKeyPair(importKeyPairRequest);
// // // sshKeyName = importKeyPairResult.getKeyName();
// // AmazonEC2Client ec2Api = buildEc2Client();
// //
// // CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest(sshKeyName);
// // CreateKeyPairResult createKeyPairResult = ec2Api.createKeyPair(createKeyPairRequest);
// //
// // sshKeyPair = extractKeyPair(createKeyPairResult);
// //
// // return sshKeyPair;
// // } catch (AmazonClientException e) {
// // throw new OpsException("Error building server", e);
// // }
// // }
//
public Server findServerById(OpenstackCloud cloud, String serverId) throws OpsException {
OpenstackComputeClient computeClient = getComputeClient(cloud);
try {
log.info("Getting server info for: " + serverId);
Server server = computeClient.root().servers().server(serverId).show();
return server;
} catch (OpenstackNotFoundException e) {
return null;
} catch (OpenstackException e) {
throw new OpsException("Error getting server", e);
}
}
Aggregations