From 6a336f0167393318639b6bc1c9ccf0484219c970 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 8 Apr 2020 14:16:48 +0200 Subject: Rewrite the build-docker action --- .dockerignore | 8 +++ lib/actions/build-docker.bash | 108 +++++++++++++++++++++++----------------- lib/docker/alpine | 25 ---------- lib/docker/alpine.Dockerfile | 21 ++++++++ lib/docker/archlinux | 24 --------- lib/docker/archlinux.Dockerfile | 22 ++++++++ lib/docker/centos | 24 --------- lib/docker/centos.Dockerfile | 21 ++++++++ lib/docker/debian | 27 ---------- lib/docker/debian.Dockerfile | 23 +++++++++ lib/main.bash | 39 +++++++++------ 11 files changed, 179 insertions(+), 163 deletions(-) create mode 100644 .dockerignore delete mode 100644 lib/docker/alpine create mode 100644 lib/docker/alpine.Dockerfile delete mode 100644 lib/docker/archlinux create mode 100644 lib/docker/archlinux.Dockerfile delete mode 100644 lib/docker/centos create mode 100644 lib/docker/centos.Dockerfile delete mode 100644 lib/docker/debian create mode 100644 lib/docker/debian.Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..7d375a9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.git +dist +bin +!bin/rstar +lib/libmoar.so +include +share +tmp diff --git a/lib/actions/build-docker.bash b/lib/actions/build-docker.bash index f7f6241..86060df 100644 --- a/lib/actions/build-docker.bash +++ b/lib/actions/build-docker.bash @@ -1,76 +1,90 @@ -#!/usr/bin/bash +#!/usr/bin/env bash action() { local OPTIND - local base_image - local base_tag - local output_tag + local name + local version + local description + local tag local dockerfile - local template - local install_options + local target - while getopts ":B:b:f:T:t:" opt + while getopts ":T:b:d:ln:t:" opt do case "$opt" in + T) tag=$OPTARG ;; b) RSTAR_BACKEND=$OPTARG ;; - B) base_image=$OPTARG ;; - T) base_tag=$OPTARG ;; - t) output_tag=$OPTARG ;; - f) template=$OPTARG ;; + d) description=$OPTARG ;; + l) tag_latest=1 ;; + n) name=$OPTARG ;; + t) version=$OPTARG ;; *) emerg "Invalid option specified: $opt" ;; esac done - # Base image must be specified - if [[ -z $base_image ]] + shift $(( OPTIND - 1 )) + + SOURCE_DATE_EPOCH="$(git log -1 --pretty=format:%at)" + + if (( $# < 1 )) + then + alert "You must specify a base image to build for" + action_build_docker_list + return 2 + fi + + target=$1 + + if (( 1 < $# )) then - emerg "Must specify a base image with -B" - exit 2 + warn "Only $target will be built, additional arguments are being ignored!" fi - # Tag of base image defaults to 'latest' - [[ -z $base_tag ]] && base_tag="latest" + # Set defaults for the Docker tag value + [[ -z $name ]] && name="$USER/rakudo-star" - # Set a default tag for the built image with relevant information - if [[ -z "$output_tag" ]] + # Build up a nice tag if none was explicitly defined + if [[ -z $tag ]] then - output_tag="rakudo:$base_image-$base_tag" - if [[ ! -z $RSTAR_BACKEND ]] - then - output_tag="$output_tag-$RSTAR_BACKEND" - fi + [[ -z $version ]] && version="$(datetime %Y.%m)" + [[ -z $description ]] && description="$target" + + tag="$version-$description" fi - dockerfile=$(tempfile) + dockerfile="$BASEDIR/lib/docker/$target.Dockerfile" + + debug "Using $dockerfile" - # We use the Dockerfile template for this image + tag pair if it exists, - # or use the image default template - if [[ -z $template ]] && [[ -f "$BASEDIR/lib/docker/$base_image/$base_tag" ]] + if [[ ! -f $dockerfile ]] then - template="$BASEDIR/lib/docker/$base_image/$base_tag" - else - if [[ -f "$BASEDIR/lib/docker/$base_image" ]] - then - template="$BASEDIR/lib/docker/$base_image" - else - # Die if we have not found a template to use - emerg "No Dockerfile template found for '$base_image:$base_tag'!" - exit 2 - fi + alert "Target '$target' is not supported" + action_build_docker_list + return 2 fi - # Fill in template placeholders - if [[ ! -z $RSTAR_BACKEND ]] + # Build the image + docker build \ + -t "$name:$tag" \ + -f "$dockerfile" \ + --build-arg SOURCE_DATE_EPOCH="$SOURCE_DATE_EPOCH" \ + --build-arg RSTAR_BACKEND="$RSTAR_BACKEND" \ + "$BASEDIR" + + # Also tag the image as "latest" + if [[ $tag_latest ]] then - install_options="$install_options -b "'"'"$RSTAR_BACKEND"'"' + docker tag "$name:$tag" "$name:latest-$target" fi +} - sed < "$template" > "$dockerfile" \ - -e "s/{{INSTALL_OPTIONS}}/$install_options/" \ - -e "s/{{TAG}}/$base_tag/" +action_build_docker_list() { + chgdir "$BASEDIR/lib/docker" > /dev/null - # Build the image with the generated Dockerfile - docker build -t "$output_tag" -f "$dockerfile" "$BASEDIR" + info "Available targets are:" - shift $(( OPTIND -1 )) + for target in *.Dockerfile + do + info " ${target%.*}" + done } diff --git a/lib/docker/alpine b/lib/docker/alpine deleted file mode 100644 index e4770c8..0000000 --- a/lib/docker/alpine +++ /dev/null @@ -1,25 +0,0 @@ -FROM alpine:{{TAG}} AS base - -RUN apk add --no-cache \ - bash build-base git perl readline - -COPY . /home/rstar - -RUN /home/rstar/bin/rstar install \ - -p /home/raku {{INSTALL_OPTIONS}} - -RUN apk del bash build-base git perl - -FROM alpine:{{TAG}} - -COPY --from=base /home/raku /usr/local -COPY --from=base /usr/lib /usr/lib - -ENV PATH=/usr/local/share/perl6/site/bin:$PATH -ENV PATH=/usr/local/share/perl6/vendor/bin:$PATH -ENV PATH=/usr/local/share/perl6/core/bin:$PATH -ENV PERL6LIB=/app/lib - -WORKDIR /app - -CMD [ "raku" ] diff --git a/lib/docker/alpine.Dockerfile b/lib/docker/alpine.Dockerfile new file mode 100644 index 0000000..2170c0e --- /dev/null +++ b/lib/docker/alpine.Dockerfile @@ -0,0 +1,21 @@ +FROM alpine:latest AS base + +COPY . /home/rstar + +RUN apk add --no-cache bash build-base git perl readline +RUN /home/rstar/bin/rstar install -p /home/raku +RUN apk del bash build-base git perl + +FROM alpine:latest + +COPY --from=base /home/raku /usr/local +COPY --from=base /usr/lib /usr/lib + +ENV PATH=/usr/local/share/perl6/site/bin:$PATH +ENV PATH=/usr/local/share/perl6/vendor/bin:$PATH +ENV PATH=/usr/local/share/perl6/core/bin:$PATH +ENV PERL6LIB=/app/lib + +WORKDIR /app + +CMD [ "raku" ] diff --git a/lib/docker/archlinux b/lib/docker/archlinux deleted file mode 100644 index 637ba3c..0000000 --- a/lib/docker/archlinux +++ /dev/null @@ -1,24 +0,0 @@ -FROM archlinux:{{TAG}} AS base - -RUN pacman -Sy && pacman --noconfirm -S gcc make - -COPY . /home/rstar - -RUN /home/rstar/bin/rstar install \ - -p /home/raku {{INSTALL_OPTIONS}} - -RUN pacman --noconfirm -Rs gcc make - -FROM archlinux:{{TAG}} - -COPY --from=base /home/raku /usr/local -COPY --from=base /usr/lib /usr/lib - -ENV PATH=/usr/local/share/perl6/site/bin:$PATH -ENV PATH=/usr/local/share/perl6/vendor/bin:$PATH -ENV PATH=/usr/local/share/perl6/core/bin:$PATH -ENV PERL6LIB=/app/lib - -WORKDIR /app - -CMD [ "raku" ] diff --git a/lib/docker/archlinux.Dockerfile b/lib/docker/archlinux.Dockerfile new file mode 100644 index 0000000..24663dd --- /dev/null +++ b/lib/docker/archlinux.Dockerfile @@ -0,0 +1,22 @@ +FROM archlinux:latest AS base + +COPY . /home/rstar + +RUN pacman -Sy +RUN pacman --noconfirm -S gcc make +RUN /home/rstar/bin/rstar install -p /home/raku +RUN pacman --noconfirm -Rs gcc make + +FROM archlinux:latest + +COPY --from=base /home/raku /usr/local +COPY --from=base /usr/lib /usr/lib + +ENV PATH=/usr/local/share/perl6/site/bin:$PATH +ENV PATH=/usr/local/share/perl6/vendor/bin:$PATH +ENV PATH=/usr/local/share/perl6/core/bin:$PATH +ENV PERL6LIB=/app/lib + +WORKDIR /app + +CMD [ "raku" ] diff --git a/lib/docker/centos b/lib/docker/centos deleted file mode 100644 index 7c6b8d4..0000000 --- a/lib/docker/centos +++ /dev/null @@ -1,24 +0,0 @@ -FROM centos:{{TAG}} AS base - -RUN yum -y install perl git gcc make - -COPY . /home/rstar - -RUN /home/rstar/bin/rstar install \ - -p /home/raku {{INSTALL_OPTIONS}} - -RUN yum -y remove perl git gcc make - -FROM centos:{{TAG}} - -COPY --from=base /home/raku /usr/local -COPY --from=base /usr/lib64 /usr/lib64 - -ENV PATH=/usr/local/share/perl6/site/bin:$PATH -ENV PATH=/usr/local/share/perl6/vendor/bin:$PATH -ENV PATH=/usr/local/share/perl6/core/bin:$PATH -ENV PERL6LIB=/app/lib - -WORKDIR /app - -CMD ["raku"] diff --git a/lib/docker/centos.Dockerfile b/lib/docker/centos.Dockerfile new file mode 100644 index 0000000..bebe219 --- /dev/null +++ b/lib/docker/centos.Dockerfile @@ -0,0 +1,21 @@ +FROM centos:latest AS base + +COPY . /home/rstar + +RUN yum -y install perl git gcc make +RUN /home/rstar/bin/rstar install -p /home/raku +RUN yum -y remove perl git gcc make + +FROM centos:latest + +COPY --from=base /home/raku /usr/local +COPY --from=base /usr/lib64 /usr/lib64 + +ENV PATH=/usr/local/share/perl6/site/bin:$PATH +ENV PATH=/usr/local/share/perl6/vendor/bin:$PATH +ENV PATH=/usr/local/share/perl6/core/bin:$PATH +ENV PERL6LIB=/app/lib + +WORKDIR /app + +CMD ["raku"] diff --git a/lib/docker/debian b/lib/docker/debian deleted file mode 100644 index 2051a27..0000000 --- a/lib/docker/debian +++ /dev/null @@ -1,27 +0,0 @@ -FROM debian:{{TAG}} AS base - -RUN apt-get update -RUN apt-get install -y \ - git build-essential libreadline7 - -COPY . /home/rstar - -RUN /home/rstar/bin/rstar install \ - -p /home/raku {{INSTALL_OPTIONS}} - -RUN apt-get -y remove git build-essential -RUN apt-get -y autoremove - -FROM debian:{{TAG}} - -COPY --from=base /home/raku /usr/local -COPY --from=base /lib /lib - -ENV PATH=/usr/local/share/perl6/site/bin:$PATH -ENV PATH=/usr/local/share/perl6/vendor/bin:$PATH -ENV PATH=/usr/local/share/perl6/core/bin:$PATH -ENV PERL6LIB=/app/lib - -WORKDIR /app - -CMD [ "raku" ] diff --git a/lib/docker/debian.Dockerfile b/lib/docker/debian.Dockerfile new file mode 100644 index 0000000..52652a4 --- /dev/null +++ b/lib/docker/debian.Dockerfile @@ -0,0 +1,23 @@ +FROM debian:latest AS base + +COPY . /home/rstar + +RUN apt-get update +RUN apt-get install -y git build-essential libreadline7 +RUN /home/rstar/bin/rstar install -p /home/raku +RUN apt-get -y remove git build-essential +RUN apt-get -y autoremove + +FROM debian:latest + +COPY --from=base /home/raku /usr/local +COPY --from=base /lib /lib + +ENV PATH=/usr/local/share/perl6/site/bin:$PATH +ENV PATH=/usr/local/share/perl6/vendor/bin:$PATH +ENV PATH=/usr/local/share/perl6/core/bin:$PATH +ENV PERL6LIB=/app/lib + +WORKDIR /app + +CMD [ "raku" ] diff --git a/lib/main.bash b/lib/main.bash index c535ba5..7b08ed6 100644 --- a/lib/main.bash +++ b/lib/main.bash @@ -27,10 +27,11 @@ main() { # Declare some global variables declare -a RSTAR_TOOLS - declare RSTAR_BACKEND=moar - declare RSTAR_PREFIX="$BASEDIR" declare -A RSTAR_PLATFORM + [[ -z $RSTAR_BACKEND ]] && RSTAR_BACKEND="moar" + [[ -z $RSTAR_PREFIX ]] && RSTAR_PREFIX="$BASEDIR" + # Figure out system details debug "Discovering system information" discover_system @@ -74,6 +75,7 @@ usage() { cat < rstar clean [-s] rstar dist [version] rstar fetch @@ -84,20 +86,25 @@ Usage: rstar is the entry point for all utilities to deal with Rakudo Star. Actions: - clean Clean up the repository. If -s is given, the src directory - will also be removed. - dist Create a distributable tarball of this repository. If no - version identifier is specified, it will use the current year - and month in "yyyy.mm" notation. - fetch Fetch all required sources. - install Install Raku on this system. By default, MoarVM will be used - as the only backend, and the Rakudo Star directory will be - used as prefix. If neither core nor modules are given as - explicit targets, all targets will be installed. - sysinfo Show information about your system. Useful for debugging. - test Run tests on Raku and the bundled ecosystem modules. If - neither spectest nor modules are given as explicit targets, - all targets will be tested. + build-docker Build a Docker image for Rakudo Star. You can specify the + tag of the resulting image using -T, which will cause -d, + -t, and -l to be ignored. -n specifies the name of the + image. If -l is passed, a "latest" tag will also be made. + You can specify a specific backend with -b. + clean Clean up the repository. If -s is given, the src + directory will also be removed. + dist Create a distributable tarball of this repository. If no + version identifier is specified, it will use the current + year and month in "yyyy.mm" notation. + fetch Fetch all required sources. + install Install Raku on this system. By default, MoarVM will be + used as the only backend, and the Rakudo Star directory + will be used as prefix. If neither core nor modules are + given as explicit targets, all targets will be installed. + sysinfo Show information about your system. Useful for debugging. + test Run tests on Raku and the bundled ecosystem modules. If + neither spectest nor modules are given as explicit + targets, all targets will be tested. EOF } -- cgit v1.1