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 | |
| 21 | set +e -u -o pipefail |
| 22 | |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 23 | install_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 Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 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 | |
| 38 | # Edit inventory var files |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 39 | NODE_LIST=$(echo "${NODES[@]}") |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 40 | 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 Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 44 | source copy-ssh-keys.sh "${NODES[@]}" && \ |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 45 | |
| 46 | # Prepare Target Machines |
| 47 | echo -e "\nInstalling Prerequisites On Remote Machines" && \ |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 48 | ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.cfg" k8s-requirements.yaml && \ |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 49 | |
| 50 | # Install Kubespray |
| 51 | echo -e "\nInstalling Kubespray" && \ |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 52 | 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] | 53 | |
| 54 | # Export the Kubespray Config Location |
| 55 | echo -e "\nLoading Kubespray Configuration" && \ |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 56 | cp kubespray/artifacts/admin.conf "configs/${DEPLOYMENT_NAME}.conf" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | # |
| 60 | # Exports the Kubespray Config Location |
| 61 | # |
| 62 | source_kubeconfig () { |
| 63 | export KUBECONFIG=${PWD}/configs/${DEPLOYMENT_NAME}.conf |
| 64 | } |
| 65 | |
| 66 | # |
| 67 | # Helm init |
| 68 | # |
| 69 | helm_init () { |
| 70 | echo -e "\nInitializing Helm" && \ |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 71 | source_kubeconfig "$DEPLOYMENT_NAME" && \ |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 72 | helm init --upgrade |
| 73 | } |
| 74 | |
| 75 | # |
| 76 | # Deploy an insecure registry |
| 77 | # |
| 78 | deploy_insecure_registry () { |
| 79 | echo -e "\nDeploying insecure registry" && \ |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 80 | source_kubeconfig "$DEPLOYMENT_NAME" && \ |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 81 | 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 | # |
| 88 | check_pod_name () { |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 89 | if [ -z "$DEPLOYMENT_NAME" ] |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 90 | 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 | # |
| 101 | display_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 | # |
| 120 | CLI_OPT=$1 |
| 121 | DEPLOYMENT_NAME=$2 |
| 122 | shift 2 |
| 123 | DEFAULT_NODES="10.90.0.101 10.90.0.102 10.90.0.103" |
| 124 | NODES=(${@:-$DEFAULT_NODES}) |
| 125 | |
| 126 | while : |
| 127 | do |
| 128 | case $CLI_OPT in |
| 129 | -i | --install) |
| 130 | check_pod_name |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 131 | install_kubespray "$DEPLOYMENT_NAME" "$NODES" |
| 132 | helm_init "$DEPLOYMENT_NAME" |
| 133 | deploy_insecure_registry "$DEPLOYMENT_NAME" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 134 | exit 0 |
| 135 | ;; |
| 136 | -h | --help) |
| 137 | display_help |
| 138 | exit 0 |
| 139 | ;; |
| 140 | -s | --source) |
| 141 | check_pod_name |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 142 | source_kubeconfig "$DEPLOYMENT_NAME" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 143 | return 0 |
| 144 | ;; |
| 145 | --) # End of all options |
| 146 | shift |
| 147 | break |
| 148 | ;; |
| 149 | *) |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 150 | echo Error: Unknown option: "$CLI_OPT" >&2 |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 151 | echo " " |
| 152 | display_help |
| 153 | exit -1 |
| 154 | ;; |
| 155 | esac |
| 156 | done |