feat: add headless agent management to CLI and both desktop clients

This commit is contained in:
tongtong committed 2026-11-12 21:17:44 +08:00
1 parent e91fa0235c
commit 6916dd54fb
50 files changed
+4837 -148

No files matched your search

+16
View File
@@ -54,3 +54,19 @@ Get-Content "$env:LOCALAPPDATA\AndroidDeviceStreaming\agent\logs\agent.log" -Tai
Start-ScheduledTask -TaskName ADS-Agent
Stop-ScheduledTask -TaskName ADS-Agent
```
## Managed multi-phone Agent (0.2)
The deployed computer has migrated to Agent ID `home-pc` (家里电脑), retaining `home-phone` and its TCP endpoint. `ADS-Agent-home-pc` runs the native supervised Agent as SYSTEM at startup; the old `ADS-Agent` task is disabled. The migration keeps a timestamped backup under the protected Agent directory and restores the legacy files/task on failure. No reboot was performed.
`migrate-managed.ps1` migrates the earlier TCP single-phone installation. Stage the new binaries as `ads-agent.exe.new` and `adsctl.exe.new` in its directory, then run the script in an elevated PowerShell 7 terminal. It verifies the legacy task belongs to that directory before stopping it. Parameters customize directory, Agent ID, display name and old task name. New installations should use `adsctl init` and `adsctl service install` instead.
For current status and logs, use:
```powershell
& "$env:LOCALAPPDATA/AndroidDeviceStreaming/agent/adsctl.exe" status
& "$env:LOCALAPPDATA/AndroidDeviceStreaming/agent/adsctl.exe" logs --follow
Get-ScheduledTask -TaskName ADS-Agent-home-pc
```
The managed log buffer is in memory; the old runner log is historical. See [Agent management](../../docs/agent-management.md) for Linux and Windows headless setup, CLI commands, role credentials and both GUIs.
+67
View File
@@ -0,0 +1,67 @@
param(
[string]$Directory = (Join-Path $env:LOCALAPPDATA 'AndroidDeviceStreaming/agent'),
[string]$Id = 'home-pc',
[string]$Name = '家里电脑',
[string]$LegacyTask = 'ADS-Agent'
)
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$root = [IO.Path]::GetFullPath($Directory).TrimEnd('\')
$configPath = Join-Path $root 'agent.json'
$exe = Join-Path $root 'ads-agent.exe'
$ctl = Join-Path $root 'adsctl.exe'
$old = Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json
if ($old.devices -or $old.id) { throw 'Already managed; use adsctl config commands instead' }
if (-not $old.device -or -not $old.target) { throw 'Expected legacy TCP phone configuration' }
$task = Get-ScheduledTask -TaskName $LegacyTask -ErrorAction Stop
if (-not @($task.Actions | Where-Object { $_.Arguments -and $_.Arguments.Contains($root) }).Count) { throw 'Legacy task does not belong to the requested Agent directory' }
if (Get-ScheduledTask -TaskName "ADS-Agent-$Id" -ErrorAction SilentlyContinue) { throw 'Managed task already exists' }
foreach ($file in @('ads-agent.exe.new', 'adsctl.exe.new')) {
if (-not (Test-Path -LiteralPath (Join-Path $root $file))) { throw "Missing staged $file" }
}
$backup = Join-Path $root ('backup-management-' + (Get-Date -Format yyyyMMdd-HHmmss))
New-Item -ItemType Directory -Path $backup | Out-Null
foreach ($file in @('agent.json','ads-agent.exe','adsctl.exe')) {
$path = Join-Path $root $file
if (Test-Path -LiteralPath $path) { Copy-Item -LiteralPath $path -Destination $backup }
}
Export-ScheduledTask -TaskName $LegacyTask | Set-Content -LiteralPath (Join-Path $backup 'legacy-task.xml') -Encoding utf8
$next = [ordered]@{version=1;id=$Id;name=$Name;server=$old.server;connect_address=$old.connect_address;adb_server='127.0.0.1:5037';devices=@(@{id=$old.device;target=$old.target;enabled=$true})}
try {
Disable-ScheduledTask -TaskName $LegacyTask | Out-Null
Stop-ScheduledTask -TaskName $LegacyTask
Get-CimInstance Win32_Process -Filter "Name='ads-agent.exe'" | Where-Object { $_.ExecutablePath -eq $exe } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force }
Start-Sleep -Seconds 1
Move-Item -LiteralPath (Join-Path $root 'ads-agent.exe.new') -Destination $exe -Force
Move-Item -LiteralPath (Join-Path $root 'adsctl.exe.new') -Destination $ctl -Force
$next | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $configPath -Encoding utf8NoBOM
& $ctl --config $configPath config validate --file $configPath
if ($LASTEXITCODE) { throw 'Managed config validation failed' }
& $ctl --config $configPath service install
if ($LASTEXITCODE) { throw 'Managed service installation failed' }
$ready=$false
for ($i=0; $i -lt 10; $i++) {
Start-Sleep -Seconds 1
$raw = & $ctl --json --config $configPath status 2>$null
if ($LASTEXITCODE -eq 0 -and ($raw | ConvertFrom-Json).id -eq $Id) { $ready=$true;break }
}
if (-not $ready) { throw 'Managed Agent did not become ready' }
Write-Output "Migration succeeded; backup: $backup"
& $ctl --json --config $configPath status
} catch {
$failure=$_
$managed=Get-ScheduledTask -TaskName "ADS-Agent-$Id" -ErrorAction SilentlyContinue
if ($managed -and @($managed.Actions | Where-Object { $_.Execute -eq $exe -and $_.Arguments.Contains($configPath) }).Count) {
Stop-ScheduledTask -TaskName "ADS-Agent-$Id" -ErrorAction SilentlyContinue
Unregister-ScheduledTask -TaskName "ADS-Agent-$Id" -Confirm:$false
}
Get-CimInstance Win32_Process -Filter "Name='ads-agent.exe'" | Where-Object { $_.ExecutablePath -eq $exe } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }
Start-Sleep -Seconds 1
foreach ($file in @('agent.json','ads-agent.exe','adsctl.exe')) {
$saved=Join-Path $backup $file
if (Test-Path -LiteralPath $saved) { Copy-Item -LiteralPath $saved -Destination (Join-Path $root $file) -Force }
}
Enable-ScheduledTask -TaskName $LegacyTask | Out-Null
Start-ScheduledTask -TaskName $LegacyTask
throw $failure
}
+2 -2
View File
@@ -14,8 +14,8 @@ $logFile = Join-Path $logDirectory 'agent.log'
if ((Test-Path -LiteralPath $logFile) -and (Get-Item -LiteralPath $logFile).Length -gt 10MB) {
Move-Item -LiteralPath $logFile -Destination ($logFile + '.1') -Force
}
$arguments = @('--server', [string]$config.server, '--device', [string]$config.device, '--target', [string]$config.target)
if ($config.connect_address) { $arguments += @('--connect-address', [string]$config.connect_address) }
$arguments = if ($config.id -and $config.devices -ne $null) { @('--quiet', '--supervise', '--config', (Join-Path $root 'agent.json')) } else { @('--server', [string]$config.server, '--device', [string]$config.device, '--target', [string]$config.target) }
if (-not $config.id -and $config.connect_address) { $arguments += @('--connect-address', [string]$config.connect_address) }
# Keep the child in this task's process tree. Task Scheduler owns its lifecycle.
# Tracing writes to stdout; stderr is also retained for startup diagnostics.
$ErrorActionPreference = 'Continue'