4e1aa71d24c73935d6049bb40bd7ceef31171be5
[icn.git] / deploy / site / vm-mc / vm-mc.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-mc/deployment"}
16
17 FLUX_SOPS_KEY_NAME=${FLUX_SOPS_KEY_NAME:-"icn-site-vm"}
18 FLUX_SOPS_PRIVATE_KEY="${SCRIPTDIR}/../secrets/sops.asc"
19
20 # !!!NOTE!!! THE KEYS USED BELOW ARE FOR TEST PURPOSES ONLY.  DO NOT
21 # USE THESE OUTSIDE OF THIS ICN VIRTUAL TEST ENVIRONMENT.
22 function build_source {
23     # First decrypt the existing site YAML, otherwise we'll be
24     # attempting to encrypt it twice below
25     if [[ -f ${FLUX_SOPS_PRIVATE_KEY} ]]; then
26         gpg --import ${FLUX_SOPS_PRIVATE_KEY}
27         for yaml in ${SCRIPTDIR}/cluster*/*.yaml ${SCRIPTDIR}/deployment/*.yaml; do
28             sops_decrypt ${yaml} ${SCRIPTDIR}
29         done
30     fi
31
32     # Generate user password and authorized key in site YAML
33     # To login to guest, ssh -i ${SCRIPTDIR}/id_rsa
34     HASHED_PASSWORD=$(mkpasswd --method=SHA-512 --rounds 10000 "mypasswd")
35     ssh-keygen -t rsa -N "" -f ${SCRIPTDIR}/id_rsa <<<y
36     SSH_AUTHORIZED_KEY=$(cat ${SCRIPTDIR}/id_rsa.pub)
37     for yaml in ${SCRIPTDIR}/deployment/cluster-*.yaml; do
38         sed -i -e 's!hashedPassword: .*!hashedPassword: '"${HASHED_PASSWORD}"'!' ${yaml}
39         # Use ! instead of usual / to avoid escaping / in
40         # SSH_AUTHORIZED_KEY
41         sed -i -e 's!sshAuthorizedKey: .*!sshAuthorizedKey: '"${SSH_AUTHORIZED_KEY}"'!' ${yaml}
42     done
43
44     # Create root and intermediate CA certs for use by Istio in each
45     # cluster
46     clone_istio_repository
47     local -r certs_dir=${SCRIPTDIR}/../secrets/certs
48     rm -rf ${certs_dir}
49     mkdir -p ${certs_dir}
50     certs=${ISTIOPATH}/tools/certs
51     make -C ${certs} -f Makefile.selfsigned.mk ROOT_CN="EMCO Root CA" ROOTCA_ORG=project-emco.io root-ca
52     find ${certs}/root-* -exec cp '{}' ${certs_dir} ';'
53     for yaml in ${SCRIPTDIR}/deployment/cluster-*.yaml; do
54         name=$(awk '/clusterName:/ {print $2}' ${yaml})
55         make -C ${certs} -f Makefile.selfsigned.mk INTERMEDIATE_CN="EMCO Intermediate CA" INTERMEDIATE_ORG=project-emco.io ${name}-cacerts
56         cp -r ${certs}/${name} ${certs_dir}
57         kubectl create secret generic cacerts -n istio-system --dry-run=client -o yaml \
58             --from-file=${certs}/${name}/ca-cert.pem \
59             --from-file=${certs}/${name}/ca-key.pem \
60             --from-file=${certs}/${name}/root-cert.pem \
61             --from-file=${certs}/${name}/cert-chain.pem >${SCRIPTDIR}/${name}/istio-cacerts.yaml
62     done
63
64     # Encrypt the site YAML
65     create_gpg_key ${FLUX_SOPS_KEY_NAME}
66     for yaml in ${SCRIPTDIR}/cluster*/*.yaml ${SCRIPTDIR}/deployment/*.yaml; do
67         sops_encrypt ${yaml} ${FLUX_SOPS_KEY_NAME} ${SCRIPTDIR}
68     done
69
70     # ONLY FOR TEST ENVIRONMENT: save the private key used
71     export_gpg_private_key ${FLUX_SOPS_KEY_NAME} >${FLUX_SOPS_PRIVATE_KEY}
72 }
73
74 function deploy {
75     gpg --import ${FLUX_SOPS_PRIVATE_KEY}
76     flux_create_site ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH} ${FLUX_SOPS_KEY_NAME}
77 }
78
79 function clean {
80     kubectl -n flux-system delete kustomization $(flux_site_kustomization_name ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH})
81 }
82
83 function is_cluster_ready {
84     for yaml in ${SCRIPTDIR}/deployment/cluster-*.yaml; do
85         name=$(awk '/clusterName:/ {print $2}' ${yaml})
86         if [[ $(kubectl -n ${SITE_NAMESPACE} get cluster ${name} -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') != "True" ]]; then
87             return 1
88         fi
89     done
90 }
91
92 function is_control_plane_ready {
93     # Checking the Cluster resource status is not sufficient, it
94     # reports the control plane as ready before the nodes forming the
95     # control plane are ready
96     for yaml in ${SCRIPTDIR}/deployment/cluster-*.yaml; do
97         name=$(awk '/clusterName:/ {print $2}' ${yaml})
98         local replicas=$(kubectl -n ${SITE_NAMESPACE} get kubeadmcontrolplane ${name} -o jsonpath='{.spec.replicas}')
99         if [[ $(kubectl --kubeconfig=${BUILDDIR}/${name}-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} ]]; then
100             return 1
101         fi
102     done
103 }
104
105 function insert_control_plane_network_identity_into_ssh_config {
106     # This enables logging into the control plane machines from this
107     # machine without specifying the identify file on the command line
108
109     if [[ ! $(which ipcalc) ]]; then
110         apt-get install -y ipcalc
111     fi
112
113     # Create ssh config if it doesn't exist
114     mkdir -p ${HOME}/.ssh && chmod 700 ${HOME}/.ssh
115     touch ${HOME}/.ssh/config
116     chmod 600 ${HOME}/.ssh/config
117     # Add the entry for the control plane network, host value in ssh
118     # config is a wildcard
119     for yaml in ${SCRIPTDIR}/deployment/cluster-*.yaml; do
120         name=$(awk '/name:/ {print $2; exit}' ${yaml})
121         endpoint=$(helm -n ${SITE_NAMESPACE} get values -a ${name} | awk '/controlPlaneEndpoint:/ {print $2}')
122         prefix=$(helm -n ${SITE_NAMESPACE} get values -a ${name} | awk '/controlPlanePrefix:/ {print $2}')
123         host=$(ipcalc ${endpoint}/${prefix} | awk '/Network:/ {sub(/\.0.*/,".*"); print $2}')
124         if [[ $(grep -c "Host ${host}" ${HOME}/.ssh/config) != 0 ]]; then
125             sed -i -e '/Host '"${host}"'/,+3 d' ${HOME}/.ssh/config
126         fi
127         cat <<EOF >>${HOME}/.ssh/config
128 Host ${host}
129   IdentityFile ${SCRIPTDIR}/id_rsa
130   StrictHostKeyChecking no
131   UserKnownHostsFile /dev/null
132 EOF
133     done
134     # Add the identity to authorized keys on this host to enable ssh
135     # logins via its control plane address
136     authorized_key=$(cat ${SCRIPTDIR}/id_rsa.pub)
137     sed -i -e '\!'"${authorized_key}"'!d' ${HOME}/.ssh/authorized_keys
138     cat ${SCRIPTDIR}/id_rsa.pub >> ~/.ssh/authorized_keys
139 }
140
141 function wait_for_all_ready {
142     WAIT_FOR_INTERVAL=60s
143     WAIT_FOR_TRIES=30
144     wait_for is_cluster_ready
145     for yaml in ${SCRIPTDIR}/deployment/cluster-*.yaml; do
146         name=$(awk '/clusterName:/ {print $2}' ${yaml})
147         clusterctl -n ${SITE_NAMESPACE} get kubeconfig ${name} >${BUILDDIR}/${name}-admin.conf
148         chmod 600 ${BUILDDIR}/${name}-admin.conf
149     done
150     wait_for is_control_plane_ready
151     insert_control_plane_network_identity_into_ssh_config
152 }
153
154 function is_cluster_deleted {
155     for yaml in ${SCRIPTDIR}/deployment/cluster-*.yaml; do
156         name=$(awk '/clusterName:/ {print $2}' ${yaml})
157         ! kubectl -n ${SITE_NAMESPACE} get cluster ${name}
158     done
159 }
160
161 function wait_for_all_deleted {
162     WAIT_FOR_INTERVAL=60s
163     WAIT_FOR_TRIES=30
164     wait_for is_cluster_deleted
165 }
166
167 case $1 in
168     "build-source") build_source ;;
169     "clean") clean ;;
170     "deploy") deploy ;;
171     "wait") wait_for_all_ready ;;
172     "wait-clean") wait_for_all_deleted ;;
173     *) cat <<EOF
174 Usage: $(basename $0) COMMAND
175
176 Commands:
177   build-source  - Build the in-tree site values
178   clean         - Remove the site
179   deploy        - Deploy the site
180   wait          - Wait for the site to be ready
181 EOF
182        ;;
183 esac