Skip to content

Commit 92c99c7

Browse files
committed
NC | Online upgrade process small fixes
Signed-off-by: Romy <35330373+romayalon@users.noreply.github.com>
1 parent b7a2cae commit 92c99c7

File tree

4 files changed

+95
-47
lines changed

4 files changed

+95
-47
lines changed

src/cmd/nsfs.js

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const BucketSpaceFS = require('../sdk/bucketspace_fs');
4141
const SensitiveString = require('../util/sensitive_string');
4242
const endpoint_stats_collector = require('../sdk/endpoint_stats_collector');
4343
//const { RPC_BUFFERS } = require('../rpc');
44-
const pkg = require('../../package.json');
4544
const AccountSDK = require('../sdk/account_sdk');
4645
const AccountSpaceFS = require('../sdk/accountspace_fs');
4746
const NoobaaEvent = require('../manage_nsfs/manage_nsfs_events_utils').NoobaaEvent;
@@ -240,45 +239,6 @@ class NsfsAccountSDK extends AccountSDK {
240239
}
241240
}
242241

243-
async function init_nc_system(config_root) {
244-
const config_fs = new ConfigFS(config_root);
245-
const system_data = await config_fs.get_system_config_file({silent_if_missing: true});
246-
247-
const hostname = os.hostname();
248-
// If the system data already exists, we should not create it again
249-
const updated_system_json = system_data || {};
250-
if (updated_system_json[hostname]?.current_version && updated_system_json.config_directory) return;
251-
if (!updated_system_json[hostname]?.current_version) {
252-
updated_system_json[hostname] = {
253-
current_version: pkg.version,
254-
upgrade_history: { successful_upgrades: [], last_failure: undefined }
255-
};
256-
}
257-
// If it's the first time a config_directory data is added to system.json
258-
if (!updated_system_json.config_directory) {
259-
updated_system_json.config_directory = {
260-
config_dir_version: config_fs.config_dir_version,
261-
upgrade_package_version: pkg.version,
262-
phase: 'CONFIG_DIR_UNLOCKED',
263-
upgrade_history: { successful_upgrades: [], last_failure: undefined }
264-
};
265-
}
266-
try {
267-
if (system_data) {
268-
await config_fs.update_system_config_file(JSON.stringify(updated_system_json));
269-
console.log('updated NC system data with version: ', pkg.version);
270-
} else {
271-
await config_fs.create_system_config_file(JSON.stringify(updated_system_json));
272-
console.log('created NC system data with version: ', pkg.version);
273-
}
274-
} catch (err) {
275-
const msg = 'failed to create/update NC system data due to - ' + err.message;
276-
const error = new Error(msg);
277-
console.error(msg, err);
278-
throw error;
279-
}
280-
}
281-
282242
async function main(argv = minimist(process.argv.slice(2))) {
283243
try {
284244
config.DB_TYPE = 'none';
@@ -313,11 +273,6 @@ async function main(argv = minimist(process.argv.slice(2))) {
313273
const versioning = argv.versioning || 'DISABLED';
314274
const fs_root = argv._[0] || '';
315275

316-
// Do not move this function - we need to update RPM changes before starting the endpoint
317-
const config_fs = new ConfigFS(nsfs_config_root);
318-
const nc_upgrade_manager = new NCUpgradeManager(config_fs);
319-
await nc_upgrade_manager.update_rpm_upgrade();
320-
321276
const fs_config = {
322277
uid,
323278
gid,
@@ -363,7 +318,17 @@ async function main(argv = minimist(process.argv.slice(2))) {
363318
nsfs_config_root,
364319
});
365320

366-
if (!simple_mode) await init_nc_system(nsfs_config_root);
321+
if (!simple_mode) {
322+
// Do not move this function - we need to create/update RPM changes before starting the endpoint
323+
const config_fs = new ConfigFS(nsfs_config_root);
324+
const system_data = await config_fs.get_system_config_file({ silent_if_missing: true });
325+
if (system_data && system_data[os.hostname()]) {
326+
const nc_upgrade_manager = new NCUpgradeManager(config_fs);
327+
await nc_upgrade_manager.update_rpm_upgrade();
328+
} else {
329+
await config_fs.init_nc_system();
330+
}
331+
}
367332

368333
const endpoint = require('../endpoint/endpoint');
369334
await endpoint.main({

src/sdk/config_fs.js

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* Copyright (C) 2024 NooBaa */
22
'use strict';
33

4+
const os = require('os');
45
const util = require('util');
56
const _ = require('lodash');
67
const path = require('path');
78
const P = require('../util/promise');
89
const config = require('../../config');
10+
const pkg = require('../../package.json');
911
const dbg = require('../util/debug_module')(__filename);
1012
const os_utils = require('../util/os_utils');
1113
const SensitiveString = require('../util/sensitive_string');
@@ -1026,6 +1028,41 @@ class ConfigFS {
10261028
}
10271029
}
10281030

1031+
/**
1032+
* init_nc_system creates/updates system.json file
1033+
* if system.json does not exist (a new system) - host and config dir data will be set on the newly created file
1034+
* else -
1035+
* 1. if the host data already exist in system.json - return
1036+
* 2. set the host data on system.json data and update the file
1037+
* Note - config directory data on upgraded systems will be set by nc_upgrade_manager
1038+
* @returns
1039+
*/
1040+
async init_nc_system() {
1041+
const system_data = await this.get_system_config_file({silent_if_missing: true});
1042+
1043+
let updated_system_json = system_data || {};
1044+
const is_new_system = !system_data;
1045+
const hostname = os.hostname();
1046+
try {
1047+
if (is_new_system) {
1048+
updated_system_json = this._get_new_system_json_data();
1049+
await this.create_system_config_file(JSON.stringify(updated_system_json));
1050+
dbg.log0('created NC system data with version: ', pkg.version);
1051+
} else {
1052+
if (updated_system_json[hostname]?.current_version) return;
1053+
const new_host_data = this._get_new_hostname_data();
1054+
updated_system_json = { ...updated_system_json, new_host_data };
1055+
await this.update_system_config_file(JSON.stringify(updated_system_json));
1056+
dbg.log0('updated NC system data with version: ', pkg.version);
1057+
}
1058+
} catch (err) {
1059+
const msg = 'failed to create/update NC system data due to - ' + err.message;
1060+
const error = new Error(msg);
1061+
dbg.error(msg, err);
1062+
throw error;
1063+
}
1064+
}
1065+
10291066
////////////////////////
10301067
/// HELPERS ////
10311068
////////////////////////
@@ -1093,8 +1130,13 @@ class ConfigFS {
10931130
* @returns {Promise<void>}
10941131
*/
10951132
async _throw_if_config_dir_locked() {
1096-
const system_data = await this.get_system_config_file({silent_if_missing: true});
1133+
const system_data = await this.get_system_config_file({ silent_if_missing: true });
1134+
// if system was never created, currently we allow using the CLI without creating system
1135+
// we should consider changing it to throw on this scenario as well
10971136
if (!system_data) return;
1137+
if (!system_data.config_directory) {
1138+
throw new RpcError('CONFIG_DIR_VERSION_MISMATCH', `config_directory data is missing in system.json, any updates to the config directory are blocked until the config dir upgrade`);
1139+
}
10981140
const running_code_config_dir_version = this.config_dir_version;
10991141
const system_config_dir_version = system_data.config_directory.config_dir_version;
11001142
const ver_comparison = version_compare(running_code_config_dir_version, system_config_dir_version);
@@ -1107,6 +1149,40 @@ class ConfigFS {
11071149
`mentioned in system.json =${system_config_dir_version}, any updates to the config directory are blocked until the source code upgrade`);
11081150
}
11091151
}
1152+
1153+
/**
1154+
* _get_new_hostname_data returns new hostanme data for system.json
1155+
* @returns {Object}
1156+
*/
1157+
_get_new_hostname_data() {
1158+
return {
1159+
[os.hostname()]: {
1160+
current_version: pkg.version,
1161+
upgrade_history: {
1162+
successful_upgrades: []
1163+
},
1164+
},
1165+
};
1166+
}
1167+
1168+
/**
1169+
* _get_new_system_json_data returns new system.json data
1170+
* @returns {Object}
1171+
*/
1172+
_get_new_system_json_data() {
1173+
return {
1174+
...this._get_new_hostname_data(),
1175+
config_directory: {
1176+
config_dir_version: this.config_dir_version,
1177+
upgrade_package_version: pkg.version,
1178+
phase: 'CONFIG_DIR_UNLOCKED',
1179+
upgrade_history: {
1180+
successful_upgrades: [],
1181+
last_failure: undefined
1182+
}
1183+
}
1184+
};
1185+
}
11101186
}
11111187

11121188
// EXPORTS

src/upgrade/nc_upgrade_manager.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class NCUpgradeManager {
137137
/**
138138
* config_directory_defaults returns a default initial config directory object
139139
* @param {Object} system_data
140+
* @returns {Object}
140141
*/
141142
config_directory_defaults(system_data) {
142143
const hosts_old_package_version = system_data?.[hostname]?.upgrade_history?.successful_upgrades?.[0]?.from_version;
@@ -241,6 +242,7 @@ class NCUpgradeManager {
241242
/**
242243
* _run_nc_upgrade_scripts runs the config directory upgrade scripts
243244
* @param {Object} this_upgrade
245+
* @returns {Promise<Void>}
244246
*/
245247
async _run_nc_upgrade_scripts(this_upgrade) {
246248
try {
@@ -259,6 +261,7 @@ class NCUpgradeManager {
259261
* 3. upgrade_package_version is the new source code version
260262
* 4. add the finished upgrade to the successful_upgrades array
261263
* @param {Object} system_data
264+
* @param {Object} this_upgrade
262265
* @returns {Promise<Void>}
263266
*/
264267
async _update_config_dir_upgrade_finish(system_data, this_upgrade) {
@@ -281,6 +284,9 @@ class NCUpgradeManager {
281284
/**
282285
* _update_config_dir_upgrade_failed updates the system.json on failure of the upgrade
283286
* @param {Object} system_data
287+
* @param {Object} this_upgrade
288+
* @param {Error} error
289+
* @returns {Promise<Void>}
284290
*/
285291
async _update_config_dir_upgrade_failed(system_data, this_upgrade, error) {
286292
system_data.config_directory.upgrade_history.last_failure = this_upgrade;

src/upgrade/upgrade_utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function should_upgrade(server_version, container_version) {
6767
* load_required_scripts loads all scripts that should be run according to the given versions
6868
* @param {string} server_version
6969
* @param {string} container_version
70+
* @param {string} upgrade_scripts_dir
7071
*/
7172
async function load_required_scripts(server_version, container_version, upgrade_scripts_dir) {
7273
// expecting scripts directories to be in a semver format. e.g. ./upgrade_scripts/5.0.1

0 commit comments

Comments
 (0)