6b5aa89d8054a89d4fa6ed70fa131253e2d71360
[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     pushd ${KUDPATH}/kud/hosting_providers/vagrant/
68     ansible-playbook -i inventory/hosts.ini /opt/kubespray-${KUBESPRAY_VERSION}/reset.yml \
69         --become --become-user=root -e reset_confirmation=yes
70     popd
71 }
72
73 function verifier {
74     APISERVER=$(kubectl config view --minify -o \
75                     jsonpath='{.clusters[0].cluster.server}')
76     TOKEN=$(kubectl get secret \
77         $(kubectl get serviceaccount default -o \
78         jsonpath='{.secrets[0].name}') -o jsonpath='{.data.token}' | \
79         base64 --decode )
80   call_api $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
81 }
82
83 if [ "$1" == "reset" ] ; then
84     kud_reset
85     exit 0
86 fi
87
88 get_kud_repo
89 set_ssh_key
90 set_bm_kud
91 kud_install
92 verifier
93
94 exit 0