Skip to content

Commit

Permalink
docker(install): allow specifying custom lima images
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Dec 10, 2023
1 parent 348446a commit 4995997
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
4 changes: 3 additions & 1 deletion __tests__/docker/install.test.itg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ describe('install', () => {
jest.resetModules();
process.env = {
...originalEnv,
LIMA_START_ARGS: '--cpus 4 --memory 8'
LIMA_START_ARGS: '--cpus 4 --memory 8',
LIMA_IMAGES: `x86_64:https://cloud.debian.org/images/cloud/bookworm/20231013-1532/debian-12-genericcloud-amd64-20231013-1532.qcow2@sha512:6b55e88b027c14da1b55c85a25a9f7069d4560a8fdb2d948c986a585db469728a06d2c528303e34bb62d8b2984def38fd9ddfc00965846ff6e05b01d6e883bfe
aarch64:https://cloud.debian.org/images/cloud/bookworm/20231013-1532/debian-12-genericcloud-arm64-20231013-1532.qcow2`
};
});
afterEach(() => {
Expand Down
29 changes: 29 additions & 0 deletions __tests__/docker/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,32 @@ describe('getRelease', () => {
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'));
});
});

describe('limaImage', () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {
...originalEnv,
LIMA_IMAGES: `x86_64:https://cloud-images.ubuntu.com/releases/23.10/release-20231011/ubuntu-23.10-server-cloudimg-amd64.img@sha256:f6529be56da3429a56e4f5ef202bf4958201bc63f8541e478caa6e8eb712e635
aarch64:https://cloud-images.ubuntu.com/releases/23.10/release-20231011/ubuntu-23.10-server-cloudimg-arm64.img`
};
});
afterEach(() => {
process.env = originalEnv;
});
it('returns custom images', async () => {
expect(Install.limaCustomImages()).toEqual([
{
location: 'https://cloud-images.ubuntu.com/releases/23.10/release-20231011/ubuntu-23.10-server-cloudimg-amd64.img',
arch: 'x86_64',
digest: 'sha256:f6529be56da3429a56e4f5ef202bf4958201bc63f8541e478caa6e8eb712e635'
},
{
location: 'https://cloud-images.ubuntu.com/releases/23.10/release-20231011/ubuntu-23.10-server-cloudimg-arm64.img',
arch: 'aarch64',
digest: ''
}
]);
});
});
5 changes: 5 additions & 0 deletions src/docker/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ os: null
arch: null
images:
{{#each customImages}}
- location: "{{location}}"
arch: "{{arch}}"
digest: "{{digest}}"
{{/each}}
- location: "https://cloud-images.ubuntu.com/releases/22.04/release-20231026/ubuntu-22.04-server-cloudimg-amd64.img"
arch: "x86_64"
digest: "sha256:054db2d88c454bb0ad8dfd8883955e3946b57d2b0bf0d023f3ade3c93cdd14e5"
Expand Down
28 changes: 28 additions & 0 deletions src/docker/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export interface InstallOpts {
daemonConfig?: string;
}

interface LimaImage {
location: string;
arch: string;
digest?: string;
}

export class Install {
private readonly runDir: string;
private readonly version: string;
Expand Down Expand Up @@ -163,6 +169,7 @@ export class Install {
return new handlebars.SafeString(JSON.stringify(obj));
});
const limaCfg = handlebars.compile(limaYamlData)({
customImages: Install.limaCustomImages(),
daemonConfig: limaDaemonConfig,
dockerSock: `${limaDir}/docker.sock`,
dockerBinVersion: this._version,
Expand Down Expand Up @@ -512,4 +519,25 @@ EOF`,
}
return releases[version];
}

public static limaCustomImages(): LimaImage[] {
const res: LimaImage[] = [];
const env = process.env.LIMA_IMAGES;
if (!env) {
return res;
}
for (const input of Util.getList(env, {ignoreComma: true, comment: '#'})) {
const archIndex = input.indexOf(':');
const arch = input.substring(0, archIndex).trim();
const digestIndex = input.indexOf('@');
const location = input.substring(archIndex + 1, digestIndex !== -1 ? digestIndex : undefined).trim();
const digest = digestIndex !== -1 ? input.substring(digestIndex + 1).trim() : '';
res.push({
location: location,
arch: arch,
digest: digest
});
}
return res;
}
}
14 changes: 8 additions & 6 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ import * as core from '@actions/core';
import * as io from '@actions/io';
import {parse} from 'csv-parse/sync';

export interface InputListOpts {
export interface ListOpts {
ignoreComma?: boolean;
comment?: string;
quote?: string | boolean | Buffer | null;
}

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

const items = core.getInput(name);
if (items == '') {
public static getList(input: string, opts?: ListOpts): string[] {
const res: Array<string> = [];
if (input == '') {
return res;
}

const records = parse(items, {
const records = parse(input, {
columns: false,
relaxQuotes: true,
comment: opts?.comment,
Expand Down

0 comments on commit 4995997

Please sign in to comment.