oto/apps/runner/assets/script/powershell/oto_agent_bootstrap.ps1
toki ac44c85d34 feat: Windows bootstrap script and HTTP server updates
- Add PowerShell bootstrap script for Windows runner
- Update httpserver bootstrap provider, routes, and runner handlers
- Add bootstrap PowerShell test case
- Move old G07 task docs to archive
- Update server tests
2026-06-15 22:10:21 +09:00

127 lines
4.6 KiB
PowerShell

#Requires -Version 5.1
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$serverUrl = ''
$agentId = ''
$enrollmentToken = ''
$releaseBaseUrl = ''
$agentAlias = ''
$installDir = Join-Path $env:LOCALAPPDATA 'oto\bin'
$configPath = Join-Path $env:LOCALAPPDATA 'oto\agent\config.yaml'
$workspaceRoot = Join-Path $env:LOCALAPPDATA 'oto\workspace'
$logDir = Join-Path $env:LOCALAPPDATA 'oto\agent\log'
$background = $true
$i = 0
while ($i -lt $args.Count) {
switch ($args[$i]) {
'--' { $i++ }
'--server-url' { $serverUrl = $args[$i + 1]; $i += 2 }
'--agent-id' { $agentId = $args[$i + 1]; $i += 2 }
'--enrollment-token' { $enrollmentToken = $args[$i + 1]; $i += 2 }
'--release-base-url' { $releaseBaseUrl = $args[$i + 1]; $i += 2 }
'--agent-alias' { $agentAlias = $args[$i + 1]; $i += 2 }
'--install-dir' { $installDir = $args[$i + 1]; $i += 2 }
'--config-path' { $configPath = $args[$i + 1]; $i += 2 }
'--workspace-root' { $workspaceRoot = $args[$i + 1]; $i += 2 }
'--log-dir' { $logDir = $args[$i + 1]; $i += 2 }
'--no-background' { $background = $false; $i++ }
default { Write-Error "Unknown option: $($args[$i])"; exit 1 }
}
}
if (-not $serverUrl -or -not $agentId -or -not $enrollmentToken -or -not $releaseBaseUrl) {
Write-Error 'Error: Missing required arguments: --server-url, --agent-id, --enrollment-token, --release-base-url'
exit 1
}
if (-not $releaseBaseUrl.StartsWith('https://')) {
Write-Error 'Error: --release-base-url must use https://'
exit 1
}
$arch = $env:PROCESSOR_ARCHITECTURE
$archName = switch ($arch) {
'AMD64' { 'x64' }
'ARM64' { 'arm64' }
default {
Write-Error "Error: Unsupported architecture: $arch"
exit 1
}
}
$assetName = "oto-windows-$archName.zip"
$downloadUrl = "$releaseBaseUrl/$assetName"
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
New-Item -ItemType Directory -Path $tmpDir | Out-Null
try {
$zipPath = Join-Path $tmpDir $assetName
Write-Host 'Downloading OTO agent...'
Invoke-WebRequest -Uri $downloadUrl -OutFile $zipPath -UseBasicParsing
Write-Host 'Extracting OTO agent...'
Expand-Archive -Path $zipPath -DestinationPath $tmpDir -Force
$null = New-Item -ItemType Directory -Path $installDir -Force
Copy-Item -Path (Join-Path $tmpDir 'oto.exe') -Destination (Join-Path $installDir 'oto.exe') -Force
$configDir = Split-Path $configPath -Parent
$agentStateDir = Join-Path $env:LOCALAPPDATA 'oto\agent'
$pidPath = Join-Path $agentStateDir 'oto-agent.pid'
$null = New-Item -ItemType Directory -Path $configDir -Force
$null = New-Item -ItemType Directory -Path $agentStateDir -Force
$null = New-Item -ItemType Directory -Path $workspaceRoot -Force
$null = New-Item -ItemType Directory -Path $logDir -Force
$configContent = @"
agent:
id: "$agentId"
alias: "$agentAlias"
enrollment_token: "$enrollmentToken"
server:
url: "$serverUrl"
runtime:
install_dir: "$($installDir -replace '\\', '/')"
workspace_root: "$($workspaceRoot -replace '\\', '/')"
log_dir: "$($logDir -replace '\\', '/')"
"@
[System.IO.File]::WriteAllText($configPath, $configContent)
$acl = Get-Acl $configPath
$acl.SetAccessRuleProtection($true, $false)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name,
'FullControl',
'Allow'
)
$acl.SetAccessRule($rule)
Set-Acl -Path $configPath -AclObject $acl
$otoBin = Join-Path $installDir 'oto.exe'
if ($background) {
Write-Host 'Starting OTO agent in the background...'
$outLog = Join-Path $logDir 'oto-agent.out.log'
$errLog = Join-Path $logDir 'oto-agent.err.log'
$proc = Start-Process -FilePath $otoBin `
-ArgumentList "agent run --config `"$configPath`"" `
-RedirectStandardOutput $outLog `
-RedirectStandardError $errLog `
-WindowStyle Hidden `
-PassThru
[System.IO.File]::WriteAllText($pidPath, $proc.Id.ToString())
Write-Host "OTO agent started in the background. PID: $($proc.Id)"
Write-Host "Log path: $outLog"
} else {
Write-Host 'OTO agent is configured to run in the foreground.'
Write-Host 'To run it manually, use the following command:'
Write-Host " $otoBin agent run --config `"$configPath`""
}
} finally {
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue $tmpDir
}