# Installs or upgrades Factorial in $HOME\factorial. It never starts the stack: it ends by printing # the command that does (`docker compose up -d --wait` in the folder). Runs under Windows # PowerShell 5.1 and PowerShell 7: # irm https://www.runfactorial.com/install.ps1 | iex # & ([scriptblock]::Create((irm https://www.runfactorial.com/install.ps1))) -Version 0.1.3 # Installs made before this script are not supported: it knows only the stack it writes, whose # data is the `factorial-public` volume. # - The version: -Version, else FACTORIAL_VERSION, else the one the folder's compose.yaml pins, # else the site's version.txt. When version.txt is newer than the pinned one, it asks before an # upgrade. With no console to ask on, it stays on the installed version. A version older than # the pinned one is refused without -Force: the database is not migrated back. # - A new version is pulled before anything changes, so a failed pull leaves the stack running. # Then an upgrade takes the stack down (`docker compose down`, never -v: it removes the # containers and keeps the volume) and backs up the volume to backups\. A failed backup leaves # compose.yaml on the installed version. # - factorial-setup writes factorial.env and seccomp.json on the first run only. A rerun never # changes factorial.env. A `factorial-public` volume with no factorial.env in the folder stops # the script: its secrets open only with the file they were made with. # - compose.yaml is written from the site's template on a first run or a version change only # (the template's header names the tag rule). # - It never removes a volume or a folder. It reports a failure and returns, never `exit`, which # would close the window it was pasted into. # - $MinVersion is the oldest version it installs, the first with the two-container stack: a # -Version or a version.txt below it is refused. landing/build/version.ts names the same floor. # install.sh is the same flow for macOS and Linux; keep the two in step. # FACTORIAL_SITE replaces the site address, for tests. param([string]$Version = "", [switch]$Force) $MinVersion = "0.1.3" function Stop-Install([string]$Message) { throw [System.Exception]::new($Message) } function Test-Version([string]$Value) { return $Value -match '^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$' } function Assert-Supported([string]$Value) { if ([version]$Value -lt [version]$MinVersion) { Stop-Install "Factorial $Value predates this install script; use $MinVersion or later." } } # Native commands run under Continue: Windows PowerShell 5.1 turns a native stderr line into an # error that Stop throws. The exit code is the only verdict. Out-Host keeps docker's output off # the pipeline, where it would join the returned value. function Invoke-Native([string[]]$DockerArgs, [switch]$Quiet, [switch]$DropOutput) { $saved = $ErrorActionPreference $ErrorActionPreference = "Continue" try { if ($Quiet) { & docker @DockerArgs *> $null } elseif ($DropOutput) { & docker @DockerArgs | Out-Null } else { & docker @DockerArgs | Out-Host } return ($LASTEXITCODE -eq 0) } catch { return $false } finally { $ErrorActionPreference = $saved } } function Test-Docker([string[]]$DockerArgs) { return (Invoke-Native $DockerArgs -Quiet) } function Invoke-Docker([string[]]$DockerArgs, [string]$Failure = "", [switch]$DropOutput) { if (-not (Invoke-Native $DockerArgs -DropOutput:$DropOutput)) { if (-not $Failure) { $Failure = "docker $($DockerArgs -join ' ') failed. Its message is above." } Stop-Install $Failure } } function Get-SiteFile([string]$Url, [string]$Path) { try { Invoke-WebRequest -UseBasicParsing -Uri $Url -OutFile $Path } catch { Stop-Install "Cannot read $Url. Check the network, then run this again." } } function Get-ImageMatch([string]$Text, [int]$Group) { $found = [regex]::Match($Text, '(?m)^[ \t]*image:[ \t]*(\S+):([^:\s]+)[ \t]*\r?$') if ($found.Success) { return $found.Groups[$Group].Value } return "" } function Install-Factorial([string]$Requested, [bool]$Override) { $ErrorActionPreference = "Stop" $site = $env:FACTORIAL_SITE if (-not $site) { $site = "https://www.runfactorial.com" } $dir = Join-Path $HOME "factorial" if (-not $Requested) { $Requested = $env:FACTORIAL_VERSION } if ($Requested -and -not (Test-Version $Requested)) { Stop-Install "$Requested is not a version. Give one such as 0.1.3." } if ($Requested) { Assert-Supported $Requested } if (-not (Get-Command docker -ErrorAction SilentlyContinue)) { Stop-Install "Docker is not installed. Install Docker Desktop, then run this again." } if (-not (Test-Docker @("compose", "version"))) { Stop-Install "Docker Compose is not installed. Update Docker Desktop, then run this again." } if (-not (Test-Docker @("info"))) { Stop-Install "Docker is not running. Start Docker Desktop, then run this again." } New-Item -ItemType Directory -Force -Path $dir | Out-Null Set-Location $dir $envFile = Join-Path $dir "factorial.env" $composeFile = Join-Path $dir "compose.yaml" if ((Test-Docker @("volume", "inspect", "factorial-public")) -and -not (Test-Path $envFile)) { Stop-Install "A Factorial data volume already exists. Put the factorial.env it was made with in $dir, or remove the volume with docker volume rm factorial-public to start over." } [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 $download = [IO.Path]::GetTempFileName() try { Get-SiteFile "$site/version.txt" $download $newest = ([IO.File]::ReadAllText($download)).Trim() Get-SiteFile "$site/docs/run-the-public-image/compose.yaml" $download $template = [IO.File]::ReadAllText($download) } finally { Remove-Item -Force $download -ErrorAction SilentlyContinue } if (-not (Test-Version $newest)) { Stop-Install "$site/version.txt does not hold a version." } Assert-Supported $newest $image = Get-ImageMatch $template 1 if (-not $image) { Stop-Install "The compose.yaml template names no image." } $installed = "" if (Test-Path $composeFile) { $installed = Get-ImageMatch ([IO.File]::ReadAllText($composeFile)) 2 } if ($installed -and -not (Test-Version $installed)) { $installed = "" } if ($installed -and ([version]$installed -lt [version]$MinVersion)) { Stop-Install "$dir holds an install made before $MinVersion, which this script does not upgrade. Run docker compose down in $dir, move the folder away (its factorial.env opens the old factorial volume), then run this again." } $chosen = $Requested if ($chosen -and $installed -and ([version]$installed -gt [version]$chosen) -and -not $Override) { Stop-Install "Factorial $installed is installed, and $chosen is older. The database is not migrated back, so going back means restoring a backup from $dir\backups made before the upgrade. To start $chosen on this data anyway, add -Force." } if (-not $chosen -and $installed) { $chosen = $installed if ([version]$newest -gt [version]$installed) { $answer = $null try { $answer = Read-Host "Factorial $installed is installed and $newest is available. Upgrade now? A backup of the data is made first. [y/N]" } catch { $answer = $null } if ($answer -match '^(y|yes)$') { $chosen = $newest } else { Write-Host "Factorial $installed is installed and $newest is available. $installed stays. To upgrade, run this again from a terminal, or run: & ([scriptblock]::Create((irm $site/install.ps1))) -Version $newest" } } } if (-not $chosen) { $chosen = $newest } $first = -not (Test-Path $envFile) if ($chosen -ne $installed) { Invoke-Docker @("pull", "--platform", "linux/amd64", "${image}:$chosen") "Cannot pull ${image}:$chosen. Nothing changed. Check the network, then run this again." } if ($first) { Write-Host "Making the keys in $envFile." Invoke-Docker @("run", "--rm", "--platform", "linux/amd64", "-v", "${dir}:/out", "--entrypoint", "factorial-setup", "${image}:$chosen") -DropOutput } if ($installed -and $chosen -ne $installed) { $stamp = (Get-Date).ToUniversalTime().ToString("yyyyMMdd'T'HHmmss'Z'") $backup = "factorial-$installed-$stamp.tgz" $backups = Join-Path $dir "backups" Write-Host "Stopping Factorial $installed, and saving a backup of the data to $backups\$backup." Invoke-Docker @("compose", "down") New-Item -ItemType Directory -Force -Path $backups | Out-Null Invoke-Docker @("run", "--rm", "--platform", "linux/amd64", "--entrypoint", "tar", "-v", "factorial-public:/data", "-v", "${backups}:/backup", "${image}:$installed", "-czf", "/backup/$backup", "-C", "/data", ".") "The backup failed, so Factorial stays on $installed, stopped. Run docker compose up -d in $dir to start $installed again." } if ($chosen -ne $installed) { $compose = [regex]::Replace($template, '(?m)^([ \t]*image:[ \t]*\S+:)[^:\s]+', '${1}' + $chosen) [IO.File]::WriteAllText($composeFile, $compose, [Text.UTF8Encoding]::new($false)) } Write-Host "" Write-Host "Factorial $chosen is installed in $dir." if ($installed -and $chosen -ne $installed) { Write-Host "The stack is stopped until you start it." } Write-Host "To start it, run: Set-Location `$HOME\factorial; docker compose up -d --wait" Write-Host "Then open http://localhost:8000/factorial/" if ($first) { Write-Host "There, push Create an account. The first account becomes the owner." } } Push-Location try { Install-Factorial $Version $Force.IsPresent } catch { Write-Host $_.Exception.Message -ForegroundColor Red } finally { Pop-Location }