Search in sources :

Example 1 with Machine

use of org.platformlayer.ops.Machine in project platformlayer by platformlayer.

the class InstanceHelpers method getMachines.

public List<Machine> getMachines(ItemBase item, boolean required) throws OpsException {
    Class<? extends ItemBase> javaClass = item.getClass();
    ModelClass<?> modelClass = serviceProviderDictionary.getModelClass(javaClass);
    Object controller = modelClass.getProvider().getController(item);
    // TODO: Should we just recurse down through children?
    if (controller instanceof MachineCluster) {
        MachineCluster machineCluster = (MachineCluster) controller;
        return machineCluster.getMachines(required);
    } else {
        Machine machine = getMachine(item, required);
        if (machine == null) {
            return Collections.emptyList();
        }
        return Collections.singletonList(machine);
    }
}
Also used : Machine(org.platformlayer.ops.Machine)

Example 2 with Machine

use of org.platformlayer.ops.Machine in project platformlayer by platformlayer.

the class ForEach method doRecursion.

public void doRecursion(Object controller, SshKey sshKey, Class<? extends ItemBase> machineItemClass, Class<? extends ItemBase> dataItemClass) throws OpsException {
    boolean failed = false;
    OpsContext ops = OpsContext.get();
    List<ItemBase> dataItems = Lists.newArrayList();
    ItemBase contextDataItem = ops.getInstance(dataItemClass);
    if (contextDataItem != null) {
        dataItems.add(contextDataItem);
    } else {
        for (ItemBase dataItem : platformLayer.listItems(dataItemClass)) {
            dataItems.add(dataItem);
        }
    }
    Object contextMachine = ops.getInstance(machineItemClass);
    if (contextMachine != null) {
        // We are presumably building the machine item
        PlatformLayerKey targetItemKey = ops.getJobRecord().getTargetItemKey();
        ItemBase machineItem = (ItemBase) contextMachine;
        if (!Objects.equal(targetItemKey, machineItem.getKey())) {
            throw new OpsException("Expected to find same model");
        }
        Machine machine = instances.findMachine(machineItem);
        if (machine == null) {
            log.warn("Server instance not found: " + contextMachine);
            failed = true;
        } else {
            OpsTarget target = machine.getTarget(sshKey);
            failed |= processDataItems(controller, dataItems, machineItem, machine, target);
        }
    } else {
        // We are building a data item
        for (ItemBase machineItem : platformLayer.listItems(machineItemClass)) {
            if (machineItem.getState() != ManagedItemState.ACTIVE) {
                log.warn("Machine not yet active: " + machineItem);
                failed = true;
                continue;
            }
            Machine machine = instances.findMachine(machineItem);
            if (machine == null) {
                log.warn("Server instance not found: " + machineItem);
                failed = true;
                continue;
            }
            OpsTarget target = machine.getTarget(sshKey);
            failed |= processDataItems(controller, dataItems, machineItem, machine, target);
        }
    }
    if (failed) {
        throw new OpsException("Could not update all servers").setRetry(TimeSpan.ONE_MINUTE);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) OpsTarget(org.platformlayer.ops.OpsTarget) ItemBase(org.platformlayer.core.model.ItemBase) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) OpsContext(org.platformlayer.ops.OpsContext) Machine(org.platformlayer.ops.Machine)

Example 3 with Machine

use of org.platformlayer.ops.Machine in project platformlayer by platformlayer.

the class OpenstackBackupContextFactory method build.

@Override
public OpenstackBackupContext build(ItemBase item) throws OpsException {
    Machine machine = instances.findMachine(item);
    if (machine == null) {
        throw new OpsException("Cannot determine machine for: " + item);
    }
    StorageConfiguration storageConfiguration = cloud.getStorageConfiguration(machine);
    return build(storageConfiguration);
}
Also used : OpsException(org.platformlayer.ops.OpsException) OpenstackStorageConfiguration(org.platformlayer.service.cloud.openstack.ops.OpenstackStorageConfiguration) StorageConfiguration(org.platformlayer.ops.machines.StorageConfiguration) Machine(org.platformlayer.ops.Machine)

Example 4 with Machine

use of org.platformlayer.ops.Machine in project platformlayer by platformlayer.

the class ImageStoreController method getImageStore.

