fbe0890f02c45e4174f5f741632005d1e79a943f
[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 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/vm/deployment"}
16
17 function deploy {
18     gpg --import ${FLUX_SOPS_PRIVATE_KEY}
19     flux_create_site ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH} ${FLUX_SOPS_KEY_NAME}
20 }
21
22 function clean {
23     kubectl -n flux-system delete kustomization $(flux_site_kustomization_name ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH})
24 }
25
26 function is_cluster_ready {
27     [[ $(kubectl -n ${SITE_NAMESPACE} get cluster icn -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') == "True" ]]
28 }
29
30 function is_control_plane_ready {
31     # Checking the Cluster resource status is not sufficient, it
32     # reports the control plane as ready before the nodes forming the
33     # control plane are ready
34     local -r replicas=$(kubectl -n ${SITE_NAMESPACE} get kubeadmcontrolplane icn -o jsonpath='{.spec.replicas}')
35     [[ $(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} ]]
36 }
37
38 function insert_control_plane_network_identity_into_ssh_config {
39     # This enables logging into the control plane machines from this
40     # machine without specifying the identify file on the command line
41
42     if [[ ! $(which ipcalc) ]]; then
43         apt-get install -y ipcalc
44     fi
45
46     # Create ssh config if it doesn't exist
47     mkdir -p ${HOME}/.ssh && chmod 700 ${HOME}/.ssh
48     touch ${HOME}/.ssh/config
49     chmod 600 ${HOME}/.ssh/config
50     # Add the entry for the control plane network, host value in ssh
51     # config is a wildcard
52     endpoint=$(helm -n ${SITE_NAMESPACE} get values -a cluster-icn | awk '/controlPlaneEndpoint:/ {print $2}')
53     prefix=$(helm -n ${SITE_NAMESPACE} get values -a cluster-icn | awk '/controlPlanePrefix:/ {print $2}')
54     host=$(ipcalc ${endpoint}/${prefix} | awk '/Network:/ {sub(/\.0.*/,".*"); print $2}')
55     if [[ $(grep -c "Host ${host}" ${HOME}/.ssh/config) != 0 ]]; then
56         sed -i -e '/Host '"${host}"'/,+3 d' ${HOME}/.ssh/config
57     fi
58     cat <<EOF >>${HOME}/.ssh/config
59 Host ${host}
60   IdentityFile ${SCRIPTDIR}/id_rsa
61   StrictHostKeyChecking no
62   UserKnownHostsFile /dev/null
63 EOF
64     # Add the identity to authorized keys on this host to enable ssh
65     # logins via its control plane address
66     authorized_key=$(cat ${SCRIPTDIR}/id_rsa.pub)
67     sed -i -e '\!'"${authorized_key}"'!d' ${HOME}/.ssh/authorized_keys
68     cat ${SCRIPTDIR}/id_rsa.pub >> ~/.ssh/authorized_keys
69 }
70
71 function wait_for_all_ready {
72     WAIT_FOR_INTERVAL=60s
73     WAIT_FOR_TRIES=30
74     wait_for is_cluster_ready
75     clusterctl -n ${SITE_NAMESPACE} get kubeconfig icn >${BUILDDIR}/icn-admin.conf
76     chmod 600 ${BUILDDIR}/icn-admin.conf
77     wait_for is_control_plane_ready
78     insert_control_plane_network_identity_into_ssh_config
79 }
80
81 function is_cluster_deleted {
82     ! kubectl -n ${SITE_NAMESPACE} get cluster icn
83 }
84
85 function wait_for_all_deleted {
86     WAIT_FOR_INTERVAL=60s
87     WAIT_FOR_TRIES=30
88     wait_for is_cluster_deleted
89 }
90
91 case $1 in
92     "clean") clean ;;
93     "deploy") deploy ;;
94     "wait") wait_for_all_ready ;;
95     "wait-clean") wait_for_all_deleted ;;
96     *) cat <<EOF
97 Usage: $(basename $0) COMMAND
98
99 Commands:
100   clean         - Remove the site
101   deploy        - Deploy the site
102   wait          - Wait for the site to be ready
103 EOF
104        ;;
105 esac