Skip to content

Commit

Permalink
Merge pull request #8615 from romayalon/romy-config-dir-locked-unlock…
Browse files Browse the repository at this point in the history
…ed-enum

NC | Online Upgrade | Short refactoring
  • Loading branch information
romayalon authored Dec 23, 2024
2 parents df9f8b2 + ce3efbc commit 95e739f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/manage_nsfs/health.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const P = require('../util/promise');
const config = require('../../config');
const os_util = require('../util/os_utils');
const nb_native = require('../util/nb_native');
const { CONFIG_DIR_PHASES } = require('../sdk/config_fs');
const native_fs_utils = require('../util/native_fs_utils');
const { read_stream_join } = require('../util/buffer_utils');
const { make_https_request } = require('../util/http_utils');
const { TYPES } = require('./manage_nsfs_constants');
const { get_boolean_or_string_value, throw_cli_error, write_stdout_response, get_bucket_owner_account_by_id } = require('./manage_nsfs_cli_utils');
const { ManageCLIResponse } = require('./manage_nsfs_cli_responses');
const ManageCLIError = require('./manage_nsfs_cli_errors').ManageCLIError;
const { CONFIG_DIR_LOCKED, CONFIG_DIR_UNLOCKED } = require('../upgrade/nc_upgrade_manager');


