-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruncommand-activity.ts
93 lines (84 loc) · 2.32 KB
/
runcommand-activity.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
import { v4 as uuid } from "uuid";
import SWF = require("aws-sdk/clients/swf");
import S3 = require("aws-sdk/clients/s3");
import { exec } from "shelljs";
import {
initAws,
SWF_DOMAIN,
SWF_RUNCOMMAND_ACTIVITY_TASKLIST,
S3_RUNCOMMAND_OUTPUT_BUCKET
} from "./config";
import chalk from "chalk";
initAws();
const swf = new SWF();
const s3 = new S3();
interface ExecCommandOutput {
stdout: string;
stderr: string;
}
async function execCommand(command: string): Promise<ExecCommandOutput> {
return new Promise<ExecCommandOutput>((resolve, reject) => {
exec(
command,
{ encoding: "utf8", silent: false },
(code, stdout, stderr) => {
if (code) {
reject({ code, stderr });
} else {
resolve({ stdout, stderr });
}
}
);
});
}
async function uploadToS3(activityId: string, body: string): Promise<string> {
const key = `${activityId}-output.json`;
await s3
.putObject({
Bucket: S3_RUNCOMMAND_OUTPUT_BUCKET,
Key: key,
ContentType: "application/json",
Body: body
})
.promise();
return key;
}
async function run() {
while (true) {
console.log("Polling for activity tasks...");
const activityTask: SWF.Types.ActivityTask = await swf
.pollForActivityTask({
domain: SWF_DOMAIN,
taskList: { name: SWF_RUNCOMMAND_ACTIVITY_TASKLIST }
})
.promise();
console.log("Received an activity task: ", activityTask);
if (activityTask.taskToken) {
const input = activityTask.input;
console.log("Received the following command to execute: " + input);
try {
const { stdout, stderr } = await execCommand(input);
console.log("Uploading the output to S3.");
const key = await uploadToS3(
activityTask.activityId,
JSON.stringify({ stdout, stderr }, null, 2)
);
console.log(`File uploaded successfully. Key is ${key}.`);
await swf
.respondActivityTaskCompleted({
taskToken: activityTask.taskToken,
result: key
})
.promise();
} catch ({ code, stderr }) {
await swf
.respondActivityTaskFailed({
taskToken: activityTask.taskToken,
reason: JSON.stringify({ code, stderr })
})
.promise();
}
}
}
}
run();