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