#!/usr/bin/env bash
# rook-release/rook-ceph v1.19.5 - base variant: default
# Path: fetch this entry's retained objects, check their SHA-256, then apply
# them with kubectl. It uses curl, shasum, awk and kubectl. It needs no cub
# CLI and no ConfigHub account.
# Generated by scripts/generate-public-site.mjs from this entry's listing:
# https://confighub.github.io/helm-expt/site/listings/rook-release-rook-ceph-v1-19-5-default.json
# Entry page: https://confighub.github.io/helm-expt/site/charts/rook-release-rook-ceph-v1-19-5.html
set -euo pipefail

say() { printf '\n>> %s\n' "$*"; }

for tool in curl awk kubectl; do
  if ! command -v "$tool" >/dev/null 2>&1; then
    printf '%s is required and was not found.\n' "$tool" >&2
    exit 1
  fi
done
if command -v shasum >/dev/null 2>&1; then
  sha256_of() { shasum -a 256 "$1" | awk '{print $1}'; }
elif command -v sha256sum >/dev/null 2>&1; then
  sha256_of() { sha256sum "$1" | awk '{print $1}'; }
else
  printf 'shasum is required and was not found.\n' >&2
  exit 1
fi

work=./rook-release-rook-ceph-v1-19-5-default-objects
objects_url=https://raw.githubusercontent.com/confighub/helm-expt/main/recipes/rook-release/rook-ceph/v1.19.5/revisions/default/r001/rendered/release-objects.yaml
objects_sha256=26789923789aab27d01638f52680ee6de6d982268eee3740cabc733142f67ba8

say "Fetch the retained objects into $work/objects.yaml"
mkdir -p "$work"
curl -fsSL -o "$work/objects.yaml" "$objects_url"

say "Check the file against the SHA-256 in the listing"
fetched_sha256="$(sha256_of "$work/objects.yaml")"
if [ "$fetched_sha256" != "$objects_sha256" ]; then
  printf 'The file differs from the listing, so the script stops. Nothing was applied.\n  listing: %s\n  fetched: %s\n' "$objects_sha256" "$fetched_sha256" >&2
  exit 1
fi
printf 'The file matches: sha256 %s\n' "$fetched_sha256"

say "Split the file into its 133 objects. The cluster is untouched so far"
rm -rf "$work/docs" "$work/crds" "$work/rest"
mkdir -p "$work/docs" "$work/crds" "$work/rest"
awk -v dir="$work/docs" 'BEGIN { n = 0; f = sprintf("%s/doc-%04d.yaml", dir, n) } /^---[ \t]*$/ { close(f); n++; f = sprintf("%s/doc-%04d.yaml", dir, n); next } { print > f }' "$work/objects.yaml"
for n in 0024 0025 0026 0027 0028 0029 0030 0031 0032 0033 0034 0035; do mv "$work/docs/doc-$n.yaml" "$work/crds/"; done
for n in 0036 0037 0038 0039 0040 0041 0042 0043 0044 0045 0046 0047; do mv "$work/docs/doc-$n.yaml" "$work/crds/"; done
for n in 0048; do mv "$work/docs/doc-$n.yaml" "$work/crds/"; done
for n in 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0013 0014 0015 0016 0017 0018 0019 0020 0021 0022 0023 0049; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 0060 0061; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0062 0063 0064 0065 0066 0067 0068 0069 0070 0071 0072 0073; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0074 0075 0076 0077 0078 0079 0080 0081 0082 0083 0084 0085; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0086 0087 0088 0089 0090 0091 0092 0093 0094 0095 0096 0097; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0098 0099 0100 0101 0102 0103 0104 0105 0106 0107 0108 0109; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0110 0111 0112 0113 0114 0115 0116 0117 0118 0119 0120 0121; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0122 0123 0124 0125 0126 0127 0128 0129 0130 0131 0132 0133; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
ls "$work/crds" "$work/rest"

say "Check that kubectl reaches a cluster"
if ! kubectl get --raw /version >/dev/null 2>&1; then
  printf 'kubectl cannot reach a cluster. Point it at a throwaway cluster and re-run this script.\n' >&2
  exit 1
fi
kubectl config current-context 2>/dev/null || true

say "Create the default namespace if it does not exist"
if ! kubectl get namespace default >/dev/null 2>&1; then
  kubectl create namespace default
fi

