Search in sources :

Example 1 with PersistentInstance

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;
}
Also used : SshKey(org.platformlayer.ops.helpers.SshKey) DiskImageRecipe(org.platformlayer.images.model.DiskImageRecipe) PersistentInstance(org.platformlayer.instances.model.PersistentInstance)

Example 2 with PersistentInstance

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;
}
Also used : PersistentInstance(org.platformlayer.instances.model.PersistentInstance)

Example 3 with PersistentInstance

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);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) PersistentInstance(org.platformlayer.instances.model.PersistentInstance) OpsProvider(org.platformlayer.ops.OpsProvider) Tagger(org.platformlayer.ops.tagger.Tagger) TagChanges(org.platformlayer.core.model.TagChanges) Machine(org.platformlayer.ops.Machine)

Example 4 with PersistentInstance

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);
}
Also used : OpsException(org.platformlayer.ops.OpsException) PersistentInstance(org.platformlayer.instances.model.PersistentInstance) StateFilter(org.platformlayer.StateFilter) TagFilter(org.platformlayer.TagFilter) Filter(org.platformlayer.Filter)

Example 5 with PersistentInstance

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);
}
Also used : SshKey(org.platformlayer.ops.helpers.SshKey) OpsTarget(org.platformlayer.ops.OpsTarget) OpsException(org.platformlayer.ops.OpsException) PersistentInstance(org.platformlayer.instances.model.PersistentInstance) ItemBase(org.platformlayer.core.model.ItemBase) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Tag(org.platformlayer.core.model.Tag) InstanceBase(org.platformlayer.core.model.InstanceBase) Machine(org.platformlayer.ops.Machine) Handler(org.platformlayer.ops.Handler)

Aggregations

PersistentInstance (org.platformlayer.instances.model.PersistentInstance)5 OpsException (org.platformlayer.ops.OpsException)3 Machine (org.platformlayer.ops.Machine)2 SshKey (org.platformlayer.ops.helpers.SshKey)2 Filter (org.platformlayer.Filter)1 StateFilter (org.platformlayer.StateFilter)1 TagFilter (org.platformlayer.TagFilter)1 InstanceBase (org.platformlayer.core.model.InstanceBase)1 ItemBase (org.platformlayer.core.model.ItemBase)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 Tag (org.platformlayer.core.model.Tag)1 TagChanges (org.platformlayer.core.model.TagChanges)1 DiskImageRecipe (org.platformlayer.images.model.DiskImageRecipe)1 Handler (org.platformlayer.ops.Handler)1 OpsProvider (org.platformlayer.ops.OpsProvider)1 OpsTarget (org.platformlayer.ops.OpsTarget)1 Tagger (org.platformlayer.ops.tagger.Tagger)1