-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
216 lines (164 loc) · 5.04 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const core = require('@actions/core');
const exec = require('@actions/exec');
const fs = require("fs");
const path = require("path");
const os = require('os');
var workingDir = __dirname;
async function execSSH(cmd, desp = "") {
core.info(desp);
core.info("exec ssh: " + cmd);
await exec.exec("bash " + workingDir + "/run.sh execSSH", [], { input: cmd });
}
async function execSSHSH(cmd, desp = "") {
core.info(desp);
core.info("exec ssh: " + cmd);
await exec.exec("bash " + workingDir + "/run.sh execSSHSH", [], { input: cmd });
}
async function shell(cmd, cdToScriptHome = true) {
core.info("exec shell: " + cmd);
if(cdToScriptHome) {
await exec.exec("bash", [], { input: "cd " + workingDir + " && (" + cmd + ")" });
} else {
await exec.exec("bash", [], { input: cmd });
}
}
async function setup(nat, mem, cpu) {
try {
core.startGroup("Importing VM");
if(!cpu) {
cpu = os.cpus().length;//use the system all cores
}
core.info("Use cpu: " + cpu);
await shell("bash run.sh importVM '" + mem + "' '" + cpu + "'");
core.endGroup();
core.startGroup("Run onBeforeStartVM");
await shell("bash run.sh onBeforeStartVM " );
core.endGroup();
core.startGroup("Run startVM");
await shell("bash run.sh startVM " );
core.info("First boot");
await shell("bash run.sh waitForVMReady");
core.endGroup();
core.startGroup("Run onStarted in VM");
if (nat) {
let nats = nat.split("\n").filter(x => x !== "");
for (let element of nats) {
core.info("Add nat: " + element);
let segs = element.split(":");
if (segs.length === 3) {
//udp:"8081": "80"
let proto = segs[0].trim().trim('"');
let hostPort = segs[1].trim().trim('"');
let vmPort = segs[2].trim().trim('"');
await shell("bash run.sh addNAT " + proto + " " + hostPort + " " + vmPort);
} else if (segs.length === 2) {
let proto = "tcp"
let hostPort = segs[0].trim().trim('"');
let vmPort = segs[1].trim().trim('"');
await shell("bash run.sh addNAT " + proto + " " + hostPort + " " + vmPort);
}
};
}
await shell("bash run.sh onStarted" );
core.endGroup();
core.startGroup("Initialize files in VM");
let cmd1 = "mkdir -p " + process.env["HOME"] + "/work && ln -s " + process.env["HOME"] + "/work/ work";
await execSSH(cmd1, "Setting up VM");
let sync = core.getInput("sync");
if(sync == "no") {
core.info("Don't sync files, OK");
} else if (sync == "sshfs") {
core.info("Setup sshfs");
await shell("bash run.sh runSSHFSInVM");
} else {
await shell("bash run.sh installRsyncInVM");
await shell("bash run.sh rsyncToVM");
}
core.info("OK, Ready!");
core.endGroup();
}
catch (error) {
try {
await shell("bash run.sh showDebugInfo");
} catch(ex){}
core.setFailed(error.message);
throw error;
}
}
async function main() {
let debug = core.getInput("debug");
process.env.DEBUG = debug;
let release = core.getInput("release");
core.info("release: " + release);
if(release) {
process.env.VM_RELEASE=release;
}
let nat = core.getInput("nat");
core.info("nat: " + nat);
let mem = core.getInput("mem");
core.info("mem: " + mem);
let cpu = core.getInput("cpu");
core.info("cpu: " + cpu);
await setup(nat, mem, cpu);
var envs = core.getInput("envs");
console.log("envs:" + envs);
if (envs) {
fs.appendFileSync(path.join(process.env["HOME"], "/.ssh/config"), "SendEnv " + envs + "\n");
}
core.startGroup("Run onInitialized in VM");
await shell("bash run.sh onInitialized" );
core.endGroup();
core.startGroup("Run 'prepare' in VM");
var usesh = core.getInput("usesh").toLowerCase() == "true";
var prepare = core.getInput("prepare");
if (prepare) {
core.info("Running prepare: " + prepare);
if (usesh) {
await execSSHSH(prepare);
} else {
await execSSH(prepare);
}
}
core.endGroup();
core.startGroup("Run 'run' in VM");
var run = core.getInput("run");
console.log("run: " + run);
var error = null;
try {
if(run) {
if (usesh) {
await execSSHSH("cd $GITHUB_WORKSPACE;\n" + run);
} else {
await execSSH("cd $GITHUB_WORKSPACE;\n" + run);
}
}
} catch (err) {
error = err;
try {
await shell("bash run.sh showDebugInfo");
} catch(ex){}
} finally {
core.endGroup();
let copyback = core.getInput("copyback");
if(copyback !== "false") {
core.startGroup("Copy files back from the VM");
let sync = core.getInput("sync");
if(sync == "no") {
core.info("don't get back by rsync");
} else if (sync != "sshfs") {
core.info("get back by rsync");
await exec.exec("bash " + workingDir + "/run.sh rsyncBackFromVM");
}
core.endGroup();
}
if(error) {
core.setFailed(error.message);
process.exit(1);
} else {
process.exit(0);
}
}
}
main().catch(ex => {
core.setFailed(ex.message);
});