Skip to content

Commit f2a378a

Browse files
committed
fix: emit VM_INIT_END
1 parent ece9427 commit f2a378a

File tree

2 files changed

+97
-92
lines changed

2 files changed

+97
-92
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nerdctl",
3-
"version": "0.3.5",
3+
"version": "0.3.6",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"description": "Node wrapper for nerdctl",

src/vms/lima.ts

Lines changed: 96 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -288,108 +288,113 @@ export default class LimaBackend extends BaseBackend {
288288
async initVM(): Promise<boolean> {
289289
this.emit(events.VM_INIT_START);
290290

291-
if (!!(await this.status)) {
292-
await this.lima("start", this.instance);
293-
return true;
294-
}
295-
296-
const platformName = platform === "darwin" ? "macos" : "linux";
297-
const archName = isM1 ? "-aarch64" : "";
298-
299-
const url = `${LIMA_REPO}/${LIMA_VERSION}/lima-and-qemu.${platformName}${archName}.tar.gz`;
300-
const resourcesDir = path.join(this.resourcePath, platform);
301-
const limaDir = path.join(resourcesDir, "lima");
302-
const tarPath = path.join(resourcesDir, `lima-${LIMA_VERSION}.tgz`);
291+
try {
292+
if (!!(await this.status)) {
293+
await this.lima("start", this.instance);
294+
} else {
295+
const platformName = platform === "darwin" ? "macos" : "linux";
296+
const archName = isM1 ? "-aarch64" : "";
303297

304-
this.emit(events.VM_INIT_OUTPUT, `Downloading virtual machine`);
298+
const url = `${LIMA_REPO}/${LIMA_VERSION}/lima-and-qemu.${platformName}${archName}.tar.gz`;
299+
const resourcesDir = path.join(this.resourcePath, platform);
300+
const limaDir = path.join(resourcesDir, "lima");
301+
const tarPath = path.join(resourcesDir, `lima-${LIMA_VERSION}.tgz`);
305302

306-
await download(url, tarPath);
307-
await fs.promises.mkdir(limaDir, { recursive: true });
303+
this.emit(events.VM_INIT_OUTPUT, `Downloading virtual machine`);
308304

309-
this.emit(events.VM_INIT_OUTPUT, "Extracting virtual machine files");
305+
await download(url, tarPath);
306+
await fs.promises.mkdir(limaDir, { recursive: true });
310307

311-
const child = ChildProcess.spawn("/usr/bin/tar", ["-xf", tarPath], {
312-
cwd: limaDir,
313-
stdio: "inherit",
314-
});
308+
this.emit(events.VM_INIT_OUTPUT, "Extracting virtual machine files");
315309

316-
await new Promise((resolve, reject) => {
317-
child.on("exit", (code, signal) => {
318-
if (code === 0) {
319-
resolve(true);
320-
} else {
321-
reject(new Error(`Lima extract failed with ${code || signal}`));
322-
}
323-
});
324-
});
310+
const child = ChildProcess.spawn("/usr/bin/tar", ["-xf", tarPath], {
311+
cwd: limaDir,
312+
stdio: "inherit",
313+
});
325314

326-
const config: LimaConfiguration = merge({
327-
arch: null,
328-
images: [
329-
{
330-
location:
331-
"https://cloud-images.ubuntu.com/releases/22.04/release-20220420/ubuntu-22.04-server-cloudimg-amd64.img",
332-
arch: "x86_64",
333-
digest:
334-
"sha256:de5e632e17b8965f2baf4ea6d2b824788e154d9a65df4fd419ec4019898e15cd",
335-
},
336-
{
337-
location:
338-
"https://cloud-images.ubuntu.com/releases/22.04/release-20220420/ubuntu-22.04-server-cloudimg-arm64.img",
339-
arch: "aarch64",
340-
digest:
341-
"sha256:66224c7fed99ff5a5539eda406c87bbfefe8af6ff6b47d92df3187832b5b5d4f",
342-
},
343-
{
344-
location:
345-
"https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img",
346-
arch: "x86_64",
347-
},
348-
{
349-
location:
350-
"https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-arm64.img",
351-
arch: "aarch64",
352-
},
353-
],
354-
cpus: 2,
355-
memory: 2 * 1024 * 1024 * 1024,
356-
mounts: [
357-
{ location: "~", writable: true },
358-
{ location: `/tmp/${APP_NAME}`, writable: true },
359-
],
360-
ssh: { localPort: await this.sshPort },
361-
caCerts: { removeDefaults: null, files: null, certs: null },
362-
containerd: { system: null, user: null },
363-
cpuType: { aarch64: null, x86_64: null },
364-
firmware: { legacyBIOS: null },
365-
video: { display: null },
366-
networks: null,
367-
propagateProxyEnv: null,
368-
hostResolver: {
369-
enabled: null,
370-
ipv6: null,
371-
hosts: {
372-
[`host.${APP_NAME}.internal`]: "host.lima.internal",
373-
},
374-
},
375-
});
315+
await new Promise((resolve, reject) => {
316+
child.on("exit", (code, signal) => {
317+
if (code === 0) {
318+
resolve(true);
319+
} else {
320+
reject(new Error(`Lima extract failed with ${code || signal}`));
321+
}
322+
});
323+
});
376324

377-
const CONFIG_PATH = path.join(
378-
this.limaHome,
379-
"_config",
380-
`${this.instance}.yaml`
381-
);
325+
const config: LimaConfiguration = merge({
326+
arch: null,
327+
images: [
328+
{
329+
location:
330+
"https://cloud-images.ubuntu.com/releases/22.04/release-20220420/ubuntu-22.04-server-cloudimg-amd64.img",
331+
arch: "x86_64",
332+
digest:
333+
"sha256:de5e632e17b8965f2baf4ea6d2b824788e154d9a65df4fd419ec4019898e15cd",
334+
},
335+
{
336+
location:
337+
"https://cloud-images.ubuntu.com/releases/22.04/release-20220420/ubuntu-22.04-server-cloudimg-arm64.img",
338+
arch: "aarch64",
339+
digest:
340+
"sha256:66224c7fed99ff5a5539eda406c87bbfefe8af6ff6b47d92df3187832b5b5d4f",
341+
},
342+
{
343+
location:
344+
"https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img",
345+
arch: "x86_64",
346+
},
347+
{
348+
location:
349+
"https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-arm64.img",
350+
arch: "aarch64",
351+
},
352+
],
353+
cpus: 2,
354+
memory: 2 * 1024 * 1024 * 1024,
355+
mounts: [
356+
{ location: "~", writable: true },
357+
{ location: `/tmp/${APP_NAME}`, writable: true },
358+
],
359+
ssh: { localPort: await this.sshPort },
360+
caCerts: { removeDefaults: null, files: null, certs: null },
361+
containerd: { system: null, user: null },
362+
cpuType: { aarch64: null, x86_64: null },
363+
firmware: { legacyBIOS: null },
364+
video: { display: null },
365+
networks: null,
366+
propagateProxyEnv: null,
367+
hostResolver: {
368+
enabled: null,
369+
ipv6: null,
370+
hosts: {
371+
[`host.${APP_NAME}.internal`]: "host.lima.internal",
372+
},
373+
},
374+
});
382375

383-
await fs.promises.mkdir(path.dirname(CONFIG_PATH), {
384-
recursive: true,
385-
});
386-
await fs.promises.writeFile(CONFIG_PATH, yaml.stringify(config), "utf-8");
376+
const CONFIG_PATH = path.join(
377+
this.limaHome,
378+
"_config",
379+
`${this.instance}.yaml`
380+
);
387381

388-
this.emit(events.VM_INIT_OUTPUT, "Starting virtual machine");
382+
await fs.promises.mkdir(path.dirname(CONFIG_PATH), {
383+
recursive: true,
384+
});
385+
await fs.promises.writeFile(
386+
CONFIG_PATH,
387+
yaml.stringify(config),
388+
"utf-8"
389+
);
389390

390-
await this.lima("start", "--tty=false", CONFIG_PATH);
391+
this.emit(events.VM_INIT_OUTPUT, "Starting virtual machine");
391392

392-
this.emit(events.VM_INIT_END);
393+
await this.lima("start", "--tty=false", CONFIG_PATH);
394+
}
395+
} finally {
396+
this.emit(events.VM_INIT_END);
397+
}
393398

394399
return true;
395400
}

0 commit comments

Comments
 (0)