blob: 61902cd6cfb206cd00f68a3e8ff694912ba3cf26 [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
17################################################
18# #
19# Installs Kubespray on remote target machines #
20# #
21################################################
22
23#
24# Installs Kubespray on remote target machines.
25#
26install_kubespray () {
27 # Cleanup Old Kubespray Installations
28 echo -e "\nCleaning Up Old Kubespray Installation" && \
29 rm -rf kubespray
30
31 # Download Kubespray
32 echo -e "\nDownloading Kubespray" && \
33 git clone https://github.com/kubernetes-incubator/kubespray.git -b v2.4.0 && \
34
35 # Generate inventory and var files
36 echo -e "\nGenerating The Inventory File" && \
37 rm -rf inventories/${DEPLOYMENT_NAME} && \
38 cp -r kubespray/inventory inventories/${DEPLOYMENT_NAME} && \
39 CONFIG_FILE=inventories/${DEPLOYMENT_NAME}/inventory.cfg python3 kubespray/contrib/inventory_builder/inventory.py ${NODES[@]} && \
40
41 # Edit inventory var files
42 NODE_LIST=`echo ${NODES[@]}`
43 ansible-playbook k8s-configs.yaml --extra-vars "deployment_name=${DEPLOYMENT_NAME} k8s_nodes='${NODE_LIST}'"
44
45 # Copy SSH keys
46 echo -e "\nCopying Public SSH Keys To Remote Machines" && \
47 source copy-ssh-keys.sh ${NODES[@]} && \
48
49 # Prepare Target Machines
50 echo -e "\nInstalling Prerequisites On Remote Machines" && \
51 ansible-playbook -i inventories/${DEPLOYMENT_NAME}/inventory.cfg k8s-requirements.yaml && \
52
53 # Install Kubespray
54 echo -e "\nInstalling Kubespray" && \
55 ansible-playbook -i inventories/${DEPLOYMENT_NAME}/inventory.cfg kubespray/cluster.yml -b -v && \
56
57 # Export the Kubespray Config Location
58 echo -e "\nLoading Kubespray Configuration" && \
59 cp kubespray/artifacts/admin.conf configs/${DEPLOYMENT_NAME}.conf
60}
61
62#
63# Exports the Kubespray Config Location
64#
65source_kubeconfig () {
66 export KUBECONFIG=${PWD}/configs/${DEPLOYMENT_NAME}.conf
67}
68
69#
70# Helm init
71#
72helm_init () {
73 echo -e "\nInitializing Helm" && \
74 source_kubeconfig $DEPLOYMENT_NAME && \
75 helm init --upgrade
76}
77
78#
79# Deploy an insecure registry
80#
81deploy_insecure_registry () {
82 echo -e "\nDeploying insecure registry" && \
83 source_kubeconfig $DEPLOYMENT_NAME && \
84 helm install stable/docker-registry --set service.nodePort=30500,service.type=NodePort -n docker-registry
85}
86
87#
88# Checks if an arbitrary pod name is given during specifc
89# operations.
90#
91check_pod_name () {
92 if [ -z $DEPLOYMENT_NAME ]
93 then
94 echo "Missing option: podname" >&2
95 echo " "
96 display_help
97 exit -1
98 fi
99}
100
101#
102# Displays the help menu.
103#
104display_help () {
105 echo "Usage: $0 {--install|--source|--help} [podname] [ip...] " >&2
106 echo " "
107 echo " -h, --help Display this help message."
108 echo " -i, --install Install Kubespray on <podname>"
109 echo " -s, --source Source the Kubectl config for <podname>"
110 echo " "
111 echo " podname An arbitrary name representing the pod"
112 echo " ip The IP address of a remote node"
113 echo " "
114 echo "Example usages:"
115 echo " ./setup.sh -i podname 192.168.10.100 192.168.10.101 192.168.10.102"
116 echo " ./setup.sh -i podname (detault is 10.90.0.101 10.90.0.102 10.90.0.103)"
117 echo " source setup.sh -s podname"
118}
119
120#
121# Init
122#
123CLI_OPT=$1
124DEPLOYMENT_NAME=$2
125shift 2
126DEFAULT_NODES="10.90.0.101 10.90.0.102 10.90.0.103"
127NODES=(${@:-$DEFAULT_NODES})
128
129while :
130do
131 case $CLI_OPT in
132 -i | --install)
133 check_pod_name
134 install_kubespray $DEPLOYMENT_NAME $NODES
135 helm_init $DEPLOYMENT_NAME
136 deploy_insecure_registry $DEPLOYMENT_NAME
137 exit 0
138 ;;
139 -h | --help)
140 display_help
141 exit 0
142 ;;
143 -s | --source)
144 check_pod_name
145 source_kubeconfig $DEPLOYMENT_NAME
146 return 0
147 ;;
148 --) # End of all options
149 shift
150 break
151 ;;
152 *)
153 echo Error: Unknown option: $CLI_OPT >&2
154 echo " "
155 display_help
156 exit -1
157 ;;
158 esac
159done