[
    {
        "UpdateStageName": "Create MongoDB Installation Directory",
        "UpdateSourcePlatform": "All",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}mongodb"
    },
    {
        "UpdateStageName": "Create MongoDB Shell Installation Directory",
        "UpdateSourcePlatform": "All",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}mongosh"
    },
    {
        "UpdateStageName": "Create Data Directory",
        "UpdateSourcePlatform": "All",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}data"
    },
    {
        "UpdateStageName": "Create Log Directory",
        "UpdateSourcePlatform": "All",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}log/mongodb"
    },
    {
        "UpdateStageName": "Create Run Directory",
        "UpdateSourcePlatform": "Linux",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}run"
    },
    {
        "UpdateStageName": "Download MongoDB",
        "UpdateSourcePlatform": "Linux",
        "UpdateSourceArch": "x86_64",
        "UpdateSource": "Executable",
        "UpdateSourceData": "/bin/bash",
        "UpdateSourceArgs": "-c \"MongoDBRelease=\\\"{{ServerVersion}}\\\"; MongoDBVersion=\\\"{{CustomServerVersion}}\\\"; cd mongodb && if [[ -x mongodb/bin/mongod ]]; then InstalledVersion=$(mongodb/bin/mongod --version | grep \\\"^db version v\\\" | sed \\\"s/^db version v\\([0-9][0-9]*\\.[0-9][0-9]*\\.[0-9][0-9]*\\)/\\1/\\\"); else InstalledVersion=\\\"\\\"; fi; [[ -z \\\"$MongoDBVersion\\\" ]] && MongoDBVersion=$(wget -qO- \\\"https://endoflife.date/api/v1/products/mongodb/releases/$MongoDBRelease\\\" | jq -r \\\".result.latest.name\\\"); if [[ ! \\\"$MongoDBVersion\\\" =~ ^(7|8)\\.[0-9]+\\.[0-9]+$ ]]; then echo \\\"Invalid MongoDB version format specified\\\" && exit 1; elif [[ \\\"$InstalledVersion\\\" == \\\"$MongoDBVersion\\\" ]]; then echo \\\"MongoDB v$MongoDBVersion already installed. Skipping\\\" && exit 0; else [[ -f mongodb.tar.gz ]] && rm -f mongodb.tar.gz >/dev/null 2>&1; wget -qO mongodb.tar.gz https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2404-$MongoDBVersion.tgz || { echo \\\"Download failed from the MongoDB CDN. This is a fault with the CDN, not AMP. Aborting\\\"; exit 1; }; mkdir -p mongodb && rm -rf mongodb/* >/dev/null 2>&1 && tar -xzf mongodb.tar.gz -C mongodb --strip-components=1 >/dev/null 2>&1 && rm -f mongodb.tar.gz >/dev/null 2>&1 && echo \\\"MongoDB v$MongoDBVersion downloaded\\\"; fi\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Download MongoDB",
        "UpdateSourcePlatform": "Linux",
        "UpdateSourceArch": "aarch64",
        "UpdateSource": "Executable",
        "UpdateSourceData": "/bin/bash",
        "UpdateSourceArgs": "-c \"MongoDBRelease=\\\"{{ServerVersion}}\\\"; MongoDBVersion=\\\"{{CustomServerVersion}}\\\"; cd mongodb && if [[ -x mongodb/bin/mongod ]]; then InstalledVersion=$(mongodb/bin/mongod --version | grep \\\"^db version v\\\" | sed \\\"s/^db version v\\([0-9][0-9]*\\.[0-9][0-9]*\\.[0-9][0-9]*\\)/\\1/\\\"); else InstalledVersion=\\\"\\\"; fi; [[ -z \\\"$MongoDBVersion\\\" ]] && MongoDBVersion=$(wget -qO- \\\"https://endoflife.date/api/v1/products/mongodb/releases/$MongoDBRelease\\\" | jq -r \\\".result.latest.name\\\"); if [[ ! \\\"$MongoDBVersion\\\" =~ ^(7|8)\\.[0-9]+\\.[0-9]+$ ]]; then echo \\\"Invalid MongoDB version format specified\\\" && exit 1; elif [[ \\\"$InstalledVersion\\\" == \\\"$MongoDBVersion\\\" ]]; then echo \\\"MongoDB v$MongoDBVersion already installed. Skipping\\\" && exit 0; else [[ -f mongodb.tar.gz ]] && rm -f mongodb.tar.gz >/dev/null 2>&1; wget -qO mongodb.tar.gz https://fastdl.mongodb.org/linux/mongodb-linux-aarch64-ubuntu2404-$MongoDBVersion.tgz || { echo \\\"Download failed from the MongoDB CDN. This is a fault with the CDN, not AMP. Aborting\\\"; exit 1; }; mkdir -p mongodb && rm -rf mongodb/* >/dev/null 2>&1 && tar -xzf mongodb.tar.gz -C mongodb --strip-components=1 >/dev/null 2>&1 && rm -f mongodb.tar.gz >/dev/null 2>&1 && echo \\\"MongoDB v$MongoDBVersion downloaded\\\"; fi\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Download MongoDB",
        "UpdateSourcePlatform": "Windows",
        "UpdateSource": "Executable",
        "UpdateSourceData": "powershell.exe",
        "UpdateSourceArgs": "-NoProfile -Command \"$ProgressPreference='SilentlyContinue'; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $MongoDBRelease='{{ServerVersion}}'; $MongoDBVersion='{{CustomServerVersion}}'; Set-Location -Path 'mongodb'; if (Test-Path 'mongodb/bin/mongod.exe') { (& mongodb/bin/mongod.exe --version)[0] -match 'db version v(\\d+\\.\\d+\\.\\d+)' >$null; $InstalledVersion = $matches[1] } else { $InstalledVersion = '' }; if ( [string]::IsNullOrWhiteSpace($MongoDBVersion) ) { $MongoDBVersion = (Invoke-RestMethod -UseBasicParsing -Uri \\\"https://endoflife.date/api/v1/products/mongodb/releases/$MongoDBRelease\\\").result.latest.name }; if ( $MongoDBVersion -notmatch '^(6|7|8)\\.\\d+\\.\\d+$' ) { Write-Output 'Invalid MongoDB version format specified'; exit 1 } elseif ( $InstalledVersion -eq $MongoDBVersion ) { Write-Output \\\"MongoDB v$MongoDBVersion already installed. Skipping\\\"; exit 0 } else { if ( Test-Path mongodb.zip ) { Remove-Item mongodb.zip -Force -ErrorAction SilentlyContinue *> $null }; try { Invoke-WebRequest -UseBasicParsing -Uri https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-$MongoDBVersion.zip -OutFile mongodb.zip -ErrorAction Stop } catch { Write-Output \\\"Download failed from the MongoDB CDN. This is a fault with the CDN, not AMP. Aborting\\\"; exit 1; }; Expand-Archive -Path \\\"mongodb.zip\\\" -DestinationPath \\\".\\\" -Force *> $null; if ( Test-Path mongodb.zip ) { Remove-Item mongodb.zip -Force -ErrorAction SilentlyContinue *> $null }; if (Test-Path \\\"mongodb-*\\\") { Remove-Item -Recurse -Force mongodb -ErrorAction SilentlyContinue *> $null; Get-ChildItem -Directory | Where-Object { $_.Name -like \\\"mongodb-*\\\" } | Rename-Item -NewName \\\"mongodb\\\" *> $null; Write-Output \\\"MongoDB v$MongoDBVersion downloaded\\\" } }\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Download MongoDB Shell",
        "UpdateSourcePlatform": "Linux",
        "UpdateSourceArch": "x86_64",
        "UpdateSource": "Executable",
        "UpdateSourceData": "/bin/bash",
        "UpdateSourceArgs": "-c \"cd mongodb && if [[ -x mongosh/bin/mongosh ]]; then InstalledVersion=$(mongosh/bin/mongosh --version); else InstalledVersion=\\\"\\\"; fi; LatestVersion=$(wget -qO- \\\"https://api.github.com/repos/mongodb-js/mongosh/releases/latest\\\" | jq -r \\\".name\\\") && if [[ \\\"$InstalledVersion\\\" == \\\"$LatestVersion\\\" ]]; then echo \\\"MongoDB Shell v$LatestVersion already installed. Skipping\\\" && exit 0; else [[ -f mongosh.tar.gz ]] && rm -f mongosh.tar.gz >/dev/null 2>&1; wget -qO mongosh.tar.gz https://downloads.mongodb.com/compass/mongosh-$LatestVersion-linux-x64-openssl3.tgz || { echo \\\"Download failed from the MongoDB CDN. This is a fault with the CDN, not AMP. Aborting\\\"; exit 1; }; mkdir -p mongosh && rm -rf mongosh/* >/dev/null 2>&1 && tar -xzf mongosh.tar.gz -C mongosh --strip-components=1 >/dev/null 2>&1 && rm -f mongosh.tar.gz >/dev/null 2>&1 && echo \\\"MongoDB Shell v$LatestVersion downloaded\\\"; fi\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Download MongoDB Shell",
        "UpdateSourcePlatform": "Linux",
        "UpdateSourceArch": "aarch64",
        "UpdateSource": "Executable",
        "UpdateSourceData": "/bin/bash",
        "UpdateSourceArgs": "-c \"cd mongodb && if [[ -x mongosh/bin/mongosh ]]; then InstalledVersion=$(mongosh/bin/mongosh --version); else InstalledVersion=\\\"\\\"; fi; LatestVersion=$(wget -qO- \\\"https://api.github.com/repos/mongodb-js/mongosh/releases/latest\\\" | jq -r \\\".name\\\") && if [[ \\\"$InstalledVersion\\\" == \\\"$LatestVersion\\\" ]]; then echo \\\"MongoDB Shell v$LatestVersion already installed. Skipping\\\" && exit 0; else [[ -f mongosh.tar.gz ]] && rm -f mongosh.tar.gz >/dev/null 2>&1; wget -qO mongosh.tar.gz https://downloads.mongodb.com/compass/mongosh-$LatestVersion-linux-arm64-openssl3.tgz || { echo \\\"Download failed from the MongoDB CDN. This is a fault with the CDN, not AMP. Aborting\\\"; exit 1; }; mkdir -p mongosh && rm -rf mongosh/* >/dev/null 2>&1 && tar -xzf mongosh.tar.gz -C mongosh --strip-components=1 >/dev/null 2>&1 && rm -f mongosh.tar.gz >/dev/null 2>&1 && echo \\\"MongoDB Shell v$LatestVersion downloaded\\\"; fi\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Download MongoDB Shell",
        "UpdateSourcePlatform": "Windows",
        "UpdateSource": "Executable",
        "UpdateSourceData": "powershell.exe",
        "UpdateSourceArgs": "-NoProfile -Command \"$ProgressPreference='SilentlyContinue'; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Set-Location -Path 'mongodb'; if (Test-Path 'mongosh/bin/mongosh.exe') { $InstalledVersion = & mongosh/bin/mongosh.exe --version } else { $InstalledVersion = '' }; $LatestVersion = (Invoke-RestMethod -UseBasicParsing -Uri \\\"https://api.github.com/repos/mongodb-js/mongosh/releases/latest\\\").name; if ( $InstalledVersion -eq $LatestVersion ) { Write-Output \\\"MongoDB Shell v$LatestVersion already installed. Skipping\\\"; exit 0 } else { if ( Test-Path mongosh.zip ) { Remove-Item mongosh.zip -Force -ErrorAction SilentlyContinue *> $null }; try { Invoke-WebRequest -UseBasicParsing -Uri https://downloads.mongodb.com/compass/mongosh-$LatestVersion-win32-x64.zip -OutFile mongosh.zip -ErrorAction Stop } catch { Write-Output \\\"Download failed from the MongoDB CDN. This is a fault with the CDN, not AMP. Aborting\\\"; exit 1; }; Expand-Archive -Path \\\"mongosh.zip\\\" -DestinationPath \\\".\\\" -Force *> $null; if ( Test-Path mongosh.zip ) { Remove-Item mongosh.zip -Force -ErrorAction SilentlyContinue *> $null }; if (Test-Path \\\"mongosh-*\\\") { Remove-Item -Recurse -Force mongosh -ErrorAction SilentlyContinue *> $null; Get-ChildItem -Directory | Where-Object { $_.Name -like \\\"mongosh-*\\\" } | Rename-Item -NewName \\\"mongosh\\\" *> $null; Write-Output \\\"MongoDB Shell v$LatestVersion downloaded\\\" } }\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Set Up MongoDB",
        "UpdateSourcePlatform": "Linux",
        "UpdateSource": "Executable",
        "UpdateSourceData": "/bin/bash",
        "UpdateSourceArgs": "-c \"cd mongodb && export PATH=\\\"{{$FullBaseDir}}mongodb/bin:{{$FullBaseDir}}mongosh/bin:$PATH\\\"; mongosh/bin/mongosh --host 127.0.0.1 --port {{$ServerPort}} --quiet --eval \\\"db.runCommand({ ping: 1 })\\\" >/dev/null 2>&1 && exit 0; mongodb/bin/mongod --dbpath {{$FullBaseDir}}data --port {{$ServerPort}} --bind_ip 127.0.0.1 --unixSocketPrefix {{$FullBaseDir}}run --logpath {{$FullBaseDir}}log/mongodb/mongod.log --logappend --fork >/dev/null 2>&1; timeout=60; while ((timeout--)); do mongosh/bin/mongosh --host 127.0.0.1 --port {{$ServerPort}} --quiet --eval \\\"db.runCommand({ ping: 1 })\\\" >/dev/null 2>&1 && break || sleep 1; done; if ((timeout < 0)); then echo \\\"MongoDB server failed to start for setup. Aborting\\\"; exit 1; fi; mongosh/bin/mongosh \\\"mongodb://127.0.0.1:{{$ServerPort}}/admin\\\" --quiet --eval \\\"try { const adminDb = db.getSiblingDB(\\\\\\\"admin\\\\\\\"); if (!adminDb.getUser(\\\\\\\"admin\\\\\\\")) { adminDb.createUser({ user: \\\\\\\"admin\\\\\\\", pwd: \\\\\\\"{{AdminPassword}}\\\\\\\", roles: [\\\\\\\"root\\\\\\\"] }); print(\\\\\\\"Root user 'admin' created with secure password authentication\\\\\\\"); print(\\\\\\\"Use AMP's console to manage the server via the admin user\\\\\\\"); } try { if (db.getMongo().getDBNames().includes(\\\\\\\"test\\\\\\\")) { db.getSiblingDB(\\\\\\\"test\\\\\\\").dropDatabase(); void 0; } } catch (e) { } } catch (e) { if (e.codeName === \\\\\\\"Unauthorized\\\\\\\" || e.code === 13) { } else { print(\\\\\\\"Failed to connect to MongoDB: \\\\\\\" + e); quit(1); } }\\\"; mongodb/bin/mongod --shutdown --dbpath {{$FullBaseDir}}data >/dev/null 2>&1\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Set Up MongoDB",
        "UpdateSourcePlatform": "Windows",
        "UpdateSource": "Executable",
        "UpdateSourceData": "powershell.exe",
        "UpdateSourceArgs": "-NoProfile -Command \"Set-Location -Path 'mongodb'; $env:PATH = \\\"{{$FullBaseDir}}mongodb\\bin;{{$FullBaseDir}}mongosh\\bin;\\\" + $env:PATH; & mongosh/bin/mongosh.exe --host 127.0.0.1 --port {{$ServerPort}} --quiet --eval 'db.runCommand({ ping: 1 })' *> $null; if ($LASTEXITCODE -eq 0) { exit 0 }; Start-Process \\\"mongodb/bin/mongod.exe\\\" -ArgumentList \\\"--dbpath `\\\"{{$FullBaseDir}}data`\\\" --port {{$ServerPort}} --bind_ip 127.0.0.1 --logpath `\\\"{{$FullBaseDir}}log\\mongodb\\mongod.log`\\\" --logappend\\\" -RedirectStandardOutput \\\"NUL\\\" -WindowStyle Hidden; for ($i=0; $i -lt 60; $i++) { & mongosh/bin/mongosh.exe --host 127.0.0.1 --port {{$ServerPort}} --quiet --eval 'db.runCommand({ ping: 1 })' *> $null; if ($LASTEXITCODE -eq 0) { break }; Start-Sleep -Seconds 1 }; if ($i -eq 60) { Write-Output 'MongoDB server failed to start for setup. Aborting'; exit 1 }; & mongosh/bin/mongosh.exe \\\"mongodb://127.0.0.1:{{$ServerPort}}/admin\\\" --quiet --eval \\\"try { const adminDb = db.getSiblingDB('admin'); if (!adminDb.getUser('admin')) { adminDb.createUser({ user: 'admin', pwd: '{{AdminPassword}}', roles: ['root'] }); print('Root user admin created with secure password authentication'); print('Use the AMP console to manage the server via the admin user'); } try { if (db.getMongo().getDBNames().includes('test')) { db.getSiblingDB('test').dropDatabase(); void 0; } } catch (e) { } } catch (e) { if (e.codeName === 'Unauthorized' || e.code === 13) { } else { print('Failed to connect to MongoDB: ' + e); quit(1); } }\\\"; $shutdownOutput = (& mongosh/bin/mongosh.exe \\\"mongodb://127.0.0.1:{{$ServerPort}}/admin\\\" --username admin --password {{AdminPassword}} --authenticationDatabase admin --quiet --eval \\\"db.shutdownServer()\\\" 2>&1) -join \\\"`n\\\"; if ($shutdownOutput -match \\\"ECONNRESET\\\") { $global:LASTEXITCODE = 0 }\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Create mongod.conf",
        "UpdateSourcePlatform": "All",
        "UpdateSource": "CreateFile",
        "UpdateSourceData": "",
        "UpdateSourceArgs": "{{$FullBaseDir}}mongod.conf",
        "OverwriteExistingFiles": false
    }
]
