11'use strict' ;
22
3- const _ = require ( 'lodash' ) ;
43const fs = require ( 'fs' ) ;
54const ejs = require ( 'ejs' ) ;
65const timestamp = require ( 'unix-timestamp' ) ;
7- const batchflow = require ( 'batchflow' ) ;
86const internalNginx = require ( './nginx' ) ;
97const logger = require ( '../logger' ) ;
10- const db = require ( '../db' ) ;
118const utils = require ( '../lib/utils' ) ;
129const error = require ( '../lib/error' ) ;
1310
1411timestamp . round = true ;
1512
1613const internalSsl = {
1714
18- interval_timeout : 60 * 1000 ,
15+ interval_timeout : 1000 * 60 * 60 * 6 , // 6 hours
1916 interval : null ,
2017 interval_processing : false ,
2118
@@ -28,42 +25,17 @@ const internalSsl = {
2825 */
2926 processExpiringHosts : ( ) => {
3027 if ( ! internalSsl . interval_processing ) {
31- let hosts = db . hosts . find ( ) ;
32-
33- if ( hosts && hosts . length ) {
34- internalSsl . interval_processing = true ;
35-
36- batchflow ( hosts ) . sequential ( )
37- . each ( ( i , host , next ) => {
38- if ( ( typeof host . is_deleted === 'undefined' || ! host . is_deleted ) && host . ssl && typeof host . ssl_expires !== 'undefined' && ! internalSsl . hasValidSslCerts ( host ) ) {
39- // This host is due to expire in 1 day, time to renew
40- logger . info ( 'Host ' + host . hostname + ' is due for SSL renewal' ) ;
41-
42- internalSsl . renewSsl ( host )
43- . then ( ( ) => {
44- // Certificate was requested ok, update the timestamp on the host
45- db . hosts . update ( { _id : host . _id } , { ssl_expires : timestamp . now ( '+90d' ) } , {
46- multi : false ,
47- upsert : false
48- } ) ;
49- } )
50- . then ( next )
51- . catch ( err => {
52- logger . error ( err ) ;
53- next ( err ) ;
54- } ) ;
55- } else {
56- next ( ) ;
57- }
58- } )
59- . error ( err => {
60- logger . error ( err ) ;
61- internalSsl . interval_processing = false ;
62- } )
63- . end ( ( /*results*/ ) => {
64- internalSsl . interval_processing = false ;
65- } ) ;
66- }
28+ logger . info ( 'Renewing SSL certs close to expiry...' ) ;
29+ return utils . exec ( '/usr/bin/letsencrypt renew' )
30+ . then ( result => {
31+ logger . info ( result ) ;
32+ internalSsl . interval_processing = false ;
33+ return result ;
34+ } )
35+ . catch ( err => {
36+ logger . error ( err ) ;
37+ internalSsl . interval_processing = false ;
38+ } ) ;
6739 }
6840 } ,
6941
@@ -73,8 +45,7 @@ const internalSsl = {
7345 */
7446 hasValidSslCerts : host => {
7547 return fs . existsSync ( '/etc/letsencrypt/live/' + host . hostname + '/fullchain.pem' ) &&
76- fs . existsSync ( '/etc/letsencrypt/live/' + host . hostname + '/privkey.pem' ) &&
77- host . ssl_expires > timestamp . now ( '+1d' ) ;
48+ fs . existsSync ( '/etc/letsencrypt/live/' + host . hostname + '/privkey.pem' ) ;
7849 } ,
7950
8051 /**
@@ -157,10 +128,6 @@ const internalSsl = {
157128 . then ( ( ) => {
158129 return internalSsl . requestSsl ( data ) ;
159130 } ) ;
160- } )
161- . then ( ( ) => {
162- // Certificate was requested ok, update the timestamp on the host
163- db . hosts . update ( { _id : host . _id } , { ssl_expires : timestamp . now ( '+90d' ) } , { multi : false , upsert : false } ) ;
164131 } ) ;
165132 }
166133} ;
0 commit comments