-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
186 lines (174 loc) · 7.51 KB
/
index.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const https = require('https');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
var FormData = require('form-data');
function file2StringArray(filename) {
try {
if (! fs.existsSync(filename)) { return null; };
var content = fs.readFileSync(filename,"utf-8");
return content.split("\r\n"); // Windows format
} catch (err) {
console.log(String(err));
return null;
}
};
// returns {status, data}
async function api(host,cmd,fname=null) {
try {
var axiosConfig = {}, form={};
if (fname != null) {
f = fs.readFileSync(fname, 'utf8') ; // await fsp.readFile(fname) or fs.createReadStream(fname)
// console.log("Start uploading "+f.length+" bytes that ends with "+f.slice(-10));
form = new FormData();
form.append('file', f , fname);
axiosConfig = {headers: {
'Content-Type': 'multipart/form-data',
'UserName': process.env.PP_API_USER,
'Password': process.env.PP_API_PASSWORD,
'PostFile': 'True',
'Content-Length': form.getLengthSync()
}};
} else {
axiosConfig = {headers: { UserName: process.env.PP_API_USER,Password: process.env.PP_API_PASSWORD }};
form = null;
}
try {
let ret;
if("true" == process.env.PP_API_ENABLE_HTTPS) {
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
axiosConfig.httpsAgent = httpsAgent;
ret = await axios.post('https://'+host+'/poly/com.polypaths.ent.gui.pprequest.polyAPI?'+cmd, form, axiosConfig);
} else {
ret = await axios.post('http://'+host+'/poly/com.polypaths.ent.gui.pprequest.polyAPI?'+cmd, form, axiosConfig);
}
return {status:ret.status, data:ret.data}; //ret.data.trim()
} catch (e) {
if (e.response == undefined)
return {status:-1, data:e.errno};
else
return {status:e.response.status, data:e.response.data};
}
} catch (error) {
return {status:error.status, data:"ERROR"+","+error.message};
}
};
async function batch(host,cmd_array){
var check_status = false;
for (var i=0; i<cmd_array.length; i++) {
var arr = cmd_array[i].split(" ");
if (arr[0] === "cmd") {
var cmd = arr.slice(1).join(' ');
const execSync = require('child_process').execSync;
execSync(cmd,{stdio: 'inherit'});
} else if (arr[0] === "comment" || arr[0] === "//") {
} else if (arr[0] === "check-on") {
check_status = true;
console.log("Check On (exit if failed)");
} else if (arr[0] === "check-off") {
check_status = false;
console.log("Check Off (continue to run)");
} else {
//console.log(`Running "${cmd_array[i]}" on "${host}"`);
var fname = null;
for (var a=0;a<arr.length;a++) {
if (arr[a] == "job_file" || arr[a] == "in_file" || arr[a] == "filter_file") {
fname = arr[a+1];
}
}
var ret = await api(host,cmd_array[i],fname);
console.log(ret.data);
if (check_status && ret.status !== 200) {
process.exit(ret.status);
}
}
}
process.exit(0);
};
const version = "Polyapi v1.10 9/26/2023";
async function main() {
var args = process.argv.slice(2);
if (process.env.PP_API_HOST==undefined || process.env.PP_API_USER==undefined ||
process.env.PP_API_PASSWORD==undefined) {
console.log(version);
console.log("Environment variables PP_API_HOST, PP_API_USER and PP_API_PASSWORD need to be set.");
} else if (args.length < 1) {
console.log(version);
console.log("Usage: polyapi.exe <command|batch file|report file>");
} else {
var host = process.env.PP_API_HOST ; //args[0];
var cmd_array = file2StringArray(args[0]);
if (args.length > 2 && args[1].toUpperCase() === "REPORT") {
var command = args.slice(1).join(' ');
var ret = await api(host,command);
if (ret.status != 200 || (ret.data != undefined && ret.data.includes("ERROR:"))) {
console.log(ret.data.trim());
process.exit(-1);
} else {
try{
var fs = require("fs");
fs.writeFileSync(args[0],ret.data,'utf8');
process.exit(0);
} catch (error) {
console.log(error.message);
process.exit(-3);
}
}
} else if (cmd_array !== null) {
console.log(`Running "${args[0]}" in batch mode on "${host}"`);
await batch(host,cmd_array);
console.log("Batch finished.");
} else {
var fname = null;
for (var a=0;a<args.length;a++) {
if(args[a] == "job_file") {
var command = args.slice(1, args.length).join(' ');
var ret = await api(host, "getisjobfileinput "+command,null); // check the job file is input or output base on job type
if(ret.data == 1) { // if it is input, will use postfile option
fname = args[a+1];
args[a+1] = path.basename(fname);
}
} else if (args[a] == "in_file" || args[a] == "filter_file") {
fname = args[a+1];
args[a+1] = path.basename(fname);
}
}
var command = args.join(' '); // args.slice(1).join(' ');
var ret = await api(host,encodeURIComponent(command),fname);
var msg = "",ret_number=0;
if (ret.data != undefined) {
if (typeof ret.data == "number") {
ret_number = ret.data;
console.log(ret_number);
process.exit(ret_number);
} else if (typeof ret.data == "object"){
console.log(JSON.stringify(ret.data));
process.exit(0);
} else {
msg = ret.data.trim();
console.log(msg);
}
} else {
console.log("Returned data is undefined; status:"+ret.status);
}
if (msg.includes("ERROR:") || msg.includes("not supported"))
process.exit(-1);
if (ret.status == 200) {
if (msg.includes("ERROR:") || msg.includes("not supported"))
process.exit(-1);
else if (msg.includes("Finished"))
process.exit(0);
else if (msg.startsWith("Running") || msg.startsWith("Waiting")
|| msg.startsWith("Scheduled") || msg.startsWith("Pending"))
process.exit(2);
else if (msg.includes("Failed") || msg.includes("Killed"))
process.exit(-2);
else
process.exit(0); // Successful
} else {
process.exit(-3); // System Error
}
}
}
};
main();