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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ RUN install-tool docker 20.10.7
### Url replacement

You can replace the default urls used to download the tools.
This is currently only supported by the `docker` tool installer.
Checkout #1067 for additional support.
This is currently only supported by these tool installers:

- `docker`
- `dart`

Checkout [#1067](https://github.com/containerbase/base/issues/1067) for additional support.

```Dockerfile
FROM containerbase/base
Expand Down
2 changes: 2 additions & 0 deletions src/cli/install-tool/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Container } from 'inversify';
import { rootContainer } from '../services';
import { InstallDartService } from '../tools/dart';
import { InstallDockerService } from '../tools/docker';
import { logger } from '../utils';
import { InstallLegacyToolService } from './install-legacy-tool.service';
Expand All @@ -16,6 +17,7 @@ function prepareContainer(): Container {

// tool services
container.bind(INSTALL_TOOL_TOKEN).to(InstallDockerService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallDartService);

logger.trace('preparing container done');
return container;
Expand Down
2 changes: 2 additions & 0 deletions src/cli/prepare-tool/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Container } from 'inversify';
import { rootContainer } from '../services';
import { PrepareDartService } from '../tools/dart';
import { PrepareDockerService } from '../tools/docker';
import { logger } from '../utils';
import { PrepareLegacyToolsService } from './prepare-legacy-tools.service';
Expand All @@ -16,6 +17,7 @@ function prepareContainer(): Container {

// tool services
container.bind(PREPARE_TOOL_TOKEN).to(PrepareDockerService);
container.bind(PREPARE_TOOL_TOKEN).to(PrepareDartService);

logger.trace('preparing container done');
return container;
Expand Down
5 changes: 5 additions & 0 deletions src/cli/services/env.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export class EnvService {
return env.CONTAINERBASE_CACHE_DIR ?? null;
}

get home(): string {
// TODO: validate
return env.HOME!;
}

get isRoot(): boolean {
return this.uid === 0;
}
Expand Down
109 changes: 109 additions & 0 deletions src/cli/tools/dart/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import fs from 'node:fs/promises';
import { join } from 'node:path';
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import semver from 'semver';
import { InstallToolBaseService } from '../../install-tool/install-tool-base.service';
import { PrepareToolBaseService } from '../../prepare-tool/prepare-tool-base.service';
import { EnvService, HttpService, PathService } from '../../services';
import { logger } from '../../utils';

// Dart SDK sample urls
// https://storage.googleapis.com/dart-archive/channels/stable/release/1.11.0/sdk/dartsdk-linux-x64-release.zip
// https://storage.googleapis.com/dart-archive/channels/stable/release/2.18.0/sdk/dartsdk-linux-x64-release.zip
// https://storage.googleapis.com/dart-archive/channels/stable/release/2.19.4/sdk/dartsdk-linux-x64-release.zip.sha256sum
// https://storage.googleapis.com/dart-archive/channels/stable/release/2.19.4/sdk/dartsdk-linux-arm64-release.zip
// https://storage.googleapis.com/dart-archive/channels/stable/release/2.19.4/sdk/dartsdk-linux-arm64-release.zip.sha256sum

@injectable()
export class PrepareDartService extends PrepareToolBaseService {
readonly name = 'dart';

constructor(@inject(EnvService) private envSvc: EnvService) {
super();
}

async execute(): Promise<void> {
await fs.mkdir(`${this.envSvc.home}/.dart`);
await fs.writeFile(
`${this.envSvc.home}/.dart/dartdev.json`,
'{ "firstRun": false, "enabled": false }'
);
await fs.mkdir(`${this.envSvc.userHome}/.dart`);
await fs.writeFile(
`${this.envSvc.userHome}/.dart/dartdev.json`,
'{ "firstRun": false, "enabled": false }'
);

// fs isn't recursive, so we use system binaries
await execa('chown', [
'-R',
this.envSvc.userName,
`${this.envSvc.userHome}/.dart`,
]);
await execa('chmod', ['-R', 'g=u', `${this.envSvc.userHome}/.dart`]);
}
}

@injectable()
export class InstallDartService extends InstallToolBaseService {
readonly name = 'dart';

private get arch(): string {
switch (this.envSvc.arch) {
case 'arm64':
return 'arm64';
case 'amd64':
return 'x64';
}
}

constructor(
@inject(EnvService) envSvc: EnvService,
@inject(PathService) pathSvc: PathService,
@inject(HttpService) private http: HttpService
) {
super(pathSvc, envSvc);
}

override async install(version: string): Promise<void> {
const ver = semver.parse(version);
if (!ver) {
throw new Error(`Invalid version: ${version}`);
}
if (ver.major < 2) {
throw new Error(`Dart SDK version < v2 is not supported: ${version}`);
}
const channel = 'stable';
const sdkUrl = `https://storage.googleapis.com/dart-archive/channels/${channel}/release/${version}/sdk`;
const sdkFile = `dartsdk-linux-${this.arch}-release.zip`;
const url = `${sdkUrl}/${sdkFile}`;

logger.debug({ url: `${url}.sha256sum` }, `download ${this.name} checksum`);
const checksumFile = await this.http.download({ url: `${url}.sha256sum` });
const expectedChecksum = (await fs.readFile(checksumFile, 'utf-8'))
.split('\n')
.find((l) => l.includes(sdkFile))
?.split(' ')[0];

logger.debug({ url }, `download ${this.name}`);
const file = await this.http.download({
url,
expectedChecksum,
checksumType: 'sha256',
});

const path = await this.pathSvc.createVersionedToolPath(this.name, version);
await execa('bsdtar', ['-xf', file, '-C', path, '--strip', '1']);
}

override async link(version: string): Promise<void> {
const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin');

await this.shellwrapper({ name: 'dart', srcDir: src });
}

override async test(_version: string): Promise<void> {
await execa('dart', ['--version'], { stdio: 'inherit' });
}
}
60 changes: 0 additions & 60 deletions src/usr/local/containerbase/tools/v2/dart.sh

This file was deleted.