Search in sources :

Example 41 with Command

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

the class AptPackageManager method doDpkgConfigure.

private void doDpkgConfigure(OpsTarget target) throws OpsException {
    CommandEnvironment commandEnvironment = buildEnvironmentWithProxy(target);
    Command command = Command.build("dpkg --configure -a");
    command = command.setEnvironment(commandEnvironment).setTimeout(TimeSpan.TEN_MINUTES);
    target.executeCommand(command);
}
Also used : Command(org.platformlayer.ops.Command) CommandEnvironment(org.platformlayer.ops.CommandEnvironment)

Example 42 with Command

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

the class AptPackageManager method getInstalledPackageInfo.

public List<String> getInstalledPackageInfo(OpsTarget target) throws OpsException {
    AptInfoCache cached = getCache(target);
    if (cached.installedPackages == null) {
        Command command = Command.build("/usr/bin/dpkg --get-selections");
        ProcessExecution execution = target.executeCommand(command);
        final List<String> packages = Lists.newArrayList();
        for (String line : Splitter.on("\n").split(execution.getStdOut())) {
            line = line.trim();
            if (line.isEmpty()) {
                continue;
            }
            List<String> tokens = Lists.newArrayList(Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings().split(line));
            if (tokens.size() != 2) {
                throw new OpsException("Error parsing line; expected 2 items: " + line);
            }
            String state = tokens.get(1);
            if (state.equals("install")) {
                String packageName = tokens.get(0);
                int colonIndex = packageName.indexOf(':');
                if (colonIndex != -1) {
                    // Architecture sometimes follows package name
                    packageName = packageName.substring(0, colonIndex);
                }
                packages.add(packageName);
            } else if (state.equals("deinstall")) {
            // Not installed (?)
            } else {
                throw new OpsException("Unknown package state in line: " + line);
            }
        }
        cached.installedPackages = packages;
    } else {
        log.debug("Re-using cached package info");
    }
    return cached.installedPackages;
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 43 with Command

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

the class LdapMasterPassword method doOperation.

@Handler
public void doOperation(OpsTarget target) throws OpsException {
    // TODO: Make this idempotent
    LdifRecord configLdif;
    {
        Command ldapSearch = Command.build("ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -b cn=config olcRootDN=cn=admin,cn=config dn olcRootDN olcRootPW");
        List<LdifRecord> ldifs = LdifRecord.parse(target.executeCommand(ldapSearch).getStdOut());
        if (ldifs.size() != 1) {
            throw new OpsException("Expected exactly one LDIF record for config");
        }
        configLdif = ldifs.get(0);
    }
    {
        StringBuilder modify = new StringBuilder();
        modify.append("dn: " + configLdif.getLdapDn().toString() + "\n");
        modify.append("replace: olcRootPW\n");
        modify.append("olcRootPW: " + LdapPasswords.getLdapPasswordEncoded(ldapSecret.plaintext()) + "\n");
        modify.append("\n");
        File tempDir = target.createTempDir();
        File modifyFile = new File(tempDir, "modifypw.ldif");
        FileUpload.upload(target, modifyFile, modify.toString());
        Command ldapModify = Command.build("ldapmodify -Q -Y EXTERNAL -H ldapi:/// -f {0}", modifyFile);
        target.executeCommand(ldapModify);
    }
    ldap.writeLdapServerPassword(target, ldapSecret);
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) List(java.util.List) File(java.io.File) Handler(org.platformlayer.ops.Handler)

Example 44 with Command

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

the class OpenLdapManager method doLdapQuery.

public static List<LdifRecord> doLdapQuery(OpsTarget target, LdapDN bindDN, String ldapPassword, LdapDN searchBaseDN, String filter, SearchScope searchScope) throws OpsException {
    Command command = Command.build(CMD_LDAP_SEARCH);
    // Pure LDIF, no extra junk
    command.addLiteral("-LLL");
    // Simple auth
    command.addLiteral("-x");
    // Bind DN
    command.addLiteral("-D");
    command.addQuoted(bindDN.toLdifEncoded());
    // Simple auth password
    command.addLiteral("-w");
    command.addQuoted(ldapPassword);
    // Search base
    command.addLiteral("-b");
    command.addQuoted(searchBaseDN.toLdifEncoded());
    // Scope
    command.addLiteral("-s");
    command.addLiteral(searchScope.toString().toLowerCase());
    if (!Strings.isNullOrEmpty(filter)) {
        command.addQuoted(filter);
    }
    ProcessExecution processExecution = target.executeCommand(command);
    return LdifRecord.parse(processExecution.getStdOut());
}
Also used : Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 45 with Command

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

the class PostgresqlServerBootstrap method handler.

@Handler
public void handler() throws OpsException {
    PostgresqlServer model = OpsContext.get().getInstance(PostgresqlServer.class);
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    // su postgres -c "psql --command=\"ALTER USER postgres WITH PASSWORD 'secret';\""
    String password = model.rootPassword.plaintext();
    String sql = String.format("ALTER USER postgres WITH PASSWORD '%s';", password);
    String psql = String.format("psql --command=\"%s\"", sql);
    Command command = Command.build("su postgres -c {0}", psql);
    target.executeCommand(command);
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) Command(org.platformlayer.ops.Command) PostgresqlServer(org.platformlayer.service.postgresql.model.PostgresqlServer) Handler(org.platformlayer.ops.Handler)

Aggregations

Command (org.platformlayer.ops.Command)72 File (java.io.File)21 Handler (org.platformlayer.ops.Handler)17 OpsException (org.platformlayer.ops.OpsException)16 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)16 CommandEnvironment (org.platformlayer.ops.CommandEnvironment)11 OpsTarget (org.platformlayer.ops.OpsTarget)9 InetAddress (java.net.InetAddress)4 CurlRequest (org.platformlayer.ops.helpers.CurlRequest)4 Md5Hash (com.fathomdb.hash.Md5Hash)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Map (java.util.Map)3 FilesystemInfo (org.platformlayer.ops.filesystem.FilesystemInfo)3 ImageFormat (org.platformlayer.ops.images.ImageFormat)3 List (java.util.List)2 Properties (java.util.Properties)2 RequestBuilder (org.openstack.client.common.RequestBuilder)2 AddressModel (org.platformlayer.core.model.AddressModel)2 Tag (org.platformlayer.core.model.Tag)2