const HOSTNAME = 'localhost';
Expand Down Expand Up @@ -472,10 +472,10 @@ class NSFSHealth {
*/
_get_config_dir_upgrade_status(config_dir_data) {
if (config_dir_data.in_progress_upgrade) return { in_progress_upgrade: config_dir_data.in_progress_upgrade };
if (config_dir_data.phase === CONFIG_DIR_LOCKED) {
if (config_dir_data.phase === CONFIG_DIR_PHASES.CONFIG_DIR_LOCKED) {
return { error: 'last_upgrade_failed', last_failure: config_dir_data.upgrade_history.last_failure };
}
if (config_dir_data.phase === CONFIG_DIR_UNLOCKED) {
if (config_dir_data.phase === CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED) {
return { message: 'there is no in-progress upgrade' };
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/sdk/config_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ const CONFIG_TYPES = Object.freeze({
const JSON_SUFFIX = '.json';
const SYMLINK_SUFFIX = '.symlink';

const CONFIG_DIR_PHASES = Object.freeze({
CONFIG_DIR_LOCKED: 'CONFIG_DIR_LOCKED',
CONFIG_DIR_UNLOCKED: 'CONFIG_DIR_UNLOCKED'
});

// TODO: A General Disclaimer about symlinks manipulated by this class -
// currently we use direct symlink()/ unlink()
// safe_link / safe_unlink can be better but the current impl causing ELOOP - Too many levels of symbolic links
Expand All @@ -71,6 +76,8 @@ const SYMLINK_SUFFIX = '.symlink';
* The upgrade script will run via `noobaa-cli upgrade run command`
*/

const CONFIG_DIR_VERSION = '1.0.0';

class ConfigFS {

/**
Expand All @@ -81,7 +88,7 @@ class ConfigFS {
constructor(config_root, config_root_backend, fs_context) {
this.config_root = config_root;
this.config_root_backend = config_root_backend || config.NSFS_NC_CONFIG_DIR_BACKEND;
this.config_dir_version = '1.0.0';
this.config_dir_version = CONFIG_DIR_VERSION;
this.old_accounts_dir_path = path.join(config_root, CONFIG_SUBDIRS.ACCOUNTS);
this.accounts_by_name_dir_path = path.join(config_root, CONFIG_SUBDIRS.ACCOUNTS_BY_NAME);
this.identities_dir_path = path.join(config_root, CONFIG_SUBDIRS.IDENTITIES);
Expand Down Expand Up @@ -1191,7 +1198,7 @@ class ConfigFS {
config_directory: {
config_dir_version: this.config_dir_version,
upgrade_package_version: pkg.version,
phase: 'CONFIG_DIR_UNLOCKED',
phase: CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED,
upgrade_history: {
successful_upgrades: [],
last_failure: undefined
Expand All @@ -1206,4 +1213,5 @@ exports.SYMLINK_SUFFIX = SYMLINK_SUFFIX;
exports.JSON_SUFFIX = JSON_SUFFIX;
exports.CONFIG_SUBDIRS = CONFIG_SUBDIRS;
exports.CONFIG_TYPES = CONFIG_TYPES;
exports.CONFIG_DIR_PHASES = CONFIG_DIR_PHASES;
exports.ConfigFS = ConfigFS;
9 changes: 5 additions & 4 deletions src/test/unit_tests/jest_tests/test_cli_upgrade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { TMP_PATH, exec_manage_cli, clean_config_dir, fail_test_if_default_config
const { ManageCLIError } = require('../../../manage_nsfs/manage_nsfs_cli_errors');
const { ManageCLIResponse } = require('../../../manage_nsfs/manage_nsfs_cli_responses');
const { TYPES, UPGRADE_ACTIONS } = require('../../../manage_nsfs/manage_nsfs_constants');
const { CONFIG_DIR_PHASES } = require('../../../sdk/config_fs');

const config_root = path.join(TMP_PATH, 'config_root_cli_upgrade_test');
const config_fs = new ConfigFS(config_root);
Expand Down Expand Up @@ -73,7 +74,7 @@ const old_expected_system_json2 = {
config_directory: {
'config_dir_version': '1.0.0',
'upgrade_package_version': '5.18.0',
'phase': 'CONFIG_DIR_UNLOCKED',
'phase': CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED,
'upgrade_history': {
'successful_upgrades': [
{
Expand Down Expand Up @@ -102,7 +103,7 @@ const old_expected_system_json5 = {
config_directory: {
'config_dir_version': '0.0.0',
'upgrade_package_version': '5.17.0',
'phase': 'CONFIG_DIR_UNLOCKED',
'phase': CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED,
'upgrade_history': {
'successful_upgrades': [
{
Expand Down Expand Up @@ -130,7 +131,7 @@ const new_expected_system_json = {
},
config_directory: {
'config_dir_version': '1.0.0',
'phase': 'CONFIG_DIR_UNLOCKED',
'phase': CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED,
'upgrade_history': {
'successful_upgrades': [
{
Expand Down Expand Up @@ -164,7 +165,7 @@ const new_expected_system_json = {
// },
// config_directory: {
// 'config_dir_version': '1',
// 'phase': 'CONFIG_DIR_UNLOCKED',
// 'phase': CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED,
// 'upgrade_history': {
// 'successful_upgrades': [
// {
Expand Down
24 changes: 12 additions & 12 deletions src/test/unit_tests/jest_tests/test_nc_upgrade_manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const fs_utils = require('../../../util/fs_utils');
const config = require('../../../../config');
const pkg = require('../../../../package.json');
const { NCUpgradeManager, DEFAULT_NC_UPGRADE_SCRIPTS_DIR, OLD_DEFAULT_PACKAGE_VERSION,
OLD_DEFAULT_CONFIG_DIR_VERSION, CONFIG_DIR_UNLOCKED, CONFIG_DIR_LOCKED } = require('../../../upgrade/nc_upgrade_manager');
const { ConfigFS } = require('../../../sdk/config_fs');
OLD_DEFAULT_CONFIG_DIR_VERSION } = require('../../../upgrade/nc_upgrade_manager');
const { ConfigFS, CONFIG_DIR_PHASES } = require('../../../sdk/config_fs');
const { TMP_PATH, create_redirect_file, create_config_dir,
fail_test_if_default_config_dir_exists, clean_config_dir, TEST_TIMEOUT } = require('../../system_tests/test_utils');

Expand Down Expand Up @@ -84,7 +84,7 @@ const old_expected_system_json_has_config_directory = {
config_directory: {
'config_dir_version': '1.0.0',
'upgrade_package_version': '5.18.0',
'phase': 'CONFIG_DIR_UNLOCKED',
'phase': CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED,
'upgrade_history': {
'successful_upgrades': [{
'timestamp': 1724687496424,
Expand Down Expand Up @@ -158,7 +158,7 @@ const old_expected_system_json_empty_successful_upgrades = {
},
config_directory: {
'config_dir_version': '1',
'phase': 'CONFIG_DIR_UNLOCKED',
'phase': CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED,
'upgrade_history': {
'successful_upgrades': []
}
Expand Down Expand Up @@ -513,7 +513,7 @@ describe('nc upgrade manager - upgrade config directory', () => {
const expected_data = {
...system_data,
config_directory: {
phase: CONFIG_DIR_UNLOCKED,
phase: CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED,
config_dir_version: this_upgrade.config_dir_to_version,
upgrade_package_version: this_upgrade.package_to_version,
upgrade_history: {
Expand Down Expand Up @@ -558,7 +558,7 @@ describe('nc upgrade manager - upgrade config directory', () => {
const expected_data = {
...system_data,
config_directory: {
phase: CONFIG_DIR_UNLOCKED,
phase: CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED,
config_dir_version: this_upgrade.config_dir_to_version,
upgrade_package_version: this_upgrade.package_to_version,
upgrade_history: {
Expand Down Expand Up @@ -599,14 +599,14 @@ describe('nc upgrade manager - upgrade config directory', () => {
package_from_version: '5.18.0',
package_to_version: '5.18.1',
};
system_data.config_directory.phase = CONFIG_DIR_LOCKED;
system_data.config_directory.phase = CONFIG_DIR_PHASES.CONFIG_DIR_LOCKED;
const new_error = new Error('this is a new last failure error');
await config_fs.create_system_config_file(JSON.stringify(system_data));
await nc_upgrade_manager._update_config_dir_upgrade_failed(system_data, this_upgrade, new_error);
const expected_data = {
...system_data,
config_directory: {
phase: CONFIG_DIR_LOCKED,
phase: CONFIG_DIR_PHASES.CONFIG_DIR_LOCKED,
config_dir_version: system_data.config_directory.config_dir_version,
upgrade_package_version: system_data.config_directory.upgrade_package_version,
upgrade_history: {
Expand All @@ -627,7 +627,7 @@ describe('nc upgrade manager - upgrade config directory', () => {
'package_from_version': '5.17.0',
'package_to_version': '5.18.0'
}];
system_data.config_directory.phase = CONFIG_DIR_LOCKED;
system_data.config_directory.phase = CONFIG_DIR_PHASES.CONFIG_DIR_LOCKED;
system_data.config_directory.upgrade_history.last_failure = {
'timestamp': 1714687496424,
'running_host': hostname,
Expand All @@ -653,7 +653,7 @@ describe('nc upgrade manager - upgrade config directory', () => {
const expected_data = {
...system_data,
config_directory: {
phase: CONFIG_DIR_LOCKED,
phase: CONFIG_DIR_PHASES.CONFIG_DIR_LOCKED,
config_dir_version: system_data.config_directory.config_dir_version,
upgrade_package_version: system_data.config_directory.upgrade_package_version,
upgrade_history: {
Expand All @@ -680,7 +680,7 @@ function assert_upgrade_start_data(actual_upgrade_start, expected_system_data) {
actual_upgrade_start.config_directory;
const expected_in_progress_upgrade = expected_system_data.config_directory?.in_progress_upgrade;

expect(phase).toBe(CONFIG_DIR_LOCKED);
expect(phase).toBe(CONFIG_DIR_PHASES.CONFIG_DIR_LOCKED);
expect(config_dir_version).toBe(expected_system_data.config_directory?.config_dir_version);
expect(upgrade_package_version).toBe(expected_system_data.config_directory?.upgrade_package_version);
expect(upgrade_history).toEqual(expected_system_data.config_directory?.upgrade_history);
Expand Down Expand Up @@ -727,7 +727,7 @@ function assert_config_dir_defaults(actual_config_dir_defaults, system_data) {
OLD_DEFAULT_PACKAGE_VERSION;
expect(config_dir_version).toBe(OLD_DEFAULT_CONFIG_DIR_VERSION);
expect(upgrade_package_version).toBe(expected_package_from_version);
expect(phase).toBe(CONFIG_DIR_UNLOCKED);
expect(phase).toBe(CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED);
expect(upgrade_history).toEqual({ successful_upgrades: [] });
}

Expand Down
10 changes: 5 additions & 5 deletions src/test/unit_tests/test_nc_health.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { get_process_fs_context } = require('../../util/native_fs_utils');
const { ManageCLIError } = require('../../manage_nsfs/manage_nsfs_cli_errors');
const { TYPES, DIAGNOSE_ACTIONS, ACTIONS } = require('../../manage_nsfs/manage_nsfs_constants');
const { TMP_PATH, create_fs_user_by_platform, delete_fs_user_by_platform, exec_manage_cli } = require('../system_tests/test_utils');
const { CONFIG_DIR_UNLOCKED, CONFIG_DIR_LOCKED } = require('../../upgrade/nc_upgrade_manager');
const { CONFIG_DIR_PHASES } = require('../../sdk/config_fs');

const tmp_fs_path = path.join(TMP_PATH, 'test_nc_health');
const DEFAULT_FS_CONFIG = get_process_fs_context();
Expand All @@ -39,7 +39,7 @@ const get_service_state_mock_default_response = [{ service_status: 'active', pid
const get_endpoint_response_mock_default_response = [{ response: { response_code: 'RUNNING', total_fork_count: 0 } }];
const get_system_config_mock_default_response = [{
...valid_system_json, config_directory: {
phase: CONFIG_DIR_UNLOCKED, config_dir_version: '1.0.0',
phase: CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED, config_dir_version: '1.0.0',
package_version: pkg.version, upgrade_history: []
} }];
const default_mock_upgrade_status = { message: 'there is no in-progress upgrade' };
Expand Down Expand Up @@ -188,7 +188,7 @@ mocha.describe('nsfs nc health', function() {
valid_system_json.config_directory = {
'config_dir_version': config_fs.config_dir_version,
'upgrade_package_version': pkg.version,
'phase': CONFIG_DIR_UNLOCKED
'phase': CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED
};
await Health.config_fs.create_system_config_file(JSON.stringify(valid_system_json));
set_mock_functions(Health, {
Expand Down Expand Up @@ -608,7 +608,7 @@ mocha.describe('nsfs nc health', function() {
valid_system_json.config_directory = {
'config_dir_version': config_fs.config_dir_version,
'upgrade_package_version': pkg.version,
'phase': CONFIG_DIR_LOCKED,
'phase': CONFIG_DIR_PHASES.CONFIG_DIR_LOCKED,
upgrade_history: {
successful_upgrades: [],
last_failure: { error: 'mock error'}
Expand All @@ -624,7 +624,7 @@ mocha.describe('nsfs nc health', function() {
valid_system_json.config_directory = {
config_dir_version: config_fs.config_dir_version,
upgrade_package_version: pkg.version,
phase: CONFIG_DIR_LOCKED,
phase: CONFIG_DIR_PHASES.CONFIG_DIR_LOCKED,
upgrade_history: {
successful_upgrades: [],
},
Expand Down
12 changes: 4 additions & 8 deletions src/upgrade/nc_upgrade_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ const path = require('path');
const util = require('util');
const pkg = require('../../package.json');
const dbg = require('../util/debug_module')(__filename);
const { CONFIG_DIR_PHASES } = require('../sdk/config_fs');
const { should_upgrade, run_upgrade_scripts, version_compare } = require('./upgrade_utils');

const hostname = os.hostname();

const CONFIG_DIR_LOCKED = 'CONFIG_DIR_LOCKED';
const CONFIG_DIR_UNLOCKED = 'CONFIG_DIR_UNLOCKED';
// prior to 5.18.0 - there is no config dir version, the config dir version to be used on the first upgrade is 0.0.0 (5.17.0 -> 5.18.0)
const OLD_DEFAULT_CONFIG_DIR_VERSION = '0.0.0';
const OLD_DEFAULT_PACKAGE_VERSION = '5.17.0';
Expand Down Expand Up @@ -143,7 +141,7 @@ class NCUpgradeManager {
return {
config_dir_version: OLD_DEFAULT_CONFIG_DIR_VERSION,
upgrade_package_version: hosts_old_package_version || OLD_DEFAULT_PACKAGE_VERSION,
phase: CONFIG_DIR_UNLOCKED,
phase: CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED,
upgrade_history: { successful_upgrades: [] }
};
}
Expand Down Expand Up @@ -227,7 +225,7 @@ class NCUpgradeManager {
async _update_config_dir_upgrade_start(system_data, options) {
const updated_config_directory = {
...system_data.config_directory,
phase: CONFIG_DIR_LOCKED,
phase: CONFIG_DIR_PHASES.CONFIG_DIR_LOCKED,
config_dir_version: options.config_dir_from_version,
upgrade_package_version: options.package_from_version,
in_progress_upgrade: {
Expand Down Expand Up @@ -276,7 +274,7 @@ class NCUpgradeManager {
const successful_upgrades = upgrade_history?.successful_upgrades || [];

const updated_config_directory = {
phase: CONFIG_DIR_UNLOCKED,
phase: CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED,
config_dir_version: this_upgrade.config_dir_to_version,
upgrade_package_version: this_upgrade.package_to_version,
upgrade_history: {
Expand Down Expand Up @@ -305,8 +303,6 @@ class NCUpgradeManager {


exports.NCUpgradeManager = NCUpgradeManager;
exports.CONFIG_DIR_UNLOCKED = CONFIG_DIR_UNLOCKED;
exports.CONFIG_DIR_LOCKED = CONFIG_DIR_LOCKED;
exports.OLD_DEFAULT_CONFIG_DIR_VERSION = OLD_DEFAULT_CONFIG_DIR_VERSION;
exports.OLD_DEFAULT_PACKAGE_VERSION = OLD_DEFAULT_PACKAGE_VERSION;
exports.DEFAULT_NC_UPGRADE_SCRIPTS_DIR = DEFAULT_NC_UPGRADE_SCRIPTS_DIR;

0 comments on commit 95e739f

Please sign in to comment.