From 248640cd3e42e070b586ce721a473e786874ba7b Mon Sep 17 00:00:00 2001 From: Vasil Rangelov Date: Mon, 17 Apr 2017 00:09:23 +0300 Subject: [PATCH 1/2] Changed index.js to return a Promise that resolves to the status to return on the command line, which greenlock.js now uses when closing the process. --- bin/greenlock.js | 4 +++- index.js | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/bin/greenlock.js b/bin/greenlock.js index 7323a9e..a0c273b 100755 --- a/bin/greenlock.js +++ b/bin/greenlock.js @@ -103,6 +103,8 @@ cli.main(function(_, options) { return; } - require('../').run(args); + require('../').run(args).then(function (status) { + process.exit(status); + }); }); }); diff --git a/index.js b/index.js index f5d3971..933aafa 100644 --- a/index.js +++ b/index.js @@ -98,7 +98,7 @@ module.exports.run = function (args) { } // Note: can't use args directly as null values will overwrite template values - le.register({ + return le.register({ debug: args.debug , email: args.email , agreeTos: args.agreeTos @@ -147,12 +147,12 @@ module.exports.run = function (args) { ); console.log(""); - process.exit(0); + return 0; }, function (err) { console.error('[Error]: greenlock-cli'); console.error(err.stack || new Error('get stack').stack); - process.exit(1); + return 1; }); }; From e073b273b3e95ee90b3555edc2e3757f2901d7d0 Mon Sep 17 00:00:00 2001 From: Vasil Rangelov Date: Wed, 17 May 2017 18:20:36 +0300 Subject: [PATCH 2/2] Fixed standalone server closing, and made the index.js promise resolve only after the standalone server has closed. --- index.js | 14 ++++++++------ lib/servers.js | 24 ++++++++++++++++++++---- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 933aafa..408bc87 100644 --- a/index.js +++ b/index.js @@ -84,13 +84,13 @@ module.exports.run = function (args) { if (servers) { if (args.tlsSni01Port) { - servers = servers.startServers( + servers.startServers( [], args.tlsSni01Port , { debug: args.debug, httpsOptions: le.httpsOptions } ); } else { - servers = servers.startServers( + servers.startServers( args.http01Port || [80], [] , { debug: args.debug } ); @@ -117,10 +117,6 @@ module.exports.run = function (args) { console.log("Renewing them now"); return certs._renewing; }).then(function (certs) { - if (servers) { - servers.closeServers(); - } - console.log(""); console.log("Got certificate(s) for " + certs.altnames.join(', ')); console.log("\tIssued at " + new Date(certs.issuedAt).toISOString() + ""); @@ -147,6 +143,12 @@ module.exports.run = function (args) { ); console.log(""); + if (servers) { + return servers.closeServers({ debug: args.debug }).then(function() { + return 0; + }); + } + return 0; }, function (err) { console.error('[Error]: greenlock-cli'); diff --git a/lib/servers.js b/lib/servers.js index fcae91f..9bb5c51 100644 --- a/lib/servers.js +++ b/lib/servers.js @@ -77,11 +77,27 @@ module.exports.create = function (challenge) { } - , closeServers: function () { - servers._servers.forEach(function (server) { - server.close(); + , closeServers: function (opts) { + opts = opts || {}; + return new Promise(function (done) { + var closedServers = 0; + var serversToClose = servers._servers.length; + if (0 === serversToClose) { + done(); + } + + servers._servers.forEach(function (server) { + server.close(function () { + if (serversToClose === ++closedServers) { + if (opts.debug) { + console.info('Closed all servers'); + } + servers._servers = []; + done(); + } + }); + }); }); - servers._servers = []; } };