Search in sources :

Example 16 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class ConfigureHostname method handle.

@Handler
public void handle() throws OpsException {
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    if (Strings.isNullOrEmpty(hostname)) {
        throw new IllegalArgumentException();
    }
    // Fix hostname
    File hostsFile = new File("/etc/hosts");
    String hostLine = "127.0.0.1\t" + hostname;
    String hosts = target.readTextFile(hostsFile);
    boolean found = false;
    for (String line : Splitter.on("\n").trimResults().split(hosts)) {
        if (line.equals(hostLine)) {
            found = true;
        }
    }
    if (!found) {
        hosts += "\n" + hostLine + "\n";
        FileUpload.upload(target, hostsFile, hosts);
    }
    FileUpload.upload(target, new File("/etc/hostname"), hostname);
    {
        ProcessExecution execution = target.executeCommand("hostname");
        String currentHostname = execution.getStdOut().trim();
        if (!currentHostname.equals(hostname)) {
            if (!DetectVirtualization.isLxc(target)) {
                // This actually can't be done within an LXC instance, which is why we go to extraordinary lengths
                // to
                // set it on creation
                target.executeCommand("hostname {0}", hostname);
            } else {
                log.warn("Unable to change hostname on LXC: " + currentHostname + " -> " + hostname);
            }
        }
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) File(java.io.File) Handler(org.platformlayer.ops.Handler)

Example 17 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class ZookeeperUtils method sendCommand.

public static ZookeeperResponse sendCommand(OpsTarget target, InetSocketAddress socketAddress, String zkCommand) throws OpsException {
    Command command = Command.build("echo {0} | nc {1} {2}", zkCommand, socketAddress.getAddress().getHostAddress(), socketAddress.getPort());
    ProcessExecution execution = target.executeCommand(command);
    return new ZookeeperResponse(execution.getStdOut());
}
Also used : Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 18 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class GitServerController method doOperation.

public void doOperation(Managed<GitServer> managed) throws OpsException, IOException {
    initializeService();
    GitServer model = (GitServer) managed.getModel();
    Tag tag = new Tag(Tag.CONDUCTOR_ID, managed.getConductorId());
    SshKey sshKey = service.getSshKey();
    DiskImageRecipe recipe = imageFactory.loadDiskImageResource(getClass(), "DiskImageRecipe.xml");
    String securityGroup = service.getSecurityGroupName();
    // Git isn't particularly memory intensive (?)
    int minimumMemoryMB = 256;
    Managed<PersistentInstance> foundPersistentInstance = persistentInstances.getOrCreate(tag, recipe, model.dnsName, sshKey.getName(), securityGroup, minimumMemoryMB);
    OpenstackComputeMachine machine = persistentInstances.getMachine(foundPersistentInstance);
    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"));
    String adminUser = "gitadmin";
    target.executeCommand("adduser --group --system {0}", adminUser);
    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");
    target.mkdir(adminSshDir);
    String passphrase = "";
    target.executeCommand("ssh-keygen -t rsa -f {0} -P {1}", privateKeyFile, passphrase);
    String privateKeyData = target.readTextFile(privateKeyFile);
    String publicKeyData = target.readTextFile(publicKeyFile);
    target.executeCommand("cat {0} | sudo -H -u gitosis gitosis-init", publicKeyFile);
    target.setFileContents(authorizedKeys, publicKeyData);
    target.executeCommand("chown -R {0} {1}", adminUser, adminSshDir);
    target.executeCommand("chmod -R 600 {0}", adminSshDir);
    target.executeCommand("chmod 700 {0}", adminSshDir);
    target.executeCommand("chsh -s /bin/bash {0}", adminUser);
    SshKey adminSshKey = new SshKey(null, adminUser, KeyPairUtils.deserialize(privateKeyData));
    OpsTarget adminTarget = machine.getTarget(adminSshKey);
    {
        ProcessExecution execution = adminTarget.executeCommand("ssh-keyscan 127.0.0.1");
        File knownHosts = new File(adminSshDir, "known_hosts");
        adminTarget.setFileContents(knownHosts, execution.getStdOut());
    }
// adminTarget.executeCommand("git clone gitosis@127.0.0.1:gitosis-admin.git /home/gitadmin/gitosis-admin");
// adminSshKey.
//
// adminTarget.executeCommand("git clone git@)
// git clone git@YOUR_SERVER_HOSTNAME:gitosis-admin.git
// cd gitosis-admin
}
Also used : SshKey(org.platformlayer.ops.helpers.SshKey) OpsTarget(org.platformlayer.ops.OpsTarget) DiskImageRecipe(org.platformlayer.service.imagefactory.v1.DiskImageRecipe) PersistentInstance(org.platformlayer.service.instancesupervisor.v1.PersistentInstance) GitServer(org.platformlayer.service.gitosis.model.GitServer) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) Tag(org.platformlayer.conductor.Tag) OpenstackComputeMachine(org.platformlayer.ops.OpenstackComputeMachine) File(java.io.File)

Example 19 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class OpsTargetBase method createTempDir.

protected File createTempDir(File tempDirBase) throws OpsException {
    int maxRetries = 10;
    Random random = new Random();
    for (int i = 1; i <= maxRetries; i++) {
        String randomDirName = Long.toHexString(random.nextLong());
        File tempDir = new File(tempDirBase, randomDirName);
        try {
            executeCommand("mkdir {0}", tempDir);
            return tempDir;
        } catch (ProcessExecutionException e) {
            ProcessExecution execution = e.getExecution();
            if (i < maxRetries && execution != null && execution.getExitCode() == 1 && execution.getStdErr().contains("File exists")) {
            // Loop again
            } else {
                throw new OpsException("Error creating directory", e);
            }
        }
    }
    throw new IllegalStateException("Unreachable?");
}
Also used : Random(java.util.Random) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) File(java.io.File)

Example 20 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class CurlRequest method executeRequest.

public CurlResult executeRequest(OpsTarget target) throws OpsException {
    Command command = toCommand();
    ProcessExecution execution = target.executeCommand(command);
    return parseResponse(execution);
}
Also used : Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Aggregations

ProcessExecution (org.platformlayer.ops.process.ProcessExecution)25 Command (org.platformlayer.ops.Command)16 OpsException (org.platformlayer.ops.OpsException)8 ProcessExecutionException (org.platformlayer.ops.process.ProcessExecutionException)8 File (java.io.File)5 OpsTarget (org.platformlayer.ops.OpsTarget)3 Md5Hash (com.fathomdb.hash.Md5Hash)2 UnknownHostException (java.net.UnknownHostException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 RequestBuilder (org.openstack.client.common.RequestBuilder)2 Handler (org.platformlayer.ops.Handler)2 FilesystemInfo (org.platformlayer.ops.filesystem.FilesystemInfo)2 CurlRequest (org.platformlayer.ops.helpers.CurlRequest)2 SshKey (org.platformlayer.ops.helpers.SshKey)2 ImageFormat (org.platformlayer.ops.images.ImageFormat)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Inet6Address (java.net.Inet6Address)1 List (java.util.List)1