fix(striker-ui-api): complete edit file endpoint

main
Tsu-ba-me 3 years ago
parent 3d202d4f32
commit eb840b3d9c
  1. 49
      striker-ui-api/lib/accessDB.js
  2. 1
      striker-ui-api/lib/consts/SERVER_PATHS.js
  3. 4
      striker-ui-api/lib/request_handlers/files/buildGetFiles.js
  4. 101
      striker-ui-api/routes/files.js

@ -40,17 +40,7 @@ const execStrikerAccessDatabase = (
}; };
}; };
const accessDB = { const execDatabaseModuleSubroutine = (subName, subParams, options) => {
query: (query, accessMode, options) => {
const args = ['--query', query];
if (accessMode) {
args.push('--mode', accessMode);
}
return execStrikerAccessDatabase(args, options);
},
sub: (subName, subParams, options) => {
const args = ['--sub', subName]; const args = ['--sub', subName];
if (subParams) { if (subParams) {
@ -62,7 +52,44 @@ const accessDB = {
return { return {
stdout: stdout['sub_results'], stdout: stdout['sub_results'],
}; };
};
const accessDB = {
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) {
args.push('--mode', accessMode);
}
return execStrikerAccessDatabase(args, options);
}, },
dbSub: execDatabaseModuleSubroutine,
dbSubRefreshTimestamp: () =>
execDatabaseModuleSubroutine('refresh_timestamp').stdout,
}; };
module.exports = accessDB; module.exports = accessDB;

@ -8,6 +8,7 @@ const SERVER_PATHS = {
}, },
usr: { usr: {
sbin: { sbin: {
'anvil-sync-shared': {},
'striker-access-database': {}, 'striker-access-database': {},
}, },
}, },

@ -1,4 +1,4 @@
const accessDB = require('../../accessDB'); const { dbQuery } = require('../../accessDB');
const buildGetFiles = (query) => (request, response) => { const buildGetFiles = (query) => (request, response) => {
console.log('Calling CLI script to get data.'); console.log('Calling CLI script to get data.');
@ -6,7 +6,7 @@ const buildGetFiles = (query) => (request, response) => {
let queryStdout; let queryStdout;
try { try {
({ stdout: queryStdout } = accessDB.query( ({ stdout: queryStdout } = dbQuery(
typeof query === 'function' ? query(request) : query, typeof query === 'function' ? query(request) : query,
)); ));
} catch (queryError) { } catch (queryError) {

@ -1,6 +1,10 @@
const express = require('express'); 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 getFilesOverview = require('../lib/request_handlers/files/getFilesOverview');
const getFileDetail = require('../lib/request_handlers/files/getFileDetail'); const getFileDetail = require('../lib/request_handlers/files/getFileDetail');
const uploadSharedFiles = require('../middlewares/uploadSharedFiles'); const uploadSharedFiles = require('../middlewares/uploadSharedFiles');
@ -28,30 +32,98 @@ router
const { fileUUID } = request.params; const { fileUUID } = request.params;
const { fileName, fileLocations, fileType } = request.body; const { fileName, fileLocations, fileType } = request.body;
const anvilSyncSharedFunctions = [];
let query = ''; 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 += ` query += `
UPDATE files UPDATE files
SET SET
${fileName ? `file_name = '${fileName}',` : ''} file_type = '${fileType}',
${fileType ? `file_type = '${fileType}',` : ''} modified_date = '${dbSubRefreshTimestamp()}'
modified_date = '${accessDB.sub('refresh_timestamp').stdout}' WHERE file_uuid = '${fileUUID}';`;
WHERE
file_uuid = '${fileUUID}';`; anvilSyncSharedFunctions.push(() =>
dbJobAnvilSyncShared(
'check_mode',
`file_uuid=${fileUUID}`,
'0143',
'0144',
),
);
} }
if (fileLocations) { if (fileLocations) {
fileLocations.forEach(({ fileLocationUUID, isFileLocationActive }) => { 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 += ` query += `
UPDATE file_locations UPDATE file_locations
SET SET
file_location_active = '${fileLocationActive}', file_location_active = '${fileLocationActive}',
modified_date = '${accessDB.sub('refresh_timestamp').stdout}' modified_date = '${dbSubRefreshTimestamp()}'
WHERE file_location_uuid = '${fileLocationUUID}';`; 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; let queryStdout;
try { try {
({ stdout: queryStdout } = accessDB.query(query, 'write')); ({ stdout: queryStdout } = dbQuery(query, 'write'));
} catch (queryError) { } catch (queryError) {
console.log(`Query error: ${queryError}`); console.log(`Query error: ${queryError}`);
@ -74,6 +146,15 @@ router
2, 2,
)}`, )}`,
); );
anvilSyncSharedFunctions.forEach((fn, index) => {
console.log(
`Anvil sync shared [${index}] output: [${JSON.stringify(
fn(),
null,
2,
)}]`,
);
});
response.status(200).send(queryStdout); response.status(200).send(queryStdout);
}); });

Loading…
Cancel
Save