use of org.platformlayer.instances.model.PersistentInstance in project platformlayer by platformlayer.
the class InstanceBuilder method buildPersistentInstanceTemplate.
private PersistentInstance buildPersistentInstanceTemplate() throws OpsException {
SshKey sshKey = service.getSshKey();
String securityGroup = service.getSecurityGroupName();
DiskImageRecipe recipeTemplate = diskImageRecipe.get();
if (recipeTemplate.getKey() == null) {
// TODO: Something nicer than a UUID
String recipeId = UUID.randomUUID().toString();
recipeTemplate.setKey(PlatformLayerKey.fromId(recipeId));
}
DiskImageRecipe recipe = imageFactory.getOrCreateRecipe(recipeTemplate);
PersistentInstance persistentInstanceTemplate = new PersistentInstance();
persistentInstanceTemplate.setDnsName(dnsName);
persistentInstanceTemplate.setSshPublicKey(SshKeys.serialize(sshKey.getKeyPair().getPublic()));
persistentInstanceTemplate.setSecurityGroup(securityGroup);
persistentInstanceTemplate.setMinimumRam(minimumMemoryMb);
persistentInstanceTemplate.setCloud(cloud);
persistentInstanceTemplate.setHostPolicy(hostPolicy);
persistentInstanceTemplate.setRecipe(recipe.getKey());
String id = dnsName;
if (Strings.isNullOrEmpty(id)) {
id = UUID.randomUUID().toString();
}
persistentInstanceTemplate.setKey(PlatformLayerKey.fromId(id));
for (int publicPort : publicPorts) {
persistentInstanceTemplate.getPublicPorts().add(publicPort);
}
return persistentInstanceTemplate;
}
use of org.platformlayer.instances.model.PersistentInstance in project platformlayer by platformlayer.
the class InstanceBuilder method getOrCreate.
PersistentInstance getOrCreate(Tag tag, PersistentInstance persistentInstance) throws PlatformLayerClientException, OpsException {
PersistentInstance foundPersistentInstance = instanceSupervisor.findPersistentInstance(tag);
if (!OpsContext.isDelete()) {
// We always PUT it (should be idempotent)
// if (foundPersistentInstance == null) {
PersistentInstance created = platformLayer.putItemByTag(persistentInstance, tag);
foundPersistentInstance = created;
// }
// if (foundPersistentInstance == null) {
// // String imageId = imageFactory.getOrCreateImage(recipe);
// // persistentInstance.setImageId(imageId);
//
// Tags tags = persistentInstance.getTags();
// tags.add(tag);
//
// try {
// // TODO: Parent tag isn't getting set??
// PersistentInstance created = platformLayer.createItem(persistentInstance);
// foundPersistentInstance = created;
// } catch (PlatformLayerClientException e) {
// throw new OpsException("Error registering persistent instance", e);
// }
// }
//
// if (foundPersistentInstance == null) {
// throw new IllegalStateException();
// }
}
if (OpsContext.isDelete()) {
if (foundPersistentInstance != null) {
platformLayer.ensureDeleted(foundPersistentInstance);
}
}
return foundPersistentInstance;
}
use of org.platformlayer.instances.model.PersistentInstance in project platformlayer by platformlayer.
the class PersistentInstanceMapper method addChildren.
@Override
protected void addChildren() throws OpsException {
final PersistentInstance model = OpsContext.get().getInstance(PersistentInstance.class);
{
// Add tag with instance id to persistent instance (very helpful for DNS service!)
Tagger tagger = injected(Tagger.class);
tagger.platformLayerKey = model.getKey();
tagger.tagChangesProvider = new OpsProvider<TagChanges>() {
@Override
public TagChanges get() throws OpsException {
Machine machine = OpsContext.get().getInstance(Machine.class);
if (machine == null) {
if (OpsContext.isDelete()) {
return null;
}
throw new OpsException("No machine in scope");
}
TagChanges changeTags = new TagChanges();
changeTags.addTags.add(Tag.INSTANCE_KEY.build(machine.getKey()));
platformLayer.changeTags(model.getKey(), changeTags, null);
return changeTags;
}
};
addChild(tagger);
}
}
use of org.platformlayer.instances.model.PersistentInstance in project platformlayer by platformlayer.
the class InstanceSupervisor method findPersistentInstance.
public PersistentInstance findPersistentInstance(Tag tag) throws OpsException {
Filter filter = TagFilter.byTag(tag);
filter = StateFilter.excludeDeleted(filter);
List<PersistentInstance> instances = Lists.newArrayList(platformLayer.listItems(PersistentInstance.class, filter));
if (instances.size() == 0) {
return null;
}
if (instances.size() != 1) {
throw new OpsException("Found multiple instances with tag: " + tag);
}
return instances.get(0);
}
use of org.platformlayer.instances.model.PersistentInstance in project platformlayer by platformlayer.
the class InstanceBuilder method doOperation.
@Handler
public void doOperation() throws OpsException, IOException {
ItemBase item = ops.getInstance(ItemBase.class);
Tag parentTag = Tag.buildParentTag(item.getKey());
PersistentInstance persistentInstanceTemplate = buildPersistentInstanceTemplate();
persistentInstanceTemplate.getTags().add(parentTag);
// Set during doOperation
Machine machine = null;
PersistentInstance persistentInstance = null;
InstanceBase instance = null;
OpsTarget target = null;
persistentInstance = getOrCreate(parentTag, persistentInstanceTemplate);
if (persistentInstance != null) {
// We have to connect to the underlying machine not-via-DNS for Dns service => use instance id
// TODO: Should we always use the instance id??
instance = instances.findInstance(persistentInstance);
if (instance == null && !OpsContext.isDelete()) {
// A machine has not (yet) been assigned
throw new OpsException("Machine is not yet built").setRetry(TimeSpan.ONE_MINUTE);
}
}
if (instance != null) {
machine = cloudHelpers.toMachine(instance);
}
if (addTagToManaged && !OpsContext.isDelete()) {
// Add tag with instance id to persistent instance (very helpful for
// DNS service!)
PlatformLayerKey machineKey = machine.getKey();
platformLayer.addTag(item.getKey(), Tag.INSTANCE_KEY.build(machineKey));
}
SshKey sshKey = service.getSshKey();
if (machine != null) {
if (OpsContext.isDelete()) {
target = null;
machine = null;
} else {
target = machine.getTarget(sshKey);
}
}
RecursionState recursion = getRecursionState();
if (OpsContext.isDelete() && machine == null) {
// Don't recurse into no machine :-)
recursion.setPreventRecursion(true);
}
recursion.pushChildScope(Machine.class, machine);
recursion.pushChildScope(PersistentInstance.class, persistentInstance);
recursion.pushChildScope(InstanceBase.class, instance);
recursion.pushChildScope(OpsTarget.class, target);
}
Aggregations