aboutsummaryrefslogtreecommitdiffstats
path: root/build-hcloud-snapshot.sh
blob: 00ec32de501c9792257132432451c43278a4c4db (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
#!/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
SSH_KEY="default-key"

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=$SSH_KEY" \
    .
)

parse_args() {
  while [ $# -gt 0 ]; do
    case "$1" in
    --version)
      VERSION="$2"
      shift 2
      ;;
    --arch)
      ARCH="$2"
      shift 2
      ;;
    --ssh-key)
      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
)

main "$@"