Search in sources :

Example 6 with SshKey

use of org.platformlayer.ops.helpers.SshKey in project platformlayer by platformlayer.

the class GitHelpers method getAdminTarget.

OpsTarget getAdminTarget(OpsTarget rootTarget, Machine machine) throws OpsException, IOException {
    String adminUser = "gitadmin";
    File adminHomeDir = new File("/home", adminUser);
    File adminSshDir = new File(adminHomeDir, ".ssh");
    File privateKeyFile = new File(adminSshDir, "id_rsa");
    // File publicKeyFile = new File(adminSshDir, "id_rsa.pub");
    // File authorizedKeys = new File(adminSshDir, "authorized_keys");
    String privateKeyData = rootTarget.readTextFile(privateKeyFile);
    SshKey adminSshKey = new SshKey(null, adminUser, KeyPairUtils.deserialize(privateKeyData));
    return machine.getTarget(adminSshKey);
}
Also used : SshKey(org.platformlayer.ops.helpers.SshKey) File(java.io.File)

Example 7 with SshKey

use of org.platformlayer.ops.helpers.SshKey in project platformlayer by platformlayer.

the class MemcachedServiceController method doOperation.

public void doOperation(Managed<MemcachedService> managed) throws OpsException, IOException {
    String key = managed.getConductorId();
    SshKey sshKey = service.getSshKey();
    Tag tag = new Tag(Tag.CONDUCTOR_ID, key);
    DiskImageRecipe recipe = imageFactory.loadDiskImageResource(getClass(), "DiskImageRecipe.xml");
    String securityGroup = service.getSecurityGroupName();
    // TODO: This needs to be configurable. Use tags?
    int minimumMemoryMb = 2048;
    Managed<PersistentInstance> foundPersistentInstance = persistentInstances.getOrCreate(tag, recipe, managed.getModel().dnsName, sshKey.getName(), securityGroup, minimumMemoryMb);
    OpenstackComputeMachine machine = persistentInstances.getMachine(foundPersistentInstance);
// KeyPair sshKey = sshKeys.getOrCreate(sshKeyName);
// OpsTarget target = machine.getTarget(sshKey);
//
// target.mkdir(new File("/opt/scripts"));
// target.setFileContents(new File("/opt/scripts/dnsdatabasemonitor"),
// ResourceUtils.loadString(getClass(), "dnsdatabasemonitor"));
// target.setFileContents(new
// File("/etc/monit/conf.d/dnsdatabasemonitor"),
// ResourceUtils.loadString(getClass(), "monitrc"));
}
Also used : SshKey(org.platformlayer.ops.helpers.SshKey) DiskImageRecipe(org.platformlayer.service.imagefactory.v1.DiskImageRecipe) PersistentInstance(org.platformlayer.service.instancesupervisor.v1.PersistentInstance) Tag(org.platformlayer.conductor.Tag) OpenstackComputeMachine(org.platformlayer.ops.OpenstackComputeMachine)

Example 8 with SshKey

use of org.platformlayer.ops.helpers.SshKey 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)

Example 9 with SshKey

use of org.platformlayer.ops.helpers.SshKey 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);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Instance(com.google.api.services.compute.model.Instance) GoogleCloudInstance(org.platformlayer.service.cloud.google.model.GoogleCloudInstance) PublicKey(java.security.PublicKey) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) MachineCreationRequest(org.platformlayer.ops.MachineCreationRequest) Operation(com.google.api.services.compute.model.Operation) SshKey(org.platformlayer.ops.helpers.SshKey) OpsTarget(org.platformlayer.ops.OpsTarget) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) GoogleComputeClient(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeClient) GoogleCloud(org.platformlayer.service.cloud.google.model.GoogleCloud) Tag(org.platformlayer.core.model.Tag) Tags(org.platformlayer.core.model.Tags) GoogleComputeMachine(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeMachine) TimeoutException(java.util.concurrent.TimeoutException) Handler(org.platformlayer.ops.Handler)

Example 10 with SshKey

use of org.platformlayer.ops.helpers.SshKey in project platformlayer by platformlayer.

the class GitHelpers method doForAllServers.

public void doForAllServers(GitosisOperation operation) throws Exception {
    boolean failed = false;
    for (Managed<GitServer> gitServer : platformLayer.listItems(GitServer.class)) {
        if (gitServer.getState() != ManagedItemState.ACTIVE) {
            log.warn("Server not yet active: " + gitServer);
            failed = true;
            continue;
        }
        OpenstackComputeMachine machine = instances.findMachine(gitServer);
        if (machine == null) {
            log.warn("Server instance not found: " + gitServer);
            failed = true;
            continue;
        }
        SshKey sshKey = service.getSshKey();
        OpsTarget rootTarget = machine.getTarget(sshKey);
        OpsTarget adminTarget = getAdminTarget(rootTarget, machine);
        doOperation(adminTarget, operation);
    }
    if (failed) {
        throw new OpsException("Could not update all DNS servers in cluster").setRetry(TimeSpan.ONE_MINUTE);
    }
}
Also used : SshKey(org.platformlayer.ops.helpers.SshKey) OpsTarget(org.platformlayer.ops.OpsTarget) OpsException(org.platformlayer.ops.OpsException) GitServer(org.platformlayer.service.gitosis.model.GitServer) OpenstackComputeMachine(org.platformlayer.ops.OpenstackComputeMachine)

Aggregations

SshKey (org.platformlayer.ops.helpers.SshKey)14 OpsTarget (org.platformlayer.ops.OpsTarget)10 OpsException (org.platformlayer.ops.OpsException)7 Handler (org.platformlayer.ops.Handler)5 Machine (org.platformlayer.ops.Machine)5 PublicKey (java.security.PublicKey)4 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)4 File (java.io.File)3 Tag (org.platformlayer.core.model.Tag)3 Tags (org.platformlayer.core.model.Tags)3 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)3 OpaqueMachine (org.platformlayer.ops.OpaqueMachine)3 OpenstackComputeMachine (org.platformlayer.ops.OpenstackComputeMachine)3 Tag (org.platformlayer.conductor.Tag)2 DiskImageRecipe (org.platformlayer.images.model.DiskImageRecipe)2 PersistentInstance (org.platformlayer.instances.model.PersistentInstance)2 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)2 GitServer (org.platformlayer.service.gitosis.model.GitServer)2 Instance (com.google.api.services.compute.model.Instance)1 Operation (com.google.api.services.compute.model.Operation)1