Skip to content

Instantly share code, notes, and snippets.

@bsutton
Last active March 1, 2020 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsutton/fe9994a713a2398e656d9a83489bc070 to your computer and use it in GitHub Desktop.
Save bsutton/fe9994a713a2398e656d9a83489bc070 to your computer and use it in GitHub Desktop.
Renew Lets Encrypt certificate
#! /usr/bin/env dshell
import 'dart:io';
import 'package:dshell/dshell.dart';
void main(List<String> args) {
var parser = ArgParser();
parser.addFlag("production", abbr: 'p', defaultsTo: false);
var result = parser.parse(args);
if (result.rest.length != 2) {
print(
'''You must provide a certificate name like \'host.somedomain.org\' and your email address.
''');
usage();
exit(0);
}
var certName = result.rest[0];
var emailaddress = result.rest[1];
var useProduction = result['production'] as bool;
var lets_staging = 'https://acme-staging-v02.api.letsencrypt.org/directory';
var lets_production = 'https://acme-v02.api.letsencrypt.org/directory';
var server = lets_staging;
if (useProduction) {
server = lets_production;
}
// check that docker is installed
if (which('docker').isEmpty) {
printerr(red('You need to install docker first'));
exit(1);
}
print("Using: $server");
// namecheap api user and key.
var username = read('namecheap_username').firstLine;
var key = read('namecheap_key').firstLine;
var saveDir = join(pwd, 'certificates');
setEnv('NAMECHEAP_API_USER', username);
setEnv('NAMECHEAP_API_KEY', key);
'docker run -v $saveDir:/.lego --env NAMECHEAP_API_USER --env NAMECHEAP_API_KEY goacme/lego --server=$server --dns namecheap --email $emailaddress --domains "$certName" --accept-tos run'
.run;
print('keys have been saved to $saveDir');
}
void usage() {
print('''Usage:
certbot_renew.dart [--production|-p] <cert doman> <email address>
If the production switch isn't passed then a trial cert is obtained from the
staging server.
e.g.
certbot_renew.dart host.somedomain.org myemail@somedomain.org
''');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment