Pin ara version to 0.16.4
[releng-xci.git] / xci / installer / kubespray / deploy.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2017 Huawei
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 set -o errexit
11 set -o nounset
12 set -o pipefail
13
14 K8_XCI_PLAYBOOKS="$(dirname $(realpath ${BASH_SOURCE[0]}))/playbooks"
15 export ANSIBLE_ROLES_PATH=$HOME/.ansible/roles:/etc/ansible/roles:${XCI_PATH}/xci/playbooks/roles
16
17 #-------------------------------------------------------------------------------
18 # Configure localhost
19 #-------------------------------------------------------------------------------
20 # This playbook
21 # - removes directories that were created by the previous xci run
22 # - clones opnfv/releng-xci repository
23 # - clones kubernetes-incubator/kubespray repository
24 # - creates log directory
25 #-------------------------------------------------------------------------------
26
27 echo "Info: Configuring localhost for kubespray"
28 echo "-----------------------------------------------------------------------"
29 cd $XCI_PLAYBOOKS
30 ansible-playbook ${XCI_ANSIBLE_PARAMS} -e XCI_PATH="${XCI_PATH}" \
31         -i dynamic_inventory.py configure-localhost.yml
32 echo "-----------------------------------------------------------------------"
33 echo "Info: Configured localhost for kubespray"
34
35 #-------------------------------------------------------------------------------
36 # Configure deployment host, opnfv
37 #-------------------------------------------------------------------------------
38 # This playbook
39 # - removes directories that were created by the previous xci run
40 # - synchronize opnfv/releng-xci and kubernetes-incubator/kubespray repositories
41 # - generates/prepares ssh keys
42 # - copies flavor files to be used by kubespray
43 # - install packages required by kubespray
44 #-------------------------------------------------------------------------------
45 echo "Info: Configuring opnfv deployment host for kubespray"
46 echo "-----------------------------------------------------------------------"
47 cd $K8_XCI_PLAYBOOKS
48 ansible-playbook ${XCI_ANSIBLE_PARAMS} \
49         -i ${XCI_PLAYBOOKS}/dynamic_inventory.py configure-opnfvhost.yml
50 echo "-----------------------------------------------------------------------"
51 echo "Info: Configured opnfv deployment host for kubespray"
52
53 #-------------------------------------------------------------------------------
54 # Configure target hosts for kubespray
55 #-------------------------------------------------------------------------------
56 # This playbook is only run for the all flavors except aio since aio is configured by the configure-opnfvhost.yml
57 # This playbook
58 # - adds public keys to target hosts
59 # - install packages required by kubespray
60 # - configures haproxy service
61 #-------------------------------------------------------------------------------
62 if [ $XCI_FLAVOR != "aio" ]; then
63     echo "Info: Configuring target hosts for kubespray"
64     echo "-----------------------------------------------------------------------"
65     cd $K8_XCI_PLAYBOOKS
66     ansible-playbook ${XCI_ANSIBLE_PARAMS} \
67             -i ${XCI_PLAYBOOKS}/dynamic_inventory.py configure-targethosts.yml
68     echo "-----------------------------------------------------------------------"
69     echo "Info: Configured target hosts for kubespray"
70 fi
71
72
73 echo "Info: Using kubespray to deploy the kubernetes cluster"
74 echo "-----------------------------------------------------------------------"
75 ssh root@$OPNFV_HOST_IP "set -o pipefail; export XCI_FLAVOR=$XCI_FLAVOR; export INSTALLER_TYPE=$INSTALLER_TYPE; \
76         export IDF=/root/releng-xci/xci/var/idf.yml; export PDF=/root/releng-xci/xci/var/pdf.yml; \
77         cd releng-xci/.cache/repos/kubespray/; ansible-playbook \
78         -i opnfv_inventory/dynamic_inventory.py cluster.yml -b | tee setup-kubernetes.log"
79 scp root@$OPNFV_HOST_IP:~/releng-xci/.cache/repos/kubespray/setup-kubernetes.log \
80         $LOG_PATH/setup-kubernetes.log
81
82
83 cd $K8_XCI_PLAYBOOKS
84 ansible-playbook ${XCI_ANSIBLE_PARAMS} \
85     -i ${XCI_PLAYBOOKS}/dynamic_inventory.py configure-kubenet.yml
86 echo
87 echo "-----------------------------------------------------------------------"
88 echo "Info: Kubernetes installation is successfully completed!"
89 echo "-----------------------------------------------------------------------"
90
91 # Configure the kubernetes authentication in opnfv host. In future releases
92 # kubectl is no longer an artifact so we should not fail if it's not available.
93 # This needs to be removed in the future
94 ssh root@$OPNFV_HOST_IP "mkdir -p ~/.kube/;\
95          cp -f ~/admin.conf ~/.kube/config; \
96          cp -f ~/kubectl /usr/local/bin || true"
97
98 #-------------------------------------------------------------------------------
99 # Execute post-installation tasks
100 #-------------------------------------------------------------------------------
101 # Playbook post.yml is used in order to execute any post-deployment tasks that
102 # are required for the scenario under test.
103 #-------------------------------------------------------------------------------
104 echo "-----------------------------------------------------------------------"
105 echo "Info: Running post-deployment scenario role"
106 echo "-----------------------------------------------------------------------"
107 cd $K8_XCI_PLAYBOOKS
108 ansible-playbook ${XCI_ANSIBLE_PARAMS} -i ${XCI_PLAYBOOKS}/dynamic_inventory.py \
109     post-deployment.yml
110 echo "-----------------------------------------------------------------------"
111 echo "Info: Post-deployment scenario role execution done"
112 echo "-----------------------------------------------------------------------"
113 echo
114 echo "Login opnfv host ssh root@$OPNFV_HOST_IP
115 according to the user-guide to create a service
116 https://kubernetes.io/docs/user-guide/walkthrough/k8s201/"
117 echo
118 echo "-----------------------------------------------------------------------"
119 echo "Info: Kubernetes login details"
120 echo "-----------------------------------------------------------------------"
121 echo
122 # Get the dashborad URL
123 DASHBOARD_SERVICE=$(ssh root@$OPNFV_HOST_IP "kubectl get service -n kube-system |grep kubernetes-dashboard")
124 DASHBOARD_PORT=$(echo ${DASHBOARD_SERVICE} | awk '{print $5}' |awk -F "[:/]" '{print $2}')
125 KUBER_SERVER_URL=$(ssh root@$OPNFV_HOST_IP "grep -r server ~/.kube/config")
126 echo "Info: Kubernetes Dashboard URL:"
127 echo $KUBER_SERVER_URL | awk '{print $2}'| sed -n "s#:[0-9]*\$#:$DASHBOARD_PORT#p"
128
129 # Get the dashborad user and password
130 MASTER_IP=$(echo ${KUBER_SERVER_URL} | awk '{print $2}' |awk -F "[:/]" '{print $4}')
131 USER_CSV=$(ssh root@$MASTER_IP " cat /etc/kubernetes/users/known_users.csv")
132 USERNAME=$(echo $USER_CSV |awk -F ',' '{print $2}')
133 PASSWORD=$(echo $USER_CSV |awk -F ',' '{print $1}')
134 echo "Info: Dashboard username: ${USERNAME}"
135 echo "Info: Dashboard password: ${PASSWORD}"
136
137 # vim: set ts=4 sw=4 expandtab: