Skip to content

Commit c60cc75

Browse files
committed
feat(nginx): setup ssl renew cron script
refs #216 - add crontab script that calls `ghost ssl-renew` every month - add support for setup stage "descriptions" - longer blocks of text - deps: [email protected]
1 parent b4eb57a commit c60cc75

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

extensions/nginx/commands/ssl-renew.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const letsencrypt = require('../letsencrypt');
55
class SslRenewCommand extends cli.Command {
66
run() {
77
let instance = this.system.getInstance();
8+
instance.checkEnvironment();
89

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

extensions/nginx/index.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class NginxExtension extends cli.Extension {
2121

2222
cmd.addStage('nginx', this.setupNginx.bind(this));
2323
cmd.addStage('ssl', this.setupSSL.bind(this));
24+
cmd.addStage('ssl-renew', this.setupRenew.bind(this), 'automatic ssl renewal');
2425
}
2526

2627
setupNginx(argv, ctx, task) {
@@ -215,6 +216,15 @@ class NginxExtension extends cli.Extension {
215216
}], false);
216217
}
217218

219+
setupRenew(argv, ctx) {
220+
return this._cron((cron) => {
221+
// Ensure any crontab with the same instance name is removed
222+
cron.remove({comment: ctx.instance.name});
223+
let cmd = `cd ${ctx.instance.dir} && ${process.argv.slice(0, 2).join(' ')} ssl-renew`;
224+
cron.create(cmd, '@monthly', ctx.instance.name);
225+
});
226+
}
227+
218228
_addProxyBlock(location, port) {
219229
location._add('proxy_set_header', 'X-Forwarded-For $proxy_add_x_forwarded_for');
220230
location._add('proxy_set_header', 'X-Forwarded-Proto $scheme');
@@ -231,11 +241,30 @@ class NginxExtension extends cli.Extension {
231241
let parsedUrl = url.parse(instance.config.get('url'));
232242
let confFile = `${parsedUrl.hostname}.conf`;
233243

244+
let promises = [
245+
this._cron(cron => cron.remove({comment: instance.name}))
246+
];
247+
234248
if (fs.existsSync(`/etc/nginx/sites-available/${confFile}`)) {
235-
return this.ui.sudo(`rm /etc/nginx/sites-available/${confFile}`).then(() => {
236-
return this.ui.sudo(`rm /etc/nginx/sites-enabled/${confFile}`);
237-
}).catch(() => Promise.reject(new cli.errors.SystemError('Nginx config file link could not be removed, you will need to do this manually.')));
249+
promises.push(
250+
this.ui.sudo(`rm /etc/nginx/sites-available/${confFile}`).then(() => {
251+
return this.ui.sudo(`rm /etc/nginx/sites-enabled/${confFile}`);
252+
}).catch(
253+
() => Promise.reject(new cli.errors.SystemError('Nginx config file link could not be removed, you will need to do this manually.'))
254+
)
255+
);
238256
}
257+
258+
return Promise.all(promises);
259+
}
260+
261+
_cron(fn) {
262+
const crontab = require('crontab');
263+
264+
return Promise.fromCallback(cb => crontab.load(cb)).then((cron) => {
265+
fn(cron);
266+
return Promise.fromCallback(cb => cron.save(cb));
267+
});
239268
}
240269

241270
restartNginx() {

lib/commands/setup.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ class SetupCommand extends Command {
3333
this.stages = [];
3434
}
3535

36-
addStage(name, fn) {
36+
addStage(name, fn, description) {
3737
this.stages.push({
3838
name: name,
39+
description: description || name,
3940
fn: fn
4041
});
4142
}
@@ -94,7 +95,7 @@ class SetupCommand extends Command {
9495
}
9596

9697
return this.ui.listr([{
97-
title: `Setting up ${stage.name}`,
98+
title: `Setting up ${stage.description}`,
9899
task: (ctx, task) => stage.fn(argv, ctx, task)
99100
}], {single: true, instance: instance});
100101
});
@@ -117,7 +118,7 @@ class SetupCommand extends Command {
117118
return stage.fn(argv, ctx, task);
118119
}
119120

120-
return this.ui.confirm(`Do you wish to set up ${stage.name}?`, true).then((res) => {
121+
return this.ui.confirm(`Do you wish to set up ${stage.description}?`, true).then((res) => {
121122
if (!res.yes) {
122123
return task.skip();
123124
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"bluebird": "3.5.0",
4343
"chalk": "1.1.3",
4444
"cli-table2": "0.2.0",
45+
"crontab": "1.1.3",
4546
"debug": "2.6.8",
4647
"decompress": "4.2.0",
4748
"download": "6.2.5",

yarn.lock

+10
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,12 @@ create-error-class@^3.0.0:
572572
dependencies:
573573
capture-stack-trace "^1.0.0"
574574

575+
576+
version "1.1.3"
577+
resolved "https://registry.yarnpkg.com/crontab/-/crontab-1.1.3.tgz#5469a2a76c505d8b7808e5b769061ab26dca3420"
578+
dependencies:
579+
underscore "^1.6.0"
580+
575581
cross-spawn-async@^2.1.1:
576582
version "2.2.5"
577583
resolved "https://registry.yarnpkg.com/cross-spawn-async/-/cross-spawn-async-2.2.5.tgz#845ff0c0834a3ded9d160daca6d390906bb288cc"
@@ -3660,6 +3666,10 @@ unbzip2-stream@^1.0.9:
36603666
buffer "^3.0.1"
36613667
through "^2.3.6"
36623668

3669+
underscore@^1.6.0:
3670+
version "1.8.3"
3671+
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.8.3.tgz#4f3fb53b106e6097fcf9cb4109f2a5e9bdfa5022"
3672+
36633673
unique-string@^1.0.0:
36643674
version "1.0.0"
36653675
resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-1.0.0.tgz#9e1057cca851abb93398f8b33ae187b99caec11a"

0 commit comments

Comments
 (0)