Skip to content

Commit

Permalink
Merge pull request #3 from theohbrothers/refactor/download-binaries-d…
Browse files Browse the repository at this point in the history
…irectly-instead-of-using-npx

Refactor: Download binaries directly instead of using npx
  • Loading branch information
leojonathanoh authored Nov 7, 2023
2 parents 15ac0d8 + da10733 commit b00adfb
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 19 deletions.
1 change: 1 addition & 0 deletions generate/definitions/VARIANTS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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']
}
Expand Down
189 changes: 180 additions & 9 deletions generate/templates/Dockerfile.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion test.ps1

This file was deleted.

30 changes: 21 additions & 9 deletions variants/1.0.3/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b00adfb

Please sign in to comment.