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);
}
}
}
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);
}
}
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();
}
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!");
}
}
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);
}
Aggregations