diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 025ba7b..51fbb10 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,11 +1,11 @@ image: docker:latest variables: + GIT_STRATEGY: fetch PACKER_VERSION: 1.2.0 ANNOUNCE: http://10.105.255.0:6969/announce WEBSEED: http://10.105.255.0/labsync/$CI_COMMIT_REF_NAME/$CI_PIPELINE_ID/images - PACKER_DOCKER_IMAGE: ${CI_REGISTRY_IMAGE}/packer - MKTORRENT_DOCKER_IMAGE: ${CI_REGISTRY_IMAGE}/mktorrent + DOCKER_IMAGE: ${CI_REGISTRY_IMAGE}/labsync-builder stages: - prepare @@ -25,8 +25,7 @@ dockerimage: script: - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY - make dockerimg - - docker push $PACKER_DOCKER_IMAGE - - docker push $MKTORRENT_DOCKER_IMAGE + - docker push $DOCKER_IMAGE tags: - fablab @@ -41,14 +40,3 @@ squashfs: tags: - fablab -mktorrent: - stage: torrent - script: - - make images/debian-stretch.torrent - artifacts: - paths: - - images - expire_in: 2 weeks - tags: - - fablab - diff --git a/Makefile b/Makefile index b9a7658..5498308 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,7 @@ PACKER_VERSION ?= 1.2.0 ANNOUNCE ?= http://10.2.2.1:6969/announce WEBSEED ?= http://10.2.2.1 -PACKER_DOCKER_IMAGE ?= labsync-packer -MKTORRENT_DOCKER_IMAGE ?= labsync-mktorrent +DOCKER_IMAGE ?= labsync-builder CWD=$(abspath $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))) @@ -27,7 +26,7 @@ fi \ ) .PHONY: default -default: dockerimg images/debian-stretch.squashfs images/debian-stretch.torrent +default: dockerimg images/debian-stretch.squashfs .PHONY: clean clean: @@ -36,8 +35,7 @@ clean: .PHONY: dockerimg dockerimg: - docker build -t "$(PACKER_DOCKER_IMAGE)" --build-arg "PACKER_VERSION=$(PACKER_VERSION)" packer/docker - docker build -t "$(MKTORRENT_DOCKER_IMAGE)" mktorrent + docker build -t "$(DOCKER_IMAGE)" --build-arg "PACKER_VERSION=$(PACKER_VERSION)" docker images: [ ! -d "$@" ] && mkdir "$@" @@ -51,19 +49,22 @@ images/debian-stretch.squashfs: images -v "${PWD}/images:/images" \ -e "user=`id -u`" \ -e "group=`id -g`" \ - "$(PACKER_DOCKER_IMAGE)" \ + -e "ANNOUNCE=$(ANNOUNCE)" \ + -e "WEBSEED=$(WEBSEED)" \ + "$(DOCKER_IMAGE)" \ debian-stretch images/debian-stretch.torrent: images docker run \ --rm \ - -v "${PWD}/images:/workdir" \ + -v "${PWD}/images:/images" \ -e "user=`id -u`" \ -e "group=`id -g`" \ -e "ANNOUNCE=$(ANNOUNCE)" \ -e "WEBSEED=$(WEBSEED)" \ - "$(MKTORRENT_DOCKER_IMAGE)" \ - debian-stretch.squashfs + -e "TASK=torrent" \ + "$(DOCKER_IMAGE)" \ + debian-stretch # updates the initramfs # only used for development diff --git a/packer/docker/Dockerfile b/docker/Dockerfile similarity index 97% rename from packer/docker/Dockerfile rename to docker/Dockerfile index 6b0c57b..37ca316 100644 --- a/packer/docker/Dockerfile +++ b/docker/Dockerfile @@ -2,6 +2,7 @@ FROM docker:dind RUN apk add --no-cache \ ansible \ + mktorrent \ openssh-client \ squashfs-tools diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 0000000..50c592e --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,91 @@ +#!/bin/sh +set -e + +make_torrent() { + (cd /images + if [ "$NAME" = "" ]; then + # remove file extension + NAME="$(echo "$1" | sed 's/\.[^.]*//')" + fi + + if [ "$WEBSEED" = "" ]; then + echo '$WEBSEED not provided' >&2 + exit 1 + fi + if [ "$ANNOUNCE" = "" ]; then + echo '$ANNOUNCE not provided' >&2 + exit 1 + fi + + TORRENT_FILE="$NAME.torrent" + WEBSEED_URL="$WEBSEED/$1" + + if [ -e "$TORRENT_FILE" ]; then rm "$TORRENT_FILE"; fi + + mktorrent-borg \ + -n "$NAME" \ + -a "$ANNOUNCE" \ + -o "$TORRENT_FILE" \ + -l 22 \ + -w "$WEBSEED_URL" \ + "$1" + + if [ "$user" != "" ] && [ "$group" != "" ]; then + chown "$user:$group" "$TORRENT_FILE" + fi + ) +} + +run_packer() { + packer build "$PACKER_JSON" + + mkdir -p build/rootfs + tar xf build/rootfs.tar -C build/rootfs + + mv build/rootfs/artifacts/initramfs "/images/$INITRAMFS_FILE" + mv build/rootfs/artifacts/linux "/images/$LINUX_FILE" + + rm -rf build/rootfs/artifacts + + rm -- \ + build/rootfs/.dockerenv \ + build/rootfs/initrd.* \ + build/rootfs/vmlinuz* + + if [ -e "/images/$SQUASHFS_FILE" ]; then + rm "/images/$SQUASHFS_FILE" + fi + + mksquashfs build/rootfs "/images/$SQUASHFS_FILE" \ + -comp lzo \ + -Xcompression-level 9 + + rm -rf build + + if [ "$user" != "" ] && [ "$group" != "" ]; then + chown $user:$group \ + "/images/$INITRAMFS_FILE" \ + "/images/$LINUX_FILE" \ + "/images/$SQUASHFS_FILE" + fi +} + +NAME="$1" + +if [ "$NAME" = "" ]; then + echo 'no name supplied, stopping.' >&2 + exit 1 +fi + +export PACKER_JSON="$NAME.json" +export INITRAMFS_FILE="$NAME.initramfs" +export LINUX_FILE="$NAME.linux" +export SQUASHFS_FILE="$NAME.squashfs" + +if [ -z "$TASK" ] || [ "$TASK" == "packer" ]; then + run_packer +fi +if [ -z "$TASK" ] || [ "$TASK" == "torrent" ]; then + make_torrent "$NAME.squashfs" +fi + diff --git a/mktorrent/Dockerfile b/mktorrent/Dockerfile deleted file mode 100644 index 6410554..0000000 --- a/mktorrent/Dockerfile +++ /dev/null @@ -1,11 +0,0 @@ -FROM alpine - -RUN apk add --no-cache mktorrent \ - && mkdir /workdir - -COPY entrypoint.sh /usr/local/bin/entrypoint.sh - -WORKDIR /workdir - -ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] - diff --git a/mktorrent/entrypoint.sh b/mktorrent/entrypoint.sh deleted file mode 100755 index fc058b9..0000000 --- a/mktorrent/entrypoint.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh - -set -e - -if [ "$NAME" = "" ]; then - # remove file extension - NAME="$(echo "$1" | sed 's/\.[^.]*//')" -fi - -if [ "$WEBSEED" = "" ]; then - echo '$WEBSEED not provided' >&2 - exit 1 -fi -if [ "$ANNOUNCE" = "" ]; then - echo '$ANNOUNCE not provided' >&2 - exit 1 -fi - -TORRENT_FILE="$NAME.torrent" -WEBSEED_URL="$WEBSEED/$1" - -if [ -e "$TORRENT_FILE" ]; then rm "$TORRENT_FILE"; fi - -mktorrent-borg \ - -n "$NAME" \ - -a "$ANNOUNCE" \ - -o "$TORRENT_FILE" \ - -l 22 \ - -w "$WEBSEED_URL" \ - "$1" - -if [ "$user" != "" ] && [ "$group" != "" ]; then - chown "$user:$group" "$TORRENT_FILE" -fi diff --git a/packer/docker/entrypoint.sh b/packer/docker/entrypoint.sh deleted file mode 100755 index 5261f98..0000000 --- a/packer/docker/entrypoint.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/sh - -set -e - -NAME="$1" - -if [ "$NAME" = "" ]; then - echo 'no name supplied, stopping.' >&2 - exit 1 -fi - -PACKER_JSON="$NAME.json" -INITRAMFS_FILE="$NAME.initramfs" -LINUX_FILE="$NAME.linux" -SQUASHFS_FILE="$NAME.squashfs" - -packer build "$PACKER_JSON" - -mkdir -p build/rootfs -tar xf build/rootfs.tar -C build/rootfs - -mv build/rootfs/artifacts/initramfs "/images/$INITRAMFS_FILE" -mv build/rootfs/artifacts/linux "/images/$LINUX_FILE" - -rm -rf build/rootfs/artifacts - -rm -- \ - build/rootfs/.dockerenv \ - build/rootfs/initrd.* \ - build/rootfs/vmlinuz* - -if [ -e "/images/$SQUASHFS_FILE" ]; then - rm "/images/$SQUASHFS_FILE" -fi - -mksquashfs build/rootfs "/images/$SQUASHFS_FILE" \ - -comp lzo \ - -Xcompression-level 9 - -rm -rf build - -if [ "$user" != "" ] && [ "$group" != "" ]; then - chown $user:$group \ - "/images/$INITRAMFS_FILE" \ - "/images/$LINUX_FILE" \ - "/images/$SQUASHFS_FILE" -fi - -