11/* Copyright (C) 2024 NooBaa */
22'use strict' ;
33
4+ const os = require ( 'os' ) ;
45const util = require ( 'util' ) ;
56const _ = require ( 'lodash' ) ;
67const path = require ( 'path' ) ;
78const P = require ( '../util/promise' ) ;
89const config = require ( '../../config' ) ;
10+ const pkg = require ( '../../package.json' ) ;
911const dbg = require ( '../util/debug_module' ) ( __filename ) ;
1012const os_utils = require ( '../util/os_utils' ) ;
1113const 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
0 commit comments