summaryrefslogtreecommitdiff
path: root/roles/apps/nextcloud/templates/nextcloud-upgrade.j2
blob: f59f3be6bc8d8ffd611126c5485e378c031e86ff (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
#!/bin/bash

INST_NAME="$1"
VERSION="$2"
if [ -z "$INST_NAME" ] || [ -z "$VERSION" ]; then
  echo "Usage: $0 <instance> <version>"
  exit 1
fi

set -eu

K8S_CONFIG_HASH_D="/etc/kubernetes/config-hashes/"
K8S_CONFIG_HASH_FILE="$K8S_CONFIG_HASH_D/nextcloud-$INST_NAME.yml"
K8S_MANIFEST_D="/etc/kubernetes/manifests/"
K8S_MANIFEST_FILE="$K8S_MANIFEST_D/nextcloud-$INST_NAME.yml"
if [ ! -e "$K8S_MANIFEST_FILE" ]; then
  echo "could not find manifest file: $K8S_MANIFEST_FILE"
  exit 2
fi

TMP_D=$(mktemp -d -t nextcloud-upgrade.XXXXXXX)
function cleanup {
  rm -rf "$TMP_D"
}
trap cleanup EXIT

IMAGE_BUILD_D="{{ nextcloud_base_path }}/$INST_NAME/build"
IMAGE_NAME="nextcloud"
if [ -e "$IMAGE_BUILD_D/Dockerfile" ]; then
  ## this only works if docker is installed...
  echo "*** Building custom image"
  echo ""
  sed "0,/FROM \(.*\):.*/s//FROM \1:$VERSION/" -i "$IMAGE_BUILD_D/Dockerfile"
  IMAGE_NAME="nextcloud/$INST_NAME"
  docker build --rm --network host -t "$IMAGE_NAME:$VERSION" "$IMAGE_BUILD_D"
  echo ""
else
  echo "*** Pre-Pulling the image"
  echo ""
  crictl pull "docker.io/library/nextcloud:$VERSION"
  echo ""
fi
{% if nextcloud_zfs is defined %}

echo "*** creating ZFS snapshot"
echo ""

IMAGE_NAME_ESCAPED=${IMAGE_NAME/\//\\/}
CURRENT_VERSION=$(cat "$K8S_MANIFEST_FILE" | awk '/image: "'"$IMAGE_NAME_ESCAPED"':.*"/ { print($2) }' | tr -d '"' | cut -d ':' -f 2)
zfs snapshot "{{ nextcloud_zfs.pool }}/{{ nextcloud_zfs.name }}/$INST_NAME@upgrade_$CURRENT_VERSION-to-$VERSION""_$(date '+%Y-%m-%m_%H:%M:%S')"
{% endif %}

echo "*** Rebuilding config-hash file"
echo ""
cat "$K8S_CONFIG_HASH_FILE" | grep '^/.*:' | sed 's/:$//' | xargs sha256sum | awk '{ print($2":\n  checksum: "$1) }' > "$TMP_D/config-hash.yml"
CONFIG_HASH=$(sha256sum "$TMP_D/config-hash.yml" | awk '{ print($1) }')

echo "*** Patching manifest file"
echo ""
sed -e "s#image: \"$IMAGE_NAME:.*\"#image: \"$IMAGE_NAME:$VERSION\"#" -e "s#config-hash: \".*\"#config-hash: \"$CONFIG_HASH\"#" "$K8S_MANIFEST_FILE" > "$TMP_D/manifest.yml"
set +e
diff -u "$K8S_MANIFEST_FILE" "$TMP_D/manifest.yml"
if [ $? -eq 0 ]; then
  echo "patching file failed?"
  exit 2
fi
cat "$TMP_D/config-hash.yml" > "$K8S_CONFIG_HASH_FILE"
cat "$TMP_D/manifest.yml" > "$K8S_MANIFEST_FILE"
echo ""

exit 0