Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support url rewrite #1210

Merged
merged 12 commits into from
Jul 22, 2023
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ jobs:
with:
node-version: ${{ env.NODE_VERSION }}

- name: build
run: yarn build

- name: bats
run: yarn test:bats

Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@ You can replace the default urls used to download the tools.
The number of `URL_REPLACE_*_FROM` and `URL_REPLACE_*_TO` environment variables must match.
Theses variables are case sensitive.
The numbers will be processed in numerical order and can have gaps.
This is currently only supported by these tool installers:

- `docker`
- `dart`
Almost all tools are noew supported.
viceice marked this conversation as resolved.
Show resolved Hide resolved
Only tooles which are installed by `gem`, `npm` or `pip` are not supported.
viceice marked this conversation as resolved.
Show resolved Hide resolved
They can be configured via thier own environment variables.

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

Expand Down
54 changes: 54 additions & 0 deletions src/cli/command/download-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { createWriteStream } from 'node:fs';
import { mkdir } from 'node:fs/promises';
import { dirname } from 'node:path';
import { pipeline } from 'node:stream/promises';
import { Command, Option } from 'clipanion';
import { got } from 'got';
import prettyMilliseconds from 'pretty-ms';
import { EnvService, rootContainer } from '../services';
import { logger } from '../utils';

export class DownloadFileCommand extends Command {
static override paths = [['download', 'file'], ['df']];

static override usage = Command.Usage({
description: 'Downloads a file and optionally validates the checksum.',
});

url = Option.String({ required: true });
output = Option.String({ required: true });

// checksum = Option.String('-c,--checksum');
// algo = Option.String('-a,--algo');

// dryRun = Option.Boolean('-d,--dry-run', false);

async execute(): Promise<number | void> {
const start = Date.now();
let error = false;
logger.info({ url: this.url, output: this.output }, `Downloading file ...`);
try {
const container = rootContainer.createChild();

const env = container.get(EnvService);
const path = dirname(this.output);

await mkdir(path, { recursive: true });

const nUrl = env.replaceUrl(this.url);
await pipeline(got.stream(nUrl), createWriteStream(this.output));

return 0;
} catch (err) {
logger.fatal(err);
error = true;
return 1;
} finally {
logger.info(
`Download completed ${
error ? 'with errors ' : ''
} in ${prettyMilliseconds(Date.now() - start)}.`
);
}
}
}
2 changes: 2 additions & 0 deletions src/cli/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { argv0 } from 'node:process';
import type { Cli } from 'clipanion';
import type { CliMode } from '../utils';
import { logger } from '../utils/logger';
import { DownloadFileCommand } from './download-file';
import { InstallToolCommand, InstallToolShortCommand } from './install-tool';
import { PrepareToolCommand, PrepareToolShortCommand } from './prepare-tool';
import { prepareToolVersion } from './utils';
Expand All @@ -28,6 +29,7 @@ export function prepareCommands(

cli.register(InstallToolCommand);
cli.register(PrepareToolCommand);
cli.register(DownloadFileCommand);
return prepareToolVersion(mode, args);
}

