Skip to content

Commit 7929028

Browse files
authored
Merge pull request #220 from crazy-max/lima-custom-images
docker(install): allow specifying custom lima images
2 parents 911d146 + 4995997 commit 7929028

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

__tests__/docker/install.test.itg.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ describe('install', () => {
2929
jest.resetModules();
3030
process.env = {
3131
...originalEnv,
32-
LIMA_START_ARGS: '--cpus 4 --memory 8'
32+
LIMA_START_ARGS: '--cpus 4 --memory 8',
33+
LIMA_IMAGES: `x86_64:https://cloud.debian.org/images/cloud/bookworm/20231013-1532/debian-12-genericcloud-amd64-20231013-1532.qcow2@sha512:6b55e88b027c14da1b55c85a25a9f7069d4560a8fdb2d948c986a585db469728a06d2c528303e34bb62d8b2984def38fd9ddfc00965846ff6e05b01d6e883bfe
34+
aarch64:https://cloud.debian.org/images/cloud/bookworm/20231013-1532/debian-12-genericcloud-arm64-20231013-1532.qcow2`
3335
};
3436
});
3537
afterEach(() => {

__tests__/docker/install.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,32 @@ describe('getRelease', () => {
7171
await expect(Install.getRelease('foo')).rejects.toThrow(new Error('Cannot find Docker release foo in https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/docker-releases.json'));
7272
});
7373
});
74+
75+
describe('limaImage', () => {
76+
const originalEnv = process.env;
77+
beforeEach(() => {
78+
jest.resetModules();
79+
process.env = {
80+
...originalEnv,
81+
LIMA_IMAGES: `x86_64:https://cloud-images.ubuntu.com/releases/23.10/release-20231011/ubuntu-23.10-server-cloudimg-amd64.img@sha256:f6529be56da3429a56e4f5ef202bf4958201bc63f8541e478caa6e8eb712e635
82+
aarch64:https://cloud-images.ubuntu.com/releases/23.10/release-20231011/ubuntu-23.10-server-cloudimg-arm64.img`
83+
};
84+
});
85+
afterEach(() => {
86+
process.env = originalEnv;
87+
});
88+
it('returns custom images', async () => {
89+
expect(Install.limaCustomImages()).toEqual([
90+
{
91+
location: 'https://cloud-images.ubuntu.com/releases/23.10/release-20231011/ubuntu-23.10-server-cloudimg-amd64.img',
92+
arch: 'x86_64',
93+
digest: 'sha256:f6529be56da3429a56e4f5ef202bf4958201bc63f8541e478caa6e8eb712e635'
94+
},
95+
{
96+
location: 'https://cloud-images.ubuntu.com/releases/23.10/release-20231011/ubuntu-23.10-server-cloudimg-arm64.img',
97+
arch: 'aarch64',
98+
digest: ''
99+
}
100+
]);
101+
});
102+
});

src/docker/assets.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ os: null
144144
arch: null
145145
146146
images:
147+
{{#each customImages}}
148+
- location: "{{location}}"
149+
arch: "{{arch}}"
150+
digest: "{{digest}}"
151+
{{/each}}
147152
- location: "https://cloud-images.ubuntu.com/releases/22.04/release-20231026/ubuntu-22.04-server-cloudimg-amd64.img"
148153
arch: "x86_64"
149154
digest: "sha256:054db2d88c454bb0ad8dfd8883955e3946b57d2b0bf0d023f3ade3c93cdd14e5"

src/docker/install.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export interface InstallOpts {
4141
daemonConfig?: string;
4242
}
4343

44+
interface LimaImage {
45+
location: string;
46+
arch: string;
47+
digest?: string;
48+
}
49+
4450
export class Install {
4551
private readonly runDir: string;
4652
private readonly version: string;
@@ -136,8 +142,9 @@ export class Install {
136142
await io.mkdirP(limaDir);
137143
const dockerHost = `unix://${limaDir}/docker.sock`;
138144

139-
// avoid brew to upgrade unrelated packages.
145+
// avoid brew to auto update and upgrade unrelated packages.
140146
let envs = Object.assign({}, process.env, {
147+
HOMEBREW_NO_AUTO_UPDATE: '1',
141148
HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK: '1'
142149
}) as {
143150
[key: string]: string;
@@ -149,6 +156,10 @@ export class Install {
149156
});
150157
}
151158

159+
await core.group('Lima version', async () => {
160+
await Exec.exec('lima', ['--version'], {env: envs});
161+
});
162+
152163
await core.group('Creating lima config', async () => {
153164
let limaDaemonConfig = {};
154165
if (this.daemonConfig) {
@@ -158,6 +169,7 @@ export class Install {
158169
return new handlebars.SafeString(JSON.stringify(obj));
159170
});
160171
const limaCfg = handlebars.compile(limaYamlData)({
172+
customImages: Install.limaCustomImages(),
161173
daemonConfig: limaDaemonConfig,
162174
dockerSock: `${limaDir}/docker.sock`,
163175
dockerBinVersion: this._version,
@@ -182,7 +194,7 @@ export class Install {
182194
};
183195

184196
await core.group('Starting lima instance', async () => {
185-
const limaStartArgs = ['start', `--name=${this.limaInstanceName}`, '--tty=false'];
197+
const limaStartArgs = ['start', `--name=${this.limaInstanceName}`];
186198
if (process.env.LIMA_START_ARGS) {
187199
limaStartArgs.push(process.env.LIMA_START_ARGS);
188200
}
@@ -507,4 +519,25 @@ EOF`,
507519
}
508520
return releases[version];
509521
}
522+
523+
public static limaCustomImages(): LimaImage[] {
524+
const res: LimaImage[] = [];
525+
const env = process.env.LIMA_IMAGES;
526+
if (!env) {
527+
return res;
528+
}
529+
for (const input of Util.getList(env, {ignoreComma: true, comment: '#'})) {
530+
const archIndex = input.indexOf(':');
531+
const arch = input.substring(0, archIndex).trim();
532+
const digestIndex = input.indexOf('@');
533+
const location = input.substring(archIndex + 1, digestIndex !== -1 ? digestIndex : undefined).trim();
534+
const digest = digestIndex !== -1 ? input.substring(digestIndex + 1).trim() : '';
535+
res.push({
536+
location: location,
537+
arch: arch,
538+
digest: digest
539+
});
540+
}
541+
return res;
542+
}
510543
}

src/util.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@ import * as core from '@actions/core';
1919
import * as io from '@actions/io';
2020
import {parse} from 'csv-parse/sync';
2121

22-
export interface InputListOpts {
22+
export interface ListOpts {
2323
ignoreComma?: boolean;
2424
comment?: string;
2525
quote?: string | boolean | Buffer | null;
2626
}
2727

2828
export class Util {
29-
public static getInputList(name: string, opts?: InputListOpts): string[] {
30-
const res: Array<string> = [];
29+
public static getInputList(name: string, opts?: ListOpts): string[] {
30+
return this.getList(core.getInput(name), opts);
31+
}
3132

32-
const items = core.getInput(name);
33-
if (items == '') {
33+
public static getList(input: string, opts?: ListOpts): string[] {
34+
const res: Array<string> = [];
35+
if (input == '') {
3436
return res;
3537
}
3638

37-
const records = parse(items, {
39+
const records = parse(input, {
3840
columns: false,
3941
relaxQuotes: true,
4042
comment: opts?.comment,

0 commit comments

Comments
 (0)