aboutsummaryrefslogtreecommitdiffstats
path: root/build-hcloud-snapshot.sh
blob: 40f3dcbb91808a3d43e2d062ac0f7c3da2585ad7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
#
# DESCRIPTION:
#   Create a OpenBSD snapshot in Hetzner Cloud.
#
# USAGE:
#   ./build-openbsd-image.sh
#   export HCLOUD_TOKEN="xxxxx-xxxxx"
#   ./build-hcloud-snapshot.sh
#
set -o nounset
set -o errexit

export PACKER_LOG=1
SCRIPTDIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)

VERSION=7.9
ARCH=amd64
PUBLIC_SSH_KEY="$HOME/.ssh/id_rsa.pub"

main() (
  parse_args "$@"
  img_path="${SCRIPTDIR}/openbsd-image/build/openbsd-${VERSION}-${ARCH}.raw"
  build_compressed_image "$img_path"
  cd "$SCRIPTDIR/hcloud-snapshot/"
  packer init .
  packer build \
    -var "hcloud_token=${HCLOUD_TOKEN:?}" \
    -var "image_path=${img_path}.zst" \
    -var "version=$VERSION" \
    -var "arch=$ARCH" \
    -var "ssh_key=$(get_ssh_key_name "$PUBLIC_SSH_KEY")" \
    .
)

parse_args() {
  while [ $# -gt 0 ]; do
    case "$1" in
    --version)
      VERSION="$2"
      shift 2
      ;;
    --arch)
      ARCH="$2"
      shift 2
      ;;
    --public-ssh-key)
      PUBLIC_SSH_KEY="$2"
      shift 2
      ;;
    *)
      echo "Unknown option: $1"
      shift
      ;;
    esac
  done
}

build_compressed_image() (
  if [ ! -e "$1.zst" ]; then
    zstd -T0 "$1" -o "$1.zst"
  fi
)

get_ssh_key_name() {
  awk '{print $3}' "$1"
}

main "$@"