wait_for_crd() {
  crd_name="$1"
  deadline=$(( $(date +%s) + 180 ))
  until kubectl get "crd/${crd_name}" >/dev/null 2>&1; do
    if [ "$(date +%s)" -ge "$deadline" ]; then
      printf "CRD %s did not appear within 180 seconds.\n" "$crd_name" >&2
      return 1
    fi
    sleep 2
  done
  kubectl wait --for=condition=Established --timeout=120s "crd/${crd_name}"
}

say "Check the 25 CRDs this file holds"
missing_crds=0
if ! kubectl get crd/cephconnections.csi.ceph.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/clientprofiles.csi.ceph.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/clientprofilemappings.csi.ceph.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/drivers.csi.ceph.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/operatorconfigs.csi.ceph.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephblockpoolradosnamespaces.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephblockpools.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephbucketnotifications.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephbuckettopics.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephclients.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephclusters.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephcosidrivers.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephfilesystemmirrors.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephfilesystems.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephfilesystemsubvolumegroups.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephnfses.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephnvmeofgateways.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephobjectrealms.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephobjectstores.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephobjectstoreusers.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephobjectzonegroups.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephobjectzones.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/cephrbdmirrors.ceph.rook.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/objectbucketclaims.objectbucket.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/objectbuckets.objectbucket.io >/dev/null 2>&1; then
  missing_crds=1
fi
if [ "$missing_crds" -eq 1 ]; then
  say "A CRD is missing, so apply the file's CRDs first"
  kubectl apply --server-side -f "$work/crds"
  crd_outcome="were applied first"
elif kubectl diff --server-side -f "$work/crds" >/dev/null 2>&1; then
  say "The CRDs already exist and match this file. The script leaves them under their current owner"
  crd_outcome="already existed and matched, so the script left them alone"
elif [ "${APPLY_CRDS:-0}" = "1" ]; then
  say "The existing CRDs differ from this file. APPLY_CRDS=1 is set, so the script applies the file's CRDs"
  kubectl apply --server-side -f "$work/crds"
  crd_outcome="differed and were applied, because APPLY_CRDS=1 was set"
else
  say "The CRDs already exist and differ from this file, or another tool owns them"
  cat <<'EOF_CRDS'
This script does not change CRDs it did not just create.
If this is an upgrade, the objects that follow may need the newer CRDs.
To let this script apply the file's CRDs, re-run with:
  APPLY_CRDS=1 bash try-objects.sh
EOF_CRDS
  exit 1
fi
wait_for_crd cephconnections.csi.ceph.io
wait_for_crd clientprofiles.csi.ceph.io
wait_for_crd clientprofilemappings.csi.ceph.io
wait_for_crd drivers.csi.ceph.io
wait_for_crd operatorconfigs.csi.ceph.io
wait_for_crd cephblockpoolradosnamespaces.ceph.rook.io
wait_for_crd cephblockpools.ceph.rook.io
wait_for_crd cephbucketnotifications.ceph.rook.io
wait_for_crd cephbuckettopics.ceph.rook.io
wait_for_crd cephclients.ceph.rook.io
wait_for_crd cephclusters.ceph.rook.io
wait_for_crd cephcosidrivers.ceph.rook.io
wait_for_crd cephfilesystemmirrors.ceph.rook.io
wait_for_crd cephfilesystems.ceph.rook.io
wait_for_crd cephfilesystemsubvolumegroups.ceph.rook.io
wait_for_crd cephnfses.ceph.rook.io
wait_for_crd cephnvmeofgateways.ceph.rook.io
wait_for_crd cephobjectrealms.ceph.rook.io
wait_for_crd cephobjectstores.ceph.rook.io
wait_for_crd cephobjectstoreusers.ceph.rook.io
wait_for_crd cephobjectzonegroups.ceph.rook.io
wait_for_crd cephobjectzones.ceph.rook.io
wait_for_crd cephrbdmirrors.ceph.rook.io
wait_for_crd objectbucketclaims.objectbucket.io
wait_for_crd objectbuckets.objectbucket.io

say "Apply the other 108 objects"
kubectl apply -f "$work/rest"

say "Done. The 25 CRDs $crd_outcome. kubectl applied the other 108 objects of this entry's retained file."
say "The script stops here. It did not wait for any workload to start"
say "Not run by this script: 1 step of this entry's plan. The entry page says who runs it."
cat <<'EOF_NOT_RUN'
   - Prune protection for the 20 object(s) carrying the keep promise.
EOF_NOT_RUN
