882a8bda26532bee7c5fcd6da6b1ed3e63ebdf15
[icn.git] / deploy / site / vm / vm.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
9 BUILDDIR=${SCRIPTDIR/deploy/build}
10 mkdir -p ${BUILDDIR}
11
12 function build {
13     SSH_AUTHORIZED_KEY=$(cat ${HOME}/.ssh/id_rsa.pub)
14     # Use ! instead of usual / to avoid escaping / in
15     # SSH_AUTHORIZED_KEY
16     sed -e 's!sshAuthorizedKey: .*!sshAuthorizedKey: '"${SSH_AUTHORIZED_KEY}"'!' ${SCRIPTDIR}/cluster-e2etest-values.yaml >${BUILDDIR}/cluster-e2etest-values.yaml
17 }
18
19 function release_name {
20     local -r values_path=$1
21     name=$(basename ${values_path})
22     echo ${name%-values.yaml}
23 }
24
25 function deploy {
26     for values in ${BUILDDIR}/machine-*-values.yaml; do
27         helm -n metal3 install $(release_name ${values}) ${SCRIPTDIR}/../../machine --create-namespace -f ${values}
28     done
29     helm -n metal3 install cluster-e2etest ${SCRIPTDIR}/../../cluster --create-namespace -f ${BUILDDIR}/cluster-e2etest-values.yaml
30 }
31
32 function clean {
33     helm -n metal3 uninstall cluster-e2etest
34     for values in ${BUILDDIR}/machine-*-values.yaml; do
35         helm -n metal3 uninstall $(release_name ${values})
36     done
37 }
38
39 function is_cluster_ready {
40     [[ $(kubectl -n metal3 get cluster e2etest -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') == "True" ]]
41 }
42
43 function is_control_plane_ready {
44     # Checking the Cluster resource status is not sufficient, it
45     # reports the control plane as ready before the nodes forming the
46     # control plane are ready
47     local -r replicas=$(kubectl -n metal3 get kubeadmcontrolplane e2etest -o jsonpath='{.spec.replicas}')
48     [[ $(kubectl --kubeconfig=${BUILDDIR}/e2etest-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} ]]
49 }
50
51 function wait_for_all_ready {
52     WAIT_FOR_INTERVAL=60s
53     WAIT_FOR_TRIES=30
54     wait_for is_cluster_ready
55     clusterctl -n metal3 get kubeconfig e2etest >${BUILDDIR}/e2etest-admin.conf
56     chmod 600 ${BUILDDIR}/e2etest-admin.conf
57     wait_for is_control_plane_ready
58 }
59
60 case $1 in
61     "build") build ;;
62     "clean") clean ;;
63     "deploy") deploy ;;
64     "wait") wait_for_all_ready ;;
65     *) cat <<EOF
66 Usage: $(basename $0) COMMAND
67
68 Commands:
69   build         - Build the site deployment values
70   clean         - Remove the site
71   deploy        - Deploy the site
72   wait          - Wait for the site to be ready
73 EOF
74        ;;
75 esac