blob: b45dafce4660f9ef8d1c3faff01e1944728b46c1 [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 Williams249ac7f2018-05-18 13:34:14 -070021set -e -u -o pipefail
Zack Williams11b2e5c2018-05-18 09:50:54 -070022
Luca Pretec97ad882018-05-04 11:22:14 -070023install_kubespray () {
24 # Cleanup Old Kubespray Installations
Zack Williams249ac7f2018-05-18 13:34:14 -070025 echo "Cleaning Up Old Kubespray Installation"
Luca Pretec97ad882018-05-04 11:22:14 -070026 rm -rf kubespray
27
28 # Download Kubespray
Zack Williams249ac7f2018-05-18 13:34:14 -070029 echo "Downloading Kubespray"
30 git clone https://github.com/kubernetes-incubator/kubespray.git -b "v2.4.0"
Luca Pretec97ad882018-05-04 11:22:14 -070031
32 # Generate inventory and var files
Zack Williams249ac7f2018-05-18 13:34:14 -070033 echo "Generating The Inventory File"
34 rm -rf "inventories/${DEPLOYMENT_NAME}"
35 cp -r kubespray/inventory "inventories/${DEPLOYMENT_NAME}"
36 CONFIG_FILE="inventories/${DEPLOYMENT_NAME}/inventory.cfg" python3 kubespray/contrib/inventory_builder/inventory.py "${NODES[@]}"
Luca Pretec97ad882018-05-04 11:22:14 -070037
Zack Williams249ac7f2018-05-18 13:34:14 -070038 # Add configuration to inventory
39 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 -070040
41 # Prepare Target Machines
Zack Williams249ac7f2018-05-18 13:34:14 -070042 echo "Installing Prerequisites On Remote Machines"
43 ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.cfg" k8s-requirements.yaml
Luca Pretec97ad882018-05-04 11:22:14 -070044
45 # Install Kubespray
Zack Williams249ac7f2018-05-18 13:34:14 -070046 echo "Installing Kubespray"
47 ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.cfg" kubespray/cluster.yml -b -v
Luca Pretec97ad882018-05-04 11:22:14 -070048
49 # Export the Kubespray Config Location
Zack Williams249ac7f2018-05-18 13:34:14 -070050 echo "Loading Kubespray Configuration"
Zack Williams11b2e5c2018-05-18 09:50:54 -070051 cp kubespray/artifacts/admin.conf "configs/${DEPLOYMENT_NAME}.conf"
Luca Pretec97ad882018-05-04 11:22:14 -070052}
53
54#
55# Exports the Kubespray Config Location
56#
57source_kubeconfig () {
58 export KUBECONFIG=${PWD}/configs/${DEPLOYMENT_NAME}.conf
59}
60
61#
Luca Pretec97ad882018-05-04 11:22:14 -070062# Checks if an arbitrary pod name is given during specifc
63# operations.
64#
65check_pod_name () {
Zack Williams11b2e5c2018-05-18 09:50:54 -070066 if [ -z "$DEPLOYMENT_NAME" ]
Luca Pretec97ad882018-05-04 11:22:14 -070067 then
68 echo "Missing option: podname" >&2
69 echo " "
70 display_help
71 exit -1
72 fi
73}
74
75#
76# Displays the help menu.
77#
78display_help () {
79 echo "Usage: $0 {--install|--source|--help} [podname] [ip...] " >&2
80 echo " "
81 echo " -h, --help Display this help message."
82 echo " -i, --install Install Kubespray on <podname>"
83 echo " -s, --source Source the Kubectl config for <podname>"
84 echo " "
85 echo " podname An arbitrary name representing the pod"
86 echo " ip The IP address of a remote node"
87 echo " "
88 echo "Example usages:"
89 echo " ./setup.sh -i podname 192.168.10.100 192.168.10.101 192.168.10.102"
Zack Williams249ac7f2018-05-18 13:34:14 -070090 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 -070091 echo " source setup.sh -s podname"
92}
93
94#
95# Init
96#
97CLI_OPT=$1
98DEPLOYMENT_NAME=$2
99shift 2
Zack Williams249ac7f2018-05-18 13:34:14 -0700100DEFAULT_NODES=(10.90.0.101 10.90.0.102 10.90.0.103)
101NODES=("${@:-${DEFAULT_NODES[@]}}")
102
103REMOTE_SSH_USER="${REMOTE_SSH_USER:-cord}"
Luca Pretec97ad882018-05-04 11:22:14 -0700104
105while :
106do
107 case $CLI_OPT in
108 -i | --install)
109 check_pod_name
Zack Williams249ac7f2018-05-18 13:34:14 -0700110 install_kubespray
Luca Pretec97ad882018-05-04 11:22:14 -0700111 exit 0
112 ;;
113 -h | --help)
114 display_help
115 exit 0
116 ;;
117 -s | --source)
118 check_pod_name
Zack Williams249ac7f2018-05-18 13:34:14 -0700119 source_kubeconfig
Luca Pretec97ad882018-05-04 11:22:14 -0700120 return 0
121 ;;
122 --) # End of all options
123 shift
124 break
125 ;;
126 *)
Zack Williams11b2e5c2018-05-18 09:50:54 -0700127 echo Error: Unknown option: "$CLI_OPT" >&2
Luca Pretec97ad882018-05-04 11:22:14 -0700128 echo " "
129 display_help
130 exit -1
131 ;;
132 esac
133done