-
Notifications
You must be signed in to change notification settings - Fork 18
/
vm.ts
158 lines (132 loc) · 3.94 KB
/
vm.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
import * as fs from 'fs'
import * as path from 'path'
import {ChildProcess, spawn} from 'child_process'
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as vm from './vm'
import {ExecuteOptions} from './utility'
import {wait} from './wait'
import * as architecture from './architecture'
export enum Accelerator {
hvf,
tcg
}
export interface Configuration {
memory: string
cpuCount: number
diskImage: fs.PathLike
ssHostPort: number
// qemu
cpu: string
accelerator: Accelerator
machineType: string
// xhyve
uuid: string
resourcesDiskImage: fs.PathLike
userboot: fs.PathLike
firmware?: fs.PathLike
}
export abstract class Vm {
ipAddress!: string
static readonly user = 'runner'
readonly hypervisorPath: fs.PathLike
protected vmProcess!: ChildProcess
protected readonly architecture: architecture.Architecture
protected readonly configuration: vm.Configuration
protected readonly hypervisorDirectory: fs.PathLike
protected readonly resourcesDirectory: fs.PathLike
constructor(
hypervisorDirectory: fs.PathLike,
resourcesDirectory: fs.PathLike,
hypervisorBinary: fs.PathLike,
architecture: architecture.Architecture,
configuration: vm.Configuration
) {
this.hypervisorDirectory = hypervisorDirectory
this.resourcesDirectory = resourcesDirectory
this.architecture = architecture
this.configuration = configuration
this.hypervisorPath = path.join(
hypervisorDirectory.toString(),
hypervisorBinary.toString()
)
}
async init(): Promise<void> {
core.info('Initializing VM')
}
async run(): Promise<void> {
core.info('Booting VM')
core.debug(this.command.join(' '))
this.vmProcess = spawn('sudo', this.command, {
detached: true,
stdio: ['ignore', 'inherit', 'inherit']
})
if (this.vmProcess.exitCode) {
throw Error(
`Failed to start VM process, exit code: ${this.vmProcess.exitCode}`
)
}
this.vmProcess.unref()
this.ipAddress = await this.getIpAddress()
}
async wait(timeout: number): Promise<void> {
for (let index = 0; index < timeout; index++) {
core.info('Waiting for VM to be ready...')
const result = await this.execute('true', {
/*log: false,
silent: true,*/
ignoreReturnCode: true
})
if (result === 0) {
core.info('VM is ready')
return
}
await wait(1000)
}
throw Error(
`Waiting for VM to become ready timed out after ${timeout} seconds`
)
}
async setupWorkDirectory(
homeDirectory: string,
workDirectory: string
): Promise<void> {
const homeDirectoryLinuxHost = `/home/${Vm.user}/work`
await this.execute(
`rm -rf '${homeDirectoryLinuxHost}' && ` +
`sudo mkdir -p '${workDirectory}' && ` +
`sudo chown -R '${Vm.user}' '${homeDirectory}' && ` +
`ln -sf '${homeDirectory}/' '${homeDirectoryLinuxHost}'`
)
}
protected async shutdown(): Promise<void> {
throw Error('Not implemented')
}
async execute(
command: string,
options: ExecuteOptions = {}
): Promise<number> {
const defaultOptions = {log: true}
options = {...defaultOptions, ...options}
if (options.log) core.info(`Executing command inside VM: ${command}`)
const buffer = Buffer.from(command)
return await exec.exec('ssh', this.executeBaseArgs, {
input: buffer,
silent: options.silent,
ignoreReturnCode: options.ignoreReturnCode
})
}
async execute2(args: string[], intput: Buffer): Promise<number> {
return await exec.exec('ssh', this.executeBaseArgs.concat(args), {
input: intput
})
}
protected async getIpAddress(): Promise<string> {
throw Error('Not implemented')
}
protected abstract get command(): string[]
private get executeBaseArgs(): string[] {
const baseArgs = ['-t', `${Vm.user}@${this.ipAddress}`]
return core.isDebug() ? baseArgs.concat(['-vvv']) : baseArgs
}
}