From 3ab4ae20f98101f5e2b88d84db6b28a1d563c69e Mon Sep 17 00:00:00 2001 From: Nikola Jokic Date: Fri, 3 Jun 2022 15:15:19 +0200 Subject: [PATCH 1/2] added network prune --- .../docker/src/dockerCommands/container.ts | 11 ++++++++++ packages/docker/src/hooks/cleanup-job.ts | 22 +++++-------------- packages/docker/src/index.ts | 2 +- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/packages/docker/src/dockerCommands/container.ts b/packages/docker/src/dockerCommands/container.ts index 9d46166..d1ad5c3 100644 --- a/packages/docker/src/dockerCommands/container.ts +++ b/packages/docker/src/dockerCommands/container.ts @@ -171,6 +171,17 @@ export async function containerNetworkRemove(network: string): Promise { await runDockerCommand(dockerArgs) } +export async function containerNetworkPrune(): Promise { + const dockerArgs = [ + 'network', + 'prune', + '--filter', + `label=${getRunnerLabel()}` + ] + + await runDockerCommand(dockerArgs) +} + export async function containerPrune(): Promise { const dockerPSArgs: string[] = [ 'ps', diff --git a/packages/docker/src/hooks/cleanup-job.ts b/packages/docker/src/hooks/cleanup-job.ts index f855db3..acb7d40 100644 --- a/packages/docker/src/hooks/cleanup-job.ts +++ b/packages/docker/src/hooks/cleanup-job.ts @@ -1,21 +1,9 @@ import { - containerRemove, - containerNetworkRemove + containerNetworkPrune, + containerPrune } from '../dockerCommands/container' -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export async function cleanupJob(args, state, responseFile): Promise { - const containerIds: string[] = [] - if (state?.container) { - containerIds.push(state.container) - } - if (state?.services) { - containerIds.push(state.services) - } - if (containerIds.length > 0) { - await containerRemove(containerIds) - } - if (state.network) { - await containerNetworkRemove(state.network) - } +export async function cleanupJob(): Promise { + await containerPrune() + await containerNetworkPrune() } diff --git a/packages/docker/src/index.ts b/packages/docker/src/index.ts index c620392..6a373b7 100644 --- a/packages/docker/src/index.ts +++ b/packages/docker/src/index.ts @@ -28,7 +28,7 @@ async function run(): Promise { await prepareJob(args as PrepareJobArgs, responseFile) return exit(0) case Command.CleanupJob: - await cleanupJob(null, state, null) + await cleanupJob() return exit(0) case Command.RunScriptStep: await runScriptStep(args as RunScriptStepArgs, state) From c65ec28bbb162abe45cea98f67f17164e24ef871 Mon Sep 17 00:00:00 2001 From: Nikola Jokic Date: Fri, 3 Jun 2022 16:01:24 +0200 Subject: [PATCH 2/2] added force cleanup for network and cleanUp hook is cleaning up based on the label --- packages/docker/src/dockerCommands/container.ts | 1 + packages/docker/tests/cleanup-job-test.ts | 13 +++---------- packages/docker/tests/container-pull-test.ts | 2 +- packages/docker/tests/e2e-test.ts | 16 ++++++++-------- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/packages/docker/src/dockerCommands/container.ts b/packages/docker/src/dockerCommands/container.ts index d1ad5c3..e47fc84 100644 --- a/packages/docker/src/dockerCommands/container.ts +++ b/packages/docker/src/dockerCommands/container.ts @@ -175,6 +175,7 @@ export async function containerNetworkPrune(): Promise { const dockerArgs = [ 'network', 'prune', + '--force', '--filter', `label=${getRunnerLabel()}` ] diff --git a/packages/docker/tests/cleanup-job-test.ts b/packages/docker/tests/cleanup-job-test.ts index efee5b0..a9cf3fe 100644 --- a/packages/docker/tests/cleanup-job-test.ts +++ b/packages/docker/tests/cleanup-job-test.ts @@ -1,7 +1,7 @@ -import { prepareJob, cleanupJob } from '../src/hooks' -import { v4 as uuidv4 } from 'uuid' import * as fs from 'fs' import * as path from 'path' +import { v4 as uuidv4 } from 'uuid' +import { cleanupJob, prepareJob } from '../src/hooks' import TestSetup from './test-setup' const prepareJobInputPath = path.resolve( @@ -50,13 +50,6 @@ describe('cleanup job', () => { }) it('should cleanup successfully', async () => { - const prepareJobOutputContent = fs.readFileSync( - prepareJobOutputPath, - 'utf-8' - ) - const parsedPrepareJobOutput = JSON.parse(prepareJobOutputContent) - await expect( - cleanupJob(prepareJobData.args, parsedPrepareJobOutput.state, null) - ).resolves.not.toThrow() + await expect(cleanupJob()).resolves.not.toThrow() }) }) diff --git a/packages/docker/tests/container-pull-test.ts b/packages/docker/tests/container-pull-test.ts index 77bb5cb..bab1cae 100644 --- a/packages/docker/tests/container-pull-test.ts +++ b/packages/docker/tests/container-pull-test.ts @@ -4,7 +4,7 @@ jest.useRealTimers() describe('container pull', () => { it('should fail', async () => { - const arg = { image: 'doesNotExist' } + const arg = { image: 'does-not-exist' } await expect(containerPull(arg.image, '')).rejects.toThrow() }) it('should succeed', async () => { diff --git a/packages/docker/tests/e2e-test.ts b/packages/docker/tests/e2e-test.ts index cdc889b..e1e02ab 100644 --- a/packages/docker/tests/e2e-test.ts +++ b/packages/docker/tests/e2e-test.ts @@ -1,12 +1,12 @@ -import { - prepareJob, - cleanupJob, - runScriptStep, - runContainerStep -} from '../src/hooks' import * as fs from 'fs' import * as path from 'path' import { v4 as uuidv4 } from 'uuid' +import { + cleanupJob, + prepareJob, + runContainerStep, + runScriptStep +} from '../src/hooks' import TestSetup from './test-setup' const prepareJobJson = fs.readFileSync( @@ -83,7 +83,7 @@ describe('e2e', () => { await expect( runContainerStep(containerStepData.args, resp.state) ).resolves.not.toThrow() - await expect(cleanupJob(resp, resp.state, null)).resolves.not.toThrow() + await expect(cleanupJob()).resolves.not.toThrow() }) it('should prepare job, then run script step, then run container step with Dockerfile then cleanup', async () => { @@ -112,6 +112,6 @@ ENTRYPOINT [ "tail", "-f", "/dev/null" ] await expect( runContainerStep(containerStepDataCopy.args, resp.state) ).resolves.not.toThrow() - await expect(cleanupJob(resp, resp.state, null)).resolves.not.toThrow() + await expect(cleanupJob()).resolves.not.toThrow() }) })