# Installs the trdrs back office on Windows and starts it, from PowerShell: # # irm https://trdrs.co/install/backoffice.ps1 | iex # # It does what install.sh does on a Mac or Linux. It downloads the release this script was published # with, checks it against its published checksum, writes an environment file with no secret in it and # starts the server on http://127.0.0.1:8787. It asks nothing. When the server starts it opens the # trdrs page that approves this back office; once the owner approves, it opens the back office itself, # connected, to choose a sign-in password and pick or create the venue. # # TRDRS_BACKOFFICE_DIR, PORT, TRDRS_ENVIRONMENT, TRDRS_API_URL and TRDRS_APP_ORIGIN may be set in the # environment ($env:PORT = '8788'), TRDRS_NO_START=1 installs without starting and TRDRS_NO_BROWSER=1 # prints the addresses without opening a browser. Production needs TRDRS_ENVIRONMENT=production and an # explicit TRDRS_API_URL. # # Running it again in the same place installs the newer release beside the old one and keeps the # existing environment file and data directory, so an update is the same command. The whole script # sits in one function called on its last line, so a download cut short runs nothing. It runs in the # PowerShell that piped it, so it never changes that session's folder and never exits it: a failure # stops the script with its reason. function Install-TrdrsBackoffice { $ErrorActionPreference = 'Stop' # Windows PowerShell draws a progress bar per downloaded block, which makes a download crawl. $ProgressPreference = 'SilentlyContinue' $version = '0.3.29' function Say([string]$text) { [Console]::Error.WriteLine($text) } # A thrown error stops the whole pasted line, and PowerShell prints its reason. function Fail([string]$text) { throw "trdrs back office: $text" } function Setting([string]$name, [string]$fallback) { $value = [Environment]::GetEnvironmentVariable($name) if ($value) { $value } else { $fallback } } function Need([string]$name) { if (-not (Get-Command $name -CommandType Application -ErrorAction SilentlyContinue)) { Fail "$name is required but not installed." } } # Only this user may read what the path holds: inherited access is removed and the user alone is # granted it. A user profile is already closed to other users, but the installation may sit in any # folder, such as one under C:\ that every user of the machine can read. function Restrict([string]$path, [string]$grant) { if ($onWindows) { $user = [Security.Principal.WindowsIdentity]::GetCurrent().User.Value & icacls $path /inheritance:r /grant:r "*${user}:$grant" | Out-Null if ($LASTEXITCODE -ne 0) { Fail "Could not make $path readable only by you." } } else { # PowerShell on a Mac or Linux closes it with the modes the shell installer gives it. & chmod $(if ($grant -eq 'F') { '600' } else { '700' }) $path if ($LASTEXITCODE -ne 0) { Fail "Could not make $path readable only by you." } } } $onWindows = [Environment]::OSVersion.Platform -eq [PlatformID]::Win32NT # Windows PowerShell 5.1 on an older .NET offers TLS 1.0 alone unless asked for 1.2. [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 # The web image rewrites this to the host that served the script, so the sandbox copy downloads the # sandbox release and the production copy the production one. $downloads = Setting 'TRDRS_DOWNLOAD_URL' 'https://trdrs.co/downloads' $environment = Setting 'TRDRS_ENVIRONMENT' 'sandbox' $dir = Setting 'TRDRS_BACKOFFICE_DIR' 'trdrs-backoffice' $port = Setting 'PORT' '8787' switch -CaseSensitive ($environment) { 'sandbox' { $api = Setting 'TRDRS_API_URL' 'https://sandbox.trdrs.co' $appOrigin = Setting 'TRDRS_APP_ORIGIN' 'https://sandbox.trdrs.co' } 'production' { if (-not $env:TRDRS_API_URL) { Fail 'Production needs TRDRS_API_URL set to the production API origin.' } $api = $env:TRDRS_API_URL $appOrigin = Setting 'TRDRS_APP_ORIGIN' 'https://app.trdrs.co' } default { Fail 'TRDRS_ENVIRONMENT must be sandbox or production.' } } Need tar; Need node $nodeMajor = [int](& node -p 'parseInt(process.versions.node)') if ($nodeMajor -lt 22) { Fail "The back office needs Node.js 22 or newer; this machine has $(& node -v)." } Say "Installing trdrs back office $version ($environment) into $dir" $installation = (New-Item -ItemType Directory -Force -Path $dir).FullName $release = "trdrs-backoffice-$version" $releaseDir = Join-Path $installation $release if (Test-Path -LiteralPath $releaseDir -PathType Container) { Say "Release $version is already unpacked here; keeping it." } else { $scratch = Join-Path ([IO.Path]::GetTempPath()) "trdrs-backoffice-$([Guid]::NewGuid().ToString('N'))" New-Item -ItemType Directory -Path $scratch | Out-Null try { $archive = Join-Path $scratch "$release.tgz" $checksum = "$archive.sha256" try { Invoke-WebRequest -UseBasicParsing -Uri "$downloads/$release.tgz" -OutFile $archive } catch { Fail "Could not download $downloads/$release.tgz" } try { Invoke-WebRequest -UseBasicParsing -Uri "$downloads/$release.tgz.sha256" -OutFile $checksum } catch { Fail 'Could not download the checksum.' } $expected = ([IO.File]::ReadAllText($checksum).Trim() -split '\s+')[0] $actual = (Get-FileHash -Algorithm SHA256 -LiteralPath $archive).Hash if (-not $expected -or $actual -ne $expected) { Fail 'The download does not match its published checksum. Nothing was installed.' } & tar -xzf $archive -C $installation if ($LASTEXITCODE -ne 0) { Fail "Could not unpack $release.tgz." } } finally { Remove-Item -LiteralPath $scratch -Recurse -Force -ErrorAction SilentlyContinue } Say "Downloaded and verified $release.tgz" } # The data directory sits beside the releases, so an update keeps the selected venue. # Only this user may read the installation's settings. $dataDir = Join-Path $installation 'data' New-Item -ItemType Directory -Force -Path $dataDir | Out-Null Restrict $dataDir '(OI)(CI)F' # Written as UTF-8 with no byte order mark and LF line ends, which Node's environment file reader # takes as written. The data directory is written with forward slashes, which Node takes on # Windows, so no backslash in it reads as an escape inside the double quotes. $envFile = Join-Path $installation '.env' $utf8 = New-Object Text.UTF8Encoding $false if (Test-Path -LiteralPath $envFile) { Say 'Keeping the existing .env.' } else { $lines = @( "TRDRS_ENVIRONMENT=$environment" "TRDRS_API_URL=$api" "TRDRS_APP_ORIGIN=$appOrigin" 'HOST=127.0.0.1' "PORT=$port" "TRDRS_DATA_DIR=`"$($dataDir.Replace('\', '/'))`"" 'TRDRS_SECURE_COOKIE=false' 'TRDRS_SESSION_HOURS=12' ) [IO.File]::WriteAllText($envFile, (($lines -join "`n") + "`n"), $utf8) Restrict $envFile 'F' Say "Wrote .env. Venue selection is kept in $dataDir, readable only by you." } # current names the release the launcher runs. An update made from inside the back office points it # at the release it installs and restarts into that one; running this script points it here. [IO.File]::WriteAllText((Join-Path $installation 'current'), "$release`r`n", $utf8) # start.cmd runs from cmd, PowerShell or a double click, where a PowerShell script would need its # execution policy changed first. It starts the launcher of the release current names. $start = Join-Path $installation 'start.cmd' [IO.File]::WriteAllText($start, "@echo off`r`ncd /d `"%~dp0`"`r`nset /p release=