Search in sources :

Example 26 with OpsTarget

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

the class NexusApp method handler.

@Handler
public void handler() throws IOException, OpsException {
    // TODO: This needs to be idempotent
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    String url = "http://nexus.sonatype.org/downloads/all/nexus-webapp-1.9.2.4.war";
    File warFile = new File("/var/lib/jetty/wars/nexus-webapp-1.9.2.4.war");
    target.executeCommand("wget {0} -O {1}", url, warFile);
    // Whatever version of nexus we have, we want it to be the root
    target.symlink(warFile, new File("/var/lib/jetty/webapps/root.war"), false);
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) File(java.io.File) Handler(org.platformlayer.ops.Handler)

Example 27 with OpsTarget

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

the class NexusBootstrap method handler.

@Handler
public void handler() throws OpsException, IOException {
    // TODO: This needs to be idempotent
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    // Nexus needs a workdir; by default it's in the home directory of the user we're running under
    // With jetty, the jetty user can't create this directory; we do it
    File sonatypeDir = new File("/usr/share/jetty/sonatype-work");
    target.mkdir(sonatypeDir, "750");
    File nexusDir = new File(sonatypeDir, "nexus");
    target.mkdir(nexusDir, "750");
    File confDir = new File(nexusDir, "conf");
    target.mkdir(confDir, "750");
    {
        String contents = ResourceUtils.get(getClass(), "conf/security.xml");
        FileUpload.upload(target, new File(confDir, "security.xml"), contents);
    }
    {
        String contents = ResourceUtils.get(getClass(), "conf/security-configuration.xml");
        FileUpload.upload(target, new File(confDir, "security-configuration.xml"), contents);
    }
    {
        // TODO: Bind with a low-privilege account
        // TODO: Don't hard-code this stuff
        String ldapHost = "192.168.192.67";
        String ldapDomain = "dc=com,dc=fathomscale";
        String ldapUsername = "cn=Manager," + ldapDomain;
        String ldapPassword = "adminsecret";
        Map<String, Object> vars = Maps.newHashMap();
        vars.put("searchBase", ldapDomain);
        vars.put("systemUsername", ldapUsername);
        vars.put("systemPassword", encryptNexusPassword(ldapPassword));
        vars.put("ldapHost", ldapHost);
        // TODO: This is a bit limiting; we should use memberOf
        // Avoids escaping ${username}
        vars.put("groupMemberFormat", "uid=${username},ou=Users,dc=com,dc=fathomscale");
        String resourcePath = templates.toResourcePath(this, "conf/ldap.xml");
        String contents = templates.runTemplate(resourcePath, vars);
        FileUpload.upload(target, new File(confDir, "ldap.xml"), contents);
    }
    target.chown(sonatypeDir, "jetty", "jetty", true, false);
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) File(java.io.File) Map(java.util.Map) Handler(org.platformlayer.ops.Handler)

Example 28 with OpsTarget

use of org.platformlayer.ops.OpsTarget 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 29 with OpsTarget

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

the class PosixUser method doOperation.

@Handler
public void doOperation() throws OpsException {
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    // TODO: Only if user not found
    {
        Command command = Command.build("adduser");
        command.addLiteral("--system");
        command.addLiteral("--no-create-home");
        if (shell != null) {
            command.addLiteral("--shell").addFile(shell);
        }
        if (!Strings.isNullOrEmpty(primaryGroup)) {
            command.addLiteral("--ingroup");
            command.addQuoted(primaryGroup);
        }
        command.addQuoted(userName);
        target.executeCommand(command);
    }
    for (String secondaryGroup : secondaryGroups) {
        Command command = Command.build("adduser");
        command.addQuoted(userName);
        command.addQuoted(secondaryGroup);
        target.executeCommand(command);
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) Command(org.platformlayer.ops.Command) Handler(org.platformlayer.ops.Handler)

Example 30 with OpsTarget

use of org.platformlayer.ops.OpsTarget 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

OpsTarget (org.platformlayer.ops.OpsTarget)41 Handler (org.platformlayer.ops.Handler)17 File (java.io.File)14 Machine (org.platformlayer.ops.Machine)12 OpsException (org.platformlayer.ops.OpsException)11 SshKey (org.platformlayer.ops.helpers.SshKey)10 Command (org.platformlayer.ops.Command)9 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)6 OpaqueMachine (org.platformlayer.ops.OpaqueMachine)6 OpsContext (org.platformlayer.ops.OpsContext)6 ItemBase (org.platformlayer.core.model.ItemBase)4 PublicKey (java.security.PublicKey)3 Tag (org.platformlayer.core.model.Tag)3 Tags (org.platformlayer.core.model.Tags)3 BindingScope (org.platformlayer.ops.BindingScope)3 CommandEnvironment (org.platformlayer.ops.CommandEnvironment)3 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)3 FilesystemInfo (org.platformlayer.ops.filesystem.FilesystemInfo)3 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)3 ServiceType (org.platformlayer.ids.ServiceType)2