From 3b532d1b9160a990cc4aa3298e10818aefcf3e2e Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 28 Feb 2023 20:15:33 +0100 Subject: [PATCH] docker: install with custom runDir Signed-off-by: CrazyMax --- __tests__/docker/install.test.e2e.ts | 6 +++++- scripts/setup-docker-linux.sh | 29 +++++++++++++--------------- scripts/setup-docker-win.ps1 | 19 +++++++++--------- src/docker/install.ts | 21 ++++++++++---------- 4 files changed, 37 insertions(+), 38 deletions(-) diff --git a/__tests__/docker/install.test.e2e.ts b/__tests__/docker/install.test.e2e.ts index e75d60f..243f0a8 100644 --- a/__tests__/docker/install.test.e2e.ts +++ b/__tests__/docker/install.test.e2e.ts @@ -14,11 +14,15 @@ * limitations under the License. */ +import path from 'path'; import {describe, expect, test} from '@jest/globals'; import {Install} from '../../src/docker/install'; import {Docker} from '../../src/docker/docker'; +// prettier-ignore +const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-jest'); + describe('install', () => { // prettier-ignore test.each(['23.0.0'])( @@ -26,7 +30,7 @@ describe('install', () => { await expect((async () => { const install = new Install(); const toolPath = await install.download(version); - await install.install(toolPath, version); + await install.install(toolPath, tmpDir, version); await Docker.printVersion(); await Docker.printInfo(); })()).resolves.not.toThrow(); diff --git a/scripts/setup-docker-linux.sh b/scripts/setup-docker-linux.sh index 763a707..239724f 100755 --- a/scripts/setup-docker-linux.sh +++ b/scripts/setup-docker-linux.sh @@ -18,6 +18,8 @@ if ! command -v dockerd &> /dev/null; then false fi +mkdir -p "$RUNDIR" + ( echo "Starting dockerd" set -x @@ -31,21 +33,16 @@ fi ) & tries=60 -echo "Waiting for daemon to start..." while ! docker version &> /dev/null; do - ((tries--)) - if [ $tries -le 0 ]; then - printf "\n" - if [ -z "$DOCKER_HOST" ]; then - echo >&2 "error: daemon failed to start" - echo >&2 " check $RUNDIR/docker.log for details" - else - echo >&2 "error: daemon at $DOCKER_HOST fails to 'docker version':" - docker version >&2 || true - fi - false - fi - printf "." - sleep 2 + ((tries--)) + if [ $tries -le 0 ]; then + if [ -z "$DOCKER_HOST" ]; then + echo >&2 "error: daemon failed to start" + else + echo >&2 "error: daemon at $DOCKER_HOST fails to 'docker version':" + docker version >&2 || true + fi + false + fi + sleep 2 done -printf "\n" diff --git a/scripts/setup-docker-win.ps1 b/scripts/setup-docker-win.ps1 index 982a003..7ba3e5d 100644 --- a/scripts/setup-docker-win.ps1 +++ b/scripts/setup-docker-win.ps1 @@ -3,8 +3,8 @@ param( [Parameter(Mandatory = $true)] [string]$ToolDir, - [Parameter(Mandatory = $false)] - [string]$TmpDir, + [Parameter(Mandatory = $true)] + [string]$RunDir, [Parameter(Mandatory = $true)] [string]$DockerHost) @@ -12,9 +12,8 @@ param( $pwver = (Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine -Name 'PowerShellVersion').PowerShellVersion Write-Host "PowerShell version: $pwver" -# Create temp directory -if (!$TmpDir) { $TmpDir = $env:TEMP } -New-Item -ItemType Directory "$TmpDir" -ErrorAction SilentlyContinue | Out-Null +# Create run directory +New-Item -ItemType Directory "$RunDir" -ErrorAction SilentlyContinue | Out-Null # Remove existing service if (Get-Service docker -ErrorAction SilentlyContinue) { @@ -37,14 +36,14 @@ $env:DOCKER_HOST = $DockerHost Write-Host "DOCKER_HOST: $env:DOCKER_HOST" Write-Host "Creating service" -New-Item -ItemType Directory "$TmpDir\moby-root" -ErrorAction SilentlyContinue | Out-Null -New-Item -ItemType Directory "$TmpDir\moby-exec" -ErrorAction SilentlyContinue | Out-Null +New-Item -ItemType Directory "$RunDir\moby-root" -ErrorAction SilentlyContinue | Out-Null +New-Item -ItemType Directory "$RunDir\moby-exec" -ErrorAction SilentlyContinue | Out-Null Start-Process -Wait -NoNewWindow "$ToolDir\dockerd" ` -ArgumentList ` "--host=$DockerHost", ` - "--data-root=$TmpDir\moby-root", ` - "--exec-root=$TmpDir\moby-exec", ` - "--pidfile=$TmpDir\docker.pid", ` + "--data-root=$RunDir\moby-root", ` + "--exec-root=$RunDir\moby-exec", ` + "--pidfile=$RunDir\docker.pid", ` "--register-service" Write-Host "Starting service" Start-Service -Name docker diff --git a/src/docker/install.ts b/src/docker/install.ts index 687c07f..9b20435 100644 --- a/src/docker/install.ts +++ b/src/docker/install.ts @@ -24,7 +24,6 @@ import * as io from '@actions/io'; import * as tc from '@actions/tool-cache'; import * as scripts from '../scripts'; -import {Context} from '../context'; import {Exec} from '../exec'; import {Util} from '../util'; @@ -41,7 +40,7 @@ export class Install { if (os.platform() == 'win32') { extractFolder = await tc.extractZip(downloadPath); } else { - extractFolder = await tc.extractTar(downloadPath, path.join(Context.tmpDir(), 'docker')); + extractFolder = await tc.extractTar(downloadPath); } if (Util.isDirectory(path.join(extractFolder, 'docker'))) { extractFolder = path.join(extractFolder, 'docker'); @@ -65,7 +64,7 @@ export class Install { return tooldir; } - public async install(toolDir: string, version: string, channel?: string): Promise { + public async install(toolDir: string, runDir: string, version: string, channel?: string): Promise { channel = channel || 'stable'; switch (os.platform()) { case 'darwin': { @@ -73,11 +72,11 @@ export class Install { break; } case 'linux': { - await this.installLinux(toolDir); + await this.installLinux(toolDir, runDir); break; } case 'win32': { - await this.installWindows(toolDir); + await this.installWindows(toolDir, runDir); break; } default: { @@ -87,7 +86,7 @@ export class Install { } private async installDarwin(toolDir: string, version: string, channel?: string): Promise { - const colimaDir = path.join(os.homedir(), '.colima', 'default'); + const colimaDir = path.join(os.homedir(), '.colima', 'default'); // TODO: create a custom colima profile to avoid overlap with other actions await io.mkdirP(colimaDir); const dockerHost = `unix://${colimaDir}/docker.sock`; @@ -130,15 +129,15 @@ export class Install { }); } - private async installLinux(toolDir: string): Promise { - const dockerHost = `unix://${path.join(Context.tmpDir(), 'docker.sock')}`; + private async installLinux(toolDir: string, runDir: string): Promise { + const dockerHost = `unix://${path.join(runDir, 'docker.sock')}`; await core.group('Install Docker daemon', async () => { const bashPath: string = await io.which('bash', true); await Exec.exec('sudo', ['-E', bashPath, scripts.setupDockerLinux], { env: Object.assign({}, process.env, { TOOLDIR: toolDir, - RUNDIR: Context.tmpDir(), + RUNDIR: runDir, DOCKER_HOST: dockerHost }) as { [key: string]: string; @@ -152,12 +151,12 @@ export class Install { }); } - private async installWindows(toolDir: string): Promise { + private async installWindows(toolDir: string, runDir: string): Promise { const dockerHost = 'npipe:////./pipe/setup_docker_action'; const setupCmd = await Util.powershellCommand(scripts.setupDockerWin, { ToolDir: toolDir, - TmpDir: Context.tmpDir(), + RunDir: runDir, DockerHost: dockerHost }); await core.group('Install Docker daemon service', async () => {