Skip to content

Commit

Permalink
fix: init prepared tools from entrypoint (#3568)
Browse files Browse the repository at this point in the history
* fix: init prepared tools from entrypoint

* test: disable test

* test: fix tests
  • Loading branch information
viceice authored Nov 7, 2024
1 parent f650629 commit 08963ab
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/cli/command/init-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class InitToolCommand extends Command {
'Initialize a tool into the container. This creates missing files and directories.',
examples: [
['Initialize node', '$0 init tool node'],
['Initialize all tools', '$0 init tool all'],
['Initialize all prepared tools', '$0 init tool all'],
],
});

Expand Down
6 changes: 6 additions & 0 deletions src/cli/prepare-tool/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs/promises';
import { beforeAll, describe, expect, test, vi } from 'vitest';
import { rootPath } from '../../../test/path';
import { PathService, rootContainer } from '../services';
import { initializeTools, prepareTools } from '.';

vi.mock('del');
Expand Down Expand Up @@ -30,6 +31,10 @@ describe('index', () => {
rootPath('usr/local/containerbase/tools/v2/dummy.sh'),
'',
);

const child = rootContainer.createChild();
const pathSvc = child.get(PathService);
await pathSvc.setPrepared('bun');
});

test('prepareTools', async () => {
Expand All @@ -40,5 +45,6 @@ describe('index', () => {
test('initializeTools', async () => {
expect(await initializeTools(['bun', 'dummy'])).toBeUndefined();
expect(await initializeTools(['not-exist'])).toBeUndefined();
expect(await initializeTools(['all'])).toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion src/cli/prepare-tool/prepare-tool.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class PrepareToolService {
await this.pathSvc.ensureBasePaths();

if (tools.length === 1 && tools[0] === 'all') {
for (const tool of supportedTools) {
for (const tool of await this.pathSvc.findPreparedTools()) {
const res = await this._initTool(tool, dryRun);
if (res) {
return res;
Expand Down
14 changes: 14 additions & 0 deletions src/cli/services/path.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { env } from 'node:process';
import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is';
import { inject, injectable } from 'inversify';
import { fileRights, logger, pathExists, tool2path } from '../utils';
import { EnvService } from './env.service';
Expand Down Expand Up @@ -168,6 +169,18 @@ export class PathService {
.map((t) => t.substring(0, t.length - 3));
}

async findPreparedTools(): Promise<string[]> {
const file = join(this.varPath, 'tool.prep');

if (!(await this.fileExists(file))) {
return [];
}

return (await fs.readFile(file, 'utf-8'))
.split('\n')
.filter(isNonEmptyStringAndNotWhitespace);
}

async fileExists(filePath: string): Promise<boolean> {
return await pathExists(filePath, 'file');
}
Expand All @@ -194,6 +207,7 @@ export class PathService {

async setPrepared(tool: string): Promise<void> {
await fs.writeFile(this.toolPreparePath(tool), '');
await fs.appendFile(join(this.varPath, 'tool.prep'), `${tool}\n`);
}

toolInitPath(tool: string): string {
Expand Down
5 changes: 5 additions & 0 deletions src/usr/local/containerbase/bin/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ if [[ -f "/usr/local/etc/env" && -z "${CONTAINERBASE_ENV+x}" ]]; then
. /usr/local/etc/env
fi

if [[ ! -d "/tmp/containerbase" ]]; then
# initialize all prepared tools
containerbase-cli init tool all
fi

exec dumb-init -- "$@"
46 changes: 23 additions & 23 deletions test/node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,6 @@ RUN node --version
RUN npm --version


## Test for #136
USER 0

ARG RE2_DOWNLOAD_MIRROR=https://github.com/containerbase/node-re2-prebuild/releases/download RE2_DOWNLOAD_SKIP_PATH=1
# renovate: datasource=npm
RUN npm install -g re2@1.20.9

#--------------------------------------
# test: yarn
#--------------------------------------
Expand Down Expand Up @@ -485,10 +478,17 @@ RUN set -ex; \
ls -la /opt/containerbase/; \
rm -rf /tmp/*; \
ls -la /tmp/; \
ls -la /var/lib/containerbase/; \
cat /var/lib/containerbase/tool.prep; \
true

USER 1000

RUN [[ ! -d /tmp/containerbase ]] || (echo "/tmp/containerbase should not exist"; exit 1 )
RUN containerbase-cli init tool all
RUN [[ -d /tmp/containerbase ]] || (echo "/tmp/containerbase should exist"; exit 1)
RUN rm -rf /tmp/*;

# install latest version
RUN install-tool node
RUN install-tool yarn
Expand Down Expand Up @@ -517,20 +517,20 @@ RUN set -ex; \
#--------------------------------------
FROM base

# COPY --from=testa /.dummy /.dummy
# COPY --from=testb /.dummy /.dummy
# COPY --from=testc /.dummy /.dummy
# COPY --from=testd /.dummy /.dummy
# COPY --from=teste /.dummy /.dummy
# COPY --from=testf /.dummy /.dummy
# COPY --from=testg /.dummy /.dummy
# COPY --from=testh /.dummy /.dummy
# COPY --from=testi /.dummy /.dummy
# COPY --from=testj /.dummy /.dummy
# COPY --from=testk /.dummy /.dummy
# COPY --from=testl /.dummy /.dummy
# COPY --from=testm /.dummy /.dummy
# COPY --from=testn /.dummy /.dummy
# COPY --from=testo /.dummy /.dummy
# COPY --from=testp /.dummy /.dummy
COPY --from=testa /.dummy /.dummy
COPY --from=testb /.dummy /.dummy
COPY --from=testc /.dummy /.dummy
COPY --from=testd /.dummy /.dummy
COPY --from=teste /.dummy /.dummy
COPY --from=testf /.dummy /.dummy
COPY --from=testg /.dummy /.dummy
COPY --from=testh /.dummy /.dummy
COPY --from=testi /.dummy /.dummy
COPY --from=testj /.dummy /.dummy
COPY --from=testk /.dummy /.dummy
COPY --from=testl /.dummy /.dummy
COPY --from=testm /.dummy /.dummy
COPY --from=testn /.dummy /.dummy
COPY --from=testo /.dummy /.dummy
COPY --from=testp /.dummy /.dummy
COPY --from=testq /.dummy /.dummy
2 changes: 1 addition & 1 deletion test/node/test/a/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"re2": "1.20.9",
"re2": "1.21.4",
"semver": "7.3.2"
}
}

0 comments on commit 08963ab

Please sign in to comment.