diff --git a/src/cli/services/path.service.spec.ts b/src/cli/services/path.service.spec.ts index 2fb1d877c0..e88c82f1d1 100644 --- a/src/cli/services/path.service.spec.ts +++ b/src/cli/services/path.service.spec.ts @@ -78,6 +78,45 @@ describe('cli/services/path.service', () => { ); expect(pathSvc.linksPath).toBe(rootPath('opt/containerbase/links')); expect(await pathExists(pathSvc.linksPath, 'dir')).toBe(true); + expect(pathSvc.libPath).toBe(rootPath('opt/containerbase/lib')); + expect(await pathExists(pathSvc.libPath, 'dir')).toBe(true); + expect(pathSvc.envDPath).toBe(rootPath('opt/containerbase/env.d')); + expect(await pathExists(pathSvc.envDPath, 'dir')).toBe(true); + }); + + test('ensureBasePaths keeps lib linked to a folder', async () => { + await pathSvc.ensureBasePaths(); + await ensurePaths('var/lib-volume'); + await fs.rm(pathSvc.libPath, { recursive: true, force: true }); + await fs.symlink(rootPath('var/lib-volume'), pathSvc.libPath); + + try { + await expect(pathSvc.ensureBasePaths()).resolves.toBeUndefined(); + expect(await fs.readlink(pathSvc.libPath)).toBe( + rootPath('var/lib-volume'), + ); + expect(logger.warn).not.toHaveBeenCalled(); + } finally { + await fs.rm(pathSvc.libPath); + await pathSvc.ensureBasePaths(); + } + }); + + test('ensureBasePaths only warns about an env.d which is no folder', async () => { + await pathSvc.ensureBasePaths(); + await fs.rm(pathSvc.envDPath, { recursive: true, force: true }); + await fs.symlink(rootPath('var/missing-env-d'), pathSvc.envDPath); + + try { + await expect(pathSvc.ensureBasePaths()).resolves.toBeUndefined(); + expect(logger.warn).toHaveBeenCalledWith( + { err: expect.any(Error), path: pathSvc.envDPath }, + 'cannot create the folder', + ); + } finally { + await fs.rm(pathSvc.envDPath); + await pathSvc.ensureBasePaths(); + } }); test('ensureBasePaths keeps a linked npm cache folder', async () => { diff --git a/src/cli/services/path.service.ts b/src/cli/services/path.service.ts index 14af3f3ea7..8dc4fdc54e 100644 --- a/src/cli/services/path.service.ts +++ b/src/cli/services/path.service.ts @@ -250,6 +250,20 @@ export class PathService { return join(this.envSvc.rootDir, 'opt/containerbase'); } + /** + * Path to `/opt/containerbase/env.d`, the env files of the shell tools. + */ + get envDPath(): string { + return join(this.installDir, 'env.d'); + } + + /** + * Path to `/opt/containerbase/lib`, which `/usr/local/lib` links to. + */ + get libPath(): string { + return join(this.installDir, 'lib'); + } + /** * Path to `/opt/containerbase/links`, the pointers of the commands to their * wrappers, see `LinkToolService`. @@ -324,6 +338,24 @@ export class PathService { await this.setOwner({ path, mode }); } + /** + * Creates a folder like `createDir`, but accepts a symlink to a folder, eg. + * for `lib` and `env.d` of an image which links them elsewhere. Anything + * else which prevents the folder, eg. a file or a dangling link, is only + * warned about, so it can't fail a command which didn't fail before. + */ + private async createLinkableDir(path: string): Promise { + try { + // follows a symlink + const stats = await fs.stat(path).catch(() => null); + if (!stats?.isDirectory()) { + await this.createDir(path); + } + } catch (err) { + logger.warn({ err, path }, 'cannot create the folder'); + } + } + /** * Creates a symlink at `path` pointing to `target`. An existing file, * folder or link at `path` is left as is. @@ -396,6 +428,8 @@ export class PathService { await this.createDir(this.versionPath); await this.createDir(this.binDir); await this.createDir(this.linksPath); + await this.createLinkableDir(this.libPath); + await this.createLinkableDir(this.envDPath); await this.createDir(this.sslPath); await this.createDir(this._toolInitPath); await this.createDir(join(this.cachePath, '.cache')); diff --git a/src/cli/tools/docker/buildx.spec.ts b/src/cli/tools/docker/buildx.spec.ts index 52dbb72025..88b5597c49 100644 --- a/src/cli/tools/docker/buildx.spec.ts +++ b/src/cli/tools/docker/buildx.spec.ts @@ -113,11 +113,15 @@ describe('cli/tools/docker/buildx', () => { await expect(svc.link('0.18.0')).resolves.toBeUndefined(); expect(spy).toHaveBeenCalledExactlyOnceWith('buildx', { srcDir: src }); - expect(await fs.readlink(plugin)).toBe(join(src, 'buildx')); + // the plugin runs the shim, which selects the version + const shim = join(pathSvc.binDir, 'buildx'); + expect(await fs.readlink(plugin)).toBe(shim); - // an existing plugin link is replaced + // a link to a version binary is replaced + await fs.rm(plugin); + await fs.symlink(join(src, 'buildx'), plugin); await expect(svc.link('0.18.0')).resolves.toBeUndefined(); - expect(await fs.readlink(plugin)).toBe(join(src, 'buildx')); + expect(await fs.readlink(plugin)).toBe(shim); }); test('runs the tool test', async () => { diff --git a/src/cli/tools/docker/buildx.ts b/src/cli/tools/docker/buildx.ts index d3f834fd20..91c0a38578 100644 --- a/src/cli/tools/docker/buildx.ts +++ b/src/cli/tools/docker/buildx.ts @@ -2,7 +2,8 @@ import fs from 'node:fs/promises'; import { join } from 'node:path'; import { injectFromHierarchy, injectable } from 'inversify'; import { BaseInstallService } from '../../install-tool/base-install.service.ts'; -import { pathExists, semverGte } from '../../utils/index.ts'; +import { semverGte } from '../../utils/index.ts'; +import { linkCliPlugin } from './utils.ts'; @injectable() @injectFromHierarchy() @@ -44,8 +45,8 @@ export class BuildxInstallService extends BaseInstallService { } /** - * Links the `buildx` binary into the global bin folder and as docker cli - * plugin. + * Links the `buildx` binary into the global bin folder and links the docker + * cli plugin to its shim there, so the plugin runs the linked version. */ override async link(version: string): Promise { const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin'); @@ -53,14 +54,7 @@ export class BuildxInstallService extends BaseInstallService { // to track linked version await this.shellwrapper({ srcDir: src }); - const tgt = join( - this.pathSvc.cachePath, - `.docker/cli-plugins/docker-${this.name}`, - ); - if (await pathExists(tgt)) { - await fs.rm(tgt); - } - await fs.symlink(join(src, this.name), tgt); + await linkCliPlugin(this.pathSvc, `docker-${this.name}`, this.name); } /** Checks that `docker buildx version` runs. */ diff --git a/src/cli/tools/docker/compose.spec.ts b/src/cli/tools/docker/compose.spec.ts index b736df2f3a..8fb789c5d5 100644 --- a/src/cli/tools/docker/compose.spec.ts +++ b/src/cli/tools/docker/compose.spec.ts @@ -124,11 +124,15 @@ describe('cli/tools/docker/compose', () => { expect(spy).toHaveBeenCalledExactlyOnceWith('docker-compose', { srcDir: src, }); - expect(await fs.readlink(plugin)).toBe(join(src, 'docker-compose')); + // the plugin runs the shim, which selects the version + const shim = join(pathSvc.binDir, 'docker-compose'); + expect(await fs.readlink(plugin)).toBe(shim); - // an existing plugin link is replaced + // a link to a version binary is replaced + await fs.rm(plugin); + await fs.symlink(join(src, 'docker-compose'), plugin); await expect(svc.link('2.29.7')).resolves.toBeUndefined(); - expect(await fs.readlink(plugin)).toBe(join(src, 'docker-compose')); + expect(await fs.readlink(plugin)).toBe(shim); }); test('runs the tool test', async () => { diff --git a/src/cli/tools/docker/compose.ts b/src/cli/tools/docker/compose.ts index 709cb03022..11b443ab9e 100644 --- a/src/cli/tools/docker/compose.ts +++ b/src/cli/tools/docker/compose.ts @@ -2,7 +2,8 @@ import fs from 'node:fs/promises'; import { join } from 'node:path'; import { injectFromHierarchy, injectable } from 'inversify'; import { BaseInstallService } from '../../install-tool/base-install.service.ts'; -import { pathExists, semverGte } from '../../utils/index.ts'; +import { semverGte } from '../../utils/index.ts'; +import { linkCliPlugin } from './utils.ts'; const minVersion = '2.0.1'; @@ -57,8 +58,9 @@ export class DockerComposeInstallService extends BaseInstallService { } /** - * Links the `docker-compose` binary into the global bin folder and as - * docker cli plugin. + * Links the `docker-compose` binary into the global bin folder and links + * the docker cli plugin to its shim there, so the plugin runs the linked + * version. */ override async link(version: string): Promise { const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin'); @@ -66,14 +68,7 @@ export class DockerComposeInstallService extends BaseInstallService { // to track linked version await this.shellwrapper({ srcDir: src }); - const tgt = join( - this.pathSvc.cachePath, - `.docker/cli-plugins/${this.name}`, - ); - if (await pathExists(tgt)) { - await fs.rm(tgt); - } - await fs.symlink(join(src, this.name), tgt); + await linkCliPlugin(this.pathSvc, this.name, this.name); } /** Checks that `docker compose version` runs. */ diff --git a/src/cli/tools/docker/index.spec.ts b/src/cli/tools/docker/index.spec.ts index 8e2517c588..9b355a24af 100644 --- a/src/cli/tools/docker/index.spec.ts +++ b/src/cli/tools/docker/index.spec.ts @@ -9,6 +9,7 @@ import { LinkToolService, PathService, } from '../../services/index.ts'; +import { logger } from '../../utils/index.ts'; import { DockerInstallService, DockerPrepareService } from './index.ts'; import { scope } from '~test/http-mock.ts'; import { ensurePaths } from '~test/path.ts'; @@ -163,5 +164,52 @@ describe('cli/tools/docker/index', () => { // second run is a no-op, it would throw on existing symlinks otherwise await expect(svc.prepare()).resolves.toBeUndefined(); }); + + test('initialize links the global cli plugins folder', async () => { + const { svc, child } = await toolContext(DockerPrepareService); + const pathSvc = await child.getAsync(PathService); + const envSvc = await child.getAsync(EnvService); + const global = join(envSvc.rootDir, 'usr/local/lib/docker'); + await fs.rm(global, { recursive: true, force: true }); + + await expect(svc.initialize()).resolves.toBeUndefined(); + + expect(await fs.readlink(join(global, 'cli-plugins'))).toBe( + join(pathSvc.cachePath, '.docker', 'cli-plugins'), + ); + }); + + test('initialize links the plugins of the commands which have a shim', async () => { + const { svc, child } = await toolContext(DockerPrepareService); + const pathSvc = await child.getAsync(PathService); + const plugins = join(pathSvc.cachePath, '.docker', 'cli-plugins'); + await fs.rm(plugins, { recursive: true, force: true }); + await fs.mkdir(pathSvc.binDir, { recursive: true }); + await fs.writeFile(join(pathSvc.binDir, 'buildx'), '#!/bin/bash\n'); + await fs.rm(join(pathSvc.binDir, 'docker-compose'), { force: true }); + + await expect(svc.initialize()).resolves.toBeUndefined(); + + expect(await fs.readlink(join(plugins, 'docker-buildx'))).toBe( + join(pathSvc.binDir, 'buildx'), + ); + // no shim, no plugin link + await expect(fs.lstat(join(plugins, 'docker-compose'))).rejects.toThrow(); + + await fs.rm(join(pathSvc.binDir, 'buildx')); + }); + + test('initialize only warns when the global folder cannot be created', async () => { + const { svc } = await toolContext(DockerPrepareService); + const err = new Error('EROFS'); + vi.spyOn(fs, 'mkdir').mockRejectedValueOnce(err); + + await expect(svc.initialize()).resolves.toBeUndefined(); + + expect(logger.warn).toHaveBeenCalledWith( + { err }, + 'cannot link the docker cli plugins folder', + ); + }); }); }); diff --git a/src/cli/tools/docker/index.ts b/src/cli/tools/docker/index.ts index 3d0b08b9ac..7407ef1f46 100644 --- a/src/cli/tools/docker/index.ts +++ b/src/cli/tools/docker/index.ts @@ -3,6 +3,8 @@ import { join } from 'node:path'; import { injectFromHierarchy, injectable } from 'inversify'; import { BaseInstallService } from '../../install-tool/base-install.service.ts'; import { BasePrepareService } from '../../prepare-tool/base-prepare.service.ts'; +import { logger, pathExists } from '../../utils/index.ts'; +import { cliPlugins, linkCliPlugin } from './utils.ts'; @injectable() @injectFromHierarchy() @@ -29,11 +31,34 @@ export class DockerPrepareService extends BasePrepareService { ); } - /** Creates the docker cli plugins folder in the containerbase cache. */ + /** + * Creates the docker cli plugins folder in the containerbase cache, and the + * global cli plugins folder with its link to it, which an empty + * `/opt/containerbase` misses, as `/usr/local/lib` links there. An existing + * link is kept, and a folder which can't be written is only warned about. + * The buildx and compose plugin links, which live in the temp folder, are + * created again for the commands which have a shim. + */ override async initialize(): Promise { - await this.pathSvc.createDir( - join(this.pathSvc.cachePath, '.docker', 'cli-plugins'), - ); + const plugins = join(this.pathSvc.cachePath, '.docker', 'cli-plugins'); + await this.pathSvc.createDir(plugins); + + for (const { plugin, command } of cliPlugins) { + if (await pathExists(join(this.pathSvc.binDir, command), 'file')) { + await linkCliPlugin(this.pathSvc, plugin, command); + } + } + + const globalDocker = join(this.envSvc.rootDir, 'usr/local/lib/docker'); + try { + await fs.mkdir(globalDocker, { recursive: true }); + await this.pathSvc.createSymlink( + plugins, + join(globalDocker, 'cli-plugins'), + ); + } catch (err) { + logger.warn({ err }, 'cannot link the docker cli plugins folder'); + } } } diff --git a/src/cli/tools/docker/utils.spec.ts b/src/cli/tools/docker/utils.spec.ts new file mode 100644 index 0000000000..0655c2ab05 --- /dev/null +++ b/src/cli/tools/docker/utils.spec.ts @@ -0,0 +1,42 @@ +import fs from 'node:fs/promises'; +import { join } from 'node:path'; +import { beforeAll, describe, expect, test } from 'vitest'; +import { PathService } from '../../services/index.ts'; +import { linkCliPlugin } from './utils.ts'; +import { testContainer } from '~test/di.ts'; +import { ensurePaths } from '~test/path.ts'; + +describe('cli/tools/docker/utils', () => { + beforeAll(async () => { + await ensurePaths(['tmp', 'opt/containerbase/bin']); + }); + + test('linkCliPlugin: links the plugin to the shim', async () => { + const pathSvc = await (await testContainer()).getAsync(PathService); + const plugin = join(pathSvc.cachePath, '.docker/cli-plugins/docker-demo'); + await fs.rm(join(pathSvc.cachePath, '.docker'), { + recursive: true, + force: true, + }); + + // creates the plugins folder + await linkCliPlugin(pathSvc, 'docker-demo', 'demo'); + expect(await fs.readlink(plugin)).toBe(join(pathSvc.binDir, 'demo')); + + // a link to the shim is kept + const before = await fs.lstat(plugin); + await linkCliPlugin(pathSvc, 'docker-demo', 'demo'); + expect((await fs.lstat(plugin)).ino).toBe(before.ino); + + // another link or a file is replaced + await fs.rm(plugin); + await fs.symlink('/missing/demo', plugin); + await linkCliPlugin(pathSvc, 'docker-demo', 'demo'); + expect(await fs.readlink(plugin)).toBe(join(pathSvc.binDir, 'demo')); + + await fs.rm(plugin); + await fs.writeFile(plugin, 'binary'); + await linkCliPlugin(pathSvc, 'docker-demo', 'demo'); + expect(await fs.readlink(plugin)).toBe(join(pathSvc.binDir, 'demo')); + }); +}); diff --git a/src/cli/tools/docker/utils.ts b/src/cli/tools/docker/utils.ts new file mode 100644 index 0000000000..254baad78f --- /dev/null +++ b/src/cli/tools/docker/utils.ts @@ -0,0 +1,35 @@ +import fs from 'node:fs/promises'; +import { join } from 'node:path'; +import type { PathService } from '../../services/index.ts'; + +/** + * The docker cli plugins which are linked to the shim of their command: the + * name of the plugin file and the command of the tool. + */ +export const cliPlugins = [ + { plugin: 'docker-buildx', command: 'buildx' }, + { plugin: 'docker-compose', command: 'docker-compose' }, +] as const; + +/** + * Links a docker cli plugin to the shim of its command in the global bin + * folder, instead of to a version binary. The shim doesn't use its name, so it + * works through the plugin name too, and its pointer selects the version and + * installs it on its first call. A link to the shim is kept, any other link or + * file is replaced. + */ +export async function linkCliPlugin( + pathSvc: PathService, + plugin: string, + command: string, +): Promise { + const dir = join(pathSvc.cachePath, '.docker/cli-plugins'); + const tgt = join(dir, plugin); + const shim = join(pathSvc.binDir, command); + if ((await fs.readlink(tgt).catch(() => null)) === shim) { + return; + } + await pathSvc.createDir(dir); + await fs.rm(tgt, { force: true }); + await fs.symlink(shim, tgt); +} diff --git a/src/cli/tools/dotnet/mono.spec.ts b/src/cli/tools/dotnet/mono.spec.ts index 7324901f2f..1c277ea66b 100644 --- a/src/cli/tools/dotnet/mono.spec.ts +++ b/src/cli/tools/dotnet/mono.spec.ts @@ -158,6 +158,40 @@ describe('cli/tools/dotnet/mono', () => { ); }); + test('initialize creates the cert store folder', async () => { + const { svc, child } = await toolContext(MonoPrepareService); + const pathSvc = await child.getAsync(PathService); + const store = join(pathSvc.sslPath, 'mono'); + await fs.rm(store, { recursive: true, force: true }); + + await expect(svc.initialize()).resolves.toBeUndefined(); + // a second run keeps it + await expect(svc.initialize()).resolves.toBeUndefined(); + + expect((await fs.stat(store)).isDirectory()).toBe(true); + }); + + test('prepare installs the hook after an initialize', async () => { + const { svc, child } = await toolContext(MonoPrepareService); + const pathSvc = await child.getAsync(PathService); + const envSvc = await child.getAsync(EnvService); + const store = join(pathSvc.sslPath, 'mono'); + const hook = join( + envSvc.rootDir, + 'etc/ca-certificates/update.d/containerbase-mono-keystore', + ); + await fs.rm(store, { recursive: true, force: true }); + await fs.rm(hook, { force: true }); + + await svc.initialize(); + await expect(svc.prepare()).resolves.toBeUndefined(); + + expect(await fs.readFile(hook, 'utf8')).toContain( + 'Updating Mono key store', + ); + expect((await fs.stat(store)).isDirectory()).toBe(true); + }); + test('prepare replaces an existing mono link', async () => { const { svc, child } = await toolContext(MonoPrepareService); const envSvc = await child.getAsync(EnvService); diff --git a/src/cli/tools/dotnet/mono.ts b/src/cli/tools/dotnet/mono.ts index 4840306cc3..dcc46074e8 100644 --- a/src/cli/tools/dotnet/mono.ts +++ b/src/cli/tools/dotnet/mono.ts @@ -13,10 +13,15 @@ export class MonoPrepareService extends BasePrepareService { /** * Initializes the cache, links `~/.mono` to it, keeping any existing link, * and sets up the mono cert store with a hook that syncs it on ca - * certificate updates. + * certificate updates, when the hook is missing. */ override async prepare(): Promise { // TODO: install mono dependencies if needed + const src = join(this.pathSvc.sslPath, 'mono'); + const hook = join( + this.envSvc.rootDir, + 'etc/ca-certificates/update.d/containerbase-mono-keystore', + ); await this.initialize(); const mono = join(this.envSvc.userHome, '.mono'); await this.pathSvc.createSymlink( @@ -24,16 +29,12 @@ export class MonoPrepareService extends BasePrepareService { mono, ); - const src = join(this.pathSvc.sslPath, 'mono'); - if (!(await pathExists(src))) { - await this.pathSvc.createDir(src); + // the initialize creates the cert store folder, so the hook decides + if (!(await pathExists(hook))) { // inspired by debian mono packages const certSync = join(this.pathSvc.binDir, 'cert-sync'); await fs.writeFile( - join( - this.envSvc.rootDir, - 'etc/ca-certificates/update.d/containerbase-mono-keystore', - ), + hook, fileContent` #!/bin/sh @@ -62,12 +63,16 @@ export class MonoPrepareService extends BasePrepareService { await fs.symlink(src, tgt); } - /** Creates the `.mono` folder in the containerbase cache. */ + /** + * Creates the `.mono` folder in the containerbase cache and the mono cert + * store folder, which an empty `/opt/containerbase` misses. + */ override async initialize(): Promise { const mono = join(this.pathSvc.cachePath, '.mono'); if (!(await pathExists(mono))) { await this.pathSvc.createDir(mono); } + await this.pathSvc.createDir(join(this.pathSvc.sslPath, 'mono')); } } diff --git a/src/cli/tools/java/index.spec.ts b/src/cli/tools/java/index.spec.ts index 4c333413c4..ef076153a4 100644 --- a/src/cli/tools/java/index.spec.ts +++ b/src/cli/tools/java/index.spec.ts @@ -232,6 +232,18 @@ describe('cli/tools/java/index', () => { await fs.rm(cacerts, { force: true }); }); + test('isPrepared counts the cacerts of the image', async () => { + const { svc, pathSvc } = await toolContext(JavaInstallService); + const varCerts = path.join(pathSvc.varPath, 'cacerts'); + await fs.rm(path.join(pathSvc.sslPath, 'cacerts'), { force: true }); + await fs.mkdir(pathSvc.varPath, { recursive: true }); + await fs.writeFile(varCerts, 'certs'); + + expect(await svc.isPrepared()).toBe(true); + + await fs.rm(varCerts); + }); + test('link and alias', async () => { const { svc, pathSvc } = await toolContext(JavaJdkInstallService); const spy = vi.spyOn(LinkToolService.prototype, 'shellwrapper'); @@ -322,6 +334,23 @@ describe('cli/tools/java/index', () => { await expect(svc.initialize()).resolves.toBeUndefined(); }); + test('initialize links a missing shared cacerts to the one of the image', async () => { + const { svc, pathSvc } = await prepareContext(JavaPrepareService); + const varCerts = path.join(pathSvc.varPath, 'cacerts'); + await fs.mkdir(pathSvc.varPath, { recursive: true }); + await fs.writeFile(varCerts, 'certs'); + + await expect(svc.initialize()).resolves.toBeUndefined(); + // an existing link is kept + await expect(svc.initialize()).resolves.toBeUndefined(); + + expect(await fs.readlink(path.join(pathSvc.sslPath, 'cacerts'))).toBe( + varCerts, + ); + await fs.rm(path.join(pathSvc.sslPath, 'cacerts')); + await fs.rm(varCerts); + }); + test('prepare downloads the cert store from the tool-versions data', async () => { const { svc, pathSvc } = await prepareContext(JavaJrePrepareService); const name = 'OpenJDK21U-jre_x64_linux_hotspot_21.0.4_7.tar.gz'; @@ -357,6 +386,33 @@ describe('cli/tools/java/index', () => { expect(extract).toHaveBeenCalledOnce(); }); + test('prepare keeps the shared cacerts link of the initialize', async () => { + const { svc, pathSvc } = await prepareContext(JavaJrePrepareService); + const name = 'OpenJDK21U-jre_x64_linux_hotspot_21.0.4_7.tar.gz'; + const varCerts = path.join(pathSvc.varPath, 'cacerts'); + await fs.mkdir(pathSvc.varPath, { recursive: true }); + await fs.writeFile(varCerts, 'old certs'); + mockJreData(); + mockToolFiles([ + toolFile(`${cdnUrl}/${name}`, tarball, 'amd64'), + toolFile(`${cdnUrl}/arm64-${name}`, tarball, 'arm64'), + ]); + vi.spyOn(CompressionService.prototype, 'extract').mockImplementation( + async ({ cwd }) => { + await fs.mkdir(path.join(cwd, 'lib/security'), { recursive: true }); + await fs.writeFile(path.join(cwd, 'lib/security/cacerts'), 'certs'); + }, + ); + + // the image has a cert store, the volume has no link to it yet + await expect(svc.prepare()).resolves.toBeUndefined(); + + expect(await fs.readlink(path.join(pathSvc.sslPath, 'cacerts'))).toBe( + varCerts, + ); + await fs.rm(varCerts); + }); + test('prepare: throws without a jre version', async () => { const { svc } = await prepareContext(JavaPrepareService); const resolve = vi diff --git a/src/cli/tools/java/index.ts b/src/cli/tools/java/index.ts index d0b31dfe7d..ad840b8e16 100644 --- a/src/cli/tools/java/index.ts +++ b/src/cli/tools/java/index.ts @@ -38,11 +38,13 @@ export class JavaPrepareService extends BasePrepareService { * the host */ override async prepare(): Promise { - await this.initialize(); - const cacerts = path.join(this.pathSvc.sslPath, 'cacerts'); + // before the initialize, which links an existing cacerts of the image + const hasCacerts = await this.pathSvc.fileExists(cacerts); - if (await this.pathSvc.fileExists(cacerts)) { + await this.initialize(); + + if (hasCacerts) { // cert store already there return; } @@ -91,7 +93,8 @@ export class JavaPrepareService extends BasePrepareService { await fs.cp(path.join(tmp, 'lib/security/cacerts'), varCerts); - await fs.symlink(varCerts, cacerts); + // the initialize may have linked the cacerts of the image already + await this.pathSvc.createSymlink(varCerts, cacerts); await this.pathSvc.exportEnv({ ANDROID_HOME: '~/.android-sdk' }); @@ -100,9 +103,19 @@ export class JavaPrepareService extends BasePrepareService { /** * Creates the maven and gradle settings and the android folders in the - * containerbase cache, and points `GRADLE_USER_HOME` there. + * containerbase cache, and points `GRADLE_USER_HOME` there. A missing + * shared `ssl/cacerts`, which the JREs link to and an empty + * `/opt/containerbase` misses, is linked to the cacerts of the image again. */ override async initialize(): Promise { + const varCerts = path.join(this.pathSvc.varPath, 'cacerts'); + if (await this.pathSvc.fileExists(varCerts)) { + await this.pathSvc.createSymlink( + varCerts, + path.join(this.pathSvc.sslPath, 'cacerts'), + ); + } + await createMavenSettings(this.pathSvc); await createGradleSettings(this.pathSvc); @@ -216,10 +229,19 @@ export class JavaInstallService extends BaseInstallService { }); } - /** Whether the shared cacerts exist, which prepare creates. */ + /** + * Whether the shared cacerts exist, which prepare creates. An empty + * `/opt/containerbase` has no link to them, but the cacerts of the image + * count, `initialize` links them. + */ override async isPrepared(): Promise { - return await this.pathSvc.fileExists( - path.join(this.pathSvc.sslPath, 'cacerts'), + return ( + (await this.pathSvc.fileExists( + path.join(this.pathSvc.sslPath, 'cacerts'), + )) || + (await this.pathSvc.fileExists( + path.join(this.pathSvc.varPath, 'cacerts'), + )) ); }