Skip to content

Commit

Permalink
allow status and all flags
Browse files Browse the repository at this point in the history
  • Loading branch information
busma13 committed Sep 5, 2024
1 parent 7fd4d4a commit 3b30034
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
14 changes: 7 additions & 7 deletions packages/teraslice-cli/src/cmds/jobs/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ const yargsOptions = new YargsOptions();

export default {
command: 'export <cluster-alias> <job-id...>',
describe: 'Export job or jobs on a cluster to a json file. By default the file is saved as ~/.teraslice/export/<cluster-alias>/<job.name>.json\n',
describe: 'Export job on a cluster to a json file. By default the file is saved as ~/.teraslice/export/<cluster-alias>/<job.name>.json\n',
builder(yargs: any) {
yargs.positional('job-id', yargsOptions.buildPositional('job-id'));
yargs.options('config-dir', yargsOptions.buildOption('config-dir'));
yargs.options('export-dir', yargsOptions.buildOption('export-dir'));
yargs.options('output', yargsOptions.buildOption('output'));
yargs.options('file-name', yargsOptions.buildOption('file-name'));
yargs.options('status', yargsOptions.buildOption('jobs-status'));
yargs.options('yes', yargsOptions.buildOption('yes'));
yargs.check((argv: { jobId: string[]; fileName: string[]; }) => {
if (argv.fileName && argv.jobId.length !== argv.fileName.length) {
if (argv.fileName && argv.fileName.length !== argv.jobId.length) {
throw new Error('The number of job IDs must match the number of file names');
}
return true;
});
// yargs.options('yes', yargsOptions.buildOption('yes'));
// yargs.options('status', yargsOptions.buildOption('jobs-status'));
yargs.strict()
.example('$0 jobs export CLUSTER_ALIAS JOB1', 'exports job config as a tjm compatible JSON file')
.example('$0 jobs export CLUSTER_ALIAS JOB1 JOB2', 'exports job config for two jobs')
.example('$0 jobs export CLUSTER_ALIAS JOB1 JOB2 --file-name name1.json name2.json', 'exports two jobs with custom file names')
.example('$0 jobs export CLUSTER_ALIAS JOB1 --export-dir ./my_jobs --f job_1.json', 'exports ajob to ./my_jobs/job_1.json');
// .example('$0 jobs export CLUSTER_ALIAS all --status failing', 'exports all failing jobs on a cluster')
// .example('$0 jobs export CLUSTER_ALIAS all -y', 'exports all jobs on a cluster and bypasses the prompt');
.example('$0 jobs export CLUSTER_ALIAS JOB1 --export-dir ./my_jobs -f job_1.json', 'exports a job to ./my_jobs/job_1.json')
.example('$0 jobs export CLUSTER_ALIAS all --status failing', 'exports all failing jobs on a cluster')
.example('$0 jobs export CLUSTER_ALIAS all -y', 'exports all jobs on a cluster and bypasses the prompt');
return yargs;
},
async handler(argv: any) {
Expand Down
22 changes: 13 additions & 9 deletions packages/teraslice-cli/src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ export default class Config {
}
}

get aliasesFile(): string {
return `${this.configDir}/aliases.yaml`;
/**
* Returns the user provided export directory or a
* default directory at <configDir>/export/<clusterAlias>
*/
get exportDir(): string {
if (this.args.exportDir) {
return this.args.exportDir;
} else {
return `${this.configDir}/export/${this.args.clusterAlias}`;
}
}

get defaultExportDir(): string {
return `${this.configDir}/export/${this.args.clusterAlias}`;
get aliasesFile(): string {
return `${this.configDir}/aliases.yaml`;
}

get jobStateDir(): string {
Expand All @@ -90,7 +98,7 @@ export default class Config {
return [
this.jobStateDir,
this.assetDir,
this.defaultExportDir,
this.exportDir,
];
}

Expand All @@ -104,10 +112,6 @@ export default class Config {
fs.mkdirSync(dir, { recursive: true });
}
});

if (this.args.exportDir && !fs.existsSync(this.args.exportDir)) {
fs.mkdirSync(this.args.exportDir, { recursive: true });
}
}

private _addJobAction() {
Expand Down
28 changes: 25 additions & 3 deletions packages/teraslice-cli/src/helpers/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,12 @@ export default class Jobs {
return this.getJobIdsFromSavedState();
}

// if action is export we need to get inactive
// as well as active jobs
if (this.config.args._action === 'export') {
return this.getActiveAndInactiveJobIds();
}

return this.getActiveJobIds();
}

Expand All @@ -574,6 +580,16 @@ export default class Jobs {
return Object.keys(state);
}

private async getActiveAndInactiveJobIds() {
try {
const jobs = await this.teraslice.client.jobs.list();

return jobs.map((job) => job.job_id);
} catch (e) {
throw Error(e);
}
}

private async getActiveJobIds(): Promise<string[]> {
const controllers = await this.getClusterControllers();

Expand Down Expand Up @@ -629,11 +645,11 @@ export default class Jobs {
}

noJobsWithStatus() {
const cluster = `cluster: ${this.config.args.clusterUrl}`;
const cluster = `cluster: ${this.config.clusterUrl}`;
const targetedStatus = `${this.config.args.status.join(' or ')}`;

if (this.config.args.jobId.includes('all')) {
reply.fatal(`No jobs on ${cluster} with status ${targetedStatus}`);
reply.fatal(`No jobs on ${cluster} with status ${targetedStatus || '"any"'}`);
}

reply.fatal(`Jobs: ${this.config.args.jobId.join(', ')} on ${cluster} do not have status ${targetedStatus}`);
Expand Down Expand Up @@ -755,15 +771,21 @@ export default class Jobs {
}

async export() {
const jobIds = this.jobs.map((job) => job.id);

reply.yellow(`Saving jobFile for ${jobIds.join(', ')} on ${this.config.args.clusterAlias}`);

await pMap(
this.jobs,
(job) => this.exportOne(job.config),
{ concurrency: this.concurrency }
);

reply.green(`Saved jobFile to ${this.config.exportDir}`);
}

async exportOne(jobConfig: Teraslice.JobConfig) {
const dirName = this.config.args.exportDir || this.config.defaultExportDir;
const dirName = this.config.exportDir;
const fileNameIndex = this.config.args.jobId.indexOf(jobConfig.job_id);
const fileName = this.config.args.fileName ? this.config.args.fileName[fileNameIndex] : `${jobConfig.name}.json`;
const fullPath = path.join(dirName, fileName);
Expand Down

0 comments on commit 3b30034

Please sign in to comment.