blob: aab3986e17beca1e705907527aaaee3dcd054da4 [file] [log] [blame]
Luca Pretec97ad882018-05-04 11:22:14 -07001#!/usr/bin/env bash
2#
3# Copyright 2017-present Open Networking Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Luca Pretec97ad882018-05-04 11:22:14 -070017#
18# Installs Kubespray on remote target machines.
19#
Zack Williams11b2e5c2018-05-18 09:50:54 -070020
Zack Williamsd24053d2018-07-31 09:12:35 -070021set -e -o pipefail
Zack Williams11b2e5c2018-05-18 09:50:54 -070022
Zack Williamsbb846a22018-06-26 14:57:17 -070023KS_COMMIT="${KS_COMMIT:-73a2a180061113ac124683e5cc492ba07df33d4c}"
24
Luca Pretec97ad882018-05-04 11:22:14 -070025install_kubespray () {
26 # Cleanup Old Kubespray Installations
Zack Williams249ac7f2018-05-18 13:34:14 -070027 echo "Cleaning Up Old Kubespray Installation"
Luca Pretec97ad882018-05-04 11:22:14 -070028 rm -rf kubespray
29
30 # Download Kubespray
Zack Williams249ac7f2018-05-18 13:34:14 -070031 echo "Downloading Kubespray"
Zack Williamsbb846a22018-06-26 14:57:17 -070032 git clone https://github.com/kubernetes-incubator/kubespray.git
33 pushd kubespray
34 git checkout "$KS_COMMIT"
35 popd
Luca Pretec97ad882018-05-04 11:22:14 -070036
Zack Williamsd24053d2018-07-31 09:12:35 -070037 # create a virtualenv with specific packages, if it doesn't exist
38 if [ ! -x "ks_venv/bin/activate" ]
39 then
40 virtualenv ks_venv
41 pip install ansible==2.5.3
Kailash Khalasif3756622018-08-06 14:57:49 -070042 pip install -r kubespray/requirements.txt
Zack Williamsd24053d2018-07-31 09:12:35 -070043 fi
44
45 # shellcheck disable=SC1091
46 source ks_venv/bin/activate
47
Luca Pretec97ad882018-05-04 11:22:14 -070048 # Generate inventory and var files
Zack Williams249ac7f2018-05-18 13:34:14 -070049 echo "Generating The Inventory File"
Zack Williamsbb846a22018-06-26 14:57:17 -070050
Zack Williams249ac7f2018-05-18 13:34:14 -070051 rm -rf "inventories/${DEPLOYMENT_NAME}"
Zack Williamsbb846a22018-06-26 14:57:17 -070052 mkdir -p "inventories/${DEPLOYMENT_NAME}"
53
54 cp -r kubespray/inventory/sample/group_vars "inventories/${DEPLOYMENT_NAME}/group_vars"
Zack Williams249ac7f2018-05-18 13:34:14 -070055 CONFIG_FILE="inventories/${DEPLOYMENT_NAME}/inventory.cfg" python3 kubespray/contrib/inventory_builder/inventory.py "${NODES[@]}"
Luca Pretec97ad882018-05-04 11:22:14 -070056
Zack Williams249ac7f2018-05-18 13:34:14 -070057 # Add configuration to inventory
58 ansible-playbook k8s-configs.yaml --extra-vars "deployment_name=${DEPLOYMENT_NAME} k8s_nodes='${NODES[*]}' kubespray_remote_ssh_user='${REMOTE_SSH_USER}'"
Luca Pretec97ad882018-05-04 11:22:14 -070059
60 # Prepare Target Machines
Zack Williams249ac7f2018-05-18 13:34:14 -070061 echo "Installing Prerequisites On Remote Machines"
62 ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.cfg" k8s-requirements.yaml
Luca Pretec97ad882018-05-04 11:22:14 -070063
64 # Install Kubespray
Zack Williams249ac7f2018-05-18 13:34:14 -070065 echo "Installing Kubespray"
66 ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.cfg" kubespray/cluster.yml -b -v
Luca Pretec97ad882018-05-04 11:22:14 -070067}
68
69#
70# Exports the Kubespray Config Location
71#
72source_kubeconfig () {
Zack Williamsbb846a22018-06-26 14:57:17 -070073
74 kubeconfig_path="${PWD}/inventories/${DEPLOYMENT_NAME}/artifacts/admin.conf"
75
76 if [ -f "$kubeconfig_path" ]
77 then
78 # these options are annoying outside of scripts
79 set +e +u +o pipefail
80
81 echo "setting KUBECONFIG=$kubeconfig_path"
82
83 export KUBECONFIG="$kubeconfig_path"
84 else
85 echo "kubernetes admin.conf not found at: '$kubeconfig_path'"
86 exit 1
87 fi
Luca Pretec97ad882018-05-04 11:22:14 -070088}
89
90#
Luca Pretec97ad882018-05-04 11:22:14 -070091# Checks if an arbitrary pod name is given during specifc
92# operations.
93#
94check_pod_name () {
Zack Williams11b2e5c2018-05-18 09:50:54 -070095 if [ -z "$DEPLOYMENT_NAME" ]
Luca Pretec97ad882018-05-04 11:22:14 -070096 then
97 echo "Missing option: podname" >&2
98 echo " "
99 display_help
100 exit -1
101 fi
102}
103
104#
105# Displays the help menu.
106#
107display_help () {
108 echo "Usage: $0 {--install|--source|--help} [podname] [ip...] " >&2
109 echo " "
110 echo " -h, --help Display this help message."
111 echo " -i, --install Install Kubespray on <podname>"
112 echo " -s, --source Source the Kubectl config for <podname>"
113 echo " "
114 echo " podname An arbitrary name representing the pod"
Kailash Khalasi236b0f62018-06-27 08:50:39 -0700115 echo " ip The IP address of the remote node(s)"
Luca Pretec97ad882018-05-04 11:22:14 -0700116 echo " "
117 echo "Example usages:"
118 echo " ./setup.sh -i podname 192.168.10.100 192.168.10.101 192.168.10.102"
Zack Williams249ac7f2018-05-18 13:34:14 -0700119 echo " ./setup.sh -i podname (default is 10.90.0.101 10.90.0.102 10.90.0.103)"
Luca Pretec97ad882018-05-04 11:22:14 -0700120 echo " source setup.sh -s podname"
121}
122
123#
124# Init
125#
Kailash Khalasic09de4b2018-06-13 13:56:18 -0700126if [ $# -lt 2 ]
127then
128 display_help
129 exit 0
130fi
131
Luca Pretec97ad882018-05-04 11:22:14 -0700132CLI_OPT=$1
133DEPLOYMENT_NAME=$2
134shift 2
Zack Williams249ac7f2018-05-18 13:34:14 -0700135DEFAULT_NODES=(10.90.0.101 10.90.0.102 10.90.0.103)
136NODES=("${@:-${DEFAULT_NODES[@]}}")
137
138REMOTE_SSH_USER="${REMOTE_SSH_USER:-cord}"
Luca Pretec97ad882018-05-04 11:22:14 -0700139
140while :
141do
142 case $CLI_OPT in
143 -i | --install)
144 check_pod_name
Zack Williams249ac7f2018-05-18 13:34:14 -0700145 install_kubespray
Luca Pretec97ad882018-05-04 11:22:14 -0700146 exit 0
147 ;;
148 -h | --help)
149 display_help
150 exit 0
151 ;;
152 -s | --source)
153 check_pod_name
Zack Williams249ac7f2018-05-18 13:34:14 -0700154 source_kubeconfig
Zack Williamsbb846a22018-06-26 14:57:17 -0700155 break
156 ;;
Luca Pretec97ad882018-05-04 11:22:14 -0700157 --) # End of all options
158 shift
159 break
160 ;;
161 *)
Zack Williams11b2e5c2018-05-18 09:50:54 -0700162 echo Error: Unknown option: "$CLI_OPT" >&2
Luca Pretec97ad882018-05-04 11:22:14 -0700163 echo " "
164 display_help
165 exit -1
166 ;;
167 esac
168done