#!/usr/bin/env bash
# prometheus-community/kube-prometheus-stack 84.4.0 - base variant: aicr-eks-training-v1-0-0
# 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/prometheus-community-kube-prometheus-stack-84-4-0-aicr-eks-training-v1-0-0.json
# Entry page: https://confighub.github.io/helm-expt/site/charts/prometheus-community-kube-prometheus-stack-84-4-0.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=./prometheus-community-kube-prometheus-stack-84-4-0-aicr-eks-training-v1-0-0-objects
objects_url=https://raw.githubusercontent.com/confighub/helm-expt/main/recipes/prometheus-community/kube-prometheus-stack/84.4.0/revisions/aicr-eks-training-v1-0-0/r001/rendered/release-objects.yaml
objects_sha256=55c31cf72066a59e0c7f1e67c453e9487160ab7683b7320ce4ea280b00cd194a

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 116 objects. The cluster is untouched so far"
rm -rf "$work/docs" "$work/rest"
mkdir -p "$work/docs" "$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 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 0024; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0025 0026 0027 0028 0029 0030 0031 0032 0033 0034 0035 0036; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0037 0038 0039 0040 0041 0042 0043 0044 0045 0046 0047 0048; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0049 0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 0060; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0061 0062 0063 0064 0065 0066 0067 0068 0069 0070 0071 0072; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0073 0074 0075 0076 0077 0078 0079 0080 0081 0082 0083 0084; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0085 0086 0087 0088 0089 0090 0091 0092 0093 0094 0095 0096; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0097 0098 0099 0100 0101 0102 0103 0104 0105 0106 0107 0108; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0109 0110 0111 0112 0113 0114 0115 0116; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
ls "$work/rest"

if [ "${REQUIREMENTS_READY:-0}" != "1" ]; then
  say "Stop before applying anything. This entry's plan has 3 steps that an install needs first"
  cat >&2 <<'EOF_REQUIREMENTS'
This script cannot carry out these steps of the plan. They are yours to do, with your own values:
  - kubectl apply -f <preflight-job>; kubectl wait --for=condition=complete job/<name>
  - Your applier — must apply CRDs before dependent objects
  - # create the required target facts (Secret/StorageClass/etc.) before apply
Nothing was applied. Complete them before you apply the objects.
When they are done, re-run with:
  REQUIREMENTS_READY=1 bash try-objects.sh
EOF_REQUIREMENTS
  exit 1
fi
say "REQUIREMENTS_READY=1 is set. The script takes your word for 3 steps and does not check them"

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

serves_kind() {
  kubectl api-resources --api-group="$1" --no-headers 2>/dev/null | awk -v kind="$2" '$NF == kind { found = 1 } END { exit found ? 0 : 1 }'
}

say "Check that the cluster serves the 4 kinds this file uses and does not define"
missing_kinds=""
if ! serves_kind monitoring.coreos.com Alertmanager; then
  missing_kinds="$missing_kinds Alertmanager.monitoring.coreos.com"
fi
if ! serves_kind monitoring.coreos.com Prometheus; then
  missing_kinds="$missing_kinds Prometheus.monitoring.coreos.com"
fi
if ! serves_kind monitoring.coreos.com PrometheusRule; then
  missing_kinds="$missing_kinds PrometheusRule.monitoring.coreos.com"
fi
if ! serves_kind monitoring.coreos.com ServiceMonitor; then
  missing_kinds="$missing_kinds ServiceMonitor.monitoring.coreos.com"
fi
if [ -n "$missing_kinds" ]; then
  printf 'The cluster does not serve:%s\n' "$missing_kinds" >&2
  printf 'This file uses these kinds and does not define them. Install what defines them, then re-run this script. Nothing was applied.\n' >&2
  exit 1
fi

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

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

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

say "Done. kubectl applied the 116 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: 7 steps of this entry's plan. The entry page says who runs each one."
cat <<'EOF_NOT_RUN'
   - kubectl apply -f <preflight-job>; kubectl wait --for=condition=complete job/<name>
   - Your applier — must apply CRDs before dependent objects
   - # create the required target facts (Secret/StorageClass/etc.) before apply
   - Your cluster — at uninstall, automatically
   - Your delivery — a post-apply check (receipted)
   - kubectl apply -f <action-job>; kubectl wait --for=condition=complete job/<name>
   - kubectl get validatingwebhookconfigurations,mutatingwebhookconfigurations
EOF_NOT_RUN
