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 | d24053d | 2018-07-31 09:12:35 -0700 | [diff] [blame] | 21 | set -e -o pipefail |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 22 | |
Luca Prete | 736dad8 | 2019-02-13 11:39:42 -0800 | [diff] [blame] | 23 | KS_COMMIT="${KS_COMMIT:-master}" |
Zack Williams | bb846a2 | 2018-06-26 14:57:17 -0700 | [diff] [blame] | 24 | |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 25 | install_kubespray () { |
| 26 | # Cleanup Old Kubespray Installations |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 27 | echo "Cleaning Up Old Kubespray Installation" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 28 | rm -rf kubespray |
| 29 | |
| 30 | # Download Kubespray |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 31 | echo "Downloading Kubespray" |
Zack Williams | bb846a2 | 2018-06-26 14:57:17 -0700 | [diff] [blame] | 32 | git clone https://github.com/kubernetes-incubator/kubespray.git |
| 33 | pushd kubespray |
| 34 | git checkout "$KS_COMMIT" |
| 35 | popd |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 36 | |
Zack Williams | d24053d | 2018-07-31 09:12:35 -0700 | [diff] [blame] | 37 | # create a virtualenv with specific packages, if it doesn't exist |
| 38 | if [ ! -x "ks_venv/bin/activate" ] |
| 39 | then |
Wei-Yu Chen | db5386b | 2019-04-11 18:05:32 -0700 | [diff] [blame] | 40 | virtualenv ks_venv --python=python3 |
Zack Williams | 92196ac | 2018-09-26 15:02:40 -0700 | [diff] [blame] | 41 | # shellcheck disable=SC1091 |
| 42 | source ks_venv/bin/activate |
| 43 | |
Wei-Yu Chen | db5386b | 2019-04-11 18:05:32 -0700 | [diff] [blame] | 44 | pip3 install ansible==2.7 |
| 45 | pip3 install -r kubespray/requirements.txt |
Zack Williams | 92196ac | 2018-09-26 15:02:40 -0700 | [diff] [blame] | 46 | else |
| 47 | # shellcheck disable=SC1091 |
| 48 | source ks_venv/bin/activate |
Zack Williams | d24053d | 2018-07-31 09:12:35 -0700 | [diff] [blame] | 49 | fi |
| 50 | |
Zack Williams | d24053d | 2018-07-31 09:12:35 -0700 | [diff] [blame] | 51 | |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 52 | # Generate inventory and var files |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 53 | echo "Generating The Inventory File" |
Zack Williams | bb846a2 | 2018-06-26 14:57:17 -0700 | [diff] [blame] | 54 | |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 55 | rm -rf "inventories/${DEPLOYMENT_NAME}" |
Zack Williams | bb846a2 | 2018-06-26 14:57:17 -0700 | [diff] [blame] | 56 | mkdir -p "inventories/${DEPLOYMENT_NAME}" |
| 57 | |
| 58 | cp -r kubespray/inventory/sample/group_vars "inventories/${DEPLOYMENT_NAME}/group_vars" |
Wei-Yu Chen | db5386b | 2019-04-11 18:05:32 -0700 | [diff] [blame] | 59 | CONFIG_FILE="inventories/${DEPLOYMENT_NAME}/inventory.yml" python3 kubespray/contrib/inventory_builder/inventory.py "${NODES[@]}" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 60 | |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 61 | # Add configuration to inventory |
| 62 | 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] | 63 | |
| 64 | # Prepare Target Machines |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 65 | echo "Installing Prerequisites On Remote Machines" |
Wei-Yu Chen | db5386b | 2019-04-11 18:05:32 -0700 | [diff] [blame] | 66 | ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.yml" k8s-requirements.yaml |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 67 | |
| 68 | # Install Kubespray |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 69 | echo "Installing Kubespray" |
Wei-Yu Chen | db5386b | 2019-04-11 18:05:32 -0700 | [diff] [blame] | 70 | ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.yml" kubespray/cluster.yml -b -v |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | # |
| 74 | # Exports the Kubespray Config Location |
| 75 | # |
| 76 | source_kubeconfig () { |
Zack Williams | bb846a2 | 2018-06-26 14:57:17 -0700 | [diff] [blame] | 77 | |
| 78 | kubeconfig_path="${PWD}/inventories/${DEPLOYMENT_NAME}/artifacts/admin.conf" |
| 79 | |
| 80 | if [ -f "$kubeconfig_path" ] |
| 81 | then |
| 82 | # these options are annoying outside of scripts |
| 83 | set +e +u +o pipefail |
| 84 | |
| 85 | echo "setting KUBECONFIG=$kubeconfig_path" |
| 86 | |
| 87 | export KUBECONFIG="$kubeconfig_path" |
| 88 | else |
| 89 | echo "kubernetes admin.conf not found at: '$kubeconfig_path'" |
| 90 | exit 1 |
| 91 | fi |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | # |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 95 | # Checks if an arbitrary pod name is given during specifc |
| 96 | # operations. |
| 97 | # |
| 98 | check_pod_name () { |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 99 | if [ -z "$DEPLOYMENT_NAME" ] |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 100 | then |
| 101 | echo "Missing option: podname" >&2 |
| 102 | echo " " |
| 103 | display_help |
| 104 | exit -1 |
| 105 | fi |
| 106 | } |
| 107 | |
| 108 | # |
| 109 | # Displays the help menu. |
| 110 | # |
| 111 | display_help () { |
| 112 | echo "Usage: $0 {--install|--source|--help} [podname] [ip...] " >&2 |
| 113 | echo " " |
| 114 | echo " -h, --help Display this help message." |
| 115 | echo " -i, --install Install Kubespray on <podname>" |
| 116 | echo " -s, --source Source the Kubectl config for <podname>" |
| 117 | echo " " |
| 118 | echo " podname An arbitrary name representing the pod" |
Kailash Khalasi | 236b0f6 | 2018-06-27 08:50:39 -0700 | [diff] [blame] | 119 | echo " ip The IP address of the remote node(s)" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 120 | echo " " |
| 121 | echo "Example usages:" |
| 122 | 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] | 123 | 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] | 124 | echo " source setup.sh -s podname" |
| 125 | } |
| 126 | |
| 127 | # |
| 128 | # Init |
| 129 | # |
Kailash Khalasi | c09de4b | 2018-06-13 13:56:18 -0700 | [diff] [blame] | 130 | if [ $# -lt 2 ] |
| 131 | then |
| 132 | display_help |
| 133 | exit 0 |
| 134 | fi |
| 135 | |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 136 | CLI_OPT=$1 |
| 137 | DEPLOYMENT_NAME=$2 |
| 138 | shift 2 |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 139 | DEFAULT_NODES=(10.90.0.101 10.90.0.102 10.90.0.103) |
| 140 | NODES=("${@:-${DEFAULT_NODES[@]}}") |
| 141 | |
| 142 | REMOTE_SSH_USER="${REMOTE_SSH_USER:-cord}" |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 143 | |
| 144 | while : |
| 145 | do |
| 146 | case $CLI_OPT in |
| 147 | -i | --install) |
| 148 | check_pod_name |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 149 | install_kubespray |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 150 | exit 0 |
| 151 | ;; |
| 152 | -h | --help) |
| 153 | display_help |
| 154 | exit 0 |
| 155 | ;; |
| 156 | -s | --source) |
| 157 | check_pod_name |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 158 | source_kubeconfig |
Zack Williams | bb846a2 | 2018-06-26 14:57:17 -0700 | [diff] [blame] | 159 | break |
| 160 | ;; |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 161 | --) # End of all options |
| 162 | shift |
| 163 | break |
| 164 | ;; |
| 165 | *) |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 166 | echo Error: Unknown option: "$CLI_OPT" >&2 |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 167 | echo " " |
| 168 | display_help |
| 169 | exit -1 |
| 170 | ;; |
| 171 | esac |
| 172 | done |