Merge branch 'promiseProcess' into 'master'

Promise based run when using in a "library-ish" mode

See merge request !2
This commit is contained in:
AJ ONeal 2017-05-17 10:15:02 -06:00
commit e21f758e60
3 changed files with 34 additions and 14 deletions

View File

@ -103,6 +103,8 @@ cli.main(function(_, options) {
return; return;
} }
require('../').run(args); require('../').run(args).then(function (status) {
process.exit(status);
});
}); });
}); });

View File

@ -84,13 +84,13 @@ module.exports.run = function (args) {
if (servers) { if (servers) {
if (args.tlsSni01Port) { if (args.tlsSni01Port) {
servers = servers.startServers( servers.startServers(
[], args.tlsSni01Port [], args.tlsSni01Port
, { debug: args.debug, httpsOptions: le.httpsOptions } , { debug: args.debug, httpsOptions: le.httpsOptions }
); );
} }
else { else {
servers = servers.startServers( servers.startServers(
args.http01Port || [80], [] args.http01Port || [80], []
, { debug: args.debug } , { debug: args.debug }
); );
@ -98,7 +98,7 @@ module.exports.run = function (args) {
} }
// Note: can't use args directly as null values will overwrite template values // Note: can't use args directly as null values will overwrite template values
le.register({ return le.register({
debug: args.debug debug: args.debug
, email: args.email , email: args.email
, agreeTos: args.agreeTos , agreeTos: args.agreeTos
@ -117,10 +117,6 @@ module.exports.run = function (args) {
console.log("Renewing them now"); console.log("Renewing them now");
return certs._renewing; return certs._renewing;
}).then(function (certs) { }).then(function (certs) {
if (servers) {
servers.closeServers();
}
console.log(""); console.log("");
console.log("Got certificate(s) for " + certs.altnames.join(', ')); console.log("Got certificate(s) for " + certs.altnames.join(', '));
console.log("\tIssued at " + new Date(certs.issuedAt).toISOString() + ""); console.log("\tIssued at " + new Date(certs.issuedAt).toISOString() + "");
@ -147,12 +143,18 @@ module.exports.run = function (args) {
); );
console.log(""); console.log("");
process.exit(0); if (servers) {
return servers.closeServers({ debug: args.debug }).then(function() {
return 0;
});
}
return 0;
}, function (err) { }, function (err) {
console.error('[Error]: greenlock-cli'); console.error('[Error]: greenlock-cli');
console.error(err.stack || new Error('get stack').stack); console.error(err.stack || new Error('get stack').stack);
process.exit(1); return 1;
}); });
}; };

View File

@ -77,11 +77,27 @@ module.exports.create = function (challenge) {
} }
, closeServers: function () { , 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) { servers._servers.forEach(function (server) {
server.close(); server.close(function () {
}); if (serversToClose === ++closedServers) {
if (opts.debug) {
console.info('Closed all servers');
}
servers._servers = []; servers._servers = [];
done();
}
});
});
});
} }
}; };