Merge "Add wait command to VM e2etest site"
[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}/clusters-values.yaml >${BUILDDIR}/clusters-values.yaml
17 }
18
19 function deploy {
20     helm -n metal3 install machines ${SCRIPTDIR}/../../machines --create-namespace -f ${BUILDDIR}/machines-values.yaml
21     helm -n metal3 install clusters ${SCRIPTDIR}/../../clusters --create-namespace -f ${BUILDDIR}/clusters-values.yaml
22 }
23
24 function clean {
25     helm -n metal3 uninstall clusters
26     helm -n metal3 uninstall machines
27 }
28
29 function is_cluster_ready {
30     [[ $(kubectl -n metal3 get cluster e2etest -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') == "True" ]]
31 }
32
33 function are_kustomizations_ready {
34     [[ $(kubectl --kubeconfig=${BUILDDIR}/e2etest-admin.conf get Kustomization -n flux-system -o jsonpath='{range .items[*]}{.status.conditions[?(@.type=="Ready")].status}{"\n"}{end}' | grep -c -v True) == 0 ]]
35 }
36
37 function are_helmreleases_ready {
38     [[ $(kubectl --kubeconfig=${BUILDDIR}/e2etest-admin.conf get HelmRelease -n flux-system -o jsonpath='{range .items[*]}{.status.conditions[?(@.type=="Ready")].status}{"\n"}{end}' | grep -c -v True) == 0 ]]
39 }
40
41 function wait_for_all_ready {
42     WAIT_FOR_INTERVAL=60s
43     wait_for is_cluster_ready
44     clusterctl -n metal3 get kubeconfig e2etest >${BUILDDIR}/e2etest-admin.conf
45     chmod 600 ${BUILDDIR}/e2etest-admin.conf
46     # TODO The following checks are not ideal: resources created by
47     # operators aren't detected here, but this is the best that can be
48     # currently done
49     WAIT_FOR_INTERVAL=30s
50     wait_for are_kustomizations_ready
51     wait_for are_helmreleases_ready
52 }
53
54 case $1 in
55     "build") build ;;
56     "clean") clean ;;
57     "deploy") deploy ;;
58     "wait") wait_for_all_ready ;;
59     *) cat <<EOF
60 Usage: $(basename $0) COMMAND
61
62 Commands:
63   build         - Build the site deployment values
64   clean         - Remove the site
65   deploy        - Deploy the site
66   wait          - Wait for the site to be ready
67 EOF
68        ;;
69 esac