9ba2d403dada770c26b9a6c92f6371cd7f777898
[icn.git] / deploy / kud / kud_bm_launch.sh
1 #!/usr/bin/env bash
2 set -eu -o pipefail
3
4 SCRIPTDIR="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
5 LIBDIR="$(dirname $(dirname ${SCRIPTDIR}))/env/lib"
6
7 source $LIBDIR/common.sh
8
9 export KUBESPRAY_VERSION=2.16.0
10
11 function get_kud_repo {
12     clone_kud_repository
13     export KUD_ADDONS=multus
14 }
15
16 function set_ssh_key {
17     if ! [ -f ~/.ssh/id_rsa ]; then
18         echo "Generating rsa key for this host"
19         ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa <&-
20     fi
21
22     if ! grep -qF "$(ssh-keygen -y -f ~/.ssh/id_rsa)" ~/.ssh/authorized_keys; then
23         ssh-keygen -y -f ~/.ssh/id_rsa >> ~/.ssh/authorized_keys
24     fi
25
26     chmod og-wx ~/.ssh/authorized_keys
27 }
28
29 function set_bm_kud {
30     pushd ${KUDPATH}/kud/hosting_providers/vagrant/inventory
31     HOST_IP=${HOST_IP:-$(hostname -I | cut -d ' ' -f 1)}
32     DOCKER_OPTIONS=""
33     if [[ ! -z "${DOCKER_REGISTRY_MIRRORS+x}" ]]; then
34         OPTIONS=""
35         for mirror in ${DOCKER_REGISTRY_MIRRORS}; do
36             OPTIONS="${OPTIONS} --registry-mirror=${mirror}"
37         done
38         DOCKER_OPTIONS="docker_options=\"${OPTIONS# }\""
39     fi
40     cat <<EOL > hosts.ini
41 [all]
42 $HOSTNAME ansible_ssh_host=${HOST_IP} ansible_ssh_port=22 ${DOCKER_OPTIONS}
43
44 [kube-master]
45 $HOSTNAME
46
47 [kube-node]
48 $HOSTNAME
49
50 [etcd]
51 $HOSTNAME
52
53 [k8s-cluster:children]
54 kube-node
55 kube-master
56 EOL
57     popd
58 }
59
60 function kud_install {
61     pushd ${KUDPATH}/kud/hosting_providers/vagrant/
62     ./installer.sh | tee kud_deploy.log
63     popd
64 }
65
66 function kud_reset {
67     # Pick up the kubespray ANSIBLE_CONFIG from /etc/environment
68     source /etc/environment
69     pushd ${KUDPATH}/kud/hosting_providers/vagrant/
70     ansible-playbook -i inventory/hosts.ini /opt/kubespray-${KUBESPRAY_VERSION}/reset.yml \
71         --become --become-user=root -e reset_confirmation=yes
72     popd
73 }
74
75 function verifier {
76     APISERVER=$(kubectl config view --minify -o \
77                     jsonpath='{.clusters[0].cluster.server}')
78     TOKEN=$(kubectl get secret \
79         $(kubectl get serviceaccount default -o \
80         jsonpath='{.secrets[0].name}') -o jsonpath='{.data.token}' | \
81         base64 --decode )
82   call_api $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
83 }
84
85 if [ "$1" == "reset" ] ; then
86     kud_reset
87     exit 0
88 fi
89
90 get_kud_repo
91 set_ssh_key
92 set_bm_kud
93 kud_install
94 verifier
95
96 exit 0