Skip to content

Commit 70390c8

Browse files
committed
docker: tearDown method
Signed-off-by: CrazyMax <[email protected]>
1 parent ac9d9d9 commit 70390c8

File tree

4 files changed

+81
-9
lines changed

4 files changed

+81
-9
lines changed

__tests__/docker/install.test.e2e.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe('install', () => {
3333
await install.install(toolPath, tmpDir, version);
3434
await Docker.printVersion();
3535
await Docker.printInfo();
36+
await install.tearDown(tmpDir);
3637
})()).resolves.not.toThrow();
3738
});
3839
});

src/docker/assets.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export const setupDockerWinPs1 = (): string => {
2525
return get('docker-setup-win.ps1', setupDockerWinPs1Data);
2626
};
2727

28+
export const dockerServiceLogsPs1 = (): string => {
29+
return get('docker-service-logs.ps1', dockerServiceLogsPs1Data);
30+
};
31+
2832
export const colimaYaml = (): string => {
2933
return get('colima.yaml', colimaYamlData);
3034
};
@@ -146,7 +150,9 @@ While ($true) {
146150
Start-Sleep -Seconds 1
147151
}
148152
Write-Host "Docker daemon started successfully!"
153+
`;
149154

155+
export const dockerServiceLogsPs1Data = `
150156
Get-WinEvent -ea SilentlyContinue \`
151157
-FilterHashtable @{ProviderName= "docker"; LogName = "application"} |
152158
Sort-Object @{Expression="TimeCreated";Descending=$false} |

src/docker/install.ts

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import * as tc from '@actions/tool-cache';
2727

2828
import {Exec} from '../exec';
2929
import {Util} from '../util';
30-
import {colimaYamlData, setupDockerLinuxSh, setupDockerWinPs1} from './assets';
30+
import {colimaYamlData, dockerServiceLogsPs1, setupDockerLinuxSh, setupDockerWinPs1} from './assets';
3131

3232
export class Install {
3333
public async download(version: string, channel?: string): Promise<string> {
@@ -134,6 +134,7 @@ export class Install {
134134

135135
private async installLinux(toolDir: string, runDir: string): Promise<void> {
136136
const dockerHost = `unix://${path.join(runDir, 'docker.sock')}`;
137+
await io.mkdirP(runDir);
137138

138139
await core.group('Start Docker daemon', async () => {
139140
const bashPath: string = await io.which('bash', true);
@@ -189,13 +190,15 @@ export class Install {
189190
private async installWindows(toolDir: string, runDir: string): Promise<void> {
190191
const dockerHost = 'npipe:////./pipe/setup_docker_action';
191192

192-
const setupCmd = await Util.powershellCommand(setupDockerWinPs1(), {
193-
ToolDir: toolDir,
194-
RunDir: runDir,
195-
DockerHost: dockerHost
196-
});
197193
await core.group('Install Docker daemon service', async () => {
194+
const setupCmd = await Util.powershellCommand(setupDockerWinPs1(), {
195+
ToolDir: toolDir,
196+
RunDir: runDir,
197+
DockerHost: dockerHost
198+
});
198199
await Exec.exec(setupCmd.command, setupCmd.args);
200+
const logCmd = await Util.powershellCommand(dockerServiceLogsPs1());
201+
await Exec.exec(logCmd.command, logCmd.args);
199202
});
200203

201204
await core.group('Create Docker context', async () => {
@@ -204,6 +207,66 @@ export class Install {
204207
});
205208
}
206209

210+
public async tearDown(runDir: string): Promise<void> {
211+
switch (os.platform()) {
212+
case 'darwin': {
213+
await this.tearDownDarwin(runDir);
214+
break;
215+
}
216+
case 'linux': {
217+
await this.tearDownLinux(runDir);
218+
break;
219+
}
220+
case 'win32': {
221+
await this.tearDownWindows();
222+
break;
223+
}
224+
default: {
225+
throw new Error(`Unsupported platform: ${os.platform()}`);
226+
}
227+
}
228+
}
229+
230+
private async tearDownDarwin(runDir: string): Promise<void> {
231+
await core.group('Docker daemon logs', async () => {
232+
await Exec.exec('colima', ['exec', '--', 'cat', '/var/log/docker.log']);
233+
});
234+
await core.group('Stopping colima', async () => {
235+
await Exec.exec('colima', ['stop', '--very-verbose']);
236+
});
237+
await core.group('Removing Docker context', async () => {
238+
await Exec.exec('docker', ['context', 'rm', '-f', 'setup-docker-action']);
239+
});
240+
await core.group(`Cleaning up runDir`, async () => {
241+
await Exec.exec('sudo', ['rm', '-rf', runDir]);
242+
});
243+
}
244+
245+
private async tearDownLinux(runDir: string): Promise<void> {
246+
await core.group('Docker daemon logs', async () => {
247+
core.info(fs.readFileSync(path.join(runDir, 'dockerd.log'), {encoding: 'utf8'}));
248+
});
249+
await core.group('Stopping Docker daemon', async () => {
250+
await Exec.exec('sudo', ['kill', fs.readFileSync(path.join(runDir, 'docker.pid')).toString().trim()]);
251+
});
252+
await core.group('Removing Docker context', async () => {
253+
await Exec.exec('docker', ['context', 'rm', '-f', 'setup-docker-action']);
254+
});
255+
await core.group(`Cleaning up runDir`, async () => {
256+
await Exec.exec('sudo', ['rm', '-rf', runDir]);
257+
});
258+
}
259+
260+
private async tearDownWindows(): Promise<void> {
261+
await core.group('Docker daemon logs', async () => {
262+
const logCmd = await Util.powershellCommand(dockerServiceLogsPs1());
263+
await Exec.exec(logCmd.command, logCmd.args);
264+
});
265+
await core.group('Removing Docker context', async () => {
266+
await Exec.exec('docker', ['context', 'rm', '-f', 'setup-docker-action']);
267+
});
268+
}
269+
207270
private downloadURL(version: string, channel: string): string {
208271
const platformOS = Install.platformOS();
209272
const platformArch = Install.platformArch();

src/util.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ export class Util {
7474
return true;
7575
}
7676

77-
public static async powershellCommand(script: string, params: Record<string, string>) {
77+
public static async powershellCommand(script: string, params?: Record<string, string>) {
7878
const powershellPath: string = await io.which('powershell', true);
7979
const escapedScript = script.replace(/'/g, "''").replace(/"|\n|\r/g, '');
8080
const escapedParams: string[] = [];
81-
for (const key in params) {
82-
escapedParams.push(`-${key} '${params[key].replace(/'/g, "''").replace(/"|\n|\r/g, '')}'`);
81+
if (params) {
82+
for (const key in params) {
83+
escapedParams.push(`-${key} '${params[key].replace(/'/g, "''").replace(/"|\n|\r/g, '')}'`);
84+
}
8385
}
8486
return {
8587
command: `"${powershellPath}"`,

0 commit comments

Comments
 (0)