aboutsummaryrefslogtreecommitdiffstats
path: root/build-hcloud-snapshot.sh
blob: 20f7f70db9a3b75d964eee827fda5c9e85a16f55 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/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
_SCRIPT_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)

VERSION=8.0
ARCH=amd64
SSH_KEY="default-key"

main() (
  parse_args "$@"
  img_path="${_SCRIPT_DIR}/openbsd-image/build/openbsd-${VERSION}-${ARCH}.raw"
  build_compressed_image "$img_path"
  cd "$_SCRIPT_DIR/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
    -h | --help)
      usage
      exit 0
      ;;
    --version)
      VERSION="$2"
      shift 2
      ;;
    --arch)
      ARCH="$2"
      shift 2
      ;;
    --ssh-key)
      SSH_KEY="$2"
      shift 2
      ;;
    *)
      echo "Unknown option: $1"
      shift
      ;;
    esac
  done
}

usage() {
  cat <<EOF
Usage: $(basename "$0") [OPTIONS]

Create an OpenBSD snapshot in Hetzner Cloud.
Requires HCLOUD_TOKEN to be set in the environment.
Run ./build-openbsd-image.sh first to produce the local image.

Options:
  --version VERSION   OpenBSD version (must match the built image) (default: $VERSION)
  --arch ARCH         Target architecture: amd64 or arm64 (default: $ARCH)
  --ssh-key NAME      Hetzner SSH key name to add to the snapshot (default: $SSH_KEY)
  -h, --help          Show this help message
EOF
}

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

main "$@"