Rename cluster-e2etest to cluster-icn
[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 FLUX_SOPS_KEY_NAME=${FLUX_SOPS_KEY_NAME:-"icn-site-vm"} # TODO Replace ICN test key with real key
14
15 function build_source {
16     sops_encrypt_site ${SCRIPTDIR}/site.yaml ${FLUX_SOPS_KEY_NAME}
17 }
18
19 function deploy {
20     flux_create_site https://gerrit.akraino.org/r/icn master deploy/site/pod11 ${FLUX_SOPS_KEY_NAME}
21 }
22
23 function clean {
24     kubectl -n flux-system delete kustomization icn-master-site-pod11
25 }
26
27 function is_cluster_ready {
28     [[ $(kubectl -n metal3 get cluster icn -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') == "True" ]]
29 }
30
31 function is_control_plane_ready {
32     # Checking the Cluster resource status is not sufficient, it
33     # reports the control plane as ready before the nodes forming the
34     # control plane are ready
35     local -r replicas=$(kubectl -n metal3 get kubeadmcontrolplane icn -o jsonpath='{.spec.replicas}')
36     [[ $(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} ]]
37 }
38
39 function insert_control_plane_network_identity_into_ssh_config {
40     # This enables logging into the control plane machines from this
41     # machine without specifying the identify file on the command line
42
43     # Create ssh config if it doesn't exist
44     mkdir -p ${HOME}/.ssh && chmod 700 ${HOME}/.ssh
45     touch ${HOME}/.ssh/config
46     chmod 600 ${HOME}/.ssh/config
47     # Add the entry for the control plane network, host value in ssh
48     # config is a wildcard
49     endpoint=$(helm -n metal3 get values -a cluster-icn | awk '/controlPlaneEndpoint:/ {print $2}')
50     prefix=$(helm -n metal3 get values -a cluster-icn | awk '/controlPlanePrefix:/ {print $2}')
51     host=$(ipcalc ${endpoint}/${prefix} | awk '/Network:/ {sub(/\.0.*/,".*"); print $2}')
52     if [[ $(grep -c "Host ${host}" ${HOME}/.ssh/config) != 0 ]]; then
53         sed -i -e '/Host '"${host}"'/,+1 d' ${HOME}/.ssh/config
54     fi
55     cat <<EOF >>${HOME}/.ssh/config
56 Host ${host}
57   IdentityFile ${SCRIPTDIR}/id_rsa
58 EOF
59 }
60
61 function wait_for_all_ready {
62     WAIT_FOR_INTERVAL=60s
63     WAIT_FOR_TRIES=30
64     wait_for is_cluster_ready
65     clusterctl -n metal3 get kubeconfig icn >${BUILDDIR}/icn-admin.conf
66     chmod 600 ${BUILDDIR}/icn-admin.conf
67     wait_for is_control_plane_ready
68     insert_control_plane_network_identity_into_ssh_config
69 }
70
71 case $1 in
72     "build-source") build_source ;;
73     "clean") clean ;;
74     "deploy") deploy ;;
75     "wait") wait_for_all_ready ;;
76     *) cat <<EOF
77 Usage: $(basename $0) COMMAND
78
79 Commands:
80   build-source  - Rebuild the in-tree site files
81   clean         - Remove the site
82   deploy        - Deploy the site
83   wait          - Wait for the site to be ready
84 EOF
85        ;;
86 esac