Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/cli/services/path.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
34 changes: 34 additions & 0 deletions src/cli/services/path.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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<void> {
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.
Expand Down Expand Up @@ -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'));
Expand Down
10 changes: 7 additions & 3 deletions src/cli/tools/docker/buildx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
16 changes: 5 additions & 11 deletions src/cli/tools/docker/buildx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -44,23 +45,16 @@ 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<void> {
const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin');

// 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. */
Expand Down
10 changes: 7 additions & 3 deletions src/cli/tools/docker/compose.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
17 changes: 6 additions & 11 deletions src/cli/tools/docker/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -57,23 +58,17 @@ 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<void> {
const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin');

// 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. */
Expand Down
48 changes: 48 additions & 0 deletions src/cli/tools/docker/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
);
});
});
});
33 changes: 29 additions & 4 deletions src/cli/tools/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<void> {
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');
}
}
}

Expand Down
42 changes: 42 additions & 0 deletions src/cli/tools/docker/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -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'));
});
});
Loading
Loading