Skip to content

Commit ddf730f

Browse files
authored
Pre requisites upgrade (#1370)
* Upgrading the logic for terraform and other installation * Updating pre-requisites with correct version of components and improved logic * Updating log logic
1 parent 1cde0f6 commit ddf730f

File tree

1 file changed

+58
-24
lines changed

1 file changed

+58
-24
lines changed

scripts/infra-automation/pre-requisites.sh

+58-24
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ EOF
3535
# Get required version for a tool
3636
get_tool_required_version() {
3737
case $1 in
38-
terraform) echo "1.11.0" ;;
39-
kubectl) echo "1.28.0" ;;
40-
helm) echo "3.16.0" ;;
38+
terraform) echo "1.10.1" ;;
39+
kubectl) echo "1.31.8" ;;
40+
helm) echo "3.17.3" ;;
4141
jq) echo "1.7.1" ;;
4242
aws) echo "2.17.0" ;;
4343
gcloud) echo "500.0.0" ;;
@@ -75,7 +75,7 @@ check_sudo() {
7575
HAS_SUDO=false
7676
if tool_exists sudo; then
7777
# Check if user has sudo privileges by attempting a harmless command
78-
if sudo -n true 2>/dev/null; then
78+
if sudo -v 2>/dev/null; then
7979
HAS_SUDO=true
8080
log_debug "Sudo access is available"
8181
else
@@ -103,10 +103,17 @@ confirm_installation() {
103103
[[ $response =~ ^[nN] ]] && { log_error "Skipping $1 installation"; return 1; } || return 0
104104
}
105105

106+
# Function to compare versions, returns true if the current version is greater than or equal to the required version
106107
version_compare() {
107108
local v1=${1#v} v2=${2#v}
108109
log_debug "Comparing versions: $v1 >= $v2"
109-
[ "$(echo "$v1" | awk -F. '{printf "%d%02d", $1, $2}')" -ge "$(echo "$v2" | awk -F. '{printf "%d%02d", $1, $2}')" ]
110+
local current_version="$(echo "$v1" | awk -F. '{printf "%d%02d", $1, $2}')"
111+
local required_version="$(echo "$v2" | awk -F. '{printf "%d%02d", $1, $2}')"
112+
if [ "$current_version" -ge "$required_version" ]; then
113+
true
114+
else
115+
false
116+
fi
110117
}
111118

112119
get_cloud_tools() {
@@ -200,7 +207,7 @@ get_tool_version() {
200207
case $1 in
201208
terraform) version=$(terraform version | head -n1 | cut -d'v' -f2) ;;
202209
kubectl) version=$(kubectl version --client -o json | jq -r '.clientVersion.gitVersion' | cut -d'v' -f2) ;;
203-
helm) version=$(helm version --short | cut -d'v' -f2) ;;
210+
helm) version=$(helm version --short | cut -d'+' -f1) ;;
204211
aws) version=$(aws --version 2>&1 | cut -d'/' -f2) ;;
205212
gcloud) version=$(gcloud version 2>/dev/null | grep "Google Cloud SDK" | awk '{print $4}') ;;
206213
az) version=$(az version | jq -r '."azure-cli"') ;;
@@ -210,35 +217,59 @@ get_tool_version() {
210217
echo "$version"
211218
}
212219

