From eb840b3d9cb04fb6872926c3bd55c6c76694f9e9 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 19 Jan 2022 18:28:54 -0500 Subject: [PATCH] fix(striker-ui-api): complete edit file endpoint --- striker-ui-api/lib/accessDB.js | 55 +++++++--- striker-ui-api/lib/consts/SERVER_PATHS.js | 1 + .../request_handlers/files/buildGetFiles.js | 4 +- striker-ui-api/routes/files.js | 101 ++++++++++++++++-- 4 files changed, 135 insertions(+), 26 deletions(-) diff --git a/striker-ui-api/lib/accessDB.js b/striker-ui-api/lib/accessDB.js index 448f905f..2f2f5a0b 100644 --- a/striker-ui-api/lib/accessDB.js +++ b/striker-ui-api/lib/accessDB.js @@ -40,8 +40,45 @@ const execStrikerAccessDatabase = ( }; }; +const execDatabaseModuleSubroutine = (subName, subParams, options) => { + const args = ['--sub', subName]; + + if (subParams) { + args.push('--sub-params', JSON.stringify(subParams)); + } + + const { stdout } = execStrikerAccessDatabase(args, options); + + return { + stdout: stdout['sub_results'], + }; +}; + const accessDB = { - query: (query, accessMode, options) => { + dbJobAnvilSyncShared: ( + jobName, + jobData, + jobTitle, + jobDescription, + { jobHostUUID } = { jobHostUUID: 'all' }, + ) => { + const subParams = { + file: __filename, + line: 0, + job_host_uuid: jobHostUUID, + job_command: SERVER_PATHS.usr.sbin['anvil-sync-shared'].self, + job_data: jobData, + job_name: `storage::${jobName}`, + job_title: `job_${jobTitle}`, + job_description: `job_${jobDescription}`, + job_progress: 0, + }; + console.log(JSON.stringify(subParams, null, 2)); + + return execDatabaseModuleSubroutine('insert_or_update_jobs', subParams) + .stdout; + }, + dbQuery: (query, accessMode, options) => { const args = ['--query', query]; if (accessMode) { @@ -50,19 +87,9 @@ const accessDB = { return execStrikerAccessDatabase(args, options); }, - sub: (subName, subParams, options) => { - const args = ['--sub', subName]; - - if (subParams) { - args.push('--sub-params', JSON.stringify(subParams)); - } - - const { stdout } = execStrikerAccessDatabase(args, options); - - return { - stdout: stdout['sub_results'], - }; - }, + dbSub: execDatabaseModuleSubroutine, + dbSubRefreshTimestamp: () => + execDatabaseModuleSubroutine('refresh_timestamp').stdout, }; module.exports = accessDB; diff --git a/striker-ui-api/lib/consts/SERVER_PATHS.js b/striker-ui-api/lib/consts/SERVER_PATHS.js index 79a5164a..a3030a06 100644 --- a/striker-ui-api/lib/consts/SERVER_PATHS.js +++ b/striker-ui-api/lib/consts/SERVER_PATHS.js @@ -8,6 +8,7 @@ const SERVER_PATHS = { }, usr: { sbin: { + 'anvil-sync-shared': {}, 'striker-access-database': {}, }, }, diff --git a/striker-ui-api/lib/request_handlers/files/buildGetFiles.js b/striker-ui-api/lib/request_handlers/files/buildGetFiles.js index 086cf289..29288c73 100644 --- a/striker-ui-api/lib/request_handlers/files/buildGetFiles.js +++ b/striker-ui-api/lib/request_handlers/files/buildGetFiles.js @@ -1,4 +1,4 @@ -const accessDB = require('../../accessDB'); +const { dbQuery } = require('../../accessDB'); const buildGetFiles = (query) => (request, response) => { console.log('Calling CLI script to get data.'); @@ -6,7 +6,7 @@ const buildGetFiles = (query) => (request, response) => { let queryStdout; try { - ({ stdout: queryStdout } = accessDB.query( + ({ stdout: queryStdout } = dbQuery( typeof query === 'function' ? query(request) : query, )); } catch (queryError) { diff --git a/striker-ui-api/routes/files.js b/striker-ui-api/routes/files.js index a52f03b6..c93ee9d6 100644 --- a/striker-ui-api/routes/files.js +++ b/striker-ui-api/routes/files.js @@ -1,6 +1,10 @@ const express = require('express'); -const accessDB = require('../lib/accessDB'); +const { + dbJobAnvilSyncShared, + dbQuery, + dbSubRefreshTimestamp, +} = require('../lib/accessDB'); const getFilesOverview = require('../lib/request_handlers/files/getFilesOverview'); const getFileDetail = require('../lib/request_handlers/files/getFileDetail'); const uploadSharedFiles = require('../middlewares/uploadSharedFiles'); @@ -28,30 +32,98 @@ router const { fileUUID } = request.params; const { fileName, fileLocations, fileType } = request.body; + const anvilSyncSharedFunctions = []; let query = ''; - if (fileName || fileType) { + if (fileName) { + const [[oldFileName]] = dbQuery( + `SELECT file_name FROM files WHERE file_uuid = '${fileUUID}';`, + ).stdout; + console.log(`oldFileName=[${oldFileName}],newFileName=[${fileName}]`); + + if (fileName !== oldFileName) { + query += ` + UPDATE files + SET + file_name = '${fileName}', + modified_date = '${dbSubRefreshTimestamp()}' + WHERE file_uuid = '${fileUUID}';`; + + anvilSyncSharedFunctions.push(() => + dbJobAnvilSyncShared( + 'rename', + `file_uuid=${fileUUID}\nold_name=${oldFileName}\nnew_name=${fileName}`, + '0138', + '0139', + ), + ); + } + } + + if (fileType) { query += ` UPDATE files SET - ${fileName ? `file_name = '${fileName}',` : ''} - ${fileType ? `file_type = '${fileType}',` : ''} - modified_date = '${accessDB.sub('refresh_timestamp').stdout}' - WHERE - file_uuid = '${fileUUID}';`; + file_type = '${fileType}', + modified_date = '${dbSubRefreshTimestamp()}' + WHERE file_uuid = '${fileUUID}';`; + + anvilSyncSharedFunctions.push(() => + dbJobAnvilSyncShared( + 'check_mode', + `file_uuid=${fileUUID}`, + '0143', + '0144', + ), + ); } if (fileLocations) { fileLocations.forEach(({ fileLocationUUID, isFileLocationActive }) => { - const fileLocationActive = isFileLocationActive ? 1 : 0; + let fileLocationActive = 0; + let jobName = 'purge'; + let jobTitle = '0136'; + let jobDescription = '0137'; + + if (isFileLocationActive) { + fileLocationActive = 1; + jobName = 'pull_file'; + jobTitle = '0132'; + jobDescription = '0133'; + } query += ` UPDATE file_locations SET file_location_active = '${fileLocationActive}', - modified_date = '${accessDB.sub('refresh_timestamp').stdout}' + modified_date = '${dbSubRefreshTimestamp()}' WHERE file_location_uuid = '${fileLocationUUID}';`; + + const targetHosts = dbQuery( + `SELECT + anv.anvil_node1_host_uuid, + anv.anvil_node2_host_uuid, + anv.anvil_dr1_host_uuid + FROM anvils AS anv + JOIN file_locations AS fil_loc + ON anv.anvil_uuid = fil_loc.file_location_anvil_uuid + WHERE fil_loc.file_location_uuid = '${fileLocationUUID}';`, + ).stdout; + + targetHosts.flat().forEach((hostUUID) => { + if (hostUUID) { + anvilSyncSharedFunctions.push(() => + dbJobAnvilSyncShared( + jobName, + `file_uuid=${fileUUID}`, + jobTitle, + jobDescription, + { jobHostUUID: hostUUID }, + ), + ); + } + }); }); } @@ -60,7 +132,7 @@ router let queryStdout; try { - ({ stdout: queryStdout } = accessDB.query(query, 'write')); + ({ stdout: queryStdout } = dbQuery(query, 'write')); } catch (queryError) { console.log(`Query error: ${queryError}`); @@ -74,6 +146,15 @@ router 2, )}`, ); + anvilSyncSharedFunctions.forEach((fn, index) => { + console.log( + `Anvil sync shared [${index}] output: [${JSON.stringify( + fn(), + null, + 2, + )}]`, + ); + }); response.status(200).send(queryStdout); });