Files
RemoteCompilation/deploy/windows/install.ps1
T

90 lines
4.9 KiB
PowerShell

param(
[string]$Root = 'C:\ProgramData\VibeCodingRemoteCompilation',
[Parameter(Mandatory=$true)][string]$JavaHome,
[string]$AndroidHome = 'D:\AndroidSdk\Windows'
)
$ErrorActionPreference = 'Stop'
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Administrator required' }
if (-not (Test-Path -LiteralPath "$JavaHome\bin\java.exe")) { throw 'Invalid JavaHome' }
if (-not (Test-Path -LiteralPath "$AndroidHome\platforms")) { throw 'Invalid AndroidHome' }
foreach ($name in @('remotecompile-windows-amd64.exe','WinSW-x64-v2.12.0.exe')) {
if (-not (Test-Path -LiteralPath (Join-Path $PSScriptRoot $name))) { throw "Missing $name" }
}
if (Get-Service RemoteCompile -ErrorAction SilentlyContinue) {
Stop-Service RemoteCompile
(Get-Service RemoteCompile).WaitForStatus('Stopped',[TimeSpan]::FromSeconds(45))
}
New-Item -ItemType Directory -Force -Path $Root | Out-Null
& icacls.exe $Root /inheritance:r /grant:r '*S-1-5-18:(OI)(CI)F' '*S-1-5-32-544:(OI)(CI)F' '*S-1-5-19:(OI)(CI)RX' | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'Root ACL failed' }
foreach ($dir in @('bin','logs','data','home','temp')) { New-Item -ItemType Directory -Force -Path (Join-Path $Root $dir) | Out-Null }
foreach ($dir in @('logs','data','home','temp')) {
& icacls.exe (Join-Path $Root $dir) /grant:r '*S-1-5-19:(OI)(CI)M' | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'Data ACL failed' }
}
$config = Join-Path $Root 'bin\RemoteCompileService.xml'
if (Test-Path -LiteralPath $config) {
[xml]$old = Get-Content -LiteralPath $config -Raw
$token = ($old.service.env | Where-Object name -eq 'REMOTE_BUILD_TOKEN').value
} else {
$bytes = New-Object byte[] 32
$rng = [Security.Cryptography.RandomNumberGenerator]::Create()
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
$token = ([BitConverter]::ToString($bytes)).Replace('-','').ToLowerInvariant()
}
if ($token.Length -lt 24) { throw 'Invalid token' }
Copy-Item -LiteralPath (Join-Path $PSScriptRoot 'remotecompile-windows-amd64.exe') -Destination "$Root\bin\remotecompile.exe" -Force
Copy-Item -LiteralPath (Join-Path $PSScriptRoot 'WinSW-x64-v2.12.0.exe') -Destination "$Root\bin\RemoteCompileService.exe" -Force
function XmlEscape([string]$value) { [Security.SecurityElement]::Escape($value) }
$safeRoot = XmlEscape $Root
$safeJava = XmlEscape $JavaHome
$safeSdk = XmlEscape $AndroidHome
$xml = @"
<service>
<id>RemoteCompile</id>
<name>VibeCoding Remote Compilation</name>
<description>Android remote build service, loopback only.</description>
<executable>$safeRoot\bin\remotecompile.exe</executable>
<arguments>-listen 127.0.0.1:8765 -data &quot;$safeRoot\data&quot; -tasks :app:assembleDebug</arguments>
<workingdirectory>$safeRoot</workingdirectory>
<startmode>Automatic</startmode>
<delayedAutoStart>true</delayedAutoStart>
<stoptimeout>40 sec</stoptimeout>
<onfailure action="restart" delay="10 sec"/>
<logpath>$safeRoot\logs</logpath>
<log mode="roll-by-size"><sizeThreshold>10240</sizeThreshold><keepFiles>4</keepFiles></log>
<serviceaccount><domain>NT AUTHORITY</domain><user>LocalService</user></serviceaccount>
<env name="REMOTE_BUILD_TOKEN" value="$token"/>
<env name="JAVA_HOME" value="$safeJava"/>
<env name="ANDROID_HOME" value="$safeSdk"/>
<env name="ANDROID_SDK_ROOT" value="$safeSdk"/>
<env name="GRADLE_USER_HOME" value="$safeRoot\home\.gradle"/>
<env name="HOME" value="$safeRoot\home"/>
<env name="USERPROFILE" value="$safeRoot\home"/>
<env name="TEMP" value="$safeRoot\temp"/>
<env name="TMP" value="$safeRoot\temp"/>
</service>
"@
[IO.File]::WriteAllText($config,$xml,(New-Object Text.UTF8Encoding($false)))
if (-not (Get-Service RemoteCompile -ErrorAction SilentlyContinue)) {
& "$Root\bin\RemoteCompileService.exe" install
if ($LASTEXITCODE -ne 0) { throw 'Service installation failed' }
}
Start-Service RemoteCompile
$headers = @{Authorization="Bearer $token"}
$healthy = $false
for ($i=0;$i -lt 30;$i++) {
try {
$result = Invoke-RestMethod -Uri 'http://127.0.0.1:8765/api/v1/health' -Headers $headers -TimeoutSec 2
Write-Output ('Health: ' + ($result | ConvertTo-Json -Compress)); $healthy=$true; break
} catch { Start-Sleep -Seconds 1 }
}
if (-not $healthy) { throw "Service health failed. Check $Root\logs" }
$tokenPath = Join-Path $env:USERPROFILE '.remotecompile-service-token.txt'
[IO.File]::WriteAllText($tokenPath,$token,(New-Object Text.UTF8Encoding($false)))
$sid = [Security.Principal.WindowsIdentity]::GetCurrent().User.Value
& icacls.exe $tokenPath /inheritance:r /grant:r "*$($sid):R" '*S-1-5-18:F' '*S-1-5-32-544:F' | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'Token ACL failed' }
Get-CimInstance Win32_Service -Filter "Name='RemoteCompile'" | Select-Object Name,State,StartMode,StartName
Write-Output 'Client token exported privately; contents hidden.'