c533f2722c8961f00766d992e9a584dcd95aced8
[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"}
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         sops_decrypt ${SCRIPTDIR}/site.yaml
28     fi
29
30     # Generate user password and authorized key in site YAML
31     # To login to guest, ssh -i ${SCRIPTDIR}/id_rsa
32     HASHED_PASSWORD=$(mkpasswd --method=SHA-512 --rounds 10000 "mypasswd")
33     sed -i -e 's!hashedPassword: .*!hashedPassword: '"${HASHED_PASSWORD}"'!' ${SCRIPTDIR}/site.yaml
34     ssh-keygen -t rsa -N "" -f ${SCRIPTDIR}/id_rsa <<<y
35     SSH_AUTHORIZED_KEY=$(cat ${SCRIPTDIR}/id_rsa.pub)
36     # Use ! instead of usual / to avoid escaping / in
37     # SSH_AUTHORIZED_KEY
38     sed -i -e 's!sshAuthorizedKey: .*!sshAuthorizedKey: '"${SSH_AUTHORIZED_KEY}"'!' ${SCRIPTDIR}/site.yaml
39
40     # Encrypt the site YAML
41     create_gpg_key ${FLUX_SOPS_KEY_NAME}
42     sops_encrypt ${SCRIPTDIR}/site.yaml ${FLUX_SOPS_KEY_NAME}
43
44     # ONLY FOR TEST ENVIRONMENT: save the private key used
45     export_gpg_private_key ${FLUX_SOPS_KEY_NAME} >${FLUX_SOPS_PRIVATE_KEY}
46 }
47
48 function deploy {
49     gpg --import ${FLUX_SOPS_PRIVATE_KEY}
50     flux_create_site ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH} ${FLUX_SOPS_KEY_NAME}
51 }
52
53 function clean {
54     kubectl -n flux-system delete kustomization $(flux_site_kustomization_name ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH})
55 }
56
57 function is_cluster_ready {
58     [[ $(kubectl -n ${SITE_NAMESPACE} get cluster icn -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') == "True" ]]
59 }
60
61 function is_control_plane_ready {
62     # Checking the Cluster resource status is not sufficient, it
63     # reports the control plane as ready before the nodes forming the
64     # control plane are ready
65     local -r replicas=$(kubectl -n ${SITE_NAMESPACE} get kubeadmcontrolplane icn -o jsonpath='{.spec.replicas}')
66     [[ $(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} ]]
67 }
68
69 function insert_control_plane_network_identity_into_ssh_config {
70     # This enables logging into the control plane machines from this
71     # machine without specifying the identify file on the command line
72
73     if [[ ! $(which ipcalc) ]]; then
74         apt-get install -y ipcalc
75     fi
76
77     # Create ssh config if it doesn't exist
78     mkdir -p ${HOME}/.ssh && chmod 700 ${HOME}/.ssh
79     touch ${HOME}/.ssh/config
80     chmod 600 ${HOME}/.ssh/config
81     # Add the entry for the control plane network, host value in ssh
82     # config is a wildcard
83     endpoint=$(helm -n ${SITE_NAMESPACE} get values -a cluster-icn | awk '/controlPlaneEndpoint:/ {print $2}')
84     prefix=$(helm -n ${SITE_NAMESPACE} get values -a cluster-icn | awk '/controlPlanePrefix:/ {print $2}')
85     host=$(ipcalc ${endpoint}/${prefix} | awk '/Network:/ {sub(/\.0.*/,".*"); print $2}')
86     if [[ $(grep -c "Host ${host}" ${HOME}/.ssh/config) != 0 ]]; then
87         sed -i -e '/Host '"${host}"'/,+1 d' ${HOME}/.ssh/config
88     fi
89     cat <<EOF >>${HOME}/.ssh/config
90 Host ${host}
91   IdentityFile ${SCRIPTDIR}/id_rsa
92   StrictHostKeyChecking no
93   UserKnownHostsFile /dev/null
94 EOF
95     # Add the identity to authorized keys on this host to enable ssh
96     # logins via its control plane address
97     cat ${SCRIPTDIR}/id_rsa.pub >> ~/.ssh/authorized_keys
98 }
99
100 function wait_for_all_ready {
101     WAIT_FOR_INTERVAL=60s
102     WAIT_FOR_TRIES=30
103     wait_for is_cluster_ready
104     clusterctl -n ${SITE_NAMESPACE} get kubeconfig icn >${BUILDDIR}/icn-admin.conf
105     chmod 600 ${BUILDDIR}/icn-admin.conf
106     wait_for is_control_plane_ready
107     insert_control_plane_network_identity_into_ssh_config
108 }
109
110 case $1 in
111     "build-source") build_source ;;
112     "clean") clean ;;
113     "deploy") deploy ;;
114     "wait") wait_for_all_ready ;;
115     *) cat <<EOF
116 Usage: $(basename $0) COMMAND
117
118 Commands:
119   build-source  - Build the in-tree site values
120   clean         - Remove the site
121   deploy        - Deploy the site
122   wait          - Wait for the site to be ready
123 EOF
124        ;;
125 esac