Skip to content

Commit

Permalink
feat(nginx): move letsencrypt to its own file, add ssl renew command
Browse files Browse the repository at this point in the history
refs #190
- adds ssl-renew command
  • Loading branch information
acburdine committed Jul 3, 2017
1 parent 049f351 commit b4eb57a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
22 changes: 22 additions & 0 deletions extensions/nginx/commands/ssl-renew.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const cli = require('../../../lib');
const letsencrypt = require('../letsencrypt');

class SslRenewCommand extends cli.Command {
run() {
let instance = this.system.getInstance();

if (!instance.cliConfig.has('extension.sslemail')) {
return Promise.reject(new cli.errors.SystemError('No saved email found, skipping automatic letsencrypt renewal'));
}

let email = instance.cliConfig.get('extension.sslemail');
return this.ui.run(letsencrypt(instance, email, false), 'Renewing SSL certificate')
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
}
}

SslRenewCommand.description = 'Renew an SSL certificate for a Ghost installation';

module.exports = SslRenewCommand;

15 changes: 3 additions & 12 deletions extensions/nginx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ const NginxConfFile = require('nginx-conf').NginxConfFile;

const cli = require('../../lib');

const LIVE_URL = 'https://acme-v01.api.letsencrypt.org/directory';
const STAGING_URL = 'https://acme-staging.api.letsencrypt.org/directory';

class NginxExtension extends cli.Extension {
setup(cmd, argv) {
// ghost setup --local, skip
Expand Down Expand Up @@ -106,6 +103,7 @@ class NginxExtension extends cli.Extension {
}

let rootPath = path.resolve(ctx.instance.dir, 'system', 'nginx-root');
const letsencrypt = require('./letsencrypt');

return this.ui.listr([{
title: 'Checking DNS resolution',
Expand Down Expand Up @@ -155,15 +153,8 @@ class NginxExtension extends cli.Extension {
}, {
title: 'Getting SSL Certificate',
task: () => {
let letsencryptFolder = path.join(ctx.instance.dir, 'system', 'letsencrypt');
let sslGenArgs = `certonly --agree-tos --email ${argv.sslemail} --webroot --webroot-path ${rootPath}` +
` --config-dir ${letsencryptFolder} --domains ${parsedUrl.hostname} --server ${argv.sslStaging ? STAGING_URL : LIVE_URL}`;

return execa('greenlock', sslGenArgs.split(' '), {
stdio: 'ignore',
preferLocal: true,
localDir: __dirname
}).catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
return letsencrypt(ctx.instance, argv.sslemail, argv.sslstaging)
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
}
}, {
title: 'Generating Encryption Key (may take a few minutes)',
Expand Down
20 changes: 20 additions & 0 deletions extensions/nginx/letsencrypt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const url = require('url');
const path = require('path');
const execa = require('execa');

const LIVE_URL = 'https://acme-v01.api.letsencrypt.org/directory';
const STAGING_URL = 'https://acme-staging.api.letsencrypt.org/directory';

module.exports = function letsencrypt(instance, email, staging) {
let hostname = url.parse(instance.config.get('url')).hostname;
let rootPath = path.resolve(instance.dir, 'system', 'nginx-root');
let letsencryptFolder = path.join(instance.dir, 'system', 'letsencrypt');
let sslGenArgs = `certonly --agree-tos --email ${email} --webroot --webroot-path ${rootPath}` +
` --config-dir ${letsencryptFolder} --domains ${hostname} --server ${staging ? STAGING_URL : LIVE_URL}`;

return execa('greenlock', sslGenArgs.split(' '), {
preferLocal: true,
localDir: __dirname
});
};

0 comments on commit b4eb57a

Please sign in to comment.