1
1
/* Copyright (C) 2024 NooBaa */
2
2
'use strict' ;
3
3
4
+ const util = require ( 'util' ) ;
4
5
const path = require ( 'path' ) ;
5
6
const P = require ( '../../../util/promise' ) ;
6
7
const config = require ( '../../../../config' ) ;
@@ -37,13 +38,11 @@ async function run({ dbg }) {
37
38
38
39
await config_fs . create_dir_if_missing ( config_fs . identities_dir_path ) ;
39
40
await config_fs . create_dir_if_missing ( config_fs . accounts_by_name_dir_path ) ;
40
- const tmp_access_keys_path = path . join ( config_fs . access_keys_dir_path , native_fs_utils . get_config_files_tmpdir ( ) ) ;
41
- await config_fs . create_dir_if_missing ( tmp_access_keys_path ) ;
42
41
43
42
const old_account_names = await config_fs . list_old_accounts ( ) ;
44
- const failed_accounts = await upgrade_accounts_config_files ( config_fs , old_account_names , tmp_access_keys_path , dbg ) ;
43
+ const failed_accounts = await upgrade_accounts_config_files ( config_fs , old_account_names , dbg ) ;
45
44
46
- if ( failed_accounts . length > 0 ) throw new Error ( 'NC upgrade process failed, failed_accounts array length is bigger than 0' + failed_accounts ) ;
45
+ if ( failed_accounts . length > 0 ) throw new Error ( 'NC upgrade process failed, failed_accounts array length is bigger than 0' + util . inspect ( failed_accounts ) ) ;
47
46
await move_old_accounts_dir ( fs_context , config_fs , old_account_names , dbg ) ;
48
47
} catch ( err ) {
49
48
dbg . error ( 'NC upgrade process failed due to - ' , err ) ;
@@ -60,13 +59,17 @@ async function run({ dbg }) {
60
59
* @param {* } dbg
61
60
* @returns {Promise<Object[]> }
62
61
*/
63
- async function upgrade_accounts_config_files ( config_fs , old_account_names , tmp_access_keys_path , dbg ) {
62
+ async function upgrade_accounts_config_files ( config_fs , old_account_names , dbg ) {
64
63
const failed_accounts = [ ] ;
64
+
65
+ const backup_access_keys_path = path . join ( config_fs . config_root , '.backup_access_keys_dir/' ) ;
66
+ await config_fs . create_dir_if_missing ( backup_access_keys_path ) ;
67
+
65
68
for ( const account_name of old_account_names ) {
66
69
let retries = 3 ;
67
70
while ( retries > 0 ) {
68
71
try {
69
- await upgrade_account_config_file ( config_fs , account_name , tmp_access_keys_path , dbg ) ;
72
+ await upgrade_account_config_file ( config_fs , account_name , backup_access_keys_path , dbg ) ;
70
73
break ;
71
74
} catch ( err ) {
72
75
retries -= 1 ;
@@ -79,6 +82,12 @@ async function upgrade_accounts_config_files(config_fs, old_account_names, tmp_a
79
82
}
80
83
}
81
84
}
85
+ try {
86
+ // delete dir only if it's empty
87
+ await nb_native ( ) . fs . rmdir ( config_fs . fs_context , backup_access_keys_path ) ;
88
+ } catch ( err ) {
89
+ dbg . warn ( `config_dir_restructure.upgrade_accounts_cofig_files could not delete access keys backup directory ${ backup_access_keys_path } err ${ err } ` ) ;
90
+ }
82
91
return failed_accounts ;
83
92
}
84
93
@@ -90,18 +99,18 @@ async function upgrade_accounts_config_files(config_fs, old_account_names, tmp_a
90
99
* 1.4. delete account old path
91
100
* @param {import('../../../sdk/config_fs').ConfigFS } config_fs
92
101
* @param {String } account_name
93
- * @param {String } tmp_access_keys_path
102
+ * @param {String } backup_access_keys_path
94
103
* @param {* } dbg
95
104
* @returns
96
105
*/
97
- async function upgrade_account_config_file ( config_fs , account_name , tmp_access_keys_path , dbg ) {
106
+ async function upgrade_account_config_file ( config_fs , account_name , backup_access_keys_path , dbg ) {
98
107
let account_upgrade_params ;
99
108
const fs_context = config_fs . fs_context ;
100
109
try {
101
110
account_upgrade_params = await prepare_account_upgrade_params ( config_fs , account_name ) ;
102
111
await create_identity_if_missing ( fs_context , account_upgrade_params , dbg ) ;
103
112
await create_account_name_index_if_missing ( config_fs , account_upgrade_params , dbg ) ;
104
- await create_account_access_keys_index_if_missing ( config_fs , account_upgrade_params , tmp_access_keys_path , dbg ) ;
113
+ await create_account_access_keys_index_if_missing ( config_fs , account_upgrade_params , backup_access_keys_path , dbg ) ;
105
114
} catch ( err ) {
106
115
dbg . warn ( `upgrade account config failed ${ account_name } , err ${ err } ` ) ;
107
116
throw err ;
@@ -130,7 +139,7 @@ async function prepare_account_upgrade_params(config_fs, account_name) {
130
139
const identity_dir_path = config_fs . get_identity_dir_path_by_id ( _id ) ;
131
140
132
141
const is_gpfs = native_fs_utils . _is_gpfs ( fs_context ) ;
133
- const dst_file = is_gpfs ? await native_fs_utils . open_file ( fs_context , undefined , identity_path , 'r ' ) : undefined ;
142
+ const dst_file = is_gpfs ? await native_fs_utils . open_file ( fs_context , undefined , identity_path , 'w ' ) : undefined ;
134
143
135
144
return {
136
145
account_name,
@@ -192,48 +201,41 @@ async function create_account_name_index_if_missing(config_fs, account_upgrade_p
192
201
* 1. iterate all access keys array (there should be only one access_key)
193
202
* 2. check if we already have an access_key symlink pointing to the identity, if there is, continue
194
203
* 3. symlink tmp access_key path to the identity path
195
- * 4. if GPFS - linkfileat the tmp access_key path to access_key path
196
- * 5. if POSIX - rename tmp access_key path to access_key path
197
- * on GPFS it's better to use linkfileat for performance improvements rather then rename
198
- * linkfileat also overrides the existing file
199
- * TODO - test on GPFS
204
+ * 4. rename tmp access_key path to access_key path - this will replace atomically the old symlink with the new one
200
205
* @param {import('../../../sdk/config_fs').ConfigFS } config_fs
201
206
* @param {AccountUpgradeParams } account_upgrade_params
207
+ * @param {String } backup_access_keys_path
202
208
* @param {* } dbg
203
209
* @returns {Promise<Void> }
204
210
*/
205
- async function create_account_access_keys_index_if_missing ( config_fs , account_upgrade_params , tmp_access_keys_path , dbg ) {
211
+ async function create_account_access_keys_index_if_missing ( config_fs , account_upgrade_params , backup_access_keys_path , dbg ) {
206
212
const { fs_context } = config_fs ;
207
213
const { access_keys, _id, identity_path } = account_upgrade_params ;
208
214
209
215
if ( access_keys ) {
210
216
for ( const { access_key } of access_keys ) {
211
217
const access_key_path = config_fs . get_account_or_user_path_by_access_key ( access_key ) ;
212
- const tmp_access_key_path = path . join ( tmp_access_keys_path , native_fs_utils . get_config_files_tmpdir ( ) ) ;
218
+ const tmp_access_key_path = path . join ( backup_access_keys_path , config_fs . symlink ( access_key ) ) ;
213
219
const account_config_relative_path = config_fs . get_account_relative_path_by_id ( _id ) ;
214
220
215
- const access_key_already_linked = await config_fs . _is_symlink_pointing_to_identity ( access_key_path , identity_path ) ;
216
- if ( access_key_already_linked ) continue ;
221
+ try {
222
+ const access_key_already_linked = await config_fs . _is_symlink_pointing_to_identity ( access_key_path , identity_path ) ;
223
+ if ( access_key_already_linked ) continue ;
224
+ } catch ( err ) {
225
+ dbg . warn ( `config_dir_restructure.create_account_access_keys_index_if_missing _is_symlink_pointing_to_identity failed ${ access_key } ` , err ) ;
226
+ }
217
227
218
228
try {
219
229
await nb_native ( ) . fs . symlink ( fs_context , account_config_relative_path , tmp_access_key_path ) ;
220
230
} catch ( err ) {
221
231
if ( err . code !== 'EEXIST' ) throw err ;
222
232
dbg . warn ( `account access key backup was already linked on a previous run of the upgrade script, continue ${ access_keys } , ${ tmp_access_key_path } ` ) ;
223
233
}
224
- let src_file ;
225
234
try {
226
- if ( native_fs_utils . _is_gpfs ( fs_context ) ) {
227
- src_file = await nb_native ( ) . fs . open ( fs_context , tmp_access_key_path , 'r' , native_fs_utils . get_umasked_mode ( config . BASE_MODE_CONFIG_FILE ) ) ;
228
- await src_file . linkfileat ( fs_context , access_key_path ) ;
229
- } else {
230
- await nb_native ( ) . fs . rename ( fs_context , tmp_access_key_path , access_key_path ) ;
231
- }
235
+ await nb_native ( ) . fs . rename ( fs_context , tmp_access_key_path , access_key_path ) ;
232
236
} catch ( err ) {
233
237
if ( err . code !== 'EEXIST' ) throw err ;
234
238
dbg . warn ( `account access key was already linked on a previous run of the upgrade script, skipping ${ access_keys } , ${ _id } ` ) ;
235
- } finally {
236
- if ( src_file ) await src_file . close ( fs_context ) ;
237
239
}
238
240
}
239
241
}
0 commit comments