use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class PackageDependency method doOperation.
@Handler
public void doOperation() throws OpsException {
if (OpsContext.isDelete()) {
// Should we delete the packages? Probably not, because others may also need them
log.debug("Not removing package on delete (in case someone else also uses it)");
}
if (OpsContext.isConfigure() || OpsContext.isValidate()) {
OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
List<String> installedPackages = apt.getInstalledPackageInfo(target);
if (!installedPackages.contains(packageName)) {
log.info("Package not installed: " + packageName);
if (OpsContext.isValidate()) {
throw new OpsException("Package not installed: " + packageName);
}
if (repositoryKey != null) {
apt.addRepositoryKeyUrl(target, repositoryKey.getUrl());
}
if (repository != null) {
apt.addRepository(target, repository.getKey(), repository.getSource());
}
if (configuration != null) {
apt.preconfigurePackages(target, configuration);
}
// TODO: Only update once per operation?
// I think we do want to update aggressively though, because we want to be sure we're up to date
// as that could well be the reason we're running the operation!
// We definitely want to update if we added a repository etc above
apt.update(target, false);
apt.install(target, packageName);
} else {
log.debug("Package is installed: " + packageName);
}
}
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class ImageFactory method getOrCreateImage.
public DiskImage getOrCreateImage(DiskImage template) throws OpsException {
DiskImage best = null;
try {
for (DiskImage candidate : platformLayer.listItems(DiskImage.class)) {
if (isMatch(candidate, template)) {
best = candidate;
break;
}
}
if (best == null) {
// We should be owned by the recipe
PlatformLayerKey recipeKey = template.getRecipeId();
if (recipeKey != null) {
template.getTags().add(Tag.buildParentTag(recipeKey));
}
best = platformLayer.putItem(template);
}
} catch (PlatformLayerClientException e) {
throw new OpsException("Error fetching or building image", e);
}
return best;
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class PlatformLayerHelpers method setUniqueTags.
public void setUniqueTags(PlatformLayerKey key, Iterable<Tag> uniqueTags) throws OpsException {
Tags tags = getItemTags(key);
TagChanges tagChanges = new TagChanges();
for (Tag setTag : uniqueTags) {
String tagKey = setTag.getKey();
List<String> existing = tags.findAll(tagKey);
if (existing == null || existing.isEmpty()) {
tagChanges.addTags.add(setTag);
} else if (existing.size() == 1) {
String existingValue = existing.get(0);
if (!Objects.equal(existingValue, setTag.value)) {
tagChanges.addTags.add(setTag);
tagChanges.removeTags.add(Tag.build(tagKey, existingValue));
}
} else {
// We probably should replace existing tags...
throw new OpsException("Found duplicate tag for: " + setTag.key);
}
}
if (!tagChanges.isEmpty()) {
changeTags(key, tagChanges);
}
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class SolrSchemaFile method getContentsBytes.
@Override
protected byte[] getContentsBytes() throws OpsException {
InputStream is = getClass().getResourceAsStream("schema.xml");
Document dom;
try {
boolean isNamespaceAware = true;
dom = XmlHelper.parseXmlDocument(is, isNamespaceAware);
} catch (ParserConfigurationException e) {
throw new OpsException("Error parsing XML template", e);
} catch (SAXException e) {
throw new OpsException("Error parsing XML template", e);
} catch (IOException e) {
throw new OpsException("Error parsing XML template", e);
}
SolrTemplateData template = OpsContext.get().getInjector().getInstance(SolrTemplateData.class);
Element fieldsElement;
{
NodeList fieldsList = dom.getElementsByTagName("fields");
if (fieldsList.getLength() != 1) {
throw new OpsException("Expected exactly one fields element");
}
fieldsElement = (Element) fieldsList.item(0);
}
// TODO: Turn off default dynamic fields??
for (SolrSchemaField field : template.getFields()) {
boolean isDynamic = field.name.contains("*");
Element el = dom.createElement(isDynamic ? "dynamicField" : "field");
el.setAttribute("name", field.name);
el.setAttribute("type", field.type);
el.setAttribute("indexed", String.valueOf(field.indexed));
el.setAttribute("stored", String.valueOf(field.stored));
el.setAttribute("multiValued", String.valueOf(field.multiValued));
// Compression removed in 1.4.1
// if (field.compressThreshold >= 0) {
// el.setAttribute("compressed", "true");
// el.setAttribute("compressThreshold", String.valueOf(field.compressThreshold));
// }
fieldsElement.appendChild(el);
}
return Utf8.getBytes(DomUtils.toXml(dom));
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class CloudInstanceMapper method doOperation.
@Handler
public void doOperation() throws OpsException, IOException {
Tags instanceTags = instance.getTags();
GoogleCloud cloud = findCloud();
if (cloud == null) {
throw new OpsException("Could not find cloud");
}
GoogleComputeClient computeClient = googleComputeClientFactory.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));
PublicKey servicePublicKey = service.getSshKey().getKeyPair().getPublic();
Instance created = computeClient.createInstance(cloud, request, servicePublicKey);
{
Tag instanceTag = Tag.build(Tag.ASSIGNED, created.getName());
platformLayer.addTag(instance.getKey(), instanceTag);
}
assignedInstanceIds.add(created.getName());
}
}
if (assignedInstanceIds.isEmpty() && !OpsContext.isDelete()) {
throw new OpsException("Instance not yet assigned");
}
GoogleComputeMachine 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);
Instance server = computeClient.findInstanceByName(assignedInstanceId);
if (server == null) {
if (OpsContext.isConfigure()) {
throw new OpsException("Unable to find assigned server: " + assignedInstanceId);
}
} else {
server = computeClient.ensureHasPublicIp(server);
machine = new GoogleComputeMachine(computeClient, cloud, server);
SshKey sshKey = service.getSshKey();
target = machine.getTarget(GoogleComputeClient.USER_NAME, sshKey.getKeyPair());
// We need to use sudo while we set up root access
((SshOpsTarget) target).setEnsureRunningAsRoot(true);
}
}
if (!assignedInstanceIds.isEmpty() && OpsContext.isDelete()) {
for (String instanceId : assignedInstanceIds) {
Instance server = computeClient.findInstanceByName(instanceId);
if (server == null) {
log.warn("Could not find assigned server: " + instanceId + ", ignoring");
continue;
}
// TODO: Remove associated firewall rules
log.warn("Deleting firewall rules not yet implemented");
// SecurityGroup securityGroup = null;
// if (supportsSecurityGroups) {
// securityGroup = openstackHelpers.getMachineSecurityGroup(computeClient, server);
// }
Operation terminateOperation = computeClient.terminateInstance(instanceId);
try {
computeClient.waitComplete(terminateOperation, 5, TimeUnit.MINUTES);
} catch (TimeoutException e) {
throw new OpsException("Timeout while waiting for instance termination", e);
}
// 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());
// }
// }
}
if (machine != null) {
machine.refreshState();
}
}
RecursionState recursion = getRecursionState();
if (OpsContext.isDelete() && machine == null) {
recursion.setPreventRecursion(true);
} else {
recursion.pushChildScope(machine);
recursion.pushChildScope(target);
}
}
Aggregations