Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updates base builder scripts
  • Loading branch information
jkachmar committed Sep 19, 2021
commit d00576632a22ce94e18b3192f5e22d230dba9348
109 changes: 90 additions & 19 deletions 0_base/builder.sh
Original file line number Diff line number Diff line change
@@ -1,56 +1,127 @@
#!/usr/bin/env sh
set -eu

################################################################################
# Pre-flight checks.
################################################################################

command -v git >/dev/null 2>&1 || { echo >&2 "git is not installed, aborting."; exit 1; }
command -v buildah >/dev/null 2>&1 || { echo >&2 "buildah is not installed, aborting."; exit 1; }

# Start the script in the top-level repository directory no matter what.
cd "$( git rev-parse --show-toplevel )"

# XXX: Would it make sense to pull from an existing image if one exists?
image="alpine-ghc-base"
container="alpine-ghc-base-builder"
################################################################################
# Argument parsing.
#
# cf. https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/
################################################################################

container="base"
image="base"
# NOTE: The logic associated with this will have to change for GHC 9.x and up to
# support the changes introduced with the switch to `ghc-bignum`.
numeric="gmp"

usage="USAGE: $0
-h show this help text
-c CONTAINER override the default container name
default: ${container}
-i IMAGE override the default image name
default: ${image}
-n NUMERIC override the numeric library GHC is built against; either 'gmp' or 'simple'
default: ${numeric}"

while getopts "c:i:n:h" opt; do
case ${opt} in
c ) {
container="${OPTARG}"
};;
i ) {
image="${OPTARG}"
};;
n ) {
if [ "${OPTARG}" = "gmp" ] || [ "${OPTARG}" = "simple" ]; then
numeric="${OPTARG}"
else
echo "Invalid NUMERIC argument (i.e. '-n')." >&2
echo "Expected either 'gmp' or 'simple', got '${OPTARG}'" >&2
exit 1
fi;
};;
h ) {
echo "${usage}"
exit 0
};;
\? ) {
echo "${usage}"
exit 1
};;
esac
done
shift $((OPTIND -1))

if [ "$#" -ne 0 ]; then
exit 1
fi

# Add the numeric library to container and image names.
container="${container}-${numeric}"
image="${image}-${numeric}"

################################################################################
# Attempt to create a new image using the container name defined above.
# Container and basic dependencies.
################################################################################

# Create the "base" container that will be used elsewhere in the project.
#
# If the container already exists, assume that it's been created by a previous
# run of this script and just use that.
# NOTE: Alternatively: `buildah commit --rm` (at the end of the script) removes
# the working container.
#
# XXX: Reusing the container by name if it exists seems like not the best idea
# but it's convenient for development.
buildah \
--signature-policy=./policy.json \
--name "${container}" \
from --pull docker.io/library/alpine:3.14 \
|| true

# Upgrade the currently installed packages.
# Update index files and upgrade the currently installed packages.
#
# NOTE: This breaks reproducibility.
buildah run "${container}" \
apk upgrade --no-cache
apk -U upgrade

# Install basic dependencies required by 'ghcup', 'stack', and 'cabal-install'.
buildah run "${container}" \
apk add --no-cache \
apk add \
curl \
gcc \
git \
libc-dev \
xz

# TODO: Guard this behind some argument that can toggle GMP-based builds.
echo "Installing 'libgmp'."
buildah run "${container}" \
apk add --no-cache gmp-dev
if [ "${numeric}" = "gmp" ]; then
echo "Installing 'libgmp'."
buildah run "${container}" \
apk add gmp-dev
fi;

################################################################################
ghcup_version="0.1.9"
ghcup_expected_checksum="d779ada6156b08da21e40c5bf218ec21d1308d5a9e48f7b9533f56b5d063a41c"
# Download and install `ghcup`.
################################################################################

ghcup_version="0.1.16.2"
ghcup_expected_checksum="d5e43b95ce1d42263376e414f7eb7c5dd440271c7c6cd9bad446fdeff3823893"

# Fetch `ghcup`.
buildah run "${container}" \
wget \
-O "/tmp/ghcup-${ghcup_version}" \
"https://downloads.haskell.org/~ghcup/0.1.9/x86_64-linux-ghcup-${ghcup_version}"
"https://downloads.haskell.org/~ghcup/${ghcup_version}/x86_64-linux-ghcup-${ghcup_version}"

# Copy the checksum validation script into the container...
buildah copy --chmod 111 ${container} \
buildah copy --chmod 111 "${container}" \
./0_base/validate_checksum.sh \
/tmp/validate_checksum.sh

Expand All @@ -68,12 +139,12 @@ buildah run "${container}" \
buildah run "${container}" \
chmod +x /usr/bin/ghcup

# ...and clean up after ourselves.
# ...clean up any scripts.
buildah run "${container}" \
rm -rf /tmp/validate_checksum.sh

################################################################################
# Write the final `alpine-ghc-base` image from this container.
# Write the final image from this container.
buildah \
--signature-policy=./policy.json \
commit "${container}" "${image}"
1 change: 1 addition & 0 deletions 0_base/validate_checksum.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ ghcup_expected_checksum="$2"
if ! echo "${ghcup_expected_checksum} ${ghcup_path}" | sha256sum -c -; then
echo "${ghcup_path} checksum failed" >&2
echo "expected '${ghcup_expected_checksum}', but got '$( sha256sum "${ghcup_path}" )'" >&2
exit 1
fi;