Skip to content

Commit 853d5fa

Browse files
authored
Merge pull request #480 from vvoland/docker-install-image-latest-fix
docker/install: Fix latest image install on lima
2 parents e84b18a + 61c10b2 commit 853d5fa

File tree

6 files changed

+76
-28
lines changed

6 files changed

+76
-28
lines changed

__tests__/docker/install.test.itg.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {jest, describe, expect, test, beforeEach, afterEach} from '@jest/globals';
17+
import {jest, describe, test, beforeEach, afterEach, expect} from '@jest/globals';
1818
import fs from 'fs';
1919
import os from 'os';
2020
import path from 'path';
@@ -43,6 +43,7 @@ aarch64:https://cloud.debian.org/images/cloud/bookworm/20231013-1532/debian-12-g
4343
test.each([
4444
{type: 'image', tag: '27.3.1'} as InstallSourceImage,
4545
{type: 'image', tag: 'master'} as InstallSourceImage,
46+
{type: 'image', tag: 'latest'} as InstallSourceImage,
4647
{type: 'archive', version: 'v26.1.4', channel: 'stable'} as InstallSourceArchive,
4748
{type: 'archive', version: 'latest', channel: 'stable'} as InstallSourceArchive,
4849
])(
@@ -65,12 +66,17 @@ aarch64:https://cloud.debian.org/images/cloud/bookworm/20231013-1532/debian-12-g
6566
daemonConfig: `{"debug":true,"features":{"containerd-snapshotter":true}}`
6667
});
6768
await expect((async () => {
68-
await install.download();
69-
await install.install();
70-
await Docker.printVersion();
71-
await Docker.printInfo();
72-
})().finally(async () => {
73-
await install.tearDown();
74-
})).resolves.not.toThrow();
69+
try {
70+
await install.download();
71+
await install.install();
72+
await Docker.printVersion();
73+
await Docker.printInfo();
74+
} catch (error) {
75+
console.error(error);
76+
throw error;
77+
} finally {
78+
await install.tearDown();
79+
}
80+
})()).resolves.not.toThrow();
7581
}, 30 * 60 * 1000);
7682
});

src/docker/assets.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,10 @@ provision:
237237
238238
HOME=/tmp undock moby/moby-bin:{{srcImageTag}} /usr/local/bin
239239
240-
wget https://raw.githubusercontent.com/moby/moby/{{srcImageTag}}/contrib/init/systemd/docker.service \
241-
https://raw.githubusercontent.com/moby/moby/v{{srcImageTag}}/contrib/init/systemd/docker.service \
242-
-O /etc/systemd/system/docker.service || true
243-
wget https://raw.githubusercontent.com/moby/moby/{{srcImageTag}}/contrib/init/systemd/docker.socket \
244-
https://raw.githubusercontent.com/moby/moby/v{{srcImageTag}}/contrib/init/systemd/docker.socket \
245-
-O /etc/systemd/system/docker.socket || true
240+
wget https://raw.githubusercontent.com/moby/moby/{{gitCommit}}/contrib/init/systemd/docker.service \
241+
-O /etc/systemd/system/docker.service
242+
wget https://raw.githubusercontent.com/moby/moby/{{gitCommit}}/contrib/init/systemd/docker.socket \
243+
-O /etc/systemd/system/docker.socket
246244
247245
sed -i 's|^ExecStart=.*|ExecStart=/usr/local/bin/dockerd -H fd://|' /etc/systemd/system/docker.service
248246
sed -i 's|containerd.service||' /etc/systemd/system/docker.service

src/docker/install.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {Util} from '../util';
3434
import {limaYamlData, dockerServiceLogsPs1, setupDockerWinPs1} from './assets';
3535
import {GitHubRelease} from '../types/github';
3636
import {HubRepository} from '../hubRepository';
37+
import {Image} from '../types/oci/config';
3738

