docker: tearDown method

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-03-02 15:25:29 +01:00
parent ac9d9d9a1b
commit 70390c899b
4 changed files with 81 additions and 9 deletions

View File

@@ -33,6 +33,7 @@ describe('install', () => {
await install.install(toolPath, tmpDir, version);
await Docker.printVersion();
await Docker.printInfo();
await install.tearDown(tmpDir);
})()).resolves.not.toThrow();
});
});

View File

@@ -25,6 +25,10 @@ export const setupDockerWinPs1 = (): string => {
return get('docker-setup-win.ps1', setupDockerWinPs1Data);
};
export const dockerServiceLogsPs1 = (): string => {
return get('docker-service-logs.ps1', dockerServiceLogsPs1Data);
};
export const colimaYaml = (): string => {
return get('colima.yaml', colimaYamlData);
};
@@ -146,7 +150,9 @@ While ($true) {
Start-Sleep -Seconds 1
}
Write-Host "Docker daemon started successfully!"
`;
export const dockerServiceLogsPs1Data = `
Get-WinEvent -ea SilentlyContinue \`
-FilterHashtable @{ProviderName= "docker"; LogName = "application"} |
Sort-Object @{Expression="TimeCreated";Descending=$false} |

View File

@@ -27,7 +27,7 @@ import * as tc from '@actions/tool-cache';
import {Exec} from '../exec';
import {Util} from '../util';
import {colimaYamlData, setupDockerLinuxSh, setupDockerWinPs1} from './assets';
import {colimaYamlData, dockerServiceLogsPs1, setupDockerLinuxSh, setupDockerWinPs1} from './assets';
export class Install {
public async download(version: string, channel?: string): Promise<string> {
@@ -134,6 +134,7 @@ export class Install {
private async installLinux(toolDir: string, runDir: string): Promise<void> {
const dockerHost = `unix://${path.join(runDir, 'docker.sock')}`;
await io.mkdirP(runDir);
await core.group('Start Docker daemon', async () => {
const bashPath: string = await io.which('bash', true);
@@ -189,13 +190,15 @@ export class Install {
private async installWindows(toolDir: string, runDir: string): Promise<void> {
const dockerHost = 'npipe:////./pipe/setup_docker_action';
const setupCmd = await Util.powershellCommand(setupDockerWinPs1(), {
ToolDir: toolDir,
RunDir: runDir,
DockerHost: dockerHost
});
await core.group('Install Docker daemon service', async () => {
const setupCmd = await Util.powershellCommand(setupDockerWinPs1(), {
ToolDir: toolDir,
RunDir: runDir,
DockerHost: dockerHost
});
await Exec.exec(setupCmd.command, setupCmd.args);
const logCmd = await Util.powershellCommand(dockerServiceLogsPs1());
await Exec.exec(logCmd.command, logCmd.args);
});
await core.group('Create Docker context', async () => {
@@ -204,6 +207,66 @@ export class Install {
});
}
public async tearDown(runDir: string): Promise<void> {
switch (os.platform()) {
case 'darwin': {
await this.tearDownDarwin(runDir);
break;
}
case 'linux': {
await this.tearDownLinux(runDir);
break;
}
case 'win32': {
await this.tearDownWindows();
break;
}
default: {
throw new Error(`Unsupported platform: ${os.platform()}`);
}
}
}
private async tearDownDarwin(runDir: string): Promise<void> {
await core.group('Docker daemon logs', async () => {
await Exec.exec('colima', ['exec', '--', 'cat', '/var/log/docker.log']);
});
await core.group('Stopping colima', async () => {
await Exec.exec('colima', ['stop', '--very-verbose']);
});
await core.group('Removing Docker context', async () => {
await Exec.exec('docker', ['context', 'rm', '-f', 'setup-docker-action']);
});
await core.group(`Cleaning up runDir`, async () => {
await Exec.exec('sudo', ['rm', '-rf', runDir]);
});
}
private async tearDownLinux(runDir: string): Promise<void> {
await core.group('Docker daemon logs', async () => {
core.info(fs.readFileSync(path.join(runDir, 'dockerd.log'), {encoding: 'utf8'}));
});
await core.group('Stopping Docker daemon', async () => {
await Exec.exec('sudo', ['kill', fs.readFileSync(path.join(runDir, 'docker.pid')).toString().trim()]);
});
await core.group('Removing Docker context', async () => {
await Exec.exec('docker', ['context', 'rm', '-f', 'setup-docker-action']);
});
await core.group(`Cleaning up runDir`, async () => {
await Exec.exec('sudo', ['rm', '-rf', runDir]);
});
}
private async tearDownWindows(): Promise<void> {
await core.group('Docker daemon logs', async () => {
const logCmd = await Util.powershellCommand(dockerServiceLogsPs1());
await Exec.exec(logCmd.command, logCmd.args);
});
await core.group('Removing Docker context', async () => {
await Exec.exec('docker', ['context', 'rm', '-f', 'setup-docker-action']);
});
}
private downloadURL(version: string, channel: string): string {
const platformOS = Install.platformOS();
const platformArch = Install.platformArch();

View File

@@ -74,12 +74,14 @@ export class Util {
return true;
}
public static async powershellCommand(script: string, params: Record<string, string>) {
public static async powershellCommand(script: string, params?: Record<string, string>) {
const powershellPath: string = await io.which('powershell', true);
const escapedScript = script.replace(/'/g, "''").replace(/"|\n|\r/g, '');
const escapedParams: string[] = [];
for (const key in params) {
escapedParams.push(`-${key} '${params[key].replace(/'/g, "''").replace(/"|\n|\r/g, '')}'`);
if (params) {
for (const key in params) {
escapedParams.push(`-${key} '${params[key].replace(/'/g, "''").replace(/"|\n|\r/g, '')}'`);
}
}
return {
command: `"${powershellPath}"`,