[
    {
        "UpdateStageName": "Create Installation Directory",
        "UpdateSourcePlatform": "All",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}mysql"
    },
    {
        "UpdateStageName": "Create Data Directory",
        "UpdateSourcePlatform": "All",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}data"
    },
    {
        "UpdateStageName": "Create Log Directory",
        "UpdateSourcePlatform": "All",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}log/mysql"
    },
    {
        "UpdateStageName": "Create Run Directory",
        "UpdateSourcePlatform": "Linux",
        "UpdateSource": "CreateDirectory",
        "UpdateSourceArgs": "{{$FullBaseDir}}run/mysqld"
    },
    {
        "UpdateStageName": "Download MySQL",
        "UpdateSourcePlatform": "Linux",
        "UpdateSourceArch": "x86_64",
        "UpdateSource": "Executable",
        "UpdateSourceData": "/bin/bash",
        "UpdateSourceArgs": "-c \"MySQLRelease=\\\"{{ServerVersion}}\\\"; MySQLVersion=\\\"{{CustomServerVersion}}\\\"; cd mysql && if [[ -x mysql/bin/mysqld ]]; then InstalledVersion=$(mysql/bin/mysqld -V | sed \\\"s/.*Ver \\([0-9][0-9]*\\.[0-9][0-9]*\\.[0-9][0-9]*\\).*/\\1/\\\"); else InstalledVersion=\\\"\\\"; fi; [[ -z \\\"$MySQLVersion\\\" ]] && MySQLVersion=$(wget -qO- \\\"https://endoflife.date/api/v1/products/mysql/releases/$MySQLRelease\\\" | jq -r \\\".result.latest.name\\\"); if [[ ! \\\"$MySQLVersion\\\" =~ ^(8|9)\\.[0-9]+\\.[0-9]+$ ]]; then echo \\\"Invalid MySQL version format specified\\\" && exit 1; elif [[ \\\"$InstalledVersion\\\" == \\\"$MySQLVersion\\\" ]]; then echo \\\"MySQL v$MySQLVersion already installed. Skipping\\\" && exit 0; else MySQLMinorVersion=\\\"${MySQLVersion%.*}\\\"; [[ -f mysql.tar.xz ]] && rm -f mysql.tar.xz >/dev/null 2>&1; wget -qO mysql.tar.xz https://cdn.mysql.com/Downloads/MySQL-$MySQLMinorVersion/mysql-$MySQLVersion-linux-glibc2.28-x86_64.tar.xz || { echo \\\"Download failed from the MySQL CDN. This is a fault with the CDN, not AMP. Aborting\\\"; exit 1; }; { [[ -d mysql/lib/plugin ]] && mv mysql/lib/plugin plugin-bak || true; } && mkdir -p mysql && rm -rf mysql/* >/dev/null 2>&1 && mkdir -p mysql/lib && { [[ -d plugin-bak ]] && mv plugin-bak mysql/lib/plugin || true; } && tar -xf mysql.tar.xz -C mysql --strip-components=1 >/dev/null 2>&1 && rm -f mysql.tar.xz >/dev/null 2>&1 && echo \\\"MySQL v$MySQLVersion downloaded\\\"; fi\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Download MySQL",
        "UpdateSourcePlatform": "Linux",
        "UpdateSourceArch": "aarch64",
        "UpdateSource": "Executable",
        "UpdateSourceData": "/bin/bash",
        "UpdateSourceArgs": "-c \"MySQLRelease=\\\"{{ServerVersion}}\\\"; MySQLVersion=\\\"{{CustomServerVersion}}\\\"; cd mysql && if [[ -x mysql/bin/mysqld ]]; then InstalledVersion=$(mysql/bin/mysqld -V | sed \\\"s/.*Ver \\([0-9][0-9]*\\.[0-9][0-9]*\\.[0-9][0-9]*\\).*/\\1/\\\"); else InstalledVersion=\\\"\\\"; fi; if [[ -z \\\"$MySQLVersion\\\" ]]; then MySQLVersion=$(wget -qO- \\\"https://endoflife.date/api/v1/products/mysql/releases/$MySQLRelease\\\" | jq -r \\\".result.latest.name\\\"); fi; if [[ ! \\\"$MySQLVersion\\\" =~ ^(8|9)\\.[0-9]+\\.[0-9]+$ ]]; then echo \\\"Invalid MySQL version format specified\\\" && exit 1; elif [[ \\\"$InstalledVersion\\\" == \\\"$MySQLVersion\\\" ]]; then echo \\\"MySQL v$MySQLVersion already installed. Skipping\\\" && exit 0; else MySQLMinorVersion=\\\"${MySQLVersion%.*}\\\"; [[ -f mysql.tar.xz ]] && rm -f mysql.tar.xz >/dev/null 2>&1; wget -qO mysql.tar.xz https://cdn.mysql.com/Downloads/MySQL-$MySQLMinorVersion/mysql-$MySQLVersion-linux-glibc2.28-aarch64.tar.xz || { echo \\\"Download failed from the MySQL CDN. This is a fault with the CDN, not AMP. Aborting\\\"; exit 1; }; { [[ -d mysql/lib/plugin ]] && mv mysql/lib/plugin plugin-bak || true; } && mkdir -p mysql && rm -rf mysql/* >/dev/null 2>&1 && mkdir -p mysql/lib && { [[ -d plugin-bak ]] && mv plugin-bak mysql/lib/plugin || true; } && tar -xf mysql.tar.xz -C mysql --strip-components=1 >/dev/null 2>&1 && rm -f mysql.tar.xz >/dev/null 2>&1 && echo \\\"MySQL v$MySQLVersion downloaded\\\"; fi\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Download MySQL",
        "UpdateSourcePlatform": "Windows",
        "UpdateSource": "Executable",
        "UpdateSourceData": "powershell.exe",
        "UpdateSourceArgs": "-NoProfile -Command \"$ProgressPreference='SilentlyContinue'; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $MySQLRelease='{{ServerVersion}}'; $MySQLVersion='{{CustomServerVersion}}'; Set-Location -Path 'mysql'; if (Test-Path 'mysql/bin/mysqld.exe') { $match = & mysql/bin/mysqld.exe -V | Select-String -Pattern 'Ver (\\d+\\.\\d+\\.\\d+)'; $InstalledVersion = $match.Matches[0].Groups[1].Value } else { $InstalledVersion = '' }; if ( [string]::IsNullOrWhiteSpace($MySQLVersion) ) { $MySQLVersion = (Invoke-RestMethod -UseBasicParsing -Uri \\\"https://endoflife.date/api/v1/products/mysql/releases/$MySQLRelease\\\").result.latest.name }; if ( $MySQLVersion -notmatch '^(8|9)\\.\\d+\\.\\d+$' ) { Write-Output 'Invalid MySQL version format specified'; exit 1 } elseif ( $InstalledVersion -eq $MySQLVersion ) { Write-Output \\\"MySQL v$MySQLVersion already installed. Skipping\\\"; exit 0 } else { $splitVersion = $MySQLVersion -split '\\.'; $MySQLMinorVersion = \\\"$($splitVersion[0]).$($splitVersion[1])\\\"; if ( Test-Path mysql.zip ) { Remove-Item mysql.zip -Force -ErrorAction SilentlyContinue *> $null }; try { Invoke-WebRequest -UseBasicParsing -Uri https://cdn.mysql.com/Downloads/MySQL-$MySQLMinorVersion/mysql-$MySQLVersion-winx64.zip -OutFile mysql.zip -ErrorAction Stop } catch { Write-Output \\\"Download failed from the MySQL CDN. This is a fault with the CDN, not AMP. Aborting\\\"; exit 1; }; Expand-Archive -Path \\\"mysql.zip\\\" -DestinationPath \\\".\\\" -Force *> $null; if ( Test-Path mysql.zip ) { Remove-Item mysql.zip -Force -ErrorAction SilentlyContinue *> $null }; if (Test-Path \\\"mysql-*\\\") { if ( Test-Path 'mysql/lib/plugin' ) { Move-Item 'mysql/lib/plugin' 'plugin-bak' -Force *> $null }; Remove-Item mysql/* -Recurse -Force -ErrorAction SilentlyContinue *> $null; New-Item 'mysql/lib' -ItemType Directory -Force | Out-Null; Move-Item 'plugin-bak' 'mysql/lib/plugin' -Force *> $null; $source = Get-ChildItem -Directory -Filter 'mysql-*' | Select-Object -First 1; if ($source) { robocopy $source.FullName 'mysql' /E /MOVE /NFL /NDL /NJH /NJS /NP *> $null; if ($LASTEXITCODE -gt 7) { exit $LASTEXITCODE } }; Write-Output \\\"MySQL v$MySQLVersion downloaded\\\" } }\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Initialize MySQL",
        "UpdateSourcePlatform": "Linux",
        "UpdateSource": "Executable",
        "UpdateSourceData": "/bin/bash",
        "UpdateSourceArgs": "-c \"cd mysql && { [[ -f \\\"{{$FullBaseDir}}data/mysql.ibd\\\" ]] || { mysql/bin/mysqld --initialize-insecure --basedir=\\\"{{$FullBaseDir}}mysql\\\" --datadir=\\\"{{$FullBaseDir}}data\\\" >/dev/null && echo -e \\\"\\nDefault database tables created\\nSuper-users amp@localhost and root@localhost created with unix socket authentication\\nUse AMP's console to manage the server via the amp@localhost user\\n\\\"; }; }\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Initialize MySQL",
        "UpdateSourcePlatform": "Windows",
        "UpdateSource": "Executable",
        "UpdateSourceData": "powershell.exe",
        "UpdateSourceArgs": "-NoProfile -Command \"Set-Location -Path 'mysql'; if (Test-Path '{{$FullBaseDir}}data/mysql.ibd') { exit 0 } else { & mysql/bin/mysqld.exe --initialize-insecure --basedir=\\\"{{$FullBaseDir}}mysql\\\" --datadir=\\\"{{$FullBaseDir}}data\\\" 1> $null; if ($LASTEXITCODE -eq 0) { Write-Output \\\"`nDefault database tables created`nSuper-user root@localhost created with secure password authentication`nUse AMP's console to manage the server via the root@localhost user`n\\\" } }\"",
        "SkipOnFailure": false
    },
    {
        "UpdateStageName": "Create my.cnf",
        "UpdateSourcePlatform": "All",
        "UpdateSource": "CreateFile",
        "UpdateSourceData": "",
        "UpdateSourceArgs": "{{$FullBaseDir}}my.cnf",
        "OverwriteExistingFiles": false
    }
]