@Override
public org.platformlayer.ops.images.ImageStore getImageStore() throws OpsException {
    String endpoint = model.getTags().findUnique("endpoint");
    if (endpoint == null) {
        log.warn("ImageStore not yet active: " + model);
        return null;
    }
    URI url;
    try {
        url = new URI(endpoint);
    } catch (URISyntaxException e) {
        throw new OpsException("Cannot parse endpoint: " + endpoint, e);
    }
    if (url.getScheme().equals("ssh")) {
        String myAddress = url.getHost();
        Machine machine = new OpaqueMachine(NetworkPoint.forPublicHostname(myAddress));
        // This is nasty; we're in the context of another service here...
        SshKey sshKey = sshKeys.findOtherServiceKey(new ServiceType("imagestore"));
        OpsTarget target = machine.getTarget("imagestore", sshKey.getKeyPair());
        DirectImageStore directImageStore = OpsContext.get().getInjector().getInstance(DirectImageStore.class);
        directImageStore.connect(target);
        return directImageStore;
    } else {
        throw new OpsException("Unknown protocol for endpoint: " + endpoint);
    }
}
Also used : SshKey(org.platformlayer.ops.helpers.SshKey) OpsException(org.platformlayer.ops.OpsException) OpsTarget(org.platformlayer.ops.OpsTarget) ServiceType(org.platformlayer.ids.ServiceType) OpaqueMachine(org.platformlayer.ops.OpaqueMachine) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Machine(org.platformlayer.ops.Machine) OpaqueMachine(org.platformlayer.ops.OpaqueMachine) DirectImageStore(org.platformlayer.ops.images.direct.DirectImageStore)

Example 5 with Machine

use of org.platformlayer.ops.Machine in project platformlayer by platformlayer.

the class PersistentInstanceMapper method handler.

@Handler
public void handler(PersistentInstance model) throws OpsException {
    Machine machine = null;
    Tag tagForInstance = Tag.buildParentTag(model.getKey());
    // boolean instanceIsTagged = false;
    // See if we have an instance id tag
    {
    // String instanceKey = model.getTags().findUnique(Tag.INSTANCE_KEY);
    // if (instanceKey != null) {
    // InstanceBase foundInstance = cloud.findInstanceByInstanceKey(PlatformLayerKey.parse(instanceKey));
    // if (foundInstance == null) {
    // throw new IllegalStateException("Tagged with instance id, but instance not found: " + instanceKey);
    // }
    // if (foundInstance.getState() == ManagedItemState.DELETED) {
    // log.warn("Found deleted instance: " + foundInstance);
    // } else {
    // machine = cloudHelpers.toMachine(foundInstance);
    // if (machine == null) {
    // throw new IllegalStateException();
    // }
    // }
    // 
    // // instanceIsTagged = true;
    // }
    }
    if (machine == null) {
        // We may have created a machine, but failed to tag the instance
        machine = cloud.findMachine(tagForInstance);
    }
    if (!OpsContext.isDelete()) {
        // We always PUT the machine
        // TODO: Check if unchanged??
        MachineCreationRequest request = buildMachineCreationRequest(model);
        request.tags.add(tagForInstance);
        machine = cloudHelpers.putInstanceByTag(request, model.getKey(), tagForInstance);
    // if (machine == null) {
    // // No machine
    // MachineCreationRequest request = buildMachineCreationRequest(model);
    // request.tags.add(tagForInstance);
    // 
    // machine = cloudHelpers.createInstance(request, model.getKey(), tagForInstance);
    // }
    } else {
        if (machine != null) {
            cloudHelpers.terminateMachine(machine);
        }
    }
    getRecursionState().pushChildScope(Machine.class, machine);
}
Also used : Tag(org.platformlayer.core.model.Tag) MachineCreationRequest(org.platformlayer.ops.MachineCreationRequest) Machine(org.platformlayer.ops.Machine) Handler(org.platformlayer.ops.Handler)

Aggregations

Machine (org.platformlayer.ops.Machine)27 OpsException (org.platformlayer.ops.OpsException)13 OpsTarget (org.platformlayer.ops.OpsTarget)12 ItemBase (org.platformlayer.core.model.ItemBase)7 NetworkPoint (org.platformlayer.ops.networks.NetworkPoint)7 OpsContext (org.platformlayer.ops.OpsContext)5 SshKey (org.platformlayer.ops.helpers.SshKey)5 InetAddress (java.net.InetAddress)4 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)4 Tag (org.platformlayer.core.model.Tag)4 BindingScope (org.platformlayer.ops.BindingScope)4 Handler (org.platformlayer.ops.Handler)4 OpaqueMachine (org.platformlayer.ops.OpaqueMachine)4 List (java.util.List)3 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 TagChanges (org.platformlayer.core.model.TagChanges)2 Tags (org.platformlayer.core.model.Tags)2 ServiceType (org.platformlayer.ids.ServiceType)2