@@ -29,7 +29,9 @@ const internalNginx = {
29
29
. then ( ( ) => {
30
30
// Nginx is OK
31
31
// 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 ) ;
33
35
} )
34
36
. then ( ( ) => {
35
37
return internalNginx . generateConfig ( host_type , host ) ;
@@ -80,6 +82,9 @@ const internalNginx = {
80
82
. patch ( {
81
83
meta : combined_meta
82
84
} )
85
+ . then ( ( ) => {
86
+ internalNginx . renameConfigAsError ( host_type , host )
87
+ } )
83
88
. then ( ( ) => {
84
89
return internalNginx . deleteConfig ( host_type , host , true ) ;
85
90
} ) ;
@@ -121,13 +126,10 @@ const internalNginx = {
121
126
* @returns {String }
122
127
*/
123
128
getConfigName : ( host_type , host_id ) => {
124
- host_type = host_type . replace ( new RegExp ( '-' , 'g' ) , '_' ) ;
125
-
126
129
if ( host_type === 'default' ) {
127
130
return '/data/nginx/default_host/site.conf' ;
128
131
}
129
-
130
- return '/data/nginx/' + host_type + '/' + host_id + '.conf' ;
132
+ return '/data/nginx/' + internalNginx . getFileFriendlyHostType ( host_type ) + '/' + host_id + '.conf' ;
131
133
} ,
132
134
133
135
/**
@@ -155,12 +157,12 @@ const internalNginx = {
155
157
156
158
const locationRendering = async ( ) => {
157
159
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 } ,
159
161
{ ssl_forced : host . ssl_forced } , { caching_enabled : host . caching_enabled } , { block_exploits : host . block_exploits } ,
160
162
{ allow_websocket_upgrade : host . allow_websocket_upgrade } , { http2_support : host . http2_support } ,
161
163
{ hsts_enabled : host . hsts_enabled } , { hsts_subdomains : host . hsts_subdomains } , { access_list : host . access_list } ,
162
164
{ certificate : host . certificate } , host . locations [ i ] ) ;
163
-
165
+
164
166
if ( locationCopy . forward_host . indexOf ( '/' ) > - 1 ) {
165
167
const splitted = locationCopy . forward_host . split ( '/' ) ;
166
168
@@ -177,7 +179,7 @@ const internalNginx = {
177
179
} ;
178
180
179
181
locationRendering ( ) . then ( ( ) => resolve ( renderedLocations ) ) ;
180
-
182
+
181
183
} ) ;
182
184
} ,
183
185
@@ -187,10 +189,10 @@ const internalNginx = {
187
189
* @returns {Promise }
188
190
*/
189
191
generateConfig : ( host_type , host ) => {
190
- host_type = host_type . replace ( new RegExp ( '-' , 'g' ) , '_' ) ;
192
+ const nice_host_type = internalNginx . getFileFriendlyHostType ( host_type ) ;
191
193
192
194
if ( debug_mode ) {
193
- logger . info ( 'Generating ' + host_type + ' Config:' , host ) ;
195
+ logger . info ( 'Generating ' + nice_host_type + ' Config:' , JSON . stringify ( host , null , 2 ) ) ;
194
196
}
195
197
196
198
// logger.info('host = ' + JSON.stringify(host, null, 2));
@@ -201,10 +203,10 @@ const internalNginx = {
201
203
202
204
return new Promise ( ( resolve , reject ) => {
203
205
let template = null ;
204
- let filename = internalNginx . getConfigName ( host_type , host . id ) ;
206
+ let filename = internalNginx . getConfigName ( nice_host_type , host . id ) ;
205
207
206
208
try {
207
- template = fs . readFileSync ( __dirname + '/../templates/' + host_type + '.conf' , { encoding : 'utf8' } ) ;
209
+ template = fs . readFileSync ( __dirname + '/../templates/' + nice_host_type + '.conf' , { encoding : 'utf8' } ) ;
208
210
} catch ( err ) {
209
211
reject ( new error . ConfigurationError ( err . message ) ) ;
210
212
return ;
@@ -214,7 +216,7 @@ const internalNginx = {
214
216
let origLocations ;
215
217
216
218
// Manipulate the data a bit before sending it to the template
217
- if ( host_type !== 'default' ) {
219
+ if ( nice_host_type !== 'default' ) {
218
220
host . use_default_location = true ;
219
221
if ( typeof host . advanced_config !== 'undefined' && host . advanced_config ) {
220
222
host . use_default_location = ! internalNginx . advancedConfigHasDefaultLocation ( host . advanced_config ) ;
@@ -319,69 +321,82 @@ const internalNginx = {
319
321
} ) ;
320
322
} ,
321
323
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
+
322
347
/**
323
348
* This removes the temporary nginx config file generated by `generateLetsEncryptRequestConfig`
324
349
*
325
350
* @param {Object } certificate
326
- * @param {Boolean } [throw_errors]
327
351
* @returns {Promise }
328
352
*/
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 ) ;
349
357
resolve ( ) ;
350
358
} ) ;
351
359
} ,
352
360
353
361
/**
354
362
* @param {String } host_type
355
363
* @param {Object } [host]
356
- * @param {Boolean } [throw_errors ]
364
+ * @param {Boolean } [delete_err_file ]
357
365
* @returns {Promise }
358
366
*/
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 ) ;
379
375
}
380
-
381
376
resolve ( ) ;
382
377
} ) ;
383
378
} ,
384
379
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
+
385
400
/**
386
401
* @param {String } host_type
387
402
* @param {Array } hosts
@@ -399,13 +414,12 @@ const internalNginx = {
399
414
/**
400
415
* @param {String } host_type
401
416
* @param {Array } hosts
402
- * @param {Boolean } [throw_errors]
403
417
* @returns {Promise }
404
418
*/
405
- bulkDeleteConfigs : ( host_type , hosts , throw_errors ) => {
419
+ bulkDeleteConfigs : ( host_type , hosts ) => {
406
420
let promises = [ ] ;
407
421
hosts . map ( function ( host ) {
408
- promises . push ( internalNginx . deleteConfig ( host_type , host , throw_errors ) ) ;
422
+ promises . push ( internalNginx . deleteConfig ( host_type , host , true ) ) ;
409
423
} ) ;
410
424
411
425
return Promise . all ( promises ) ;
0 commit comments