You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.0 KiB
95 lines
2.0 KiB
const { spawnSync } = require('child_process'); |
|
|
|
const SERVER_PATHS = require('./consts/SERVER_PATHS'); |
|
|
|
const execStrikerAccessDatabase = ( |
|
args, |
|
options = { |
|
timeout: 10000, |
|
encoding: 'utf-8', |
|
}, |
|
) => { |
|
const { error, stdout, stderr } = spawnSync( |
|
SERVER_PATHS.usr.sbin['striker-access-database'].self, |
|
args, |
|
options, |
|
); |
|
|
|
if (error) { |
|
throw error; |
|
} |
|
|
|
if (stderr) { |
|
throw new Error(stderr); |
|
} |
|
|
|
let output; |
|
|
|
try { |
|
output = JSON.parse(stdout); |
|
} catch (stdoutParseError) { |
|
output = stdout; |
|
|
|
console.warn( |
|
`Failed to parse striker-access-database output [${output}]; error: [${stdoutParseError}]`, |
|
); |
|
} |
|
|
|
return { |
|
stdout: output, |
|
}; |
|
}; |
|
|
|
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 = { |
|
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;
|
|
|