Skip to content

Commit 1148a8b

Browse files
committed
NC | Lifecycle | Convert NCLifecycle to a class
Signed-off-by: Romy <35330373+romayalon@users.noreply.github.com>
1 parent 8269aa9 commit 1148a8b

File tree

9 files changed

+810
-802
lines changed

9 files changed

+810
-802
lines changed

src/cmd/manage_nsfs.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const { account_id_cache } = require('../sdk/accountspace_fs');
2727
const ManageCLIError = require('../manage_nsfs/manage_nsfs_cli_errors').ManageCLIError;
2828
const ManageCLIResponse = require('../manage_nsfs/manage_nsfs_cli_responses').ManageCLIResponse;
2929
const manage_nsfs_glacier = require('../manage_nsfs/manage_nsfs_glacier');
30-
const noobaa_cli_lifecycle = require('../manage_nsfs/nc_lifecycle');
30+
const { NCLifecycle } = require('../manage_nsfs/nc_lifecycle');
3131
const manage_nsfs_logging = require('../manage_nsfs/manage_nsfs_logging');
3232
const noobaa_cli_diagnose = require('../manage_nsfs/diagnose');
3333
const noobaa_cli_upgrade = require('../manage_nsfs/upgrade');
@@ -892,7 +892,8 @@ async function lifecycle_management(args) {
892892
const should_continue_last_run = get_boolean_or_string_value(args.continue);
893893
try {
894894
const options = { disable_service_validation, disable_runtime_validation, short_status, should_continue_last_run };
895-
const { should_run, lifecycle_run_status } = await noobaa_cli_lifecycle.run_lifecycle_under_lock(config_fs, options);
895+
const nc_lifecycle = new NCLifecycle(config_fs, options);
896+
const { should_run, lifecycle_run_status } = await nc_lifecycle.run_lifecycle_under_lock();
896897
if (should_run) {
897898
write_stdout_response(ManageCLIResponse.LifecycleSuccessful, lifecycle_run_status);
898899
} else {

src/manage_nsfs/manage_nsfs_cli_utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async function get_options_from_file(file_path) {
124124
// we don't pass neither config_root_backend nor fs_backend
125125
const fs_context = native_fs_utils.get_process_fs_context();
126126
try {
127-
const input_options_with_data = await native_fs_utils.read_file(fs_context, file_path);
127+
const input_options_with_data = await native_fs_utils.read_file(fs_context, file_path, { parse_json: true });
128128
return input_options_with_data;
129129
} catch (err) {
130130
if (err.code === 'ENOENT') throw_cli_error(ManageCLIError.InvalidFilePath, file_path);

src/manage_nsfs/nc_lifecycle.js

Lines changed: 785 additions & 780 deletions
Large diffs are not rendered by default.

src/manage_nsfs/nc_master_key_manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class NCMasterKeysManager {
140140
try {
141141
const stat = await nb_native().fs.stat(fs_context, master_keys_path);
142142
if (stat.ctime.getTime() === this.last_init_time) return;
143-
const master_keys = await native_fs_utils.read_file(fs_context, master_keys_path);
143+
const master_keys = await native_fs_utils.read_file(fs_context, master_keys_path, { parse_json: true });
144144

145145
this._set_keys(master_keys);
146146
this.last_init_time = stat.ctime.getTime();

src/test/system_tests/test_utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,12 +737,15 @@ async function clean_config_dir(config_fs, custom_config_dir_path) {
737737
* @param {nb.NativeFSContext} fs_context
738738
* @param {String} file_path
739739
* @param {Object} file_data
740+
* @param {{stringify_json?: Boolean}} [options={}]
740741
*/
741-
async function create_file(fs_context, file_path, file_data) {
742+
async function create_file(fs_context, file_path, file_data, options = {}) {
743+
const buf = Buffer.from(options?.stringify_json ? JSON.stringify(file_data) : file_data);
742744
await nb_native().fs.writeFile(
743745
fs_context,
744746
file_path,
745-
Buffer.from(JSON.stringify(file_data)), {
747+
buf,
748+
{
746749
mode: native_fs_utils.get_umasked_mode(config.BASE_MODE_FILE)
747750
}
748751
);

src/test/unit_tests/jest_tests/test_config_dir_restructure_upgrade_script.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('move_old_accounts_dir', () => {
101101
account_names_obj[account_data.name] = account_data;
102102
if (account_id % 2 === 0) {
103103
const backup_file_path = path.join(hidden_old_accounts_path, default_config_fs.json(account_data.name));
104-
await create_file(default_config_fs.fs_context, backup_file_path, account_data);
104+
await create_file(default_config_fs.fs_context, backup_file_path, account_data, { stringify_json: true });
105105
}
106106
});
107107
await move_old_accounts_dir(default_config_fs, Object.keys(account_names_obj), mock_old_version, dbg);
@@ -183,7 +183,7 @@ describe('create_account_access_keys_index_if_missing', () => {
183183
const mock_id_dir = path.join(default_config_fs.identities_dir_path, 'mock_id_dir');
184184
const mock_link_id_path = path.join(mock_id_dir, 'identity.json');
185185
await create_fresh_path(mock_id_dir);
186-
await create_file(default_config_fs.fs_context, mock_link_id_path, { mock_key: 'mock_value' });
186+
await create_file(default_config_fs.fs_context, mock_link_id_path, { mock_key: 'mock_value' }, { stringify_json: true });
187187
await symlink_account_access_keys(default_config_fs, account_data.access_keys, mock_link_id_path);
188188
const identity_path = default_config_fs.get_identity_path_by_id(account_data._id);
189189
const account_upgrade_params = { ...account_data, identity_path };
@@ -275,7 +275,7 @@ describe('create_account_name_index_if_missing', () => {
275275
const mock_id_dir = path.join(default_config_fs.identities_dir_path, 'mock_id_dir');
276276
const mock_link_id_path = path.join(mock_id_dir, 'identity.json');
277277
await create_fresh_path(mock_id_dir);
278-
await create_file(default_config_fs.fs_context, mock_link_id_path, { mock_key: 'mock_value' });
278+
await create_file(default_config_fs.fs_context, mock_link_id_path, { mock_key: 'mock_value' }, { stringify_json: true });
279279
await symlink_account_name(default_config_fs, account_data.name, mock_link_id_path);
280280
const identity_path = default_config_fs.get_identity_path_by_id(account_data._id);
281281
const account_upgrade_params = { ...account_data, account_name: account_data.name, identity_path };

src/test/unit_tests/jest_tests/test_nc_lifecycle_cli.test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ describe('noobaa cli - lifecycle notifications', () => {
739739
config.NOTIFICATION_CONNECT_DIR = tmp_conn_dir_path;
740740
fs.writeFileSync(http_connect_path, JSON.stringify(http_connect));
741741
await object_sdk.put_bucket_notification(notifications_json);
742+
await config_fs.create_config_json_file(JSON.stringify({ NOTIFICATION_LOG_DIR: tmp_lifecycle_logs_dir_path }));
742743
});
743744

744745
afterEach(async () => {
@@ -754,12 +755,10 @@ describe('noobaa cli - lifecycle notifications', () => {
754755

755756
it('lifecycle_cli - lifecycle rule with Delete notification', async () => {
756757
await object_sdk.set_bucket_lifecycle_configuration_rules({ name: test_bucket, rules: lifecycle_rule_delete_all });
757-
758758
await create_object(object_sdk, test_bucket, test_key1, 100, true);
759759
await create_object(object_sdk, test_bucket, test_key2, 100, true);
760+
await exec_manage_cli(TYPES.LIFECYCLE, '', { disable_service_validation: 'true', disable_runtime_validation: 'true', config_root }, true);
760761

761-
await exec_manage_cli(TYPES.LIFECYCLE, '', {disable_service_validation: 'true', disable_runtime_validation: 'true', config_root}, undefined,
762-
{NOTIFICATION_LOG_DIR: tmp_lifecycle_logs_dir_path});
763762
const object_list = await object_sdk.list_objects({bucket: test_bucket});
764763
expect(object_list.objects.length).toBe(0);
765764

@@ -781,9 +780,7 @@ describe('noobaa cli - lifecycle notifications', () => {
781780

782781
await create_object(object_sdk, test_bucket, test_key1, 100, false);
783782
await create_object(object_sdk, test_bucket, test_key2, 100, false);
784-
785-
await exec_manage_cli(TYPES.LIFECYCLE, '', {disable_service_validation: 'true', disable_runtime_validation: 'true', config_root}, undefined,
786-
{NOTIFICATION_LOG_DIR: tmp_lifecycle_logs_dir_path});
783+
await exec_manage_cli(TYPES.LIFECYCLE, '', { disable_service_validation: 'true', disable_runtime_validation: 'true', config_root }, true);
787784

788785
const object_list = await object_sdk.list_object_versions({bucket: test_bucket});
789786
expect(object_list.objects.length).toBe(4);
@@ -809,8 +806,8 @@ describe('noobaa cli - lifecycle notifications', () => {
809806
await create_object(object_sdk, test_bucket, test_key1, 100, false);
810807
await create_object(object_sdk, test_bucket, test_key2, 100, false);
811808

812-
await exec_manage_cli(TYPES.LIFECYCLE, '', {disable_service_validation: 'true', disable_runtime_validation: 'true', config_root}, undefined,
813-
{NOTIFICATION_LOG_DIR: tmp_lifecycle_logs_dir_path});
809+
await exec_manage_cli(TYPES.LIFECYCLE, '', { disable_service_validation: 'true', disable_runtime_validation: 'true', config_root }, true);
810+
await config_fs.delete_config_json_file();
814811

815812
const object_list = await object_sdk.list_object_versions({bucket: test_bucket});
816813
expect(object_list.objects.length).toBe(4);

src/test/unit_tests/test_bucketspace_fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ mocha.describe('bucketspace_fs', function() {
552552
assert.equal(objects.buckets.length, 1);
553553
assert.equal(objects.buckets[0].name.unwrap(), expected_bucket_name);
554554
const bucket_config_path = get_config_file_path(CONFIG_SUBDIRS.BUCKETS, expected_bucket_name);
555-
const bucket_data = await read_file(process_fs_context, bucket_config_path);
555+
const bucket_data = await read_file(process_fs_context, bucket_config_path, { parse_json: true });
556556
assert.equal(objects.buckets[0].creation_date, bucket_data.creation_date);
557557
});
558558
});
@@ -715,7 +715,7 @@ mocha.describe('bucketspace_fs', function() {
715715
const param = { name: test_bucket, versioning: 'ENABLED' };
716716
await bucketspace_fs.set_bucket_versioning(param, dummy_object_sdk);
717717
const bucket_config_path = get_config_file_path(CONFIG_SUBDIRS.BUCKETS, param.name);
718-
const bucket = await read_file(process_fs_context, bucket_config_path);
718+
const bucket = await read_file(process_fs_context, bucket_config_path, { parse_json: true });
719719
assert.equal(bucket.versioning, 'ENABLED');
720720

721721
});

src/util/native_fs_utils.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,14 @@ async function folder_delete(dir, fs_context, is_temp, silent_if_missing) {
663663
* read_file reads file and returns the parsed file data as object
664664
* @param {nb.NativeFSContext} fs_context
665665
* @param {string} _path
666+
* @param {{parse_json?: Boolean}} [options]
666667
* @return {Promise<object>}
667668
*/
668-
async function read_file(fs_context, _path) {
669+
async function read_file(fs_context, _path, options = {}) {
669670
const { data } = await nb_native().fs.readFile(fs_context, _path);
670-
const data_parsed = JSON.parse(data.toString());
671-
return data_parsed;
671+
let data_parsed;
672+
if (options?.parse_json) data_parsed = JSON.parse(data.toString());
673+
return data_parsed || data.toString();
672674
}
673675

674676

0 commit comments

Comments
 (0)