|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +if [ $# != 2 ]; then |
| 5 | + echo "Usage: $0 <app> <arch>" |
| 6 | + echo "* app: subfolder in apps/hetzner/" |
| 7 | + echo "* arch: either amd64 or arm64" |
| 8 | + echo "" |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +APP=$1 |
| 13 | +ARCH=$2 |
| 14 | + |
| 15 | +if [ -z "$HCLOUD_TOKEN" ]; then |
| 16 | + echo "No HCLOUD_TOKEN set" |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +GIT_REVISION=`git symbolic-ref HEAD 2> /dev/null | cut -b 12-`-`git log --pretty=format:\"%h\" -1 | xargs` |
| 21 | +SNAPSHOT_NAME=${APP}-${ARCH}-${GIT_REVISION}-${CI_PIPELINE_ID} |
| 22 | + |
| 23 | +export HCLOUD_SERVER_LOCATION="fsn1" |
| 24 | + |
| 25 | +# Target architecture for the new VM created by Packer |
| 26 | +if [ "$ARCH" == "amd64" ]; then |
| 27 | + export HCLOUD_SERVER_TYPE_BEFORE_UPSCALE="cx11" |
| 28 | + export HCLOUD_SERVER_TYPE="cpx21" |
| 29 | +fi |
| 30 | +if [ "$ARCH" == "arm64" ]; then |
| 31 | + export HCLOUD_SERVER_TYPE_BEFORE_UPSCALE="cax11" |
| 32 | + export HCLOUD_SERVER_TYPE="cax21" |
| 33 | +fi |
| 34 | + |
| 35 | +# Local architecture (used to execute hcloud and packer) |
| 36 | +LOCAL_ARCH='amd64' |
| 37 | +if [ $(uname -i) == 'aarch64' ]; then |
| 38 | + LOCAL_ARCH='arm64' |
| 39 | +fi |
| 40 | + |
| 41 | +if [ -z "$CI_JOB_ID" ]; then |
| 42 | + export CI_JOB_ID=`date +%s` |
| 43 | +fi |
| 44 | + |
| 45 | +# Download dependencies |
| 46 | +rm -rf hcloud || true |
| 47 | +mkdir hcloud |
| 48 | +cd hcloud |
| 49 | +wget -q "https://cs1.hetzner.de/cloud/hcloud-linux-${LOCAL_ARCH}.tar.gz" |
| 50 | +tar xzf hcloud-linux-${LOCAL_ARCH}.tar.gz |
| 51 | +export PATH=$PATH:$(pwd) |
| 52 | + |
| 53 | +cd .. |
| 54 | +rm -rf packer || true |
| 55 | +mkdir packer |
| 56 | +cd packer |
| 57 | +wget -q "https://releases.hashicorp.com/packer/1.10.3/packer_1.10.3_linux_${LOCAL_ARCH}.zip" |
| 58 | +unzip packer_1.10.3_linux_${LOCAL_ARCH}.zip |
| 59 | +export PATH=$PATH:$(pwd) |
| 60 | +cd .. |
| 61 | + |
| 62 | +# Check syntax |
| 63 | +packer validate -syntax-only apps/hetzner/${APP}/ |
| 64 | + |
| 65 | +# Create VMs, install one-click-apps, create snapshot |
| 66 | +packer build -color=false -on-error=abort -var snapshot_name=${SNAPSHOT_NAME} apps/hetzner/${APP}/ |
| 67 | + |
| 68 | +# Tag the resulting image with the application it contains |
| 69 | +snapshot_id=$(hcloud image list -o noheader -o "columns=id,description" | grep "${SNAPSHOT_NAME}" | awk '{print $1}') |
| 70 | +hcloud image add-label $snapshot_id app=${APP} |
| 71 | + |
| 72 | +# Cleanup any VMs, etc. that might be remaining |
| 73 | +./cleanup.sh ${APP} |
0 commit comments