@@ -29,7 +29,9 @@ const internalNginx = {
2929 . then ( ( ) => {
3030 // Nginx is OK
3131 // We're deleting this config regardless.
32- return internalNginx . deleteConfig ( host_type , host ) ; // Don't throw errors, as the file may not exist at all
32+ // Don't throw errors, as the file may not exist at all
33+ // Delete the .err file too
34+ return internalNginx . deleteConfig ( host_type , host , false , true ) ;
3335 } )
3436 . then ( ( ) => {
3537 return internalNginx . generateConfig ( host_type , host ) ;
@@ -80,6 +82,9 @@ const internalNginx = {
8082 . patch ( {
8183 meta : combined_meta
8284 } )
85+ . then ( ( ) => {
86+ internalNginx . renameConfigAsError ( host_type , host )
87+ } )
8388 . then ( ( ) => {
8489 return internalNginx . deleteConfig ( host_type , host , true ) ;
8590 } ) ;
@@ -121,13 +126,10 @@ const internalNginx = {
121126 * @returns {String }
122127 */
123128 getConfigName : ( host_type , host_id ) => {
124- host_type = host_type . replace ( new RegExp ( '-' , 'g' ) , '_' ) ;
125-
126129 if ( host_type === 'default' ) {
127130 return '/data/nginx/default_host/site.conf' ;
128131 }
129-
130- return '/data/nginx/' + host_type + '/' + host_id + '.conf' ;
132+ return '/data/nginx/' + internalNginx . getFileFriendlyHostType ( host_type ) + '/' + host_id + '.conf' ;
131133 } ,
132134
133135 /**
@@ -155,12 +157,12 @@ const internalNginx = {
155157
156158 const locationRendering = async ( ) => {
157159 for ( let i = 0 ; i < host . locations . length ; i ++ ) {
158- let locationCopy = Object . assign ( { } , { access_list_id : host . access_list_id } , { certificate_id : host . certificate_id } ,
160+ let locationCopy = Object . assign ( { } , { access_list_id : host . access_list_id } , { certificate_id : host . certificate_id } ,
159161 { ssl_forced : host . ssl_forced } , { caching_enabled : host . caching_enabled } , { block_exploits : host . block_exploits } ,
160162 { allow_websocket_upgrade : host . allow_websocket_upgrade } , { http2_support : host . http2_support } ,
161163 { hsts_enabled : host . hsts_enabled } , { hsts_subdomains : host . hsts_subdomains } , { access_list : host . access_list } ,
162164 { certificate : host . certificate } , host . locations [ i ] ) ;
163-
165+
164166 if ( locationCopy . forward_host . indexOf ( '/' ) > - 1 ) {
165167 const splitted = locationCopy . forward_host . split ( '/' ) ;
166168
@@ -177,7 +179,7 @@ const internalNginx = {
177179 } ;
178180
179181 locationRendering ( ) . then ( ( ) => resolve ( renderedLocations ) ) ;
180-
182+
181183 } ) ;
182184 } ,
183185
@@ -187,10 +189,10 @@ const internalNginx = {
187189 * @returns {Promise }
188190 */
189191 generateConfig : ( host_type , host ) => {
190- host_type = host_type . replace ( new RegExp ( '-' , 'g' ) , '_' ) ;
192+ const nice_host_type = internalNginx . getFileFriendlyHostType ( host_type ) ;
191193
192194 if ( debug_mode ) {
193- logger . info ( 'Generating ' + host_type + ' Config:' , host ) ;
195+ logger . info ( 'Generating ' + nice_host_type + ' Config:' , JSON . stringify ( host , null , 2 ) ) ;
194196 }
195197
196198 // logger.info('host = ' + JSON.stringify(host, null, 2));
@@ -201,10 +203,10 @@ const internalNginx = {
201203
202204 return new Promise ( ( resolve , reject ) => {
203205 let template = null ;
204- let filename = internalNginx . getConfigName ( host_type , host . id ) ;
206+ let filename = internalNginx . getConfigName ( nice_host_type , host . id ) ;
205207
206208 try {
207- template = fs . readFileSync ( __dirname + '/../templates/' + host_type + '.conf' , { encoding : 'utf8' } ) ;
209+ template = fs . readFileSync ( __dirname + '/../templates/' + nice_host_type + '.conf' , { encoding : 'utf8' } ) ;
208210 } catch ( err ) {
209211 reject ( new error . ConfigurationError ( err . message ) ) ;
210212 return ;
@@ -214,7 +216,7 @@ const internalNginx = {
214216 let origLocations ;
215217
216218 // Manipulate the data a bit before sending it to the template
217- if ( host_type !== 'default' ) {
219+ if ( nice_host_type !== 'default' ) {
218220 host . use_default_location = true ;
219221 if ( typeof host . advanced_config !== 'undefined' && host . advanced_config ) {
220222 host . use_default_location = ! internalNginx . advancedConfigHasDefaultLocation ( host . advanced_config ) ;
@@ -319,69 +321,82 @@ const internalNginx = {
319321 } ) ;
320322 } ,
321323
324+ /**
325+ * A simple wrapper around unlinkSync that writes to the logger
326+ *
327+ * @param {String } filename
328+ */
329+ deleteFile : ( filename ) => {
330+ logger . debug ( 'Deleting file: ' + filename ) ;
331+ try {
332+ fs . unlinkSync ( filename ) ;
333+ } catch ( err ) {
334+ logger . debug ( 'Could not delete file:' , JSON . stringify ( err , null , 2 ) ) ;
335+ } ;
336+ } ,
337+
338+ /**
339+ *
340+ * @param {String } host_type
341+ * @returns String
342+ */
343+ getFileFriendlyHostType : ( host_type ) => {
344+ return host_type . replace ( new RegExp ( '-' , 'g' ) , '_' ) ;
345+ } ,
346+
322347 /**
323348 * This removes the temporary nginx config file generated by `generateLetsEncryptRequestConfig`
324349 *
325350 * @param {Object } certificate
326- * @param {Boolean } [throw_errors]
327351 * @returns {Promise }
328352 */
329- deleteLetsEncryptRequestConfig : ( certificate , throw_errors ) => {
330- return new Promise ( ( resolve , reject ) => {
331- try {
332- let config_file = '/data/nginx/temp/letsencrypt_' + certificate . id + '.conf' ;
333-
334- if ( debug_mode ) {
335- logger . warn ( 'Deleting nginx config: ' + config_file ) ;
336- }
337-
338- fs . unlinkSync ( config_file ) ;
339- } catch ( err ) {
340- if ( debug_mode ) {
341- logger . warn ( 'Could not delete config:' , err . message ) ;
342- }
343-
344- if ( throw_errors ) {
345- reject ( err ) ;
346- }
347- }
348-
353+ deleteLetsEncryptRequestConfig : ( certificate ) => {
354+ const config_file = '/data/nginx/temp/letsencrypt_' + certificate . id + '.conf' ;
355+ return new Promise ( ( resolve /*, reject*/ ) => {
356+ internalNginx . deleteFile ( config_file ) ;
349357 resolve ( ) ;
350358 } ) ;
351359 } ,
352360
353361 /**
354362 * @param {String } host_type
355363 * @param {Object } [host]
356- * @param {Boolean } [throw_errors ]
364+ * @param {Boolean } [delete_err_file ]
357365 * @returns {Promise }
358366 */
359- deleteConfig : ( host_type , host , throw_errors ) => {
360- host_type = host_type . replace ( new RegExp ( '-' , 'g' ) , '_' ) ;
361-
362- return new Promise ( ( resolve , reject ) => {
363- try {
364- let config_file = internalNginx . getConfigName ( host_type , typeof host === 'undefined' ? 0 : host . id ) ;
365-
366- if ( debug_mode ) {
367- logger . warn ( 'Deleting nginx config: ' + config_file ) ;
368- }
369-
370- fs . unlinkSync ( config_file ) ;
371- } catch ( err ) {
372- if ( debug_mode ) {
373- logger . warn ( 'Could not delete config:' , err . message ) ;
374- }
375-
376- if ( throw_errors ) {
377- reject ( err ) ;
378- }
367+ deleteConfig : ( host_type , host , delete_err_file ) => {
368+ const config_file = internalNginx . getConfigName ( internalNginx . getFileFriendlyHostType ( host_type ) , typeof host === 'undefined' ? 0 : host . id ) ;
369+ const config_file_err = config_file + '.err' ;
370+
371+ return new Promise ( ( resolve /*, reject*/ ) => {
372+ internalNginx . deleteFile ( config_file ) ;
373+ if ( delete_err_file ) {
374+ internalNginx . deleteFile ( config_file_err ) ;
379375 }
380-
381376 resolve ( ) ;
382377 } ) ;
383378 } ,
384379
380+ /**
381+ * @param {String } host_type
382+ * @param {Object } [host]
383+ * @returns {Promise }
384+ */
385+ renameConfigAsError : ( host_type , host ) => {
386+ const config_file = internalNginx . getConfigName ( internalNginx . getFileFriendlyHostType ( host_type ) , typeof host === 'undefined' ? 0 : host . id ) ;
387+ const config_file_err = config_file + '.err' ;
388+
389+ return new Promise ( ( resolve /*, reject*/ ) => {
390+ fs . unlink ( config_file , ( ) => {
391+ // ignore result, continue
392+ fs . rename ( config_file , config_file_err , ( ) => {
393+ // also ignore result, as this is a debugging informative file anyway
394+ resolve ( ) ;
395+ } ) ;
396+ } ) ;
397+ } ) ;
398+ } ,
399+
385400 /**
386401 * @param {String } host_type
387402 * @param {Array } hosts
@@ -399,13 +414,12 @@ const internalNginx = {
399414 /**
400415 * @param {String } host_type
401416 * @param {Array } hosts
402- * @param {Boolean } [throw_errors]
403417 * @returns {Promise }
404418 */
405- bulkDeleteConfigs : ( host_type , hosts , throw_errors ) => {
419+ bulkDeleteConfigs : ( host_type , hosts ) => {
406420 let promises = [ ] ;
407421 hosts . map ( function ( host ) {
408- promises . push ( internalNginx . deleteConfig ( host_type , host , throw_errors ) ) ;
422+ promises . push ( internalNginx . deleteConfig ( host_type , host , true ) ) ;
409423 } ) ;
410424
411425 return Promise . all ( promises ) ;
0 commit comments