|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) 2024, Oracle and/or its affiliates. |
| 3 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 4 | + |
| 5 | +prop() { |
| 6 | + grep "${1}" ${propsFile}| grep -v "#" | cut -d'=' -f2 |
| 7 | +} |
| 8 | + |
| 9 | +generateTFVarFile() { |
| 10 | + tfVarsFile=${terraformVarDir}/terraform.tfvars |
| 11 | + rm -f ${tfVarsFile} |
| 12 | + cp ${terraformVarDir}/terraform.tfvars.template $tfVarsFile |
| 13 | + chmod 777 $tfVarsFile |
| 14 | + |
| 15 | + sed -i -e "s:@OCI_TENANCY_ID@:${tenancy_id}:g" ${tfVarsFile} |
| 16 | + sed -i -e "s:@OCI_COMPARTMENT_ID@:${compartment_id}:g" ${tfVarsFile} |
| 17 | + sed -i -e "s:@OCI_USER_ID@:${user_id}:g" ${tfVarsFile} |
| 18 | + sed -i -e "s/@OCI_FINGERPRINT@/"${fingerprint}"/g" ${tfVarsFile} |
| 19 | + sed -i -e "s:@OCI_API_PRIVATE_KEY_PATH@:${api_private_key_path}:g" ${tfVarsFile} |
| 20 | + |
| 21 | + sed -i -e "s:@OCI_REGION@:${region}:g" ${tfVarsFile} |
| 22 | + sed -i -e "s/@OCI_AVAILABILITY_DOMAIN_ID@/"${availability_domain_id}"/g" ${tfVarsFile} |
| 23 | + sed -i -e "s:@OCI_INSTANCE_PREFIX@:${prefix}:g" ${tfVarsFile} |
| 24 | + |
| 25 | + sed -i -e "s:@OCI_DEPLOY_NETWORKING@:${deploy_networking}:g" ${tfVarsFile} |
| 26 | + sed -i -e "s:@OCI_SUBNET_ID@:${subnet_id}:g" ${tfVarsFile} |
| 27 | + sed -i -e "s:@OCI_VCN_ID@:${vcn_id}:g" ${tfVarsFile} |
| 28 | + |
| 29 | + sed -i -e "s:@OCI_SSH_PUBLIC_KEY_PATH@:${ssh_public_key_path}:g" ${tfVarsFile} |
| 30 | + sed -i -e "s:@OCI_SSH_PRIVATE_KEY_PATH@:${ssh_private_key_path}:g" ${tfVarsFile} |
| 31 | + |
| 32 | + sed -i -e "s:@OCI_ENABLE_BASTION@:${enable_bastion}:g" ${tfVarsFile} |
| 33 | + |
| 34 | + sed -i -e "s:@OCI_VIRTUAL_IP@:${virtual_ip}:g" ${tfVarsFile} |
| 35 | + |
| 36 | + sed -i -e "s:@OCNE_CONTROL_PLANE_NODE_COUNT@:${control_plane_node_count}:g" ${tfVarsFile} |
| 37 | + sed -i -e "s:@OCNE_WORKER_NODE_COUNT@:${worker_node_count}:g" ${tfVarsFile} |
| 38 | + sed -i -e "s:@OCNE_ENVIRONMENT_NAME@:${environment_name}:g" ${tfVarsFile} |
| 39 | + sed -i -e "s:@OCNE_K8S_CLUSTER_NAME@:${kubernetes_name}:g" ${tfVarsFile} |
| 40 | + |
| 41 | + sed -i -e "s:@OCNE_VERSION@:${ocne_version}:g" ${tfVarsFile} |
| 42 | + |
| 43 | + sed -i -e "s#@HTTP_PROXY@#${http_proxy}#g" ${tfVarsFile} |
| 44 | + sed -i -e "s:@NO_PROXY@:${no_proxy}:g" ${tfVarsFile} |
| 45 | + |
| 46 | + echo "Generated TFVars file [${tfVarsFile}]" |
| 47 | + cat "${tfVarsFile}" |
| 48 | +} |
| 49 | + |
| 50 | +setupTerraform() { |
| 51 | + mkdir ${terraformDir} |
| 52 | + cd ${terraformDir} |
| 53 | + if [[ "${OSTYPE}" == "darwin"* ]]; then |
| 54 | + os_type="darwin" |
| 55 | + elif [[ "${OSTYPE}" == "linux"* ]]; then |
| 56 | + os_type="linux" |
| 57 | + else |
| 58 | + echo "Unsupported OS" |
| 59 | + fi |
| 60 | + curl -O https://releases.hashicorp.com/terraform/1.8.1/terraform_1.8.1_${os_type}_${platform}64.zip |
| 61 | + unzip terraform_1.8.1_${os_type}_${platform}64.zip |
| 62 | + chmod +x ${terraformDir}/terraform |
| 63 | + |
| 64 | + # install yq |
| 65 | + wget https://github.com/mikefarah/yq/releases/download/v4.35.2/yq_${os_type}_${platform}64.tar.gz |
| 66 | + tar -zxvf yq_${os_type}_${platform}64.tar.gz |
| 67 | + mv yq_${os_type}_${platform}64 yq |
| 68 | + |
| 69 | + export PATH=${terraformDir}:${PATH} |
| 70 | +} |
| 71 | + |
| 72 | +deleteOlderVersionTerraformOCIProvider() { |
| 73 | + if [ -d ~/.terraform.d/plugins ]; then |
| 74 | + echo "Deleting older version of terraform plugins dir" |
| 75 | + rm -rf ~/.terraform.d/plugins |
| 76 | + fi |
| 77 | + if [ -d ${terraformVarDir}/.terraform ]; then |
| 78 | + rm -rf ${terraformVarDir}/.terraform |
| 79 | + fi |
| 80 | + if [ -e ~/.terraformrc ]; then |
| 81 | + rm ~/.terraformrc |
| 82 | + fi |
| 83 | +} |
| 84 | + |
| 85 | +createCluster () { |
| 86 | + cd ${terraformVarDir} |
| 87 | + echo "terraform init -var-file=${terraformVarDir}/terraform.tfvars" |
| 88 | + terraform init -var-file=${terraformVarDir}/terraform.tfvars |
| 89 | + echo "terraform plan -var-file=${terraformVarDir}/terraform.tfvars" |
| 90 | + terraform plan -var-file=${terraformVarDir}/terraform.tfvars |
| 91 | + echo "terraform apply -auto-approve -var-file=${terraformVarDir}/terraform.tfvars" |
| 92 | + terraform apply -auto-approve -var-file=${terraformVarDir}/terraform.tfvars |
| 93 | +} |
| 94 | + |
| 95 | +checkKubernetesCliConnection() { |
| 96 | + echo "Confirming ${KUBERNETES_CLI:-kubectl} can connect to the server..." |
| 97 | + |
| 98 | + # Get the OCNE cluster control node private IP |
| 99 | + echo "command to get k8s_master_instance_id: oci compute instance list --compartment-id=${compartment_id} --display-name=${prefix}-control-plane-001 |jq -r '.data[] | select(."lifecycle-state" == "RUNNING") | ."id"'" |
| 100 | + k8s_master_instance_id=`oci compute instance list --compartment-id=${compartment_id} --display-name=${prefix}-control-plane-001 |jq -r '.data[] | select(."lifecycle-state" == "RUNNING") | ."id"'` |
| 101 | + echo "command to get k8s_master_instance_private_ip: oci compute instance list-vnics --compartment-id=${compartment_id} --instance-id=${k8s_master_instance_id} |jq -r '.data[]."private-ip"'" |
| 102 | + k8s_master_instance_private_ips=`oci compute instance list-vnics --compartment-id=${compartment_id} --instance-id=${k8s_master_instance_id} |jq -r '.data[]."private-ip"'` |
| 103 | + |
| 104 | + if [ -z "$k8s_master_instance_private_ips" ]; then |
| 105 | + echo "[ERROR] No active cluster found with name ${kubernetes_name}." |
| 106 | + exit 1 |
| 107 | + fi |
| 108 | + |
| 109 | + echo "OCNE K8S cluster control node private ip: ### $k8s_master_instance_private_ips ###" |
| 110 | + declare -a k8s_master_instance_private_ip=(${k8s_master_instance_private_ips//\n/ }) |
| 111 | + |
| 112 | + local local_no_proxy=${no_proxy} |
| 113 | + for i in "${k8s_master_instance_private_ip[@]}"; do |
| 114 | + local_no_proxy+=",$i" |
| 115 | + done |
| 116 | + export NO_PROXY="$local_no_proxy" |
| 117 | + echo "NO_PROXY=$NO_PROXY" |
| 118 | + |
| 119 | + export KUBECONFIG=${terraformVarDir}/kubeconfig |
| 120 | + echo "KUBECONFIG=$KUBECONFIG" |
| 121 | + |
| 122 | + local myline_output=$(${KUBERNETES_CLI:-kubectl} get nodes -o wide 2>&1) |
| 123 | + |
| 124 | + if echo "$myline_output" | grep -q "Unable to connect to the server: net/http: TLS handshake timeout"; then |
| 125 | + echo "[ERROR] Unable to connect to the server: net/http: TLS handshake timeout" |
| 126 | + echo '- could not talk to OCNE cluster, aborting' |
| 127 | + |
| 128 | + cd "${terraformVarDir}" |
| 129 | + terraform destroy -auto-approve -var-file="${terraformVarDir}/terraform.tfvars" |
| 130 | + exit 1 |
| 131 | + fi |
| 132 | + if echo "$myline_output" | grep -q "couldn't get current server API group"; then |
| 133 | + echo "[ERROR] Unable to connect to the server: couldn't get current server API group, connection refused" |
| 134 | + echo '- check errors during OKE cluster creation' |
| 135 | + echo '- could not talk to OCNE cluster, aborting' |
| 136 | + |
| 137 | + cd "${terraformVarDir}" |
| 138 | + terraform destroy -auto-approve -var-file="${terraformVarDir}/terraform.tfvars" |
| 139 | + exit 1 |
| 140 | + fi |
| 141 | +} |
| 142 | + |
| 143 | +checkClusterRunning() { |
| 144 | + checkKubernetesCliConnection |
| 145 | + |
| 146 | + local prefix=${prefix} |
| 147 | + declare -a myline=($(${KUBERNETES_CLI:-kubectl} get nodes -o wide | grep "${prefix}" | awk '{print $2}')) |
| 148 | + |
| 149 | + local max=100 |
| 150 | + local count=1 |
| 151 | + |
| 152 | + for (( i = 0; i < ${#myline[@]} ; i++ )); do |
| 153 | + while [ "${myline[i]}" != "Ready" ] && [ $count -le $max ]; do |
| 154 | + echo "[ERROR] Some Nodes in the Cluster are not in the Ready Status, sleeping for 10s..." |
| 155 | + sleep 10 |
| 156 | + myline=($(${KUBERNETES_CLI:-kubectl} get nodes -o wide | grep "${prefix}" | awk '{print $2}')) |
| 157 | + echo "myline[i]: ${myline[i]}" |
| 158 | + echo "Status is ${myline[i]} Iter [$count/$max]" |
| 159 | + count=$((count + 1)) |
| 160 | + done |
| 161 | + done |
| 162 | + |
| 163 | + local NODES=$(${KUBERNETES_CLI:-kubectl} get nodes -o wide | grep "${prefix}" | wc -l) |
| 164 | + local number_nodes=$(($control_plane_node_count + $worker_node_count)) |
| 165 | + if [ "$NODES" -eq "$number_nodes" ]; then |
| 166 | + echo '- looks good' |
| 167 | + else |
| 168 | + echo '- could not talk to OCNE cluster, aborting' |
| 169 | + cd "${terraformVarDir}" |
| 170 | + terraform destroy -auto-approve -var-file="${terraformVarDir}/${clusterTFVarsFile}.tfvars" |
| 171 | + exit 1 |
| 172 | + fi |
| 173 | + |
| 174 | + if [ $count -gt $max ]; then |
| 175 | + echo "[ERROR] Unable to start the nodes in the OCNE cluster after 200s" |
| 176 | + cd "${terraformVarDir}" |
| 177 | + terraform destroy -auto-approve -var-file="${terraformVarDir}/${clusterTFVarsFile}.tfvars" |
| 178 | + exit 1 |
| 179 | + fi |
| 180 | +} |
| 181 | + |
| 182 | + |
| 183 | +#MAIN |
| 184 | +propsFile=${1:-$PWD/oci.props} |
| 185 | +terraformVarDir=${2:-$PWD} |
| 186 | +platform=${3:-amd} |
| 187 | + |
| 188 | +#grep props's values from oci.props file |
| 189 | + |
| 190 | +tenancy_id=$(prop 'tenancy_id') |
| 191 | +compartment_id=$(prop 'compartment_id') |
| 192 | +user_id=$(prop 'user_id') |
| 193 | +fingerprint=$(prop 'fingerprint') |
| 194 | +api_private_key_path=$(prop 'api_private_key_path') |
| 195 | + |
| 196 | +region=$(prop 'region') |
| 197 | +availability_domain_id=$(prop 'availability_domain_id') |
| 198 | +prefix=$(prop 'prefix') |
| 199 | + |
| 200 | +deploy_networking=$(prop 'deploy_networking') |
| 201 | +subnet_id=$(prop 'subnet_id') |
| 202 | +vcn_id=$(prop 'vcn_id') |
| 203 | + |
| 204 | +ssh_public_key_path=$(prop 'ssh_public_key_path') |
| 205 | +ssh_private_key_path=$(prop 'ssh_private_key_path') |
| 206 | + |
| 207 | +enable_bastion=$(prop 'enable_bastion') |
| 208 | + |
| 209 | +virtual_ip=$(prop 'virtual_ip') |
| 210 | + |
| 211 | +control_plane_node_count=$(prop 'control_plane_node_count') |
| 212 | +worker_node_count=$(prop 'worker_node_count') |
| 213 | +environment_name=$(prop 'environment_name') |
| 214 | +kubernetes_name=$(prop 'kubernetes_name') |
| 215 | + |
| 216 | +ocne_version=$(prop 'ocne_version') |
| 217 | + |
| 218 | +http_proxy=$(prop 'http_proxy') |
| 219 | +no_proxy=$(prop 'no_proxy') |
| 220 | + |
| 221 | +terraformDir=$(prop 'terraform.installdir') |
| 222 | + |
| 223 | +# generate terraform configuration file with name $(clusterTFVarsFile).tfvar |
| 224 | +generateTFVarFile |
| 225 | + |
| 226 | +# cleanup previously installed terraform binaries |
| 227 | +rm -rf ${terraformDir} |
| 228 | + |
| 229 | +# download terraform binaries into ${terraformDir} |
| 230 | +setupTerraform |
| 231 | + |
| 232 | +# clean previous versions of terraform oci provider |
| 233 | +deleteOlderVersionTerraformOCIProvider |
| 234 | + |
| 235 | +# run terraform init,plan,apply to create OCNE cluster based on the provided tfvar file ${tfVarsFile} |
| 236 | +createCluster |
| 237 | + |
| 238 | +#check status of OCNE cluster nodes, destroy if can not access them |
| 239 | +export KUBECONFIG=${terraformVarDir}/kubeconfig |
| 240 | +checkClusterRunning |
| 241 | +echo "${kubernetes_name} is up and running" |
0 commit comments