Search in sources :

Example 1 with Execed

use of com.peterphi.std.system.exec.Execed in project stdlib by petergeneric.

the class FileHelper method chmod.

/**
 * Performs a chmod (which assumes this system is Linux/UNIX/Solaris/etc), replacing the permissions using octal
 *
 * @param f
 * @param permissions
 * 		<strong>REMEMBER TO SPECIFY THIS VALUE IN OCTAL (ie. with a leading zero)</strong>
 *
 * @return
 *
 * @throws IOException
 */
public static boolean chmod(final String as, final File f, final int permissions) {
    if (!f.exists()) {
        log.error("[FileHelper] {chmod} Non-existant file: " + f.getPath());
        return false;
    }
    try {
        Execed call = Exec.utilityAs(as, "chmod", Integer.toOctalString(permissions), f.getPath());
        int returnCode = call.waitForExit();
        return returnCode == 0;
    } catch (Exception e) {
        log.error("[FileHelper] {chmod} Failure: " + e.getMessage(), e);
        return false;
    }
}
Also used : Execed(com.peterphi.std.system.exec.Execed) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with Execed

use of com.peterphi.std.system.exec.Execed in project stdlib by petergeneric.

the class FileHelper method chmod.

/**
 * Performs a chmod (which assumes this system is Linux/UNIX/Solaris/etc), altering the permissions using symbols (ie. chmod
 * o+w)
 *
 * @param f
 * @param set
 * 		The permissions to set on the file
 * @param clear
 * 		The permissions to modify on the file
 *
 * @return
 *
 * @throws IOException
 */
public static boolean chmod(String as, File f, Set<ChmodBit> set, Set<ChmodBit> clear) {
    if (!f.exists()) {
        log.error("[FileHelper] {chmod} Non-existant file: " + f.getPath());
        return false;
    }
    String permissions = ChmodBit.toString(set, clear);
    try {
        Execed call = Exec.utilityAs(as, "chmod", permissions, f.getPath());
        int returnCode = call.waitForExit();
        return returnCode == 0;
    } catch (Exception e) {
        log.error("[LockRecord] {chmod} Failure: " + e.getMessage(), e);
        return false;
    }
}
Also used : Execed(com.peterphi.std.system.exec.Execed) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with Execed

use of com.peterphi.std.system.exec.Execed in project stdlib by petergeneric.

the class NginxService method installCertificates.

/**
 * Install new SSL Certificates for the host
 *
 * @param key
 * @param cert
 * @param chain
 */
public void installCertificates(final String key, final String cert, final String chain) {
    try {
        final File keyFile = File.createTempFile("key", ".pem");
        final File certFile = File.createTempFile("cert", ".pem");
        final File chainFile = File.createTempFile("chain", ".pem");
        try {
            FileHelper.write(keyFile, key);
            FileHelper.write(certFile, cert);
            FileHelper.write(chainFile, chain);
            final Execed process = Exec.rootUtility(new File(binPath, "cert-update").getAbsolutePath(), keyFile.getAbsolutePath(), certFile.getAbsolutePath(), chainFile.getAbsolutePath());
            process.waitForExit(new Timeout(30, TimeUnit.SECONDS).start(), 0);
        } finally {
            FileUtils.deleteQuietly(keyFile);
            FileUtils.deleteQuietly(certFile);
            FileUtils.deleteQuietly(chainFile);
        }
    } catch (IOException e) {
        throw new RuntimeException("Error executing cert-update command", e);
    }
}
Also used : Timeout(com.peterphi.std.threading.Timeout) Execed(com.peterphi.std.system.exec.Execed) IOException(java.io.IOException) File(java.io.File)

Example 4 with Execed

use of com.peterphi.std.system.exec.Execed in project stdlib by petergeneric.

the class NginxService method reconfigure.

/**
 * Rewrite the nginx site configuration and reload
 *
 * @param config
 * 		the nginx site configuration
 */
public void reconfigure(final String config) {
    try {
        final File tempFile = File.createTempFile("nginx", ".conf");
        try {
            FileHelper.write(tempFile, config);
            final Execed process = Exec.rootUtility(new File(binPath, "nginx-reconfigure").getAbsolutePath(), tempFile.getAbsolutePath());
            process.waitForExit(new Timeout(30, TimeUnit.SECONDS).start(), 0);
        } finally {
            FileUtils.deleteQuietly(tempFile);
        }
    } catch (IOException e) {
        throw new RuntimeException("Error executing nginx-reload command", e);
    }
    reload();
}
Also used : Timeout(com.peterphi.std.threading.Timeout) Execed(com.peterphi.std.system.exec.Execed) IOException(java.io.IOException) File(java.io.File)

Example 5 with Execed

use of com.peterphi.std.system.exec.Execed in project stdlib by petergeneric.

the class OpenSSLPKCS12 method P12toPEM.

/**
 * @param p12
 * @param p12Password
 * @param toPEM
 * @param pemPassword
 *
 * @throws IOException
 * 		if a catastrophic unexpected failure occurs during execution
 * @throws IllegalArgumentException
 * 		if the PKCS12 keystore doesn't exist
 * @throws IllegalStateException
 * 		if openssl exits with a failure condition
 */
public static void P12toPEM(File p12, String p12Password, File toPEM, String pemPassword) throws IOException {
    if (!p12.exists())
        throw new IllegalArgumentException("p12 file does not exist: " + p12.getPath());
    Execed openssl = Exec.utilityAs(null, OPENSSL, "pkcs12", "-in", p12.getPath(), "-out", toPEM.getPath(), "-passin", "pass:" + p12Password, "-nodes");
    int returnCode = openssl.waitForExit();
    if (returnCode != 0)
        throw new IllegalStateException("Unexpected openssl exit code " + returnCode + "; output:\n" + openssl.getStandardOut());
}
Also used : Execed(com.peterphi.std.system.exec.Execed)

Aggregations

Execed (com.peterphi.std.system.exec.Execed)10 IOException (java.io.IOException)8 FileNotFoundException (java.io.FileNotFoundException)4 Timeout (com.peterphi.std.threading.Timeout)3 File (java.io.File)3 Exec (com.peterphi.std.system.exec.Exec)1 Deadline (com.peterphi.std.threading.Deadline)1