[
    {
        "UpdateStageName": "Create Installation Directory",
        "UpdateSourcePlatform": "Linux",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}pgsql"
    },
    {
        "UpdateStageName": "Create Data Directory",
        "UpdateSourcePlatform": "All",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}data"
    },
    {
        "UpdateStageName": "Create Run Directory",
        "UpdateSourcePlatform": "Linux",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}run"
    },
    {
        "UpdateStageName": "Install PostgreSQL",
        "UpdateSourcePlatform": "Linux",
        "UpdateSource": "Executable",
        "UpdateSourceData": "/bin/bash",
        "UpdateSourceArgs": "-c \"PostgreSQLRelease=\\\"{{ServerVersion}}\\\"; PostgreSQLVersion=\\\"{{CustomServerVersion}}\\\"; cd postgresql && if [[ -x pgsql/bin/postgres ]]; then InstalledVersion=$(pgsql/bin/postgres -V | sed \\\"s/.*(PostgreSQL) \\([0-9][0-9]*\\.[0-9][0-9]*\\).*/\\1/\\\"); else InstalledVersion=\\\"\\\"; fi; [[ -z \\\"$PostgreSQLVersion\\\" ]] && { LatestMinor=$(wget -qO- https://www.postgresql.org/versions.json | jq -r \\\".[] | select(.major == \\\\\\\"$PostgreSQLRelease\\\\\\\") | .latestMinor\\\") && PostgreSQLVersion=\\\"$PostgreSQLRelease.${LatestMinor}\\\"; }; if [[ ! \\\"$PostgreSQLVersion\\\" =~ ^(13|14|15|16|17|18)\\.[0-9]+$ ]]; then echo \\\"Invalid PostgreSQL version format specified\\\" && exit 1; elif [[ \\\"$InstalledVersion\\\" == \\\"$PostgreSQLVersion\\\" ]]; then echo \\\"PostgreSQL v$PostgreSQLVersion already installed. Skipping\\\" && exit 0; else [[ -f postgresql.tar.gz ]] && rm -f postgresql.tar.gz >/dev/null 2>&1; mkdir -p build >/dev/null; cd build && rm -rf ./* >/dev/null 2>&1 && wget -qO postgresql.tar.gz https://ftp.postgresql.org/pub/source/v$PostgreSQLVersion/postgresql-$PostgreSQLVersion.tar.gz || { echo \\\"Download failed from the PostgreSQL CDN. This is a fault with the CDN, not AMP. Aborting\\\"; cd .. && rm -rf build >/dev/null 2>&1; exit 1; }; tar -xzf postgresql.tar.gz --strip-components=1 >/dev/null 2>&1 && rm -f postgresql.tar.gz >/dev/null 2>&1 && echo \\\"PostgreSQL v$PostgreSQLVersion downloaded\\\" && echo \\\"Building PostgreSQL from source. This will take a while\\\" && { [[ -d ../pgsql ]] && mv -f ../pgsql ../pgsql-bak >/dev/null 2>&1 || true; } && mkdir -p ../pgsql >/dev/null 2>&1 && { ./configure --prefix=\\\"{{$FullBaseDir}}pgsql\\\" >/dev/null && make world-bin -j4 >/dev/null && make install-world-bin >/dev/null && cd .. && rm -rf build pgsql-bak >/dev/null 2>&1 && echo \\\"Done\\\"; } || { cd .. && rm -rf build pgsql >/dev/null 2>&1 && { [[ -d pgsql-bak ]] && mv -f pgsql-bak pgsql >/dev/null 2>&1 || true; } && echo \\\"Building PostgreSQL failed. Aborting\\\"; exit 1; }; fi\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Download PostgreSQL",
        "UpdateSourcePlatform": "Windows",
        "UpdateSource": "Executable",
        "UpdateSourceData": "powershell.exe",
        "UpdateSourceArgs": "-NoProfile -Command \"$ProgressPreference='SilentlyContinue'; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $PostgreSQLRelease='{{ServerVersion}}'; $PostgreSQLVersion='{{CustomServerVersion}}'; Set-Location -Path 'postgresql'; if (Test-Path 'pgsql/bin/postgres.exe') { (& pgsql/bin/postgres.exe -V) -match '\\(PostgreSQL\\)\\s+(\\d+\\.\\d+)' >$null; $InstalledVersion = $matches[1] } else { $InstalledVersion = '' }; if ( [string]::IsNullOrWhiteSpace($PostgreSQLVersion) ) { $LatestMinor = ((Invoke-RestMethod -UseBasicParsing -Uri \\\"https://www.postgresql.org/versions.json\\\") | Where-Object { $_.major -eq $PostgreSQLRelease }).latestMinor; $PostgreSQLVersion=\\\"$PostgreSQLRelease.${LatestMinor}\\\" }; if ( $PostgreSQLVersion -notmatch '^(13|14|15|16|17|18)\\.\\d+$' ) { Write-Output 'Invalid PostgreSQL version format specified'; exit 1 } elseif ( $InstalledVersion -eq $PostgreSQLVersion ) { Write-Output \\\"PostgreSQL v$PostgreSQLVersion already installed. Skipping\\\"; exit 0 } else { if ( Test-Path postgresql.zip ) { Remove-Item postgresql.zip -Force -ErrorAction SilentlyContinue *> $null }; try { Invoke-WebRequest -UseBasicParsing -Uri https://get.enterprisedb.com/postgresql/postgresql-$PostgreSQLVersion-1-windows-x64-binaries.zip -OutFile postgresql.zip -ErrorAction Stop } catch { Write-Output \\\"Download failed from the PostgreSQL CDN. This is a fault with the CDN, not AMP. Aborting\\\"; exit 1; }; Remove-Item -Recurse -Force pgsql -ErrorAction SilentlyContinue *> $null; Expand-Archive -Path \\\"postgresql.zip\\\" -DestinationPath \\\".\\\" -Force *> $null; Remove-Item postgresql.zip -Force -ErrorAction SilentlyContinue *> $null; Write-Output \\\"PostgreSQL v$PostgreSQLVersion downloaded\\\" }\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Initialize PostgreSQL",
        "UpdateSourcePlatform": "Linux",
        "UpdateSource": "Executable",
        "UpdateSourceData": "/bin/bash",
        "UpdateSourceArgs": "-c \"cd postgresql && { [[ -f \\\"{{$FullBaseDir}}data/PG_VERSION\\\" ]] || { pgsql/bin/initdb --pgdata=\\\"{{$FullBaseDir}}data\\\" --auth=scram-sha-256 --username=amp --pwfile=<(echo \\\"{{AMPPassword}}\\\"); echo -e \\\"\\nDefault database cluster created, including postgres database\\nDo not delete the postgres database!\\nSuper-user amp created with secure password authentication\\nUse AMP's console to manage the server via the amp user\\n\\\"; }; } && { conf=\\\"{{$FullBaseDir}}data/pg_hba.conf\\\"; grep -Eq \\\"^local\\s+all\\s+amp\\s+peer\\\" \\\"$conf\\\" || echo \\\"local   all             amp                                     peer\\\" >> \\\"$conf\\\"; grep -Eq \\\"^local\\s+all\\s+all\\s+scram-sha-256\\\" \\\"$conf\\\" || echo \\\"local   all             all                                     scram-sha-256\\\" >> \\\"$conf\\\"; grep -Eq \\\"^host\\s+all\\s+all\\s+0\\.0\\.0\\.0/0\\s+scram-sha-256\\\" \\\"$conf\\\" || echo \\\"host    all             all             0.0.0.0/0               scram-sha-256\\\" >> \\\"$conf\\\"; grep -Eq \\\"^local\\s+all\\s+all\\s+md5\\\" \\\"$conf\\\" || echo \\\"local   all             all                                     md5\\\" >> \\\"$conf\\\"; grep -Eq \\\"^host\\s+all\\s+all\\s+0\\.0\\.0\\.0/0\\s+md5\\\" \\\"$conf\\\" || echo \\\"host    all             all             0.0.0.0/0               md5\\\" >> \\\"$conf\\\"; }\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Initialize PostgreSQL",
        "UpdateSourcePlatform": "Windows",
        "UpdateSource": "Executable",
        "UpdateSourceData": "powershell.exe",
        "UpdateSourceArgs": "-NoProfile -Command \"Set-Location -Path 'postgresql'; if (!(Test-Path -Path \\\"{{$FullBaseDir}}data/PG_VERSION\\\")) { Set-Content -Path \\\"$env:TEMP/amp_pw.txt\\\" -Value \\\"{{AMPPassword}}\\\"; pgsql/bin/initdb.exe --pgdata=\\\"{{$FullBaseDir}}data\\\" --auth=md5 --username=amp --pwfile=\\\"$env:TEMP/amp_pw.txt\\\"; Remove-Item -Path \\\"$env:TEMP/amp_pw.txt\\\" -Force; Write-Host \\\"`nDefault database cluster created, including postgres database`nDo not delete the postgres database!`nSuper-user amp created with secure password authentication`nUse AMP's console to manage the server via the amp user`n\\\" }; $conf = \\\"{{$FullBaseDir}}data/pg_hba.conf\\\"; if (-not (Select-String -Path $conf -Pattern '^\\s*local\\s+all\\s+amp\\s+127\\.0\\.0\\.1/32\\s+scram-sha-256')) { Add-Content -Path $conf -Value 'host    all             amp             127.0.0.1/32            scram-sha-256' }; if (-not (Select-String -Path $conf -Pattern '^\\s*local\\s+all\\s+all\\s+scram-sha-256')) { Add-Content -Path $conf -Value 'local   all             all                                     scram-sha-256' }; if (-not (Select-String -Path $conf -Pattern '^\\s*host\\s+all\\s+all\\s+0\\.0\\.0\\.0/0\\s+scram-sha-256')) { Add-Content -Path $conf -Value 'host    all             all             0.0.0.0/0               scram-sha-256' }; if (-not (Select-String -Path $conf -Pattern '^\\s*local\\s+all\\s+amp\\s+127\\.0\\.0\\.1/32\\s+md5')) { Add-Content -Path $conf -Value 'host    all             amp             127.0.0.1/32            md5' }; if (-not (Select-String -Path $conf -Pattern '^\\s*local\\s+all\\s+all\\s+md5')) { Add-Content -Path $conf -Value 'local   all             all                                     md5' }; if (-not (Select-String -Path $conf -Pattern '^\\s*host\\s+all\\s+all\\s+0\\.0\\.0\\.0/0\\s+md5')) { Add-Content -Path $conf -Value 'host    all             all             0.0.0.0/0               md5' }\"",
        "SkipOnFailure": false
    }
]
