docker: install with custom runDir

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-02-28 20:15:33 +01:00
parent 93fa96f54f
commit 3b532d1b91
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
4 changed files with 37 additions and 38 deletions

View File

@ -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();

View File

@ -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"

View File

@ -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

View File

@ -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<void> {
public async install(toolDir: string, runDir: string, version: string, channel?: string): Promise<void> {
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<void> {
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<void> {
const dockerHost = `unix://${path.join(Context.tmpDir(), 'docker.sock')}`;
private async installLinux(toolDir: string, runDir: string): Promise<void> {
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<void> {
private async installWindows(toolDir: string, runDir: string): Promise<void> {
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 () => {