-
Notifications
You must be signed in to change notification settings - Fork 85
/
DownloadJobs.ts
159 lines (140 loc) · 6.79 KB
/
DownloadJobs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import { AbstractSession, ImperativeExpect, IO, Logger } from "@zowe/imperative";
import { JobsConstants } from "./JobsConstants";
import { IDownloadAllSpoolContentParms } from "./doc/input/IDownloadAllSpoolContentParms";
import { IJobFile } from "./doc/response/IJobFile";
import { ZosmfRestClient } from "@zowe/core-for-zowe-sdk";
import { IDownloadSpoolContentParms } from "./doc/input/IDownloadSpoolContentParms";
import { GetJobs } from "./GetJobs";
/**
* Class to handle downloading of job information
* @export
* @class DownloadJobs
*/
export class DownloadJobs {
/**
* Default directory where output will be placed
* @static
* @type {string}
* @memberof DownloadJobs
*/
public static readonly DEFAULT_JOBS_OUTPUT_DIR: string = "./output";
/**
* Default extension of downloaded folders
* @static
* @type {string}
* @memberof DownloadJobs
*/
public static readonly DEFAULT_JOBS_OUTPUT_FILE_EXT: string = ".txt";
/**
* Download spool content to a the default download directory
* @static
* @param {AbstractSession} session - z/OSMF connection info
* @param jobFile - spool file to download
* @returns {Promise<string>} - content downloaded
* @memberof DownloadJobs
*/
public static async downloadSpoolContent(session: AbstractSession, jobFile: IJobFile) {
this.log.trace("Entered downloadSpoolContent for job file %s", JSON.stringify(jobFile));
return DownloadJobs.downloadSpoolContentCommon(session, {jobFile});
}
/**
* Download all job output (spool content) for a job to a the local directory
* @static
* @param {AbstractSession} session - z/OSMF connection info
* @param {IDownloadAllSpoolContentParms} parms - parameter object (see IDownloadAllSpoolContentParms for details)
* @returns {Promise<void>} - a promise which will resolve when the download is complete
* @memberof DownloadJobs
*/
public static async downloadAllSpoolContentCommon(session: AbstractSession, parms: IDownloadAllSpoolContentParms) {
this.log.trace("Entering downloadAllSpoolContentCommon with parms %s", JSON.stringify(parms));
ImperativeExpect.keysToBeDefined(parms, ["jobid", "jobname"], "You must specify job ID and job name on your" +
" 'parms' object to the downloadAllSpoolContent API.");
this.log.debug("Downloading all spool content for job %s(%s)", parms.jobname, parms.jobid);
const jobFiles = await GetJobs.getSpoolFiles(session, parms.jobname, parms.jobid);
// Maintain a mapping of step names and associated DD names to detect duplicate step names
const usedStepNames: {[key: string]: string[]} = {};
for (const file of jobFiles) {
let uniqueDDName = file.ddname;
if (usedStepNames[file.stepname] != null) {
let index = 1;
while (usedStepNames[file.stepname].indexOf(uniqueDDName) !== -1) {
uniqueDDName = `${file.ddname}(${index})`;
index++;
}
}
await DownloadJobs.downloadSpoolContentCommon(session, {
jobFile: {...file, ddname: uniqueDDName},
outDir: parms.outDir,
omitJobidDirectory: parms.omitJobidDirectory
});
usedStepNames[file.stepname] = [...usedStepNames[file.stepname] || [], uniqueDDName];
}
}
/**
* Download spool content to specified directory
* @static
* @param {AbstractSession} session - z/OSMF connection info
* @param {IDownloadSpoolContentParms} parms - parm object (see IDownloadSpoolContentParms interface for details)
* @returns {Promise<void>} - promise that resolves when the file is downloaded
* @memberof DownloadJobs
*/
public static async downloadSpoolContentCommon(session: AbstractSession, parms: IDownloadSpoolContentParms): Promise<void> {
this.log.trace("Entering downloadSpoolContentCommon with parms %s", JSON.stringify(parms));
ImperativeExpect.keysToBeDefined(parms, ["jobFile"], "You must specify a job file on your 'parms' parameter" +
" object to the downloadSpoolContentCommon API.");
if (parms.outDir == null) {
parms.outDir = DownloadJobs.DEFAULT_JOBS_OUTPUT_DIR;
}
const file = DownloadJobs.getSpoolDownloadFile(parms.jobFile, parms.omitJobidDirectory, parms.outDir);
this.log.debug("Downloading spool file %s for job %s(%s) to file %s",
parms.jobFile.ddname, parms.jobFile.jobname, parms.jobFile.jobid, file);
IO.createDirsSyncFromFilePath(file);
IO.createFileSync(file);
const parameters: string = "/" + parms.jobFile.jobname + "/" + parms.jobFile.jobid +
JobsConstants.RESOURCE_SPOOL_FILES + "/" + parms.jobFile.id + JobsConstants.RESOURCE_SPOOL_CONTENT;
const writeStream = IO.createWriteStream(file);
await ZosmfRestClient.getStreamed(session, JobsConstants.RESOURCE + parameters, undefined, writeStream,
true);
}
/**
* Get the file where a specified spool file (IJobFile) would be downloaded to
* @static
* @param {IJobFile} jobFile - the spool file that would be downloaded
* @param {boolean} omitJobidDirectory - if true, the job ID of the jobFile will not be included in the file path
* @param {string} outDir - parent output directory you would like to download to
* @returns {string} the file path that the spool file would be downloaded to
* @memberof DownloadJobs
*/
public static getSpoolDownloadFile(jobFile: IJobFile, omitJobidDirectory?: boolean, outDir = DownloadJobs.DEFAULT_JOBS_OUTPUT_DIR): string {
this.log.trace("getSpoolDownloadFile called with jobFile %s, omitJobIDDirectory: %s, outDir: %s",
JSON.stringify(jobFile), omitJobidDirectory + "", outDir);
let directory: string = outDir;
if (omitJobidDirectory == null || omitJobidDirectory === false) {
directory += IO.FILE_DELIM + jobFile.jobid;
}
if (jobFile.procstep != null) {
directory += IO.FILE_DELIM + jobFile.procstep;
}
if (jobFile.stepname != null) {
directory += IO.FILE_DELIM + jobFile.stepname;
}
return directory + IO.FILE_DELIM + jobFile.ddname + DownloadJobs.DEFAULT_JOBS_OUTPUT_FILE_EXT;
}
/**
* Getter for brightside logger
* @returns {Logger}
*/
private static get log(): Logger {
return Logger.getAppLogger();
}
}