35
35
# Get required version for a tool
36
36
get_tool_required_version () {
37
37
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 " ;;
41
41
jq) echo " 1.7.1" ;;
42
42
aws) echo " 2.17.0" ;;
43
43
gcloud) echo " 500.0.0" ;;
@@ -75,7 +75,7 @@ check_sudo() {
75
75
HAS_SUDO=false
76
76
if tool_exists sudo; then
77
77
# 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
79
79
HAS_SUDO=true
80
80
log_debug " Sudo access is available"
81
81
else
@@ -103,10 +103,17 @@ confirm_installation() {
103
103
[[ $response =~ ^[nN] ]] && { log_error " Skipping $1 installation" ; return 1; } || return 0
104
104
}
105
105
106
+ # Function to compare versions, returns true if the current version is greater than or equal to the required version
106
107
version_compare () {
107
108
local v1=${1# v} v2=${2# v}
108
109
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
110
117
}
111
118
112
119
get_cloud_tools () {
@@ -200,7 +207,7 @@ get_tool_version() {
200
207
case $1 in
201
208
terraform) version=$( terraform version | head -n1 | cut -d' v' -f2) ;;
202
209
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 ) ;;
204
211
aws) version=$( aws --version 2>&1 | cut -d' /' -f2) ;;
205
212
gcloud) version=$( gcloud version 2> /dev/null | grep " Google Cloud SDK" | awk ' {print $4}' ) ;;
206
213
az) version=$( az version | jq -r ' ."azure-cli"' ) ;;
@@ -210,35 +217,59 @@ get_tool_version() {
210
217
echo " $version "
211
218
}
212
219
213
- verify_tool_version () {
220
+ # Function to check if upgrade is needed for a tool
221
+ upgrade_tool_version () {
214
222
local tool=$1
215
223
local required_version
216
224
local current_version
217
225
218
226
required_version=$( get_tool_required_version " $tool " )
219
227
current_version=$( get_tool_version " $tool " )
220
228
221
- version_compare " $current_version " " $required_version " || \
229
+ if ! version_compare " $current_version " " $required_version " ; then
222
230
log_error " $tool version $current_version found. Version $required_version or higher required"
231
+ true
232
+ else
233
+ false
234
+ fi
223
235
}
224
236
225
237
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
227
240
mkdir -p " $tmp_dir " && cd " $tmp_dir " || exit 1
228
241
log_debug " Installing $tool in temporary directory: $tmp_dir "
229
242
local version
230
243
version=$( get_tool_required_version " $tool " )
231
244
232
245
case $tool in
233
246
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
237
259
;;
238
260
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
242
273
;;
243
274
helm)
244
275
log_debug " Downloading helm version $version "
@@ -346,7 +377,7 @@ install_tool() {
346
377
;;
347
378
esac
348
379
349
- cd - > /dev/null && rm -rf " $tmp_dir "
380
+ popd > /dev/null && rm -rf " $tmp_dir "
350
381
log_debug " Finished installing $tool "
351
382
}
352
383
@@ -371,13 +402,14 @@ enforce_terraform_version() {
371
402
}
372
403
373
404
verify_tools () {
405
+ log_debug " Verifying tools..."
374
406
local cloud_provider=$1
375
407
local missing_tools=()
376
408
local install_failed=()
377
409
378
410
# First, make sure Terraform is installed with the correct version
379
411
enforce_terraform_version
380
-
412
+ log_debug " Terraform verification complete "
381
413
# Check other common tools (skip terraform as we've already handled it)
382
414
for tool in " ${COMMON_TOOLS[@]} " ; do
383
415
if [ " $tool " != " terraform" ]; then
@@ -387,8 +419,8 @@ verify_tools() {
387
419
else
388
420
missing_tools+=(" $tool " )
389
421
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 " )
392
424
fi
393
425
fi
394
426
done
@@ -402,8 +434,8 @@ verify_tools() {
402
434
else
403
435
missing_tools+=(" $tool " )
404
436
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 " )
407
439
fi
408
440
done
409
441
@@ -427,6 +459,7 @@ install_cloud_tools() {
427
459
for tool in " ${tools[@]} " ; do
428
460
if ! tool_exists " $tool " ; then
429
461
if confirm_installation " $tool " ; then
462
+ log_debug " Installing $tool "
430
463
install_tool " $tool "
431
464
else
432
465
return $MISSING_TOOLS
@@ -465,7 +498,7 @@ create_backend() {
465
498
export AWS_PROFILE=" $profile "
466
499
# Verify AWS credentials
467
500
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 "
469
502
exit 1
470
503
fi
471
504
log_success " AWS authentication successful using profile: $profile "
@@ -700,10 +733,11 @@ main() {
700
733
701
734
# Step 1: Detect system
702
735
detect_system
736
+ log_info " System detection complete"
703
737
704
738
# Step 2: Install essential utilities
705
739
install_essential_utilities || log_error " Missing essential utilities may affect deployment"
706
-
740
+ log_info " Essential utilities installation complete "
707
741
# Step 3: Verify and install required tools
708
742
verify_tools " $cloud_provider "
709
743
local verify_status=$?
@@ -713,7 +747,7 @@ main() {
713
747
elif [ $verify_status -ne $SUCCESS ]; then
714
748
exit $verify_status
715
749
fi
716
-
750
+ log_info " Tool verification complete "
717
751
# Step 4: Create backend (required for all providers)
718
752
if [ -n " $config_file " ]; then
719
753
log_info " Setting up backend storage for $cloud_provider "
0 commit comments