2
2
'use strict' ;
3
3
4
4
const dbg = require ( '../util/debug_module' ) ( __filename ) ;
5
- const path = require ( 'path' ) ;
6
5
const _ = require ( 'lodash' ) ;
7
6
const P = require ( '../util/promise' ) ;
8
7
const config = require ( '../../config' ) ;
@@ -11,6 +10,7 @@ const nb_native = require('../util/nb_native');
11
10
const native_fs_utils = require ( '../util/native_fs_utils' ) ;
12
11
const { read_stream_join } = require ( '../util/buffer_utils' ) ;
13
12
const { make_https_request } = require ( '../util/http_utils' ) ;
13
+ const { JSON_SUFFIX } = require ( '../sdk/config_fs' ) ;
14
14
const { TYPES } = require ( './manage_nsfs_constants' ) ;
15
15
const { get_boolean_or_string_value, throw_cli_error, write_stdout_response } = require ( './manage_nsfs_cli_utils' ) ;
16
16
const { ManageCLIResponse } = require ( './manage_nsfs_cli_responses' ) ;
@@ -87,9 +87,9 @@ process.env.AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE = '1';
87
87
class NSFSHealth {
88
88
constructor ( options ) {
89
89
this . https_port = options . https_port ;
90
- this . config_root = options . config_root ;
91
90
this . all_account_details = options . all_account_details ;
92
91
this . all_bucket_details = options . all_bucket_details ;
92
+ this . config_fs = options . config_fs ;
93
93
}
94
94
95
95
/**
@@ -116,8 +116,8 @@ class NSFSHealth {
116
116
const service_health = service_status !== 'active' || pid === '0' || response_code !== 'RUNNING' ? 'NOTOK' : 'OK' ;
117
117
118
118
const error_code = await this . get_error_code ( service_status , pid , response_code ) ;
119
- if ( this . all_bucket_details ) bucket_details = await this . get_bucket_status ( this . config_root ) ;
120
- if ( this . all_account_details ) account_details = await this . get_account_status ( this . config_root ) ;
119
+ if ( this . all_bucket_details ) bucket_details = await this . get_bucket_status ( ) ;
120
+ if ( this . all_account_details ) account_details = await this . get_account_status ( ) ;
121
121
const health = {
122
122
service_name : NOOBAA_SERVICE ,
123
123
status : service_health ,
@@ -306,38 +306,48 @@ class NSFSHealth {
306
306
} ;
307
307
}
308
308
309
- async get_bucket_status ( config_root ) {
310
- const bucket_details = await this . get_storage_status ( config_root , TYPES . BUCKET , this . all_bucket_details ) ;
309
+ async get_bucket_status ( ) {
310
+ const bucket_details = await this . get_storage_status ( TYPES . BUCKET , this . all_bucket_details ) ;
311
311
return bucket_details ;
312
312
}
313
313
314
- async get_account_status ( config_root ) {
315
- const account_details = await this . get_storage_status ( config_root , TYPES . ACCOUNT , this . all_account_details ) ;
314
+ async get_account_status ( ) {
315
+ const account_details = await this . get_storage_status ( TYPES . ACCOUNT , this . all_account_details ) ;
316
316
return account_details ;
317
317
}
318
318
319
- async get_storage_status ( config_root , type , all_details ) {
320
- const fs_context = this . get_root_fs_context ( ) ;
321
- const config_root_type_path = this . get_config_path ( config_root , type ) ;
319
+ async get_storage_status ( type , all_details ) {
322
320
const invalid_storages = [ ] ;
323
321
const valid_storages = [ ] ;
324
322
//check for account and buckets dir paths
325
- try {
326
- await nb_native ( ) . fs . stat ( fs_context , config_root_type_path ) ;
327
- } catch ( err ) {
328
- dbg . log1 ( `Config root path missing ${ type } folder in ${ config_root_type_path } ` ) ;
323
+ let config_root_type_exists ;
324
+ let config_dir_path ;
325
+ if ( type === TYPES . BUCKET ) {
326
+ config_dir_path = this . config_fs . buckets_dir_path ;
327
+ config_root_type_exists = await this . config_fs . validate_config_dir_exists ( config_dir_path ) ;
328
+ } else if ( type === TYPES . ACCOUNT ) {
329
+ // TODO - handle iam accounts when directory structure changes - read_account_by_id
330
+ config_dir_path = this . config_fs . accounts_dir_path ;
331
+ config_root_type_exists = await this . config_fs . validate_config_dir_exists ( config_dir_path ) ;
332
+ }
333
+ // TODO - this is not a good handling for that - we need to take it to an upper level
334
+ if ( ! config_root_type_exists ) {
335
+ dbg . log1 ( `Config directory type - ${ type } is missing, ${ config_dir_path } ` ) ;
329
336
return {
330
337
invalid_storages : invalid_storages ,
331
338
valid_storages : valid_storages
332
339
} ;
333
340
}
334
- const entries = await nb_native ( ) . fs . readdir ( fs_context , config_root_type_path ) ;
335
- const config_files = entries . filter ( entree => ! native_fs_utils . isDirectory ( entree ) && entree . name . endsWith ( '.json' ) ) ;
341
+
342
+ const entries = type === TYPES . BUCKET ?
343
+ await this . config_fs . list_buckets ( ) :
344
+ await this . config_fs . list_root_accounts ( ) ;
345
+
346
+ const config_files = entries . filter ( entree => ! native_fs_utils . isDirectory ( entree ) && entree . name . endsWith ( JSON_SUFFIX ) ) ;
336
347
for ( const config_file of config_files ) {
337
348
// config_file get data or push error
338
- const config_file_path = path . join ( config_root_type_path , config_file . name ) ;
339
349
const { config_data = undefined , err_obj = undefined } =
340
- await get_config_file_data ( fs_context , config_file_path , config_file . name ) ;
350
+ await this . get_config_file_data_or_error_object ( type , config_file . name ) ;
341
351
if ( ! config_data && err_obj ) {
342
352
invalid_storages . push ( err_obj . invalid_storage ) ;
343
353
continue ;
@@ -351,9 +361,10 @@ class NSFSHealth {
351
361
config_data . nsfs_account_config . new_buckets_path ;
352
362
353
363
if ( type === TYPES . ACCOUNT ) {
364
+ const config_file_path = this . config_fs . get_account_path_by_name ( config_file . name ) ;
354
365
res = await is_new_buckets_path_valid ( config_file_path , config_data , storage_path ) ;
355
366
} else if ( type === TYPES . BUCKET ) {
356
- res = await is_bucket_storage_path_exists ( fs_context , config_data , storage_path ) ;
367
+ res = await is_bucket_storage_path_exists ( this . config_fs . fs_context , config_data , storage_path ) ;
357
368
}
358
369
if ( all_details && res . valid_storage ) {
359
370
valid_storages . push ( res . valid_storage ) ;
@@ -367,21 +378,52 @@ class NSFSHealth {
367
378
} ;
368
379
}
369
380
370
- get_config_path ( config_root , type ) {
371
- return path . join ( config_root , type === TYPES . BUCKET ? '/buckets' : '/accounts' ) ;
381
+ /**
382
+ * get_config_file_data_or_error_object return an object containing config_data or err_obj if error occurred
383
+ * @param {string } type
384
+ * @param {string } config_file_name
385
+ * @returns {Promise<object> }
386
+ */
387
+ async get_config_file_data_or_error_object ( type , config_file_name ) {
388
+ let config_data ;
389
+ let err_obj ;
390
+ try {
391
+ config_data = type === TYPES . BUCKET ?
392
+ await this . config_fs . get_bucket_by_name ( config_file_name ) :
393
+ // TODO - should be changed to id when moving to new structure for supporting iam accounts
394
+ await this . config_fs . get_account_by_name ( config_file_name ) ;
395
+ } catch ( err ) {
396
+ let err_code ;
397
+ const config_file_path = type === TYPES . BUCKET ?
398
+ await this . config_fs . get_bucket_path_by_name ( config_file_name ) :
399
+ // TODO - should be changed to id when moving to new structure for supporting iam accounts
400
+ await this . config_fs . get_account_path_by_name ( config_file_name ) ;
401
+
402
+ if ( err . code === 'ENOENT' ) {
403
+ dbg . log1 ( `Error: Config file path should be a valid path` , config_file_path , err ) ;
404
+ err_code = health_errors . MISSING_CONFIG . error_code ;
405
+ } else {
406
+ dbg . log1 ( 'Error: while accessing the config file: ' , config_file_path , err ) ;
407
+ err_code = health_errors . INVALID_CONFIG . error_code ;
408
+ }
409
+ err_obj = get_invalid_object ( config_file_name , config_file_path , undefined , err_code ) ;
410
+ }
411
+ return {
412
+ config_data,
413
+ err_obj
414
+ } ;
372
415
}
373
416
}
374
417
375
- async function get_health_status ( argv , global_config ) {
418
+ async function get_health_status ( argv , config_fs ) {
376
419
try {
377
- const config_root = global_config . config_root ;
378
420
const https_port = Number ( argv . https_port ) || config . ENDPOINT_SSL_PORT ;
379
421
const deployment_type = argv . deployment_type || 'nc' ;
380
422
const all_account_details = get_boolean_or_string_value ( argv . all_account_details ) ;
381
423
const all_bucket_details = get_boolean_or_string_value ( argv . all_bucket_details ) ;
382
424
383
425
if ( deployment_type === 'nc' ) {
384
- const health = new NSFSHealth ( { https_port, config_root , all_account_details, all_bucket_details } ) ;
426
+ const health = new NSFSHealth ( { https_port, all_account_details, all_bucket_details, config_fs } ) ;
385
427
const health_status = await health . nc_nsfs_health ( ) ;
386
428
write_stdout_response ( ManageCLIResponse . HealthStatus , health_status ) ;
387
429
} else {
@@ -432,6 +474,7 @@ async function is_new_buckets_path_valid(config_file_path, config_data, new_buck
432
474
}
433
475
434
476
try {
477
+ await nb_native ( ) . fs . stat ( account_fs_context , new_buckets_path ) ;
435
478
const accessible = await native_fs_utils . is_dir_rw_accessible ( account_fs_context , new_buckets_path ) ;
436
479
if ( ! accessible ) {
437
480
const new_err = new Error ( 'ACCESS DENIED' ) ;
@@ -452,36 +495,6 @@ async function is_new_buckets_path_valid(config_file_path, config_data, new_buck
452
495
return res_obj ;
453
496
}
454
497
455
- /**
456
- * get_config_file_data return an object containing config_data or err_obj if error occurred
457
- * @param {nb.NativeFSContext } fs_context
458
- * @param {string } config_file_path
459
- * @param {string } config_file_name
460
- * @returns {Promise<object> }
461
- */
462
- async function get_config_file_data ( fs_context , config_file_path , config_file_name ) {
463
- let config_data ;
464
- let err_obj ;
465
- try {
466
- const { data } = await nb_native ( ) . fs . readFile ( fs_context , config_file_path ) ;
467
- config_data = JSON . parse ( data . toString ( ) ) ;
468
- } catch ( err ) {
469
- let err_code ;
470
- if ( err . code === 'ENOENT' ) {
471
- dbg . log1 ( `Error: Config file path should be a valid path` , config_file_path , err ) ;
472
- err_code = health_errors . MISSING_CONFIG . error_code ;
473
- } else {
474
- dbg . log1 ( 'Error: while accessing the config file: ' , config_file_path , err ) ;
475
- err_code = health_errors . INVALID_CONFIG . error_code ;
476
- }
477
- err_obj = get_invalid_object ( config_file_name , config_file_path , undefined , err_code ) ;
478
- }
479
- return {
480
- config_data,
481
- err_obj
482
- } ;
483
- }
484
-
485
498
/**
486
499
* is_bucket_storage_path_exists checks if the underlying storage path of a bucket exists
487
500
* @param {nb.NativeFSContext } fs_context
0 commit comments