Expand Down
10 changes: 8 additions & 2 deletions src/cli/command/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export function prepareToolVersion(
args: string[]
): string[] {
switch (mode) {
case 'prepare-tool':
break;
case 'install-tool': {
if (args.length === 1) {
// install-tool node
Expand All @@ -14,10 +16,14 @@ export function prepareToolVersion(
}
case 'containerbase-cli':
default: {
if (args.length === 2) {
if (args.length === 2 && args[0] === 'it') {
// containerbase-cli it node
appendVersion(args, 1);
} else if (args.length === 3) {
} else if (
args.length === 3 &&
args[0] === 'install' &&
args[1] === 'tool'
) {
// containerbase-cli install tool node
appendVersion(args, 2);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/services/env.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class EnvService {

return (this.replacements = replacements);
}

viceice marked this conversation as resolved.
Show resolved Hide resolved
public isToolIgnored(tool: string): boolean {
if (!this.ignoredTools) {
this.ignoredTools = new Set(
Expand Down
3 changes: 3 additions & 0 deletions src/usr/local/containerbase/util.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# https://github.com/vercel/pkg/issues/1861
unset PKG_EXECPATH

# get path location
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
Expand Down
4 changes: 2 additions & 2 deletions src/usr/local/containerbase/utils/cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function download_file () {
local temp_folder=${CONTAINERBASE_CACHE_DIR:-${TEMP_DIR}}
while [ "${retry}" -gt 0 ]; do
retry=$((retry-1))
if ! curl --retry 3 --create-dirs -sSfLo "${temp_folder}/${name}" "${url}" ; then
if ! containerbase-cli df "${url}" "${temp_folder}/${name}" >&2; then
echo "Download failed: ${url}" >&2
exit 1
fi;
Expand Down Expand Up @@ -119,7 +119,7 @@ function download_file () {
# First argument is the path to the file
# Second argument is the checksum
# Third argument is the type, currently supported:
# * sha1
# * sha1sum
# * sha224sum
# * sha256sum
# * sha384sum
Expand Down
17 changes: 9 additions & 8 deletions test/bash/cache.bats
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ teardown() {

run download_file "${file}"
assert_success
assert_output "${CONTAINERBASE_CACHE_DIR}/containerbase.tar.xz"
assert_line "${CONTAINERBASE_CACHE_DIR}/containerbase.tar.xz"

run download_file "${file}" "foobar"
assert_success
assert_output "${CONTAINERBASE_CACHE_DIR}/foobar"
assert_line "${CONTAINERBASE_CACHE_DIR}/foobar"

CONTAINERBASE_CACHE_DIR= \
tmp_file=$(download_file "${file}")
Expand All @@ -178,11 +178,11 @@ teardown() {

run get_from_url "${file}"
assert_success
assert_output --regexp "^${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/containerbase\.tar\.xz"
assert_line --regexp "^${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/containerbase\.tar\.xz"

run get_from_url "${file}" test
assert_success
assert_output --regexp "${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/test"
assert_line --regexp "${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/test"

# overwrite donwload function to fail
function download_file () {
Expand All @@ -209,13 +209,13 @@ teardown() {

run get_from_url "${file}" $(basename "${file}") "${checksum}" "sha512sum"
assert_success
assert_output --regexp "^${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/containerbase\.tar\.xz"
assert_line --regexp "^${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/containerbase\.tar\.xz"

rm -rf "${CONTAINERBASE_CACHE_DIR}"

run get_from_url "${file}" test "${checksum}" "sha512sum"
assert_success
assert_output --regexp "${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/test"
assert_line --regexp "${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/test"

rm -rf "${CONTAINERBASE_CACHE_DIR}"

Expand All @@ -236,6 +236,7 @@ teardown() {
}

@test "get_from_url_with_cache_and_checksum" {
bats_require_minimum_version 1.5.0
# create cache dir
CONTAINERBASE_CACHE_DIR="${TEST_ROOT_DIR}/cache"
mkdir -p "${CONTAINERBASE_CACHE_DIR}"
Expand All @@ -244,15 +245,15 @@ teardown() {
local checksum="233c335a7f10e9f0dfd7e9d0cda802a38c15a7f13b6678c55980814f22799a70590d56888a819b6591881ec1939240d9dbe68e7e495021b4d6c6a49cdee24d80"
local file="https://github.com/containerbase/base/releases/download/7.10.0/containerbase.tar.xz"

run get_from_url "${file}" $(basename "${file}") "${checksum}" "sha512sum"
run --separate-stderr get_from_url "${file}" $(basename "${file}") "${checksum}" "sha512sum"
assert_success
assert_output --regexp "^${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/containerbase\.tar\.xz"

file_path="${output}"

run get_from_url "${file}" test "${checksum}" "sha512sum"
assert_success
assert_output --regexp "${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/test"
assert_line --regexp "${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/test"

# change checksum of cached file
echo "a" >> "${file_path}"
Expand Down
12 changes: 12 additions & 0 deletions test/bash/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ function is_root () {
echo 0
fi
}

function link_cli_tool () {
local arch=x64

if [[ "${ARCHITECTURE}" = "aarch64" ]];then
arch=arm64
fi
mkdir -p "${BIN_DIR}"
export PATH="${BIN_DIR}:${PATH}"
ln -sf "${CONTAINERBASE_DIR}/bin/containerbase-cli-${arch}" "${BIN_DIR}/containerbase-cli"
}
link_cli_tool