-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
246 lines (221 loc) · 7.67 KB
/
index.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { spawn } from "child_process";
import { getInput, setFailed, debug, startGroup, endGroup } from "@actions/core";
import { hostname, platform } from "os";
import { PowerShellSSHClient } from "./PowerShellSshClient";
async function main() {
// https://stackoverflow.com/questions/8683895/how-do-i-determine-the-current-operating-system-with-node-js
startGroup("Hyper-V action general information");
console.log(`Starting the HyperV action on Hyper-V host ${hostname} using the plattform ${platform()}`);
var isSshModeEnabledString = getInput("SSHMode", { required: false, trimWhitespace: true });
var isSshModeEnabled = getBoolean(isSshModeEnabledString);
// we check if ssh mode is enabled
// if it is enabled, we will use ssh to execute the commands on the remote machine
// ssh works on all platforms (Windows, Mac, Linux)
// if ssh mode is not enabled, we will use PowerShell to execute the commands on the remote machine
// PowerShell works only on Windows
if (!isSshModeEnabled) {
console.log("SSH mode is not enabled. Using PowerShell remote protocol.");
await executeInPowerShellRemoteMode();
}
else {
console.log("SSH mode is enabled. Using SSH protocol.");
// ssh mode is enabled
await executeInSSHMode();
}
}
async function executeInPowerShellRemoteMode() {
var isWin = process.platform === "win32";
if (isWin) {
// Executing using powershell shell
console.log("Starting executing PowerShell commands.");
// https://www.freecodecamp.org/news/node-js-child-processes-everything-you-need-to-know-e69498fe970a/
// https://nodejs.org/api/child_process.html
// https://2ality.com/2018/05/child-process-streams.html
var hyperVCmd = String.prototype.concat(".\\ps\\HyperVServer.ps1");
hyperVCmd += String.prototype.concat(createHyperVScriptCommand());
endGroup();
const pwshHyperV = spawn(getPwsh(), [hyperVCmd], {
stdio: "inherit",
});
await new Promise<void>((resolve) => {
pwshHyperV.on("close", (code) => {
console.log(`PowerShell process exited with code ${code}`);
if (code != 0) {
setFailed(`PowerShell process exited with code ${code}`);
}
resolve();
})
});
console.log("### DONE");
}
else {
console.error("Connecting via PowerShell remote protocol is only supported on Windows. Please enable SSH mode.");
}
}
function createHyperVScriptCommand() {
var action = getInput("Command", { required: true, trimWhitespace: true });
var vmName = getInput("VMName", { required: true, trimWhitespace: true });
var computername = getInput("Hostname", {
required: true,
trimWhitespace: true,
});
var CheckpointName = getInput("CheckpointName", {
required: false,
trimWhitespace: true,
});
var StartVMWaitTimeBasedCheckInterval = getInput(
"StartVMWaitTimeBasedCheckInterval",
{ required: false, trimWhitespace: true }
);
var StartVMStatusCheckType = getInput("StartVMStatusCheckType", {
required: false,
trimWhitespace: true,
});
var HyperV_StartVMWaitingNumberOfStatusNotifications = getInput(
"HyperV_StartVMWaitingNumberOfStatusNotifications",
{ required: false, trimWhitespace: true }
);
var HyperV_StartVMAppHealthyHeartbeatTimeout = getInput(
"HyperV_StartVMAppHealthyHeartbeatTimeout",
{ required: false, trimWhitespace: true }
);
var HyperV_PsModuleVersion = getInput("HyperV_PsModuleVersion", {
required: false,
trimWhitespace: true,
});
var optionalParameters = "";
if (!isEmpty(CheckpointName)) {
optionalParameters += String.prototype.concat(
" ",
"-CheckpointName",
" ",
CheckpointName
);
}
if (!isEmpty(StartVMWaitTimeBasedCheckInterval)) {
optionalParameters += String.prototype.concat(
" ",
"-StartVMWaitTimeBasedCheckInterval",
" ",
StartVMWaitTimeBasedCheckInterval
);
}
if (!isEmpty(StartVMStatusCheckType)) {
optionalParameters += String.prototype.concat(
" ",
"-StartVMStatusCheckType",
" ",
StartVMStatusCheckType
);
}
if (!isEmpty(HyperV_StartVMWaitingNumberOfStatusNotifications)) {
optionalParameters += String.prototype.concat(
" ",
"-HyperV_StartVMWaitingNumberOfStatusNotifications",
" ",
HyperV_StartVMWaitingNumberOfStatusNotifications
);
}
if (!isEmpty(HyperV_StartVMAppHealthyHeartbeatTimeout)) {
optionalParameters += String.prototype.concat(
" ",
"-HyperV_StartVMAppHealthyHeartbeatTimeout",
" ",
HyperV_StartVMAppHealthyHeartbeatTimeout
);
}
if (!isEmpty(HyperV_PsModuleVersion)) {
optionalParameters += String.prototype.concat(
" ",
"-HyperV_PsModuleVersion",
" ",
HyperV_PsModuleVersion
);
}
var hyperVCmd = String.prototype.concat(" ", "-ComputerName", " ", computername);
hyperVCmd += String.prototype.concat(" ", "-Action", " ", action);
hyperVCmd += String.prototype.concat(" ", "-VMName", " ", vmName);
hyperVCmd += String.prototype.concat(optionalParameters);
debug("### HyperV command script parameter: " + hyperVCmd);
return hyperVCmd;
}
async function executeInSSHMode() {
var sshPrivatekey = getInput("SSHPrivateKey", { required: false, trimWhitespace: true });
var sshHost = getInput("SSHHostName", { required: true, trimWhitespace: true });
var sshUsername = getInput("SSHUsername", { required: true, trimWhitespace: true });
var sshPort = Number.parseInt(getInput("SSHPort", { required: true, trimWhitespace: true }));
// we use username and password if private key is not provided (default)
var ssh = null;
if (isEmpty(sshPrivatekey)) {
console.log("### Connecting via SSH with username and password");
var sshPassword = getInput("SSHPassword", { required: true, trimWhitespace: true });
if (isEmpty(sshPassword)) {
console.error("SSH password is required if no private key is provided.");
}
ssh = new PowerShellSSHClient({
host: sshHost,
port: sshPort,
username: sshUsername,
password: sshPassword,
}, getPwsh());
}
else {
console.log("### Connecting via SSH with private key");
ssh = new PowerShellSSHClient({
host: sshHost,
port: sshPort,
username: sshUsername,
privateKey: sshPrivatekey,
});
}
var scriptArguments = createHyperVScriptCommand();
endGroup();
try {
var result = await ssh.executeScript('./ps/HyperVServer.ps1', scriptArguments);
result = result.trim();
debug("### Result: " + result);
console.log("### Done");
}
catch (error) {
if (error instanceof Error) {
setFailed(error.message);
} else {
setFailed("An unknown error occurred. Please check the logs. Error message:" + error);
throw error;
}
}
}
//source: https://stackoverflow.com/questions/1812245/what-is-the-best-way-to-test-for-an-empty-string-with-jquery-out-of-the-box
function isEmpty(value: string | null): boolean {
return (
(typeof value == "string" && !value.trim()) ||
typeof value == "undefined" ||
value === null
);
}
function getBoolean(value: any): boolean {
value = value.toLowerCase().trim();
switch (value) {
case true:
case "true":
case 1:
case "1":
case "on":
case "yes":
return true;
default:
return false;
}
}
function getPwsh(): string {
var pwshCore = getBoolean("pwshcore");
if (pwshCore) {
return "pwsh";
}
else {
return "powershell.exe";
}
}
if (require.main === module) {
main();
}