Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 1 | #!/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 Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 17 | # |
| 18 | # Installs Kubespray on remote target machines. |
| 19 | # |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 20 | |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 21 | set -e -u -o pipefail |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 22 | |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 23 | install_kubespray () { |
| 24 | # Cleanup Old Kubespray Installations |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 25 | echo "Cleaning Up Old Kubespray Installation" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 26 | rm -rf kubespray |
| 27 | |
| 28 | # Download Kubespray |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 29 | echo "Downloading Kubespray" |
| 30 | git clone https://github.com/kubernetes-incubator/kubespray.git -b "v2.4.0" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 31 | |
| 32 | # Generate inventory and var files |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 33 | 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 Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 37 | |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 38 | # 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 Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 40 | |
| 41 | # Prepare Target Machines |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 42 | echo "Installing Prerequisites On Remote Machines" |
| 43 | ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.cfg" k8s-requirements.yaml |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 44 | |
| 45 | # Install Kubespray |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 46 | echo "Installing Kubespray" |
| 47 | ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.cfg" kubespray/cluster.yml -b -v |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 48 | |
| 49 | # Export the Kubespray Config Location |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 50 | echo "Loading Kubespray Configuration" |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 51 | cp kubespray/artifacts/admin.conf "configs/${DEPLOYMENT_NAME}.conf" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | # |
| 55 | # Exports the Kubespray Config Location |
| 56 | # |
| 57 | source_kubeconfig () { |
| 58 | export KUBECONFIG=${PWD}/configs/${DEPLOYMENT_NAME}.conf |
| 59 | } |
| 60 | |
| 61 | # |
| 62 | # Helm init |
| 63 | # |
| 64 | helm_init () { |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 65 | echo "Initializing Helm" |
| 66 | source_kubeconfig "$DEPLOYMENT_NAME" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 67 | helm init --upgrade |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 68 | echo "Waiting a minute for Tiller to start" |
| 69 | sleep 60 |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | # |
| 73 | # Deploy an insecure registry |
| 74 | # |
| 75 | deploy_insecure_registry () { |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 76 | echo "Deploying insecure registry" |
| 77 | source_kubeconfig "$DEPLOYMENT_NAME" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 78 | helm install stable/docker-registry --set service.nodePort=30500,service.type=NodePort -n docker-registry |
| 79 | } |
| 80 | |
| 81 | # |
| 82 | # Checks if an arbitrary pod name is given during specifc |
| 83 | # operations. |
| 84 | # |
| 85 | check_pod_name () { |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 86 | if [ -z "$DEPLOYMENT_NAME" ] |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 87 | then |
| 88 | echo "Missing option: podname" >&2 |
| 89 | echo " " |
| 90 | display_help |
| 91 | exit -1 |
| 92 | fi |
| 93 | } |
| 94 | |
| 95 | # |
| 96 | # Displays the help menu. |
| 97 | # |
| 98 | display_help () { |
| 99 | echo "Usage: $0 {--install|--source|--help} [podname] [ip...] " >&2 |
| 100 | echo " " |
| 101 | echo " -h, --help Display this help message." |
| 102 | echo " -i, --install Install Kubespray on <podname>" |
| 103 | echo " -s, --source Source the Kubectl config for <podname>" |
| 104 | echo " " |
| 105 | echo " podname An arbitrary name representing the pod" |
| 106 | echo " ip The IP address of a remote node" |
| 107 | echo " " |
| 108 | echo "Example usages:" |
| 109 | echo " ./setup.sh -i podname 192.168.10.100 192.168.10.101 192.168.10.102" |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 110 | echo " ./setup.sh -i podname (default is 10.90.0.101 10.90.0.102 10.90.0.103)" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 111 | echo " source setup.sh -s podname" |
| 112 | } |
| 113 | |
| 114 | # |
| 115 | # Init |
| 116 | # |
| 117 | CLI_OPT=$1 |
| 118 | DEPLOYMENT_NAME=$2 |
| 119 | shift 2 |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 120 | DEFAULT_NODES=(10.90.0.101 10.90.0.102 10.90.0.103) |
| 121 | NODES=("${@:-${DEFAULT_NODES[@]}}") |
| 122 | |
| 123 | REMOTE_SSH_USER="${REMOTE_SSH_USER:-cord}" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 124 | |
| 125 | while : |
| 126 | do |
| 127 | case $CLI_OPT in |
| 128 | -i | --install) |
| 129 | check_pod_name |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 130 | install_kubespray |
| 131 | helm_init |
| 132 | deploy_insecure_registry |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 133 | exit 0 |
| 134 | ;; |
| 135 | -h | --help) |
| 136 | display_help |
| 137 | exit 0 |
| 138 | ;; |
| 139 | -s | --source) |
| 140 | check_pod_name |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame^] | 141 | source_kubeconfig |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 142 | return 0 |
| 143 | ;; |
| 144 | --) # End of all options |
| 145 | shift |
| 146 | break |
| 147 | ;; |
| 148 | *) |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 149 | echo Error: Unknown option: "$CLI_OPT" >&2 |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 150 | echo " " |
| 151 | display_help |
| 152 | exit -1 |
| 153 | ;; |
| 154 | esac |
| 155 | done |