213-
verify_tool_version() {
220+
# Function to check if upgrade is needed for a tool
221+
upgrade_tool_version() {
214222
local tool=$1
215223
local required_version
216224
local current_version
217225

218226
required_version=$(get_tool_required_version "$tool")
219227
current_version=$(get_tool_version "$tool")
220228

221-
version_compare "$current_version" "$required_version" || \
229+
if ! version_compare "$current_version" "$required_version"; then
222230
log_error "$tool version $current_version found. Version $required_version or higher required"
231+
true
232+
else
233+
false
234+
fi
223235
}
224236

225237
install_tool() {
226-
local tool=$1 tmp_dir="/tmp/tool-install"
238+
local tool=$1 tmp_dir="$(mktemp -d)"
239+
pushd "$tmp_dir" > /dev/null
227240
mkdir -p "$tmp_dir" && cd "$tmp_dir" || exit 1
228241
log_debug "Installing $tool in temporary directory: $tmp_dir"
229242
local version
230243
version=$(get_tool_required_version "$tool")
231244

232245
case $tool in
233246
terraform)
234-
log_debug "Downloading terraform version $version"
235-
wget -q "https://releases.hashicorp.com/terraform/${version}/terraform_${version}_${OS}_${ARCH}.zip" -O terraform.zip
236-
unzip -q terraform.zip && run_with_sudo mv terraform /usr/local/bin/
247+
local terraform_path
248+
if tool_exists terraform; then
249+
terraform_path=$(which terraform)
250+
fi
251+
if [[ $terraform_path == *"homebrew"* ]]; then
252+
log_debug "Terraform is installed via brew. Using brew to upgrade terraform..."
253+
brew upgrade terraform
254+
else
255+
log_debug "Downloading terraform version $version"
256+
wget -q "https://releases.hashicorp.com/terraform/${version}/terraform_${version}_${OS}_${ARCH}.zip" -O terraform.zip
257+
unzip -q terraform.zip && run_with_sudo mv terraform /usr/local/bin/
258+
fi
237259
;;
238260
kubectl)
239-
log_debug "Downloading kubectl version $version"
240-
wget -q "https://dl.k8s.io/release/v${version}/bin/${OS}/${ARCH}/kubectl" -O kubectl
241-
chmod +x kubectl && run_with_sudo mv kubectl /usr/local/bin/
261+
local kubectl_path
262+
if tool_exists kubectl; then
263+
kubectl_path=$(which kubectl)
264+
fi
265+
if [[ $kubectl_path == *"homebrew"* ]]; then
266+
log_debug "Kubectl is installed via brew. Using brew to upgrade kubectl..."
267+
brew upgrade kubectl
268+
else
269+
log_debug "Downloading kubectl version $version"
270+
wget -q "https://dl.k8s.io/release/v${version}/bin/${OS}/${ARCH}/kubectl" -O kubectl
271+
chmod +x kubectl && run_with_sudo mv kubectl /usr/local/bin/
272+
fi
242273
;;
243274
helm)
244275
log_debug "Downloading helm version $version"
@@ -346,7 +377,7 @@ install_tool() {
346377
;;
347378
esac
348379

349-
cd - > /dev/null && rm -rf "$tmp_dir"
380+
popd > /dev/null && rm -rf "$tmp_dir"
350381
log_debug "Finished installing $tool"
351382
}
352383

@@ -371,13 +402,14 @@ enforce_terraform_version() {
371402
}
372403

373404
verify_tools() {
405+
log_debug "Verifying tools..."
374406
local cloud_provider=$1
375407
local missing_tools=()
376408
local install_failed=()
377409

378410
# First, make sure Terraform is installed with the correct version
379411
enforce_terraform_version
380-
412+
log_debug "Terraform verification complete"
381413
# Check other common tools (skip terraform as we've already handled it)
382414
for tool in "${COMMON_TOOLS[@]}"; do
383415
if [ "$tool" != "terraform" ]; then
@@ -387,8 +419,8 @@ verify_tools() {
387419
else
388420
missing_tools+=("$tool")
389421
fi
390-
elif ! verify_tool_version "$tool"; then
391-
return $VERSION_ERROR
422+
elif upgrade_tool_version "$tool"; then
423+
install_tool "$tool" || install_failed+=("$tool")
392424
fi
393425
fi
394426
done
@@ -402,8 +434,8 @@ verify_tools() {
402434
else
403435
missing_tools+=("$tool")
404436
fi
405-
elif ! verify_tool_version "$tool"; then
406-
return $VERSION_ERROR
437+
elif upgrade_tool_version "$tool"; then
438+
install_tool "$tool" || install_failed+=("$tool")
407439
fi
408440
done
409441

@@ -427,6 +459,7 @@ install_cloud_tools() {
427459
for tool in "${tools[@]}"; do
428460
if ! tool_exists "$tool"; then
429461
if confirm_installation "$tool"; then
462+
log_debug "Installing $tool"
430463
install_tool "$tool"
431464
else
432465
return $MISSING_TOOLS
@@ -465,7 +498,7 @@ create_backend() {
465498
export AWS_PROFILE="$profile"
466499
# Verify AWS credentials
467500
if ! aws sts get-caller-identity >/dev/null 2>&1; then
468-
log_error "AWS authentication failed. Please run 'aws sso login' or check your credentials"
501+
log_error "AWS authentication failed. Please run 'aws sso login' or check your aws profile '$profile'export"
469502
exit 1
470503
fi
471504
log_success "AWS authentication successful using profile: $profile"
@@ -700,10 +733,11 @@ main() {
700733

701734
# Step 1: Detect system
702735
detect_system
736+
log_info "System detection complete"
703737

704738
# Step 2: Install essential utilities
705739
install_essential_utilities || log_error "Missing essential utilities may affect deployment"
706-
740+
log_info "Essential utilities installation complete"
707741
# Step 3: Verify and install required tools
708742
verify_tools "$cloud_provider"
709743
local verify_status=$?
@@ -713,7 +747,7 @@ main() {
713747
elif [ $verify_status -ne $SUCCESS ]; then
714748
exit $verify_status
715749
fi
716-
750+
log_info "Tool verification complete"
717751
# Step 4: Create backend (required for all providers)
718752
if [ -n "$config_file" ]; then
719753
log_info "Setting up backend storage for $cloud_provider"

0 commit comments

Comments
 (0)