diff --git a/AGENTS.md b/AGENTS.md index 14fb02d..7ddc242 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -9,8 +9,10 @@ from a terminal without a management server. ## Architecture -- Use PowerShell 7 for the CLI and implementation modules. Keep `bootstrap.ps1` - compatible with built-in Windows PowerShell 5.1 so a fresh machine can start. +- Use PowerShell 7 for the CLI and implementation modules. Keep `install.ps1` + and `bootstrap.ps1` compatible with built-in Windows PowerShell 5.1 so a fresh + machine can start. Keep `install.ps1` as a thin web entrypoint that stages and + invokes the bootstrap rather than duplicating setup logic. - Use WinGet for software and Git for repository distribution. - Use JSON manifests for profiles and machine definitions, with no YAML dependency. - Expose `cc` through a thin launcher for `cc.ps1`. Implement CLI commands before @@ -21,6 +23,7 @@ from a terminal without a management server. Planned layout: ```text +install.ps1 bootstrap.ps1 cc.ps1 src/ diff --git a/Jenkinsfile b/Jenkinsfile index 43c4795..bd576b6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -128,7 +128,7 @@ PY fi echo "${ps_files}" - for required in bootstrap.ps1 cc.ps1 schemas/profile.schema.json schemas/machine.schema.json; do + for required in install.ps1 bootstrap.ps1 cc.ps1 schemas/profile.schema.json schemas/machine.schema.json; do if [ ! -s "${required}" ]; then echo "ERROR: required file is missing or empty: ${required}" >&2 exit 1 diff --git a/README.md b/README.md index a824c54..8ab7dcc 100644 --- a/README.md +++ b/README.md @@ -24,26 +24,28 @@ the Windows edition and able to reach its configured sources. ## Onboard a machine -To bootstrap directly from the web, replace `example-desktop` with the machine -manifest to use and run this in Windows PowerShell. The script is staged to a -temporary file because administrator relaunch cannot work from an in-memory -script block: +Run the one-stop installer in Windows PowerShell: ```powershell -$bootstrap = Join-Path $env:TEMP "config-central-bootstrap-$([guid]::NewGuid().ToString('N')).ps1" -try { - irm 'https://proxy.dandaedre.com/gitea/Beffroi/config-central/raw/branch/main/bootstrap.ps1' -OutFile $bootstrap - powershell.exe -NoProfile -ExecutionPolicy Bypass -File $bootstrap ` - -Repository 'https://proxy.dandaedre.com/gitea/Beffroi/config-central.git' ` - -Branch main ` - -Machine example-desktop - if ($LASTEXITCODE -ne 0) { throw "Bootstrap failed with exit code $LASTEXITCODE." } -} -finally { - Remove-Item -LiteralPath $bootstrap -Force -ErrorAction SilentlyContinue -} +irm 'https://proxy.dandaedre.com/gitea/Beffroi/config-central/raw/branch/main/install.ps1' | iex ``` +On a new computer, the installer retrieves the machine manifests and selects the +only one, matches the Windows computer name, or displays a numbered prompt. On a +configured computer it reuses the saved machine automatically. It then stages +`bootstrap.ps1` as a temporary file, establishes every prerequisite, clones or +updates the repository, and applies the machine manifest. The temporary file is +removed afterward. + +For unattended installation, select the machine in the same command: + +```powershell +$env:CONFIG_CENTRAL_MACHINE = 'example-desktop'; irm 'https://proxy.dandaedre.com/gitea/Beffroi/config-central/raw/branch/main/install.ps1' | iex +``` + +The installer is safe to rerun for repair or setup updates. Use `cc sync` for +normal repository, package, and configuration updates after onboarding. + Download `bootstrap.ps1` from a trusted revision of this repository, inspect it, then run it from Windows PowerShell: @@ -54,11 +56,12 @@ powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\bootstrap.ps1 ` -Machine example-desktop ``` -Bootstrap repairs WinGet if necessary, installs Git and PowerShell 7, clones the -configured branch under `%LOCALAPPDATA%\ConfigCentral\repository`, saves the -machine selection, adds `cc` to the user PATH, and performs the first apply. It -is safe to rerun after a failed prerequisite installation. Use `-SkipApply` when -you only want to establish the checkout and launcher. +The underlying bootstrap repairs WinGet if necessary, installs Git and +PowerShell 7, clones the configured branch under +`%LOCALAPPDATA%\ConfigCentral\repository`, saves the machine selection, adds `cc` +to the user PATH, and performs the first apply. It is safe to rerun after a +failed prerequisite installation. Use `-SkipApply` when you only want to +establish the checkout and launcher. A UAC prompt appears only when administrator work is pending and the current terminal is not already elevated. The prompt covers WinGet repair or the batch diff --git a/TASKS.md b/TASKS.md index 1df44c6..7ee2727 100644 --- a/TASKS.md +++ b/TASKS.md @@ -73,6 +73,7 @@ open until it is run on disposable machines with the required account setups. - [x] Implement PowerShell 5.1 bootstrap to check or establish WinGet, Git, and PowerShell 7, with explicit handling of missing WinGet registration. - [x] Stage web bootstrap as a file and use absolute PowerShell paths for elevation. +- [x] Add a one-command installer with automatic machine selection and repair reruns. - [x] Clone the repository, save machine selection, install the launcher, and perform the first apply. - [ ] Verify bootstrap can resume after interrupted prerequisite installation. diff --git a/install.ps1 b/install.ps1 new file mode 100644 index 0000000..f55c26a --- /dev/null +++ b/install.ps1 @@ -0,0 +1,131 @@ +#requires -Version 5.1 +[CmdletBinding()] +param( + [ValidatePattern('^[a-z0-9][a-z0-9-]*$')][string]$Machine = $env:CONFIG_CENTRAL_MACHINE, + [ValidatePattern('^[A-Za-z0-9._/-]+$')][string]$Branch = $env:CONFIG_CENTRAL_BRANCH, + [switch]$SkipApply +) + +Set-StrictMode -Version 2.0 +$ErrorActionPreference = 'Stop' +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + +$repository = 'https://proxy.dandaedre.com/gitea/Beffroi/config-central.git' +$repositoryApi = 'https://proxy.dandaedre.com/gitea/api/v1/repos/Beffroi/config-central' +$rawRoot = 'https://proxy.dandaedre.com/gitea/Beffroi/config-central/raw/branch' + +if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) { + throw 'Config Central installer runs only on Windows.' +} +if ([string]::IsNullOrWhiteSpace($env:TEMP) -or [string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) { + throw 'The interactive Windows user profile is unavailable.' +} +if ([string]::IsNullOrWhiteSpace($Branch)) { $Branch = 'main' } +if ($env:CONFIG_CENTRAL_SKIP_APPLY -eq '1') { $SkipApply = $true } + +function Get-SavedMachine { + $settingsPath = Join-Path $env:LOCALAPPDATA 'ConfigCentral\settings.json' + if (-not (Test-Path -LiteralPath $settingsPath -PathType Leaf)) { return $null } + try { + $settings = Get-Content -LiteralPath $settingsPath -Raw | ConvertFrom-Json + if ($settings.machine -match '^[a-z0-9][a-z0-9-]*$') { return [string]$settings.machine } + } + catch { Write-Warning "Ignoring unreadable existing settings: $($_.Exception.Message)" } + return $null +} + +function Get-AvailableMachines { + $encodedBranch = [Uri]::EscapeDataString($Branch) + try { + $entries = Invoke-RestMethod -Uri "$repositoryApi/contents/machines?ref=$encodedBranch" -UseBasicParsing + return @( + $entries | + Where-Object { $_.type -eq 'file' -and $_.name -match '^[a-z0-9][a-z0-9-]*\.json$' } | + ForEach-Object { $_.name.Substring(0, $_.name.Length - 5) } | + Sort-Object -Unique + ) + } + catch { + Write-Warning "Could not list machine manifests: $($_.Exception.Message)" + return @() + } +} + +function Select-ConfigCentralMachine { + param([string]$Requested, [string]$Saved, [string[]]$Available) + + if (-not [string]::IsNullOrWhiteSpace($Requested)) { + if ($Available.Count -gt 0 -and $Requested -notin $Available) { + throw "Machine manifest '$Requested' does not exist on branch '$Branch'." + } + return $Requested + } + if (-not [string]::IsNullOrWhiteSpace($Saved) -and ($Available.Count -eq 0 -or $Saved -in $Available)) { + Write-Host "Reusing configured machine '$Saved'." + return $Saved + } + + $computerName = if ($env:COMPUTERNAME) { $env:COMPUTERNAME.ToLowerInvariant() } else { $null } + if ($computerName -and $computerName -in $Available) { + Write-Host "Using machine manifest '$computerName' matching this computer name." + return $computerName + } + if ($Available.Count -eq 1) { + Write-Host "Using the only available machine manifest, '$($Available[0])'." + return $Available[0] + } + if ($Available.Count -eq 0) { + $answer = Read-Host 'Machine manifest name' + if ($answer -notmatch '^[a-z0-9][a-z0-9-]*$') { throw "Invalid machine name '$answer'." } + return $answer + } + + Write-Host 'Available machine manifests:' + for ($index = 0; $index -lt $Available.Count; $index++) { + Write-Host (" {0}. {1}" -f ($index + 1), $Available[$index]) + } + $selection = Read-Host 'Select a machine number or name' + $number = 0 + if ([int]::TryParse($selection, [ref]$number) -and $number -ge 1 -and $number -le $Available.Count) { + return $Available[$number - 1] + } + if ($selection -in $Available) { return $selection } + throw "Unknown machine selection '$selection'." +} + +$availableMachines = @(Get-AvailableMachines) +$Machine = Select-ConfigCentralMachine -Requested $Machine -Saved (Get-SavedMachine) -Available $availableMachines +$bootstrapPath = Join-Path $env:TEMP "config-central-bootstrap-$([guid]::NewGuid().ToString('N')).ps1" +$encodedBranch = [Uri]::EscapeDataString($Branch) +$bootstrapUrl = "$rawRoot/$encodedBranch/bootstrap.ps1" + +try { + Write-Host "Downloading Config Central bootstrap for branch '$Branch'..." + Invoke-RestMethod -Uri $bootstrapUrl -OutFile $bootstrapPath -UseBasicParsing + if (-not (Test-Path -LiteralPath $bootstrapPath -PathType Leaf) -or (Get-Item -LiteralPath $bootstrapPath).Length -eq 0) { + throw 'Downloaded bootstrap is missing or empty.' + } + + $tokens = $null + $parseErrors = $null + [void][System.Management.Automation.Language.Parser]::ParseFile($bootstrapPath, [ref]$tokens, [ref]$parseErrors) + if ($parseErrors.Count -gt 0) { throw "Downloaded bootstrap does not parse: $($parseErrors[0].Message)" } + + $windowsPowerShell = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe' + $arguments = @( + '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $bootstrapPath, + '-Repository', $repository, '-Branch', $Branch, '-Machine', $Machine + ) + if ($SkipApply) { $arguments += '-SkipApply' } + + & $windowsPowerShell @arguments + $bootstrapExitCode = $LASTEXITCODE + if ($bootstrapExitCode -ne 0) { throw "Bootstrap failed with exit code $bootstrapExitCode." } + + Write-Host '' + Write-Host "Config Central is ready for '$Machine'." + Write-Host 'Use cc sync for normal updates, or rerun this installer to repair the setup.' +} +finally { + Remove-Item -LiteralPath $bootstrapPath -Force -ErrorAction SilentlyContinue +} diff --git a/tests/Installer.Tests.ps1 b/tests/Installer.Tests.ps1 new file mode 100644 index 0000000..e2a05ef --- /dev/null +++ b/tests/Installer.Tests.ps1 @@ -0,0 +1,42 @@ +BeforeAll { + $script:installerPath = "$PSScriptRoot/../install.ps1" + $tokens = $null + $errors = $null + $script:installerAst = [System.Management.Automation.Language.Parser]::ParseFile($installerPath, [ref]$tokens, [ref]$errors) + if ($errors.Count -gt 0) { throw "install.ps1 does not parse: $($errors[0].Message)" } + $functionAst = $installerAst.Find({ + param($node) + $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Select-ConfigCentralMachine' + }, $true) + . ([scriptblock]::Create($functionAst.Extent.Text)) +} + +Describe 'One-stop installer machine selection' { + BeforeEach { + $script:Branch = 'main' + $script:previousComputerName = $env:COMPUTERNAME + $env:COMPUTERNAME = 'OFFICE-PC' + } + + AfterEach { $env:COMPUTERNAME = $script:previousComputerName } + + It 'uses an explicit machine selection' { + Select-ConfigCentralMachine -Requested living-room -Saved office-pc -Available @('living-room', 'office-pc') | + Should -Be 'living-room' + } + + It 'reuses the saved machine on subsequent runs' { + Select-ConfigCentralMachine -Requested '' -Saved living-room -Available @('living-room', 'office-pc') | + Should -Be 'living-room' + } + + It 'matches the Windows computer name before prompting' { + Select-ConfigCentralMachine -Requested '' -Saved '' -Available @('living-room', 'office-pc') | + Should -Be 'office-pc' + } + + It 'automatically selects the only available manifest' { + Select-ConfigCentralMachine -Requested '' -Saved '' -Available @('example-desktop') | + Should -Be 'example-desktop' + } +}