Skip to content
Merged
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"got": "14.4.8",
"ini": "5.0.0",
"inversify": "7.9.1",
"nano-spawn": "1.0.3",
"pino": "9.9.4",
"pino-pretty": "13.1.1",
"pretty-ms": "9.2.0",
Expand Down Expand Up @@ -135,7 +136,8 @@
"@semantic-release/github": "patches/@semantic-release__github.patch",
"@yao-pkg/pkg": "patches/@yao-pkg__pkg.patch",
"clipanion@3.2.1": "patches/clipanion@3.2.1.patch",
"global-agent": "patches/global-agent.patch"
"global-agent": "patches/global-agent.patch",
"nano-spawn": "patches/nano-spawn.patch"
},
"ignoredBuiltDependencies": [
"esbuild",
Expand Down
25 changes: 25 additions & 0 deletions patches/nano-spawn.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
diff --git a/source/spawn.js b/source/spawn.js
index 0018a555bed498c5ff530139598158801683cb0b..389f0bc893beb2c40627e2b80ba2a653678bedcf 100644
--- a/source/spawn.js
+++ b/source/spawn.js
@@ -1,20 +1,10 @@
import {spawn} from 'node:child_process';
import {once} from 'node:events';
-import process from 'node:process';
import {applyForceShell} from './windows.js';
import {getResultError} from './result.js';

export const spawnSubprocess = async (file, commandArguments, options, context) => {
try {
- // When running `node`, keep the current Node version and CLI flags.
- // Not applied with file paths to `.../node` since those indicate a clear intent to use a specific Node version.
- // This also provides a way to opting out, e.g. using `process.execPath` instead of `node` to discard current CLI flags.
- // Does not work with shebangs, but those don't work cross-platform anyway.
- if (['node', 'node.exe'].includes(file.toLowerCase())) {
- file = process.execPath;
- commandArguments = [...process.execArgv.filter(flag => !flag.startsWith('--inspect')), ...commandArguments];
- }
-
[file, commandArguments, options] = await applyForceShell(file, commandArguments, options);
[file, commandArguments, options] = concatenateShell(file, commandArguments, options);
const instance = spawn(file, commandArguments, options);
10 changes: 8 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/cli/install-tool/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'node:fs/promises';
import { beforeAll, describe, expect, test, vi } from 'vitest';
import { VersionService, createContainer } from '../services';
import { NpmVersionResolver } from '../tools/node/resolver';
Expand All @@ -9,10 +10,10 @@ import {
RubyGemVersionResolver,
} from '../tools/ruby/utils';
import { installTool, resolveVersion } from '.';
import { ensurePaths } from '~test/path';
import { ensurePaths, rootPath } from '~test/path';

vi.mock('del');
vi.mock('execa');
vi.mock('nano-spawn');
vi.mock('../tools/bun');
vi.mock('../tools/php/composer');

Expand All @@ -22,8 +23,14 @@ describe('cli/install-tool/index', () => {
'var/lib/containerbase/tool.prep.d',
'tmp/containerbase/tool.init.d',
'opt/containerbase/data',
'usr/local/containerbase/tools/v2',
]);

await fs.writeFile(
rootPath('usr/local/containerbase/tools/v2/dummy.sh'),
'',
);

const verSvc = await createContainer().getAsync(VersionService);

await verSvc.setCurrent({
Expand Down
40 changes: 25 additions & 15 deletions src/cli/install-tool/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Container, injectFromHierarchy, injectable } from 'inversify';
import { createContainer } from '../services';
import { PathService, createContainer } from '../services';
import { ResolverMap } from '../tools';
import { BazeliskInstallService } from '../tools/bazelisk';
import { BunInstallService } from '../tools/bun';
Expand Down Expand Up @@ -68,22 +68,25 @@ import { SkopeoInstallService } from '../tools/skopeo';
import { SopsInstallService } from '../tools/sops';
import { WallyInstallService } from '../tools/wally';
import { logger } from '../utils';
import { LegacyToolInstallService } from './install-legacy-tool.service';
import {
V1ToolInstallService,
V2ToolInstallService,
} from './install-legacy-tool.service';
import { INSTALL_TOOL_TOKEN, InstallToolService } from './install-tool.service';
import { TOOL_VERSION_RESOLVER } from './tool-version-resolver';
import { ToolVersionResolverService } from './tool-version-resolver.service';

export type InstallToolType = 'gem' | 'npm' | 'pip';

function prepareInstallContainer(): Container {
async function prepareInstallContainer(): Promise<Container> {
logger.trace('preparing install container');
const container = createContainer();

// core services
container.bind(InstallToolService).toSelf();
container.bind(LegacyToolInstallService).toSelf();
container.bind(V1ToolInstallService).toSelf();

// tool services
// modern tool services
container.bind(INSTALL_TOOL_TOKEN).to(ComposerInstallService);
container.bind(INSTALL_TOOL_TOKEN).to(BazeliskInstallService);
container.bind(INSTALL_TOOL_TOKEN).to(BunInstallService);
Expand Down Expand Up @@ -117,6 +120,17 @@ function prepareInstallContainer(): Container {
container.bind(INSTALL_TOOL_TOKEN).to(YarnInstallService);
container.bind(INSTALL_TOOL_TOKEN).to(YarnSlimInstallService);

// v2 tool services
const pathSvc = await container.getAsync(PathService);
for (const tool of await pathSvc.findLegacyTools()) {
@injectable()
@injectFromHierarchy()
class GenericInstallService extends V2ToolInstallService {
override readonly name: string = tool;
}
container.bind(INSTALL_TOOL_TOKEN).to(GenericInstallService);
}

logger.trace('preparing install container done');
return container;
}
Expand Down Expand Up @@ -152,7 +166,7 @@ export async function installTool(
dryRun = false,
type?: InstallToolType,
): Promise<number | void> {
const container = prepareInstallContainer();
const container = await prepareInstallContainer();
if (type) {
switch (type) {
case 'gem': {
Expand Down Expand Up @@ -227,11 +241,9 @@ export async function installTool(
}
}
}
return (await container.getAsync(InstallToolService)).install(
tool,
version,
dryRun,
);

const svc = await container.getAsync(InstallToolService);
return svc.install(tool, version, dryRun);
}

export async function resolveVersion(
Expand Down Expand Up @@ -272,8 +284,6 @@ export async function resolveVersion(
}
}
}
return (await container.getAsync(ToolVersionResolverService)).resolve(
tool,
version,
);
const svc = await container.getAsync(ToolVersionResolverService);
return svc.resolve(tool, version);
}
102 changes: 93 additions & 9 deletions src/cli/install-tool/install-legacy-tool.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is';
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import { EnvService } from '../services';
import spawn from 'nano-spawn';
import { V2ToolService } from '../services';
import { logger } from '../utils';
import { BaseInstallService } from './base-install.service';

const defaultPipRegistry = 'https://pypi.org/simple/';

@injectable()
export class LegacyToolInstallService {
@inject(EnvService)
private readonly envSvc!: EnvService;

export class V1ToolInstallService {
async execute(tool: string, version: string): Promise<void> {
logger.debug(`Installing legacy tool ${tool} v${version} ...`);

await spawn(
'bash',
['/usr/local/containerbase/bin/v1-install-tool.sh', tool, version],
{
stdio: ['inherit', 'inherit', 1],
},
);
}
}

@injectable()
export abstract class V2ToolInstallService extends BaseInstallService {
@inject(V2ToolService)
private readonly _svc!: V2ToolService;

override async install(version: string): Promise<void> {
logger.debug(`Installing v2 tool ${this.name} v${version} ...`);
const env: NodeJS.ProcessEnv = {};

// TODO: drop when python is converted
const pipIndex = this.envSvc.replaceUrl(
defaultPipRegistry,
isNonEmptyStringAndNotWhitespace(env.CONTAINERBASE_CDN_PIP),
Expand All @@ -23,13 +40,80 @@ export class LegacyToolInstallService {
env.PIP_INDEX_URL = pipIndex;
}

await execa(
'/usr/local/containerbase/bin/install-tool.sh',
[tool, version],
await spawn(
'bash',
[
'/usr/local/containerbase/bin/v2-install-tool.sh',
'install',
this.name,
version,
],
{
stdio: ['inherit', 'inherit', 1],
env,
},
);
}

override async link(version: string): Promise<void> {
logger.debug(`Linking v2 tool ${this.name} v${version} ...`);
await spawn(
'bash',
[
'/usr/local/containerbase/bin/v2-install-tool.sh',
'link',
this.name,
version,
],
{
stdio: ['inherit', 'inherit', 1],
},
);
}

override needsInitialize(): boolean {
return this._svc.needsInitialize(this.name);
}

override needsPrepare(): boolean {
return this._svc.needsPrepare(this.name);
}

override async test(version: string): Promise<void> {
logger.debug(`Testing v2 tool ${this.name} v${version} ...`);
await spawn(
'bash',
[
'/usr/local/containerbase/bin/v2-install-tool.sh',
'test',
this.name,
version,
],
{
stdio: ['inherit', 'inherit', 1],
},
);
}

override async validate(version: string): Promise<boolean> {
logger.debug(`Validating v2 tool ${this.name} v${version} ...`);
try {
await spawn(
'bash',
[
'/usr/local/containerbase/bin/v2-install-tool.sh',
'check',
this.name,
version,
],
{
stdio: ['inherit', 'inherit', 1],
},
);
return true;
} catch (err) {
logger.debug({ err }, 'validation error');
return false;
}
}
}
10 changes: 7 additions & 3 deletions src/cli/install-tool/install-tool.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import type { ToolState } from '../services/version.service';
import { cleanAptFiles, cleanTmpFiles, isDockerBuild, logger } from '../utils';
import { MissingParent } from '../utils/codes';
import type { BaseInstallService } from './base-install.service';
import { LegacyToolInstallService } from './install-legacy-tool.service';
import { V1ToolInstallService } from './install-legacy-tool.service';

export const INSTALL_TOOL_TOKEN = Symbol('INSTALL_TOOL_TOKEN');

@injectable()
export class InstallToolService {
@inject(LegacyToolInstallService)
private readonly legacySvc!: LegacyToolInstallService;
@inject(V1ToolInstallService)
private readonly legacySvc!: V1ToolInstallService;
@multiInject(INSTALL_TOOL_TOKEN)
@optional()
private readonly toolSvcs: BaseInstallService[] = [];
Expand Down Expand Up @@ -104,6 +104,10 @@ export class InstallToolService {
logger.info(`Dry run: install tool ${tool} v${version} ...`);
return;
}
if (!(await this.pathSvc.isLegacyTool(tool, true))) {
logger.error({ tool }, 'tool not found');
return 1;
}
await this.legacySvc.execute(tool, version);
if (!(await this.versionSvc.isInstalled({ name: tool, version }))) {
await this.versionSvc.addInstalled({ name: tool, version });
Expand Down
6 changes: 3 additions & 3 deletions src/cli/install-tool/install-tool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import type { Container } from 'inversify';
import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
import { VersionService, createContainer } from '../services';
import { BunInstallService } from '../tools/bun';
import { LegacyToolInstallService } from './install-legacy-tool.service';
import { V1ToolInstallService } from './install-legacy-tool.service';
import { INSTALL_TOOL_TOKEN, InstallToolService } from './install-tool.service';
import { ensurePaths } from '~test/path';

vi.mock('del');
vi.mock('execa');
vi.mock('nano-spawn');
vi.mock('../tools/bun');
vi.mock('../tools/php/composer');
vi.mock('../prepare-tool');

describe('cli/install-tool/install-tool', () => {
const parent = createContainer();
parent.bind(InstallToolService).toSelf();
parent.bind(LegacyToolInstallService).toSelf();
parent.bind(V1ToolInstallService).toSelf();
parent.bind(INSTALL_TOOL_TOKEN).to(BunInstallService);

let child: Container;
Expand Down
Loading