-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path_runner.services.ts
More file actions
273 lines (222 loc) · 7.37 KB
/
_runner.services.ts
File metadata and controls
273 lines (222 loc) · 7.37 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import {nonNullish} from '@dfinity/utils';
import {assertAnswerCtrlC, execute, spawn} from '@junobuild/cli-tools';
import {type EmulatorPorts} from '@junobuild/config';
import {red, yellow} from 'kleur';
import prompts from 'prompts';
import {readEmulatorConfig} from '../../configs/emulator.config';
import {junoConfigExist} from '../../configs/juno.config';
import {
EMULATOR_PORT_ADMIN,
EMULATOR_PORT_CONSOLE,
EMULATOR_PORT_SERVER,
EMULATOR_SKYLAB
} from '../../constants/emulator.constants';
import {
type CliEmulatorConfig,
type CliEmulatorDerivedConfig,
type EmulatorRunnerType,
type EmulatorType
} from '../../types/emulator';
import {isHeadless} from '../../utils/process.utils';
import {
assertContainerRunnerRunning,
checkDockerVersion,
hasExistingContainer,
isContainerRunning
} from '../../utils/runner.utils';
import {initConfigNoneInteractive} from '../config/init.services';
import {createDeployTargetDir} from './_fs.services';
export const startContainer = async () => {
await assertAndInitConfig();
const parsedResult = await readEmulatorConfig();
if (!parsedResult.success) {
return;
}
const {config} = parsedResult;
const {valid} =
config.derivedConfig.runner === 'docker' ? await checkDockerVersion() : {valid: true};
if (valid === 'error' || !valid) {
return;
}
await assertContainerRunnerRunning({runner: config.derivedConfig.runner});
await startEmulator({config});
};
export const stopContainer = async () => {
const parsedResult = await readEmulatorConfig();
if (!parsedResult.success) {
return;
}
const {config} = parsedResult;
const {valid} =
config.derivedConfig.runner === 'docker' ? await checkDockerVersion() : {valid: true};
if (valid === 'error' || !valid) {
return;
}
await assertContainerRunnerRunning({runner: config.derivedConfig.runner});
await stopEmulator({config});
};
const promptEmulatorType = async (): Promise<{emulatorType: Exclude<EmulatorType, 'console'>}> => {
const {emulatorType}: {emulatorType: Exclude<EmulatorType, 'console'> | undefined} =
await prompts({
type: 'select',
name: 'emulatorType',
message: 'What kind of emulator would you like to run locally?',
choices: [
{
title: `Complete environment with Console and known services`,
value: `skylab`
},
{title: `Minimal headless setup`, value: `satellite`}
]
});
assertAnswerCtrlC(emulatorType);
return {emulatorType};
};
const promptRunnerType = async (): Promise<{runnerType: EmulatorRunnerType}> => {
const {runnerType}: {runnerType: EmulatorRunnerType | undefined} = await prompts({
type: 'select',
name: 'runnerType',
message: 'Which container runtime would you like to use?',
choices: [
{
title: 'Docker',
value: 'docker'
},
{title: 'Podman', value: 'podman'},
{title: 'Apple container', value: 'container'}
]
});
assertAnswerCtrlC(runnerType);
return {runnerType};
};
const assertAndInitConfig = async () => {
const configExist = await junoConfigExist();
if (configExist) {
return;
}
await initConfigFile();
};
const initConfigFile = async () => {
const {emulatorType} = await promptEmulatorType();
const {runnerType} = await promptRunnerType();
// Default skylab and docker, no need to specify those options in the config if they were picked.
const emulatorConfig =
emulatorType === 'skylab' && runnerType === 'docker'
? undefined
: {
runner: {
type: runnerType
},
...(emulatorType === 'satellite' ? {satellite: {}} : {skylab: {}})
};
await initConfigNoneInteractive({
emulatorConfig
});
};
const startEmulator = async ({config: extendedConfig}: {config: CliEmulatorConfig}) => {
const {
config,
derivedConfig: {emulatorType, containerName, runner, targetDeploy}
} = extendedConfig;
const {running} = await assertContainerRunning({containerName, runner});
if (running) {
console.log(yellow(`The ${runner} container ${containerName} is already running.`));
return;
}
const status = await hasExistingContainer({containerName, runner});
if ('err' in status) {
console.log(red(`Unable to check if ${runner} container ${containerName} already exists.`));
return;
}
console.log('🧪 Launching local emulator...');
if (status.exist) {
// Support for Ctrl+C:
// -a: Attach STDOUT/STDERR. Equivalent to `--attach`.
// -i: Keep STDIN open even if not attached. Equivalent to `--interactive`.
await execute({
command: runner,
args: ['start', '-a', ...(isHeadless() ? [] : ['-i']), containerName]
});
return;
}
const ports: Required<Omit<EmulatorPorts, 'timeoutInSeconds'>> = {
server: config[emulatorType]?.ports?.server ?? EMULATOR_SKYLAB.ports.server,
admin: config[emulatorType]?.ports?.admin ?? EMULATOR_SKYLAB.ports.admin
};
const portTimeoutInSeconds = config[emulatorType]?.ports?.timeoutInSeconds;
// Support Ctrl+C:
// -i: Keeps STDIN open for the container. Equivalent to `--interactive`.
// -t: Allocates a pseudo-TTY, enabling terminal-like behavior. Equivalent to `--tty`.
/**
* Example:
*
* docker run -it \
* --name juno-skylab-aaabbb \
* -p 5987:5987 \
* -p 5999:5999 \
* -p 5866:5866 \
* -v juno_skylab_test_61:/juno/.juno \
* -v "$(pwd)/juno.config.mjs:/juno/juno.config.mjs" \
* -v "$(pwd)/target/deploy:/juno/target/deploy" \
* juno-skylab-pocket-ic
*/
const volume = config.runner?.volume ?? containerName.replaceAll('-', '_');
// Podman does not auto create the path folders.
await createDeployTargetDir({targetDeploy});
const image = config.runner?.image ?? `junobuild/${emulatorType}:latest`;
const platform = config.runner?.platform;
await execute({
command: runner,
args: [
'run',
...(isHeadless() ? [] : ['-it']),
'--name',
containerName,
'-p',
`${ports.server}:${EMULATOR_PORT_SERVER}`,
'-p',
`${ports.admin}:${EMULATOR_PORT_ADMIN}`,
...('skylab' in config
? [
'-p',
`${config.skylab.ports?.console ?? EMULATOR_SKYLAB.ports.console}:${EMULATOR_PORT_CONSOLE}`
]
: []),
...(nonNullish(portTimeoutInSeconds)
? ['-e', `PORT_TIMEOUT_SECONDS=${portTimeoutInSeconds}`]
: []),
'-v',
`${volume}:/juno/.juno`,
'-v',
`${targetDeploy}:/juno/target/deploy`,
...(nonNullish(platform) ? [`--platform=${platform}`] : []),
image
]
});
};
const stopEmulator = async ({config: {derivedConfig}}: {config: CliEmulatorConfig}) => {
const {containerName, runner} = derivedConfig;
const {running} = await assertContainerRunning({containerName, runner});
if (!running) {
console.log(yellow(`The ${runner} container ${containerName} is already stopped.`));
return;
}
await spawn({
command: runner,
args: ['stop', containerName],
silentOut: true
});
};
const assertContainerRunning = async ({
containerName,
runner
}: Pick<CliEmulatorDerivedConfig, 'containerName' | 'runner'>): Promise<{running: boolean}> => {
const result = await isContainerRunning({containerName, runner});
if ('err' in result) {
console.log(
red(`Unable to verify if container ${containerName} is running. Is ${runner} installed?`)
);
process.exit(1);
}
return result;
};