3839
export interface InstallSourceImage {
3940
type: 'image';
@@ -71,6 +72,8 @@ export class Install {
7172
private _version: string | undefined;
7273
private _toolDir: string | undefined;
7374

75+
private gitCommit: string | undefined;
76+
7477
private readonly limaInstanceName = 'docker-actions-toolkit';
7578

7679
constructor(opts: InstallOpts) {
@@ -127,12 +130,28 @@ export class Install {
127130
const cli = await HubRepository.build('dockereng/cli-bin');
128131
extractFolder = await cli.extractImage(tag);
129132

133+
const moby = await HubRepository.build('moby/moby-bin');
130134
if (['win32', 'linux'].includes(platform)) {
131135
core.info(`Downloading dockerd from moby/moby-bin:${tag}`);
132-
const moby = await HubRepository.build('moby/moby-bin');
133136
await moby.extractImage(tag, extractFolder);
134137
} else if (platform == 'darwin') {
135-
// On macOS, the docker daemon binary will be downloaded inside the lima VM
138+
// On macOS, the docker daemon binary will be downloaded inside the lima VM.
139+
// However, we will get the exact git revision from the image config
140+
// to get the matching systemd unit files.
141+
core.info(`Getting git revision from moby/moby-bin:${tag}`);
142+
143+
// There's no macOS image for moby/moby-bin - a linux daemon is run inside lima.
144+
const manifest = await moby.getPlatformManifest(tag, 'linux');
145+
146+
const config = await moby.getJSONBlob<Image>(manifest.config.digest);
147+
core.debug(`Config ${JSON.stringify(config.config)}`);
148+
149+
this.gitCommit = config.config?.Labels?.['org.opencontainers.image.revision'];
150+
if (!this.gitCommit) {
151+
core.warning(`No git revision can be determined from the image. Will use master.`);
152+
this.gitCommit = 'master';
153+
}
154+
core.info(`Git revision is ${this.gitCommit}`);
136155
} else {
137156
core.warning(`dockerd not supported on ${platform}, only the Docker cli will be available`);
138157
}
@@ -193,6 +212,9 @@ export class Install {
193212
}
194213

195214
private async installDarwin(): Promise<string> {
215+
if (this.source.type == 'image' && !this.gitCommit) {
216+
throw new Error('gitCommit must be set. Run download first.');
217+
}
196218
const src = this.source;
197219
const limaDir = path.join(os.homedir(), '.lima', this.limaInstanceName);
198220
await io.mkdirP(limaDir);
@@ -229,6 +251,7 @@ export class Install {
229251
customImages: Install.limaCustomImages(),
230252
daemonConfig: limaDaemonConfig,
231253
dockerSock: `${limaDir}/docker.sock`,
254+
gitCommit: this.gitCommit,
232255
srcType: src.type,
233256
srcArchiveVersion: this._version, // Use the resolved version (e.g. latest -> 27.4.0)
234257
srcArchiveChannel: srcArchive.channel,

src/hubRepository.ts

+29-12
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import * as core from '@actions/core';
2121
import {Manifest} from './types/oci/manifest';
2222
import * as tc from '@actions/tool-cache';
2323
import fs from 'fs';
24-
import {MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_V1} from './types/oci/mediatype';
25-
import {MEDIATYPE_IMAGE_MANIFEST_V2, MEDIATYPE_IMAGE_MANIFEST_LIST_V2} from './types/docker/mediatype';
24+
import {MEDIATYPE_IMAGE_CONFIG_V1, MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_V1} from './types/oci/mediatype';
25+
import {MEDIATYPE_IMAGE_CONFIG_V1 as DOCKER_MEDIATYPE_IMAGE_CONFIG_V1, MEDIATYPE_IMAGE_MANIFEST_LIST_V2, MEDIATYPE_IMAGE_MANIFEST_V2} from './types/docker/mediatype';
2626
import {DockerHub} from './dockerhub';
2727

2828
export class HubRepository {
@@ -40,15 +40,20 @@ export class HubRepository {
4040
return new HubRepository(repository, token);
4141
}
4242

43-
// Unpacks the image layers and returns the path to the extracted image.
44-
// Only OCI indexes/manifest list are supported for now.
45-
public async extractImage(tag: string, destDir?: string): Promise<string> {
46-
const index = await this.getManifest<Index>(tag);
43+
public async getPlatformManifest(tagOrDigest: string, os?: string): Promise<Manifest> {
44+
const index = await this.getManifest<Index>(tagOrDigest);
4745
if (index.mediaType != MEDIATYPE_IMAGE_INDEX_V1 && index.mediaType != MEDIATYPE_IMAGE_MANIFEST_LIST_V2) {
46+
core.error(`Unsupported image media type: ${index.mediaType}`);
4847
throw new Error(`Unsupported image media type: ${index.mediaType}`);
4948
}
50-
const digest = HubRepository.getPlatformManifestDigest(index);
51-
const manifest = await this.getManifest<Manifest>(digest);
49+
const digest = HubRepository.getPlatformManifestDigest(index, os);
50+
return await this.getManifest<Manifest>(digest);
51+
}
52+
53+
// Unpacks the image layers and returns the path to the extracted image.
54+
// Only OCI indexes/manifest list are supported for now.
55+
public async extractImage(tag: string, destDir?: string): Promise<string> {
56+
const manifest = await this.getPlatformManifest(tag);
5257

5358
const paths = manifest.layers.map(async layer => {
5459
const url = this.blobUrl(layer.digest);
@@ -99,25 +104,35 @@ export class HubRepository {
99104
}
100105

101106
public async getManifest<T>(tagOrDigest: string): Promise<T> {
102-
const url = `https://registry-1.docker.io/v2/${this.repo}/manifests/${tagOrDigest}`;
107+
return await this.registryGet<T>(tagOrDigest, 'manifests', [MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_LIST_V2, MEDIATYPE_IMAGE_MANIFEST_V1, MEDIATYPE_IMAGE_MANIFEST_V2]);
108+
}
109+
110+
public async getJSONBlob<T>(tagOrDigest: string): Promise<T> {
111+
return await this.registryGet<T>(tagOrDigest, 'blobs', [MEDIATYPE_IMAGE_CONFIG_V1, DOCKER_MEDIATYPE_IMAGE_CONFIG_V1]);
112+
}
113+
114+
private async registryGet<T>(tagOrDigest: string, endpoint: 'manifests' | 'blobs', accept: Array<string>): Promise<T> {
115+
const url = `https://registry-1.docker.io/v2/${this.repo}/${endpoint}/${tagOrDigest}`;
103116

104117
const headers = {
105118
Authorization: `Bearer ${this.token}`,
106-
Accept: [MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_LIST_V2, MEDIATYPE_IMAGE_MANIFEST_V1, MEDIATYPE_IMAGE_MANIFEST_V2].join(', ')
119+
Accept: accept.join(', ')
107120
};
121+
108122
const resp = await HubRepository.http.get(url, headers);
109123
const body = await resp.readBody();
110124
const statusCode = resp.message.statusCode || 500;
111125
if (statusCode != 200) {
126+
core.error(`registryGet(${this.repo}:${tagOrDigest}) failed: ${statusCode} ${body}`);
112127
throw DockerHub.parseError(resp, body);
113128
}
114129

115130
return <T>JSON.parse(body);
116131
}
117132

118-
private static getPlatformManifestDigest(index: Index): string {
133+
private static getPlatformManifestDigest(index: Index, osOverride?: string): string {
119134
// This doesn't handle all possible platforms normalizations, but it's good enough for now.
120-
let pos: string = os.platform();
135+
let pos: string = osOverride || os.platform();
121136
if (pos == 'win32') {
122137
pos = 'windows';
123138
}
@@ -150,8 +165,10 @@ export class HubRepository {
150165
return true;
151166
});
152167
if (!manifest) {
168+
core.error(`Cannot find manifest for ${pos}/${arch}/${variant}`);
153169
throw new Error(`Cannot find manifest for ${pos}/${arch}/${variant}`);
154170
}
171+
155172
return manifest.digest;
156173
}
157174
}

src/types/docker/mediatype.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
export const MEDIATYPE_IMAGE_MANIFEST_LIST_V2 = 'application/vnd.docker.distribution.manifest.list.v2+json';
1818

1919
export const MEDIATYPE_IMAGE_MANIFEST_V2 = 'application/vnd.docker.distribution.manifest.v2+json';
20+
21+
export const MEDIATYPE_IMAGE_CONFIG_V1 = 'application/vnd.docker.container.image.v1+json';

src/types/oci/mediatype.ts

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export const MEDIATYPE_IMAGE_INDEX_V1 = 'application/vnd.oci.image.index.v1+json
2323
export const MEDIATYPE_IMAGE_LAYER_V1 = 'application/vnd.oci.image.layer.v1.tar';
2424

2525
export const MEDIATYPE_EMPTY_JSON_V1 = 'application/vnd.oci.empty.v1+json';
26+
27+
export const MEDIATYPE_IMAGE_CONFIG_V1 = 'application/vnd.oci.image.config.v1+json';

0 commit comments

Comments
 (0)