4 SCRIPTDIR="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
5 LIBDIR="$(dirname $(dirname $(dirname ${SCRIPTDIR})))/env/lib"
7 source $LIBDIR/common.sh
8 source $SCRIPTDIR/../common.sh
10 BUILDDIR=${SCRIPTDIR/deploy/build}
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"}
17 FLUX_SOPS_KEY_NAME=${FLUX_SOPS_KEY_NAME:-"icn-site-vm"}
18 FLUX_SOPS_PRIVATE_KEY="${SCRIPTDIR}/../secrets/sops.asc"
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}
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
41 sed -i -e 's!sshAuthorizedKey: .*!sshAuthorizedKey: '"${SSH_AUTHORIZED_KEY}"'!' ${yaml}
44 # Create root and intermediate CA certs for use by Istio in each
46 clone_istio_repository
47 local -r certs_dir=${SCRIPTDIR}/../secrets/certs
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
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}
70 # ONLY FOR TEST ENVIRONMENT: save the private key used
71 export_gpg_private_key ${FLUX_SOPS_KEY_NAME} >${FLUX_SOPS_PRIVATE_KEY}
75 gpg --import ${FLUX_SOPS_PRIVATE_KEY}
76 flux_create_site ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH} ${FLUX_SOPS_KEY_NAME}
80 kubectl -n flux-system delete kustomization $(flux_site_kustomization_name ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH})
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
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
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
109 if [[ ! $(which ipcalc) ]]; then
110 apt-get install -y ipcalc
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
127 cat <<EOF >>${HOME}/.ssh/config
129 IdentityFile ${SCRIPTDIR}/id_rsa
130 StrictHostKeyChecking no
131 UserKnownHostsFile /dev/null
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
141 function wait_for_all_ready {
142 WAIT_FOR_INTERVAL=60s
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
150 wait_for is_control_plane_ready
151 insert_control_plane_network_identity_into_ssh_config
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}
161 function wait_for_all_deleted {
162 WAIT_FOR_INTERVAL=60s
164 wait_for is_cluster_deleted
168 "build-source") build_source ;;
171 "wait") wait_for_all_ready ;;
172 "wait-clean") wait_for_all_deleted ;;
174 Usage: $(basename $0) COMMAND
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