Search in sources :

Example 1 with Timeout

use of com.peterphi.std.threading.Timeout in project stdlib by petergeneric.

the class TimeoutConverter method convert.

public Object convert(String value) {
    value = value.trim();
    final char last = value.charAt(value.length() - 1);
    if (Character.isDigit(last)) {
        return new Timeout(Long.parseLong(value), TimeUnit.MILLISECONDS);
    } else {
        Matcher matcher = pattern.matcher(value);
        if (matcher.matches()) {
            final long quantity = Long.valueOf(matcher.group(1));
            final String unit = matcher.group(2);
            return new Timeout(quantity, parseUnit(unit));
        } else {
            throw new IllegalArgumentException("Cannot parse duration: " + value);
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) Timeout(com.peterphi.std.threading.Timeout)

Example 2 with Timeout

use of com.peterphi.std.threading.Timeout 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 3 with Timeout

use of com.peterphi.std.threading.Timeout 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 4 with Timeout

use of com.peterphi.std.threading.Timeout in project stdlib by petergeneric.

the class LetsEncryptService method proveOwnership.

@Retry
public void proveOwnership(final String domain) {
    Registration registration = getRegistration();
    final Authorization authorization;
    try {
        authorization = registration.authorizeDomain(domain);
    } catch (AcmeException e) {
        throw new RuntimeException("Error creating authorisation for " + domain, e);
    }
    Dns01Challenge challenge = authorization.findChallenge(Dns01Challenge.TYPE);
    if (challenge == null)
        throw new RuntimeException("DNS Challenge is not available! Cannot prove we own " + domain);
    final String domainName = "_acme-challenge." + domain;
    log.debug("Create TXT record " + domainName + " with value: " + challenge.getDigest());
    // Create the TXT record
    dns.createDNSRecord(domainName, RecordType.TXT, challenge.getDigest());
    // Wait for a short time for the change to DNS records to propagate through Microsoft's system
    // N.B. there's no docs suggesting this is needed or that this is the right value, but Let's Encrypt challenges
    // seem to fail more regularly against the live API without this wait
    new Timeout(5, TimeUnit.SECONDS).sleep();
    // Allow the CA to start checking the TXT record
    try {
        log.trace("Challenge status " + challenge.getStatus());
        challenge.trigger();
        log.trace("Challenge status " + challenge.getStatus());
    } catch (AcmeException e) {
        throw new RuntimeException("Error triggering authorisation for " + domain, e);
    }
    // Poll waiting for the challenge to complete
    int attempts = 10;
    for (int attempt = 0; attempt < 10; attempt++) {
        log.trace("Challenge status " + challenge.getStatus());
        if (challenge.getStatus() == Status.INVALID)
            break;
        else if (challenge.getStatus() == Status.VALID)
            break;
        Timeout.TEN_SECONDS.sleep();
        try {
            challenge.update();
        } catch (AcmeException e) {
            log.warn("Error updating challenge", e);
        }
    }
    log.trace("Challenge status " + challenge.getStatus());
    dns.deleteDNSRecord(domainName, RecordType.TXT);
    if (challenge.getStatus() != Status.VALID) {
        throw new RuntimeException("Challenge " + challenge + " failed for " + domainName + "! Failed with state " + challenge.getStatus());
    } else {
        log.debug("Challenge " + challenge + " passed!");
    }
}
Also used : Authorization(org.shredzone.acme4j.Authorization) Registration(org.shredzone.acme4j.Registration) AcmeException(org.shredzone.acme4j.exception.AcmeException) Timeout(com.peterphi.std.threading.Timeout) Dns01Challenge(org.shredzone.acme4j.challenge.Dns01Challenge) Retry(com.peterphi.std.guice.common.retry.annotation.Retry)

Example 5 with Timeout

use of com.peterphi.std.threading.Timeout in project stdlib by petergeneric.

the class RetryMethodInterceptor method buildRetryManager.

private RetryManager buildRetryManager(Retry options) {
    final Timeout initial = new Timeout(options.backoffTime(), options.backoffUnit());
    ExponentialBackoff backoff = new ExponentialBackoff(initial, options.backoffExponent());
    return new RetryManager(backoff, options.maxAttempts(), attempts, attemptFailures);
}
Also used : Timeout(com.peterphi.std.threading.Timeout) RetryManager(com.peterphi.std.guice.common.retry.retry.RetryManager) ExponentialBackoff(com.peterphi.std.guice.common.retry.retry.backoff.ExponentialBackoff)

Aggregations

Timeout (com.peterphi.std.threading.Timeout)8 IOException (java.io.IOException)4 Execed (com.peterphi.std.system.exec.Execed)3 File (java.io.File)3 Injector (com.google.inject.Injector)1 CarbonConnectException (com.peterphi.carbon.exception.CarbonConnectException)1 CarbonException (com.peterphi.carbon.exception.CarbonException)1 Retry (com.peterphi.std.guice.common.retry.annotation.Retry)1 RetryManager (com.peterphi.std.guice.common.retry.retry.RetryManager)1 ExponentialBackoff (com.peterphi.std.guice.common.retry.retry.backoff.ExponentialBackoff)1 ServicePropertiesModule (com.peterphi.std.guice.common.serviceprops.ServicePropertiesModule)1 PropertyFile (com.peterphi.std.io.PropertyFile)1 Socket (java.net.Socket)1 Matcher (java.util.regex.Matcher)1 JDOMException (org.jdom2.JDOMException)1 Test (org.junit.Test)1 Authorization (org.shredzone.acme4j.Authorization)1 Registration (org.shredzone.acme4j.Registration)1 Dns01Challenge (org.shredzone.acme4j.challenge.Dns01Challenge)1 AcmeException (org.shredzone.acme4j.exception.AcmeException)1