28530fca6858ead9c20385b81af5efd257828f7a
[icn.git] / deploy / site / pod11 / pod11.sh
1 #!/usr/bin/env bash
2 set -eux -o pipefail
3
4 SCRIPTDIR="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
5 LIBDIR="$(dirname $(dirname $(dirname ${SCRIPTDIR})))/env/lib"
6
7 source $LIBDIR/common.sh
8 source $SCRIPTDIR/../common.sh
9
10 BUILDDIR=${SCRIPTDIR/deploy/build}
11 mkdir -p ${BUILDDIR}
12
13 SITE_REPO=${SITE_REPO:-"https://gerrit.akraino.org/r/icn"}
14 SITE_BRANCH=${SITE_BRANCH:-"master"}
15 SITE_PATH=${SITE_PATH:-"deploy/site/pod11"}
16
17 FLUX_SOPS_KEY_NAME=${FLUX_SOPS_KEY_NAME:-"icn-site-vm"} # TODO Replace ICN test key with real key
18
19 function build_source {
20     sops_encrypt ${SCRIPTDIR}/site.yaml ${FLUX_SOPS_KEY_NAME}
21 }
22
23 function deploy {
24     flux_create_site ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH} ${FLUX_SOPS_KEY_NAME}
25 }
26
27 function clean {
28     kubectl -n flux-system delete kustomization $(flux_site_kustomization_name ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH})
29 }
30
31 function is_cluster_ready {
32     [[ $(kubectl -n ${SITE_NAMESPACE} get cluster icn -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') == "True" ]]
33 }
34
35 function is_control_plane_ready {
36     # Checking the Cluster resource status is not sufficient, it
37     # reports the control plane as ready before the nodes forming the
38     # control plane are ready
39     local -r replicas=$(kubectl -n ${SITE_NAMESPACE} get kubeadmcontrolplane icn -o jsonpath='{.spec.replicas}')
40     [[ $(kubectl --kubeconfig=${BUILDDIR}/icn-admin.conf get nodes -l node-role.kubernetes.io/control-plane -o jsonpath='{range .items[*]}{.status.conditions[?(@.type=="Ready")].status}{"\n"}{end}' | grep -c True) == ${replicas} ]]
41 }
42
43 function wait_for_all_ready {
44     WAIT_FOR_INTERVAL=60s
45     WAIT_FOR_TRIES=30
46     wait_for is_cluster_ready
47     clusterctl -n ${SITE_NAMESPACE} get kubeconfig icn >${BUILDDIR}/icn-admin.conf
48     chmod 600 ${BUILDDIR}/icn-admin.conf
49     wait_for is_control_plane_ready
50 }
51
52 function is_cluster_deleted {
53     ! kubectl -n ${SITE_NAMESPACE} get cluster icn
54 }
55
56 function wait_for_all_deleted {
57     WAIT_FOR_INTERVAL=60s
58     WAIT_FOR_TRIES=30
59     wait_for is_cluster_deleted
60 }
61
62 case $1 in
63     "build-source") build_source ;;
64     "clean") clean ;;
65     "deploy") deploy ;;
66     "wait") wait_for_all_ready ;;
67     "wait-clean") wait_for_all_deleted ;;
68     *) cat <<EOF
69 Usage: $(basename $0) COMMAND
70
71 Commands:
72   build-source  - Rebuild the in-tree site files
73   clean         - Remove the site
74   deploy        - Deploy the site
75   wait          - Wait for the site to be ready
76 EOF
77        ;;
78 esac