Skip to content

Commit f5a63f2

Browse files
committed
Development: Add build-script and instructions
1 parent db11120 commit f5a63f2

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hcloud/
2+
packer/
3+
*.pem

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,18 @@ These files will also be used to provide the documentation on https://docs.hetzn
3232
### Metadata
3333

3434
This file will contain some metadata which we use while displaying the app to you via our Cloud Console or public API.
35+
36+
### Development
37+
38+
You can build a snapshot yourself either for testing or to use as your personal app-template via
39+
40+
```sh
41+
$ export HCLOUD_TOKEN=[...]
42+
$ ./build.sh
43+
44+
Usage: ./build.sh <app> <arch>
45+
* app: subfolder in apps/hetzner/
46+
* arch: either amd64 or arm64
47+
48+
$ ./build.sh wireguard arm64
49+
```

build.sh

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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}

cleanup.sh

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if [ -z "$1" ];
5+
then
6+
echo "please specify app label"
7+
exit 1
8+
fi
9+
10+
##
11+
# Remove old snapshots, keep last 2
12+
ids=$(hcloud image list --selector app=$1 -o noheader -o columns=id | head -n -2)
13+
14+
for i in $ids
15+
do
16+
hcloud image delete $i
17+
done
18+
19+
##
20+
# Remove old snapshots in case of an workflow failure in the past
21+
keys=$(hcloud ssh-key list | grep packer | awk '{ print $1 }')
22+
for k in $keys
23+
do
24+
hcloud ssh-key delete $k
25+
done

0 commit comments

Comments
 (0)