From da107337b43907b43bc88b51afe3e0080130186d Mon Sep 17 00:00:00 2001 From: Leonard Jonathan Oh Date: Tue, 7 Nov 2023 09:31:56 +0000 Subject: [PATCH] Refactor: Download binaries directly instead of using npx --- generate/definitions/VARIANTS.ps1 | 1 + generate/templates/Dockerfile.ps1 | 189 ++++++++++++++++++++++++++++-- test.ps1 | 1 - variants/1.0.3/Dockerfile | 30 +++-- 4 files changed, 202 insertions(+), 19 deletions(-) delete mode 100644 test.ps1 diff --git a/generate/definitions/VARIANTS.ps1 b/generate/definitions/VARIANTS.ps1 index 84153a3..dda8cd8 100644 --- a/generate/definitions/VARIANTS.ps1 +++ b/generate/definitions/VARIANTS.ps1 @@ -20,6 +20,7 @@ $VARIANTS = @( _metadata = @{ package_version = $variant['package_version'] platforms = 'linux/amd64' + # platforms = 'linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/s390x' components = $subVariant['components'] job_group_key = $variant['package_version'] } diff --git a/generate/templates/Dockerfile.ps1 b/generate/templates/Dockerfile.ps1 index 99edfd8..2b74531 100644 --- a/generate/templates/Dockerfile.ps1 +++ b/generate/templates/Dockerfile.ps1 @@ -1,17 +1,188 @@ +function Generate-DownloadBinary ($o) { + Set-StrictMode -Version Latest + + if ($o['checksumsUrl']) { + Set-Checksums "$( $o['binary'] )-$( $o['version'] )" $o['checksumsUrl'] + }else { + $release = Invoke-RestMethod "https://api.github.com/repos/$( $o['repository'] )/releases/tags/v1.0.3" + $releaseAssetsFiles = $release.assets | ? { $_.name -match [regex]::Escape($o['binary']) -and $_.name -notmatch '\.sha\d+$' } + $files = [ordered]@{} + foreach ($f in $releaseAssetsFiles ) { + $sha = & { + $shaF = $release.assets | ? { $_.name -eq "$( $f.name ).sha256" -or $_ -eq "$( $f.name ).sha512" } + $r = Invoke-WebRequest $shaF.browser_download_url + $c = if ($r.headers['Content-Type'] -eq 'text/plain') { $r.Content } else { [System.Text.Encoding]::UTF8.GetString($r.Content) } + $c = $c.Trim() -replace '^([a-fA-F0-9]+) .+', '$1' # The checksum is the first column + $c + } + $files[$f.name] = $sha + } + } + $shellVariable = "$( $o['binary'].ToUpper() -replace '[^A-Za-z0-9_]', '_' )_VERSION" +@" +# Install $( $o['binary'] ) +RUN set -eux; \ + $shellVariable=$( $o['version'] ); \ + case "`$( uname -m )" in \ + +"@ + + $o['architectures'] = if ($o.Contains('architectures')) { $o['architectures'] } else { 'linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/riscv64,linux/s390x' } + foreach ($a in ($o['architectures'] -split ',') ) { + $split = $a -split '/' + $os = $split[0] # E.g. 'linux' + $arch = $split[1] # E.g. 'amd64' + $archv = if ($split.Count -gt 2) { $split[2] } else { '' } # E.g. 'v6' or '' + switch ($a) { + "$os/386" { + $hardware = 'x86' + $regex = "$os[-_](i?$arch|x86(_64)?)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|(i?$arch|x86(_64)?)[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" + } + "$os/amd64" { + $hardware = 'x86_64' + $regex = "$os[-_]($arch|x86(_64)?)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|($arch|x86(_64)?)[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" + } + "$os/arm/v6" { + $hardware = 'armhf' + $regex = "$os[-_]($arch|arm)[-_]?($archv)?$( [regex]::Escape($o['archiveformat']) )$|($arch|arm)[-_]?($archv)?.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" + } + "$os/arm/v7" { + $hardware = 'armv7l' + $regex = "$os[-_]($arch|arm)[-_]?($archv)?$( [regex]::Escape($o['archiveformat']) )$|($arch|arm)[-_]?($archv)?.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" + } + "$os/arm64" { + $hardware = 'aarch64' + $regex = "$os[-_]($arch|aarch64)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|($arch|aarch64)[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" + } + "$os/ppc64le" { + $hardware = 'ppc64le' + $regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|$arch[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" + } + "$os/riscv64" { + $hardware = 'riscv64' + $regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|$arch[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" + } + "$os/s390x" { + $hardware = 's390x' + $regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|$arch[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" + } + default { + throw "Unsupported architecture: $a" + } + } + + $file = $files.Keys | ? { $_ -match $regex } | Select-Object -First 1 + if ($file) { + $url = if ($o['checksumsUrl']) { + Split-Path $o['checksumsUrl'] -Parent + }else { + "https://github.com/$( $o['repository'] )/releases/download/$( $o['version'] )" + } + $sha = $files[$file] +@" + '$hardware') \ + URL="$url/$file"; \ + SHA256=$sha; \ + ;; \ + +"@ + } + } + +@" + *) \ + echo "Architecture not supported"; \ + exit 1; \ + ;; \ + esac; \ + +"@ + +@" + FILE=$( $o['binary'] )$( $o['archiveformat'] ); \ + wget -q "`$URL" -O "`$FILE"; \ + echo "`$SHA256 `$FILE" | sha256sum -c -; \ + +"@ + + if ($o['archiveformat'] -match '\.tar\.gz|\.tgz') { + if ($o['archivefiles'].Count -gt 0) { +@" + tar -xvf "`$FILE" --no-same-owner --no-same-permissions -- $( $o['archivefiles'] -join ' ' ); \ + rm -f "`$FILE"; \ + +"@ + }else { +@" + tar -xvf "`$FILE" --no-same-owner --no-same-permissions; \ + rm -f "`$FILE"; \ + +"@ + } + }elseif ($o['archiveformat'] -match '\.bz2') { +@" + bzip2 -d "`$FILE"; \ + +"@ + }elseif ($o['archiveformat'] -match '\.gz') { +@" + gzip -d "`$FILE"; \ + +"@ + }elseif ($o['archiveformat'] -match '\.zip') { +@" + unzip "`$FILE" $( $o['binary'] ); \ + +"@ + } + + $destination = if ($o.Contains('destination')) { $o['destination'] } else { "/usr/local/bin/$( $o['binary'] )" } + $destinationDir = Split-Path $destination -Parent +@" + mkdir -pv $destinationDir; \ + mv -v $( $o['binary'] ) $destination; \ + chmod +x $destination; \ + $( $o['testCommand'] ); \ + +"@ + + if ($o.Contains('archivefiles')) { + if ($license = $o['archivefiles'] | ? { $_ -match 'LICENSE' }) { +@" + mkdir -p /licenses; \ + mv -v $license /licenses/$license; \ + +"@ + } + } + +@" + : + + +"@ +} + @" FROM alpine:3.17 -ENV npm_config_yes=true +"@ + -# Install pagefind -RUN set -eux; \ - apk add --no-cache npm; \ - npm_config_yes=true npx pagefind@$( $VARIANT['_metadata']['package_version'] ) --version; \ - mv ~/.npm/_npx/*/node_modules/@pagefind/*/bin/pagefind_extended /usr/local/bin/pagefind_extended; \ - pagefind_extended --version; \ - apk del npm;\ - rm -rf ~/.npm; +$RESTIC_VERSION = 'v0.15.1' +Generate-DownloadBinary @{ + binary = 'pagefind_extended' + version = "v$( $VARIANT['_metadata']['package_version'] )" + repository = 'CloudCannon/pagefind' + archiveformat = '.tar.gz' + archivefiles = @( + 'pagefind_extended' + ) + architectures = $VARIANT['_metadata']['platforms'] + testCommand = 'pagefind_extended --version' +} +@" COPY docker-entrypoint.sh /docker-entrypoint.sh RUN chmod +x docker-entrypoint.sh diff --git a/test.ps1 b/test.ps1 deleted file mode 100644 index 587e00d..0000000 --- a/test.ps1 +++ /dev/null @@ -1 +0,0 @@ -Generate-DockerImageVariants . diff --git a/variants/1.0.3/Dockerfile b/variants/1.0.3/Dockerfile index 745616d..a2d5079 100644 --- a/variants/1.0.3/Dockerfile +++ b/variants/1.0.3/Dockerfile @@ -1,15 +1,27 @@ FROM alpine:3.17 - -ENV npm_config_yes=true - -# Install pagefind +# Install pagefind_extended RUN set -eux; \ - apk add --no-cache npm; \ - npm_config_yes=true npx pagefind@1.0.3 --version; \ - mv ~/.npm/_npx/*/node_modules/@pagefind/*/bin/pagefind_extended /usr/local/bin/pagefind_extended; \ + PAGEFIND_EXTENDED_VERSION=v1.0.3; \ + case "$( uname -m )" in \ + 'x86_64') \ + URL="https://github.com/CloudCannon/pagefind/releases/download/v1.0.3/pagefind_extended-v1.0.3-x86_64-unknown-linux-musl.tar.gz"; \ + SHA256=aec60f1db9fd693986b2c3d6253f98e6710e5046a371fc1c7e3ecc5fc982a95d; \ + ;; \ + *) \ + echo "Architecture not supported"; \ + exit 1; \ + ;; \ + esac; \ + FILE=pagefind_extended.tar.gz; \ + wget -q "$URL" -O "$FILE"; \ + echo "$SHA256 $FILE" | sha256sum -c -; \ + tar -xvf "$FILE" --no-same-owner --no-same-permissions -- pagefind_extended; \ + rm -f "$FILE"; \ + mkdir -pv /usr/local/bin; \ + mv -v pagefind_extended /usr/local/bin/pagefind_extended; \ + chmod +x /usr/local/bin/pagefind_extended; \ pagefind_extended --version; \ - apk del npm;\ - rm -rf ~/.npm; + : COPY docker-entrypoint.sh /docker-entrypoint.sh RUN chmod +x docker-entrypoint.sh