blob: 6c89bfc9e2fd8b3007f4290e5110666ed2565bf3 [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
21set +e -u -o pipefail
22
Luca Pretec97ad882018-05-04 11:22:14 -070023install_kubespray () {
24 # Cleanup Old Kubespray Installations
25 echo -e "\nCleaning Up Old Kubespray Installation" && \
26 rm -rf kubespray
27
28 # Download Kubespray
29 echo -e "\nDownloading Kubespray" && \
30 git clone https://github.com/kubernetes-incubator/kubespray.git -b v2.4.0 && \
31
32 # Generate inventory and var files
33 echo -e "\nGenerating The Inventory File" && \
Zack Williams11b2e5c2018-05-18 09:50:54 -070034 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
38 # Edit inventory var files
Zack Williams11b2e5c2018-05-18 09:50:54 -070039 NODE_LIST=$(echo "${NODES[@]}")
Luca Pretec97ad882018-05-04 11:22:14 -070040 ansible-playbook k8s-configs.yaml --extra-vars "deployment_name=${DEPLOYMENT_NAME} k8s_nodes='${NODE_LIST}'"
41
42 # Copy SSH keys
43 echo -e "\nCopying Public SSH Keys To Remote Machines" && \
Zack Williams11b2e5c2018-05-18 09:50:54 -070044 source copy-ssh-keys.sh "${NODES[@]}" && \
Luca Pretec97ad882018-05-04 11:22:14 -070045
46 # Prepare Target Machines
47 echo -e "\nInstalling Prerequisites On Remote Machines" && \
Zack Williams11b2e5c2018-05-18 09:50:54 -070048 ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.cfg" k8s-requirements.yaml && \
Luca Pretec97ad882018-05-04 11:22:14 -070049
50 # Install Kubespray
51 echo -e "\nInstalling Kubespray" && \
Zack Williams11b2e5c2018-05-18 09:50:54 -070052 ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.cfg" kubespray/cluster.yml -b -v && \
Luca Pretec97ad882018-05-04 11:22:14 -070053
54 # Export the Kubespray Config Location
55 echo -e "\nLoading Kubespray Configuration" && \
Zack Williams11b2e5c2018-05-18 09:50:54 -070056 cp kubespray/artifacts/admin.conf "configs/${DEPLOYMENT_NAME}.conf"
Luca Pretec97ad882018-05-04 11:22:14 -070057}
58
59#
60# Exports the Kubespray Config Location
61#
62source_kubeconfig () {
63 export KUBECONFIG=${PWD}/configs/${DEPLOYMENT_NAME}.conf
64}
65
66#
67# Helm init
68#
69helm_init () {
70 echo -e "\nInitializing Helm" && \
Zack Williams11b2e5c2018-05-18 09:50:54 -070071 source_kubeconfig "$DEPLOYMENT_NAME" && \
Luca Pretec97ad882018-05-04 11:22:14 -070072 helm init --upgrade
73}
74
75#
76# Deploy an insecure registry
77#
78deploy_insecure_registry () {
79 echo -e "\nDeploying insecure registry" && \
Zack Williams11b2e5c2018-05-18 09:50:54 -070080 source_kubeconfig "$DEPLOYMENT_NAME" && \
Luca Pretec97ad882018-05-04 11:22:14 -070081 helm install stable/docker-registry --set service.nodePort=30500,service.type=NodePort -n docker-registry
82}
83
84#
85# Checks if an arbitrary pod name is given during specifc
86# operations.
87#
88check_pod_name () {
Zack Williams11b2e5c2018-05-18 09:50:54 -070089 if [ -z "$DEPLOYMENT_NAME" ]
Luca Pretec97ad882018-05-04 11:22:14 -070090 then
91 echo "Missing option: podname" >&2
92 echo " "
93 display_help
94 exit -1
95 fi
96}
97
98#
99# Displays the help menu.
100#
101display_help () {
102 echo "Usage: $0 {--install|--source|--help} [podname] [ip...] " >&2
103 echo " "
104 echo " -h, --help Display this help message."
105 echo " -i, --install Install Kubespray on <podname>"
106 echo " -s, --source Source the Kubectl config for <podname>"
107 echo " "
108 echo " podname An arbitrary name representing the pod"
109 echo " ip The IP address of a remote node"
110 echo " "
111 echo "Example usages:"
112 echo " ./setup.sh -i podname 192.168.10.100 192.168.10.101 192.168.10.102"
113 echo " ./setup.sh -i podname (detault is 10.90.0.101 10.90.0.102 10.90.0.103)"
114 echo " source setup.sh -s podname"
115}
116
117#
118# Init
119#
120CLI_OPT=$1
121DEPLOYMENT_NAME=$2
122shift 2
123DEFAULT_NODES="10.90.0.101 10.90.0.102 10.90.0.103"
124NODES=(${@:-$DEFAULT_NODES})
125
126while :
127do
128 case $CLI_OPT in
129 -i | --install)
130 check_pod_name
Zack Williams11b2e5c2018-05-18 09:50:54 -0700131 install_kubespray "$DEPLOYMENT_NAME" "$NODES"
132 helm_init "$DEPLOYMENT_NAME"
133 deploy_insecure_registry "$DEPLOYMENT_NAME"
Luca Pretec97ad882018-05-04 11:22:14 -0700134 exit 0
135 ;;
136 -h | --help)
137 display_help
138 exit 0
139 ;;
140 -s | --source)
141 check_pod_name
Zack Williams11b2e5c2018-05-18 09:50:54 -0700142 source_kubeconfig "$DEPLOYMENT_NAME"
Luca Pretec97ad882018-05-04 11:22:14 -0700143 return 0
144 ;;
145 --) # End of all options
146 shift
147 break
148 ;;
149 *)
Zack Williams11b2e5c2018-05-18 09:50:54 -0700150 echo Error: Unknown option: "$CLI_OPT" >&2
Luca Pretec97ad882018-05-04 11:22:14 -0700151 echo " "
152 display_help
153 exit -1
154 ;;
155 esac
156done