forked from pry0cc/axiom
-
Notifications
You must be signed in to change notification settings - Fork 24
/
axiom-account
executable file
·632 lines (557 loc) · 28.5 KB
/
axiom-account
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#!/bin/bash
AXIOM_PATH="$HOME/.axiom"
source "$AXIOM_PATH/interact/includes/vars.sh"
BASEOS="$(uname)"
case $BASEOS in
'Linux')
BASEOS='Linux'
;;
'FreeBSD')
BASEOS='FreeBSD'
alias ls='ls -G'
;;
'WindowsNT')
BASEOS='Windows'
;;
'Darwin')
BASEOS='Mac'
;;
'SunOS')
BASEOS='Solaris'
;;
'AIX') ;;
*) ;;
esac
accounts=$(ls -A1 "$AXIOM_PATH/accounts/" | grep '\.json$' | sed 's/\.json//g')
if [ -f ~/.axiom/axiom.json ] ; then
current=$(readlink -f "$AXIOM_PATH/axiom.json" | rev | cut -d / -f 1 | rev | cut -d . -f 1)> /dev/null 2>&1
fi
function bootstrap() {
account_helpers=$(find "$AXIOM_PATH/interact/account-helpers" -name '*.sh' ! -name 'generate*' -execdir basename -s '.sh' {} +)
account_str=$(echo "$account_helpers" | sed 's/ /, /g')
provider_path="$AXIOM_PATH/interact/account-helpers/$provider.sh"
bash "$provider_path"
}
if [ ! -z "$1" ]
then
path="$AXIOM_PATH/accounts/$1.json"
if [[ -f "$path" ]]
then
echo -e "${BWhite}Selecting $1...${Color_Off}"
rm "$AXIOM_PATH/axiom.json" > /dev/null 2>&1
ln -s "$AXIOM_PATH/accounts/$1.json" "$AXIOM_PATH/axiom.json"
provider="$(cat "$AXIOM_PATH/axiom.json" | jq -r '.provider')"
if [[ "$provider" == "ibm" ]]; then
jq '.provider = "ibm-classic"' "$AXIOM_PATH/accounts/$1.json" > "$AXIOM_PATH/accounts/$1.json.tmp"
mv "$AXIOM_PATH/accounts/$1.json.tmp" "$AXIOM_PATH/accounts/$1.json"
provider="ibm-classic"
fi
"$AXIOM_PATH"/interact/axiom-provider "$provider"
installed_packer_version=$(packer version 2>/dev/null | cut -d ' ' -f 2 | cut -d v -f2-)
if [[ "$(printf '%s\n' "$installed_packer_version" "$PackerVersion" | sort -V | head -n 1)" != "$PackerVersion" ]]; then
echo -e "${Yellow}Packer is either not installed or version is lower than the recommended version in ~/.axiom/interact/includes/vars.sh${Color_Off}"
echo "Installing/updating Packer version to version $PackerVersion..."
if [[ $BASEOS == "Mac" ]]; then
brew tap hashicorp/tap
brew install hashicorp/tap/packer
brew upgrade hashicorp/tap/packer
else
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" -y
sudo apt-get update && sudo apt-get install packer -y
# Check if the apt install failed or the version is still incorrect
installed_packer_version=$(packer version 2>/dev/null | cut -d ' ' -f 2 | cut -d v -f2-)
if [[ "$(printf '%s\n' "$installed_packer_version" "$PackerVersion" | sort -V | head -n 1)" != "$PackerVersion" ]]; then
echo -e "${Yellow}Failed to install Packer via package manager or verion version is still lower than the recommended version in ~/.axiom/interact/includes/vars.sh${Color_Off}"
echo -e "${Green}Downloading Packer binary instead, version $PackerVersion${Color_Off}"
# Backup to installing Packer binary
wget -q https://releases.hashicorp.com/packer/${PackerVersion}/packer_${PackerVersion}_linux_amd64.zip -P /tmp
unzip -o /tmp/packer_${PackerVersion}_linux_amd64.zip -d /tmp
sudo mv /tmp/packer /usr/bin/
rm /tmp/packer_${PackerVersion}_linux_amd64.zip
fi
fi
fi
# check installed version of doclt and update if needed. check for valid token. If token isnt valid, kick off account bootstrap
#
if [[ "$provider" == "do" ]]; then
installed_version=$(doctl version 2>/dev/null| grep version | cut -d ' ' -f 3 | cut -d '-' -f 1)
# Compare installed version with desired version
if [[ "$(printf '%s\n' "$installed_version" "$DoctlVersion" | sort -V | head -n 1)" != "$DoctlVersion" ]]; then
echo "installed_version is lower than DoctlVersion $DoctlVersion ~/.axiom/interact/includes/vars.sh"
if [[ $BASEOS == "Mac" ]]; then
echo "Installing/updating doctl to version $DoctlVersion..."
wget https://github.com/digitalocean/doctl/releases/download/v${DoctlVersion}/doctl-${DoctlVersion}-darwin-amd64.tar.gz -qO- | tar -xzv -C /usr/local/bin/
echo "doctl updated to version $DoctlVersion."
elif [[ $BASEOS == "Linux" ]]; then
if uname -a | grep -qi "Microsoft"; then
OS="UbuntuWSL"
else
OS=$(lsb_release -i | awk '{ print $3 }')
if ! command -v lsb_release &> /dev/null; then
OS="unknown-Linux"
BASEOS="Linux"
fi
fi
if [[ $OS == "Arch" ]] || [[ $OS == "ManjaroLinux" ]]; then
sudo pacman -Syu doctl --noconfirm
elif [[ $OS == "Ubuntu" ]] || [[ $OS == "Debian" ]] || [[ $OS == "Linuxmint" ]] || [[ $OS == "Parrot" ]] || [[ $OS == "Kali" ]] || [[ $OS == "unknown-Linux" ]] || [[ $OS == "UbuntuWSL" ]]; then
echo "Installing/updating doctl to version $DoctlVersion..."
wget https://github.com/digitalocean/doctl/releases/download/v${DoctlVersion}/doctl-${DoctlVersion}-linux-amd64.tar.gz -qO- | sudo tar -xzv -C /usr/local/bin/
echo "doctl updated to version $DoctlVersion."
elif [[ $OS == "Fedora" ]]; then
echo "Needs Conversation"
fi
fi
fi
echo -e "${BGreen}Installing doctl packer plugin...${Color_Off}"
packer plugins install github.com/digitalocean/digitalocean
# Validate and initialize DigitalOcean API token
token="$(jq -r '.do_key' "$AXIOM_PATH"/accounts/"$1".json)"
if ! doctl auth list &> /dev/null; then
echo -e "${BRed}Doctl is not authenticated.${Color_Off} Running bootstrap..."
doctl auth init -t "$token" || bootstrap
else
echo -e "${BGreen}Doctl is authenticated.${Color_Off}"
fi
token="$(jq -r '.do_key' "$AXIOM_PATH"/accounts/"$1".json)"
doctl auth init -t "$token" || bootstrap
fi # provider do
# check installed version of linode-cli and update if needed. check for valid token. If token isnt valid, kick off account bootstrap
#
if [[ "$provider" == "linode" ]]; then
echo -e "${Green}Checking linode-cli version...${Color_Off}"
# Get the currently installed version of linode-cli
installed_version=$(linode-cli --version 2>/dev/null | grep linode-cli | cut -d ' ' -f 2 | cut -d v -f 2-)
# Compare installed version with desired version
if [[ "$(printf '%s\n' "$installed_version" "$LinodeCliVersion" | sort -V | head -n 1)" != "$LinodeCliVersion" ]]; then
echo "installed_version is lower than LinodeCliVersion in ~/.axiom/interact/includes/vars.sh"
# Check if the installed version matches the desired version
echo "Installing/updating linode-cli to version $LinodeCliVersion..."
# Try to install or upgrade linode-cli and handle externally-managed-environment
output=$(pip3 install linode-cli --upgrade 2>&1)
if echo "$output" | grep -q "externally-managed-environment"; then
echo "Detected an externally managed environment. Retrying with --break-system-packages..."
pip3 install linode-cli --upgrade --break-system-packages
else
echo "linode-cli updated successfully or no externally managed environment detected."
fi
fi
echo -e "${BGreen}Installing linode packer plugin...${Color_Off}"
packer plugins install github.com/linode/linode
# Set up Linode CLI configuration
token="$(jq -r '.linode_key' "$AXIOM_PATH"/accounts/"$1".json)"
size="$(jq -r '.default_size' "$AXIOM_PATH"/accounts/"$1".json)"
region="$(jq -r '.region' "$AXIOM_PATH"/accounts/"$1".json)"
mkdir -p "$HOME/.config"
echo -e "[DEFAULT]\ndefault-user = axiom\n\n[axiom]\ntoken = $token\nregion = $region\ntype = $size\nimage = linode/alpine3.10" > "$HOME/.config/linode-cli"
# Validate token with Linode API
unauthed=$(curl -s -H "Authorization: Bearer $token" https://api.linode.com/v4/account | grep Invalid | wc -l | tr -d ' ')
if [[ "$unauthed" == "1" ]]; then
echo -e "Not authenticated to linode-cli"
bootstrap
else
echo -e "${BGreen}Linode-CLI is authenticated.${Color_Off}"
fi
fi # provider linode
# check installed version of ibmcloud cli and update if needed. check for valid token. If token isnt valid, kick off account bootstrap
#
if [[ "$provider" == "ibm-classic" ]] || [[ "$provider" == "ibm-vpc" ]] || [[ "$provider" == "ibm" ]] ; then
if [[ "$provider" == "ibm" ]]; then
provider=ibm-classic
fi
installed_version=$(ibmcloud version 2>/dev/null| cut -d ' ' -f 2 | cut -d + -f 1)
if [[ "$(printf '%s\n' "$installed_version" "$IBMCloudCliVersion" | sort -V | head -n 1)" != "$IBMCloudCliVersion" ]]; then
# Check if the installed version matches the required version
if [[ $BASEOS == "Mac" ]]; then
# macOS installation/update
echo -e "${BGreen}Installing ibmcloud-cli...${Color_Off}"
curl -fsSL https://clis.cloud.ibm.com/install/osx | sh
if [[ $provider == "ibm-classic" ]]; then
echo -e "${BGreen}Installing ibmcloud sl (SoftLayer) plugin...${Color_Off}"
ibmcloud plugin install sl -q -f
else
echo -e "${BGreen}Installing ibmcloud vpc plugin...${Color_Off}"
ibmcloud plugin install vpc-infrastructure -q -f
fi
elif [[ $BASEOS == "Linux" ]]; then
if uname -a | grep -qi "Microsoft"; then
OS="UbuntuWSL"
else
OS=$(lsb_release -i | awk '{ print $3 }')
if ! command -v lsb_release &> /dev/null; then
OS="unknown-Linux"
BASEOS="Linux"
fi
fi
if [[ $OS == "Arch" ]] || [[ $OS == "ManjaroLinux" ]]; then
echo "Needs Conversation for Arch or ManjaroLinux"
elif [[ $OS == "Ubuntu" ]] || [[ $OS == "Debian" ]] || [[ $OS == "Linuxmint" ]] || [[ $OS == "Parrot" ]] || [[ $OS == "Kali" ]] || [[ $OS == "unknown-Linux" ]] || [[ $OS == "UbuntuWSL" ]]; then
if ! [ -x "$(command -v ibmcloud)" ]; then
echo -e "${BGreen}Installing ibmcloud-cli on Linux...${Color_Off}"
curl -fsSL https://clis.cloud.ibm.com/install/linux | sh
fi
if [[ $provider == "ibm-classic" ]]; then
echo -e "${BGreen}Installing ibmcloud sl (SoftLayer) plugin...${Color_Off}"
ibmcloud plugin install sl -q -f
else
echo -e "${BGreen}Installing ibmcloud vpc plugin...${Color_Off}"
ibmcloud plugin install vpc-infrastructure -q -f
fi
elif [[ $OS == "Fedora" ]]; then
echo "Needs Conversation for Fedora"
fi
fi
echo "ibmcloud-cli updated to version $IBMCloudCliVersion."
fi
echo -e "${BGreen}Installing IBM Cloud packer plugin...${Color_Off}"
packer plugins install github.com/IBM/ibmcloud
# Validate Token
token="$(jq -r '.ibm_cloud_api_key' "$AXIOM_PATH"/accounts/"$1".json)"
ibmcloud login --apikey "$token" --no-region || bootstrap
if [[ $provider == "ibm-vpc" ]]; then
region="$(jq -r '.physical_region' "$AXIOM_PATH"/accounts/"$1".json)"
resource_group="$(jq -r '.resource_group' "$AXIOM_PATH"/accounts/"$1".json)"
ibmcloud target -r $region -g $resource_group
fi
fi # provider ibm
# check installed version of azure cli and update if needed. check for valid token. If token isnt valid, kick off account bootstrap
#
if [[ "$provider" == "azure" ]]; then
# Set the required Azure CLI version
installed_version=$(az version 2>/dev/null| jq -r '."azure-cli"')
if [[ "$(printf '%s\n' "$installed_version" "$AzureCliVersion" | sort -V | head -n 1)" != "$AzureCliVersion" ]]; then
# Handle macOS installation/update
if [[ $BASEOS == "Mac" ]]; then
whereis brew
if [ ! $? -eq 0 ] || [[ ! -z ${AXIOM_FORCEBREW+x} ]]; then
echo -e "${BGreen}Installing Homebrew...${Color_Off}"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
echo -e "${BGreen}Checking for Homebrew... already installed.${Color_Off}"
fi
if ! [ -x "$(command -v az)" ]; then
echo -e "${BGreen}Installing Azure CLI (az)...${Color_Off}"
brew update && brew install azure-cli
else
echo -e "${BGreen}Updating Azure CLI (az)...${Color_Off}"
brew update && brew upgrade azure-cli
fi
# Handle Linux installation/update
elif [[ $BASEOS == "Linux" ]]; then
sudo apt-get update -qq
sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg -y -qq
if uname -a | grep -qi "Microsoft"; then
OS="UbuntuWSL"
else
OS=$(lsb_release -i 2>/dev/null | awk '{ print $3 }')
if ! command -v lsb_release &> /dev/null; then
OS="unknown-Linux"
BASEOS="Linux"
fi
fi
AZ_REPO=$(lsb_release -cs)
if [[ $AZ_REPO == "kali-rolling" ]]; then
check_version=$(cat /proc/version | awk '{ print $6 $7 }' | tr -d '()' | cut -d . -f 1)
case $check_version in
Debian10)
AZ_REPO="buster"
;;
Debian11)
AZ_REPO="bullseye"
;;
Debian12)
AZ_REPO="bookworm"
;;
*)
echo "Unknown Debian version. Exiting."
exit 1
;;
esac
fi
curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list
sudo apt-get update -qq
sudo apt-get purge azure-cli -y -qq
sudo apt-get install azure-cli -y -qq
elif [[ $OS == "Fedora" ]]; then
echo "Needs Conversation for Fedora"
fi
fi
# Authenticate using client credentials stored in axiom.json
echo -e "${BGreen}Installing Azure packer plugin...${Color_Off}"
packer plugins install github.com/hashicorp/azure
client_id="$(jq -r '.client_id' "$AXIOM_PATH"/axiom.json)"
client_secret="$(jq -r '.client_secret' "$AXIOM_PATH"/axiom.json)"
tenant_id="$(jq -r '.tenant_id' "$AXIOM_PATH"/axiom.json)"
subscription_id="$(jq -r '.subscription_id' "$AXIOM_PATH"/axiom.json)"
use_azure_cli_auth="$(jq -r '.use_azure_cli_auth' "$AXIOM_PATH"/axiom.json)"
if [[ "$use_azure_cli_auth" == "true" ]]; then
echo -e "${BGreen}Authenticating with Azure using Azure CLI...${Color_Off}"
az login --service-principal --username "$client_id" --password "$client_secret" --tenant "$tenant_id" || bootstrap
az account set --subscription "$subscription_id"
sleep 3
# Check if the correct subscription is set and accessible
az account show &> /dev/null
if [[ $? -ne 0 ]]; then
echo -e "${BRed}Not authenticated to Azure CLI. Running bootstrap...${Color_Off}"
bootstrap
else
echo -e "${BGreen}Azure CLI is authenticated.${Color_Off}"
fi
else
echo -e "${BRed}Azure CLI authentication disabled in axiom.json${Color_Off}"
bootstrap
fi
fi # provider azure
# check installed version of aws cli and update if needed. check for valid token. If token isnt valid, kick off account bootstrap
#
if [[ "$provider" == "aws" ]]; then
installed_version=$(aws --version 2>/dev/null | cut -d ' ' -f 1 | cut -d '/' -f 2)
if [[ "$(printf '%s\n' "$installed_version" "$AWSCliVersion" | sort -V | head -n 1)" != "$AWSCliVersion" ]]; then
# Determine the OS type and handle installation accordingly
if [[ $BASEOS == "Mac" ]]; then
echo -e "${BGreen}Installing/Updating AWS CLI on macOS...${Color_Off}"
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /
rm AWSCLIV2.pkg
elif [[ $BASEOS == "Linux" ]]; then
if uname -a | grep -qi "Microsoft"; then
OS="UbuntuWSL"
else
OS=$(lsb_release -i 2>/dev/null | awk '{ print $3 }')
if ! command -v lsb_release &> /dev/null; then
OS="unknown-Linux"
BASEOS="Linux"
fi
fi
# Install AWS CLI based on specific Linux distribution
if [[ $OS == "Ubuntu" ]] || [[ $OS == "Debian" ]] || [[ $OS == "Linuxmint" ]] || [[ $OS == "Parrot" ]] || [[ $OS == "Kali" ]] || [[ $OS == "unknown-Linux" ]] || [[ $OS == "UbuntuWSL" ]]; then
echo -e "${BGreen}Installing/Updating AWS CLI on $OS...${Color_Off}"
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip"
cd /tmp
unzip awscliv2.zip
sudo ./aws/install
rm -rf /tmp/aws
rm /tmp/awscliv2.zip
elif [[ $OS == "Fedora" ]]; then
echo -e "${BGreen}Installing/Updating AWS CLI on Fedora...${Color_Off}"
sudo dnf install -y unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip"
cd /tmp
unzip awscliv2.zip
sudo ./aws/install
rm -rf /tmp/aws
rm /tmp/awscliv2.zip
else
echo -e "${BRed}Unsupported Linux distribution: $OS${Color_Off}"
fi
fi
fi
echo -e "${BGreen}Installing AWS packer plugin...${Color_Off}"
packer plugins install github.com/hashicorp/amazon
# Use credentials from axiom.json if available
aws_access_key="$(jq -r '.aws_access_key' "$AXIOM_PATH"/axiom.json)"
aws_secret_access_key="$(jq -r '.aws_secret_access_key' "$AXIOM_PATH"/axiom.json)"
aws_region="$(jq -r '.region' "$AXIOM_PATH"/axiom.json)"
if [[ -n "$aws_access_key" && -n "$aws_secret_access_key" ]]; then
echo -e "${BGreen}Configuring AWS CLI with credentials from axiom.json...${Color_Off}"
aws configure set aws_access_key "$aws_access_key"
aws configure set aws_secret_access_key "$aws_secret_access_key"
aws configure set region "$aws_region"
else
echo -e "${BRed}No AWS credentials found in axiom.json. Please set them manually.${Color_Off}"
bootstrap
fi
# Check if AWS CLI is authenticated
if ! aws sts get-caller-identity &> /dev/null; then
echo -e "${BRed}AWS CLI is not authenticated.${Color_Off} Running bootstrap..."
bootstrap
else
echo -e "${BGreen}AWS CLI is authenticated.${Color_Off}"
fi
fi # provider aws
# check installed version of gcloud cli and update if needed. check for valid token. If token isnt valid, kick off account bootstrap
#
if [[ "$provider" == "gcp" ]]; then
echo -e "${Green}Checking gcloud version...${Color_Off}"
# Get the currently installed version of gcloud
installed_version=$(gcloud version 2>/dev/null | grep 'Google Cloud SDK' | cut -d ' ' -f 4)
# Compare installed version with desired version (set in vars.sh)
if [[ "$(printf '%s\n' "$installed_version" "$GCloudCliVersion" | sort -V | head -n 1)" != "$GCloudCliVersion" ]]; then
echo "Installed version of gcloud is lower than required GCloudCliVersion in ~/.axiom/interact/includes/vars.sh"
# Install or update gcloud based on the OS
if [[ $BASEOS == "Mac" ]]; then
echo -e "${BGreen}Installing/Updating gcloud CLI on macOS...${Color_Off}"
curl https://sdk.cloud.google.com | bash
exec -l $SHELL
elif [[ $BASEOS == "Linux" ]]; then
if uname -a | grep -qi "Microsoft"; then
OS="UbuntuWSL"
else
OS=$(lsb_release -i 2>/dev/null | awk '{ print $3 }')
if ! command -v lsb_release &> /dev/null; then
OS="unknown-Linux"
BASEOS="Linux"
fi
fi
echo -e "${BGreen}Installing/Updating gcloud CLI on Linux...${Color_Off}"
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
sudo apt-get install apt-transport-https ca-certificates gnupg -y -qq
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo tee /usr/share/keyrings/cloud.google.gpg > /dev/null
sudo apt-get update -qq && sudo apt-get install google-cloud-sdk -y -qq
fi
fi
echo -e "${BGreen}Installing GCP packer plugin...${Color_Off}"
packer plugins install github.com/hashicorp/googlecompute
# Configure gcloud using service account credentials from axiom.json
echo -e "${BGreen}Configuring gcloud CLI with credentials from axiom.json...${Color_Off}"
service_account_key="$(jq -r '.service_account_key' "$AXIOM_PATH"/accounts/"$1".json)"
if [[ -n "$service_account_key" ]]; then
gcloud auth activate-service-account --key-file="$service_account_key"
gcloud config set project "$(jq -r '.project' "$AXIOM_PATH"/accounts/"$1".json)"
gcloud config set compute/region "$(jq -r '.physical_region' "$AXIOM_PATH"/accounts/"$1".json)"
gcloud config set compute/zone "$(jq -r '.region' "$AXIOM_PATH"/accounts/"$1".json)"
# Check if gcloud is authenticated
if ! gcloud auth list --filter=status:ACTIVE &> /dev/null; then
echo -e "${BRed}gcloud is not authenticated. Running bootstrap...${Color_Off}"
bootstrap
else
echo -e "${BGreen}gcloud is authenticated.${Color_Off}"
fi
else
echo -e "${BRed}No service account key found in axiom.json. Please set it manually.${Color_Off}"
bootstrap
fi
fi # provider gcp
# Install hetzner if not already installed, check token is valid or kick off bootstrap
#
if [[ "$provider" == "hetzner" ]]; then
installed_version=$(hcloud version 2>/dev/null| cut -d ' ' -f 2)
if [[ "$(printf '%s\n' "$installed_version" "$HetznerCliVersion" | sort -V | head -n 1)" != "$HetznerCliVersion" ]]; then
# Handle macOS installation/update
if [[ $BASEOS == "Mac" ]]; then
whereis brew
if [ ! $? -eq 0 ] || [[ ! -z ${AXIOM_FORCEBREW+x} ]]; then
echo -e "${BGreen}Installing Homebrew...${Color_Off}"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
echo -e "${BGreen}Checking for Homebrew... already installed.${Color_Off}"
fi
echo -e "${BGreen}Installing hetzner-cloud CLI (hcloud)...${Color_Off}"
brew install hcloud
# Handle Linux installation/update
elif [[ $BASEOS == "Linux" ]]; then
if uname -a | grep -qi "Microsoft"; then
OS="UbuntuWSL"
else
OS=$(lsb_release -i 2>/dev/null | awk '{ print $3 }')
if ! command -v lsb_release &> /dev/null; then
OS="unknown-Linux"
BASEOS="Linux"
fi
fi
# Install or update hcloud on different Linux distributions
if [[ $OS == "Arch" ]] || [[ $OS == "ManjaroLinux" ]]; then
sudo pacman -Syu hcloud --noconfirm
elif [[ $OS == "Ubuntu" ]] || [[ $OS == "Debian" ]] || [[ $OS == "Linuxmint" ]] || [[ $OS == "Parrot" ]] || [[ $OS == "Kali" ]] || [[ $OS == "unknown-Linux" ]] || [[ $OS == "UbuntuWSL" ]]; then
echo -e "${BGreen}Installing hetzner-cloud CLI (hcloud)...${Color_Off}"
wget -q -O /tmp/hetzner-cli.tar.gz https://github.com/hetznercloud/cli/releases/download/v${HetznerCliVersion}/hcloud-linux-amd64.tar.gz && tar -xvzf /tmp/hetzner-cli.tar.gz -C /tmp && sudo mv /tmp/hcloud /usr/bin/hcloud && rm /tmp/hetzner-cli.tar.gz
elif [[ $OS == "Fedora" ]]; then
echo "Needs Conversation for Fedora"
fi
fi
fi
echo -e "${BGreen}Installing Hetzner packer plugin...${Color_Off}"
packer plugins install github.com/hetznercloud/hcloud
# Validate token and setup configuration
token="$(jq -r '.hetzner_key' "$AXIOM_PATH"/accounts/"$1".json)"
status_code=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer $token" https://api.hetzner.cloud/v1/servers)
echo -e "${BGreen}Configuring Hetzner CLI with credentials from axiom.json...${Color_Off}"
if [[ "$status_code" == "200" ]]; then
mkdir -p "$HOME/.config/hcloud"
cat <<EOT > "$HOME/.config/hcloud/cli.toml"
active_context = "$1"
[[contexts]]
name = "$1"
token = "$token"
EOT
else
echo -e "Not authenticated to hetzner-cloud CLI (hcloud)"
bootstrap
fi
fi # end of hetzner
# Install Scaleway CLI if not already installed, check token is valid or kick off bootstrap
#
if [[ "$provider" == "scaleway" ]]; then
installed_version=$(scw version -o json 2>/dev/null | jq -r .version)
if [[ "$(printf '%s\n' "$installed_version" "$ScalewayCliVersion" | sort -V | head -n 1)" != "$ScalewayCliVersion" ]]; then
# Handle macOS installation/update
if [[ $BASEOS == "Mac" ]]; then
whereis brew
if [ ! $? -eq 0 ] || [[ ! -z ${AXIOM_FORCEBREW+x} ]]; then
echo -e "${BGreen}Installing Homebrew...${Color_Off}"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
echo -e "${BGreen}Checking for Homebrew... already installed.${Color_Off}"
fi
echo -e "${BGreen}Installing Scaleway CLI (scw)...${Color_Off}"
brew install scw
# Handle Linux installation/update
elif [[ $BASEOS == "Linux" ]]; then
if uname -a | grep -qi "Microsoft"; then
OS="UbuntuWSL"
else
OS=$(lsb_release -i 2>/dev/null | awk '{ print $3 }')
if ! command -v lsb_release &> /dev/null; then
OS="unknown-Linux"
BASEOS="Linux"
fi
fi
# Install or update scaleway-cli on different Linux distributions
if [[ $OS == "Arch" ]] || [[ $OS == "ManjaroLinux" ]]; then
pacman -S scaleway-cli
elif [[ $OS == "Ubuntu" ]] || [[ $OS == "Debian" ]] || [[ $OS == "Linuxmint" ]] || [[ $OS == "Parrot" ]] || [[ $OS == "Kali" ]] || [[ $OS == "unknown-Linux" ]] || [[ $OS == "UbuntuWSL" ]]; then
echo -e "${BGreen}Installing Scaleway Cloud CLI (scw)...${Color_Off}"
curl -s https://raw.githubusercontent.com/scaleway/scaleway-cli/master/scripts/get.sh | sh
elif [[ $OS == "Fedora" ]]; then
echo "Needs Conversation for Fedora"
fi
fi
fi
echo -e "${BGreen}Installing Scaleway packer plugin...${Color_Off}"
packer plugins install github.com/scaleway/scaleway
# Setup Scaleway account configuration
access_key="$(jq -r '.access_key' "$AXIOM_PATH/accounts/$1.json")"
secret_key="$(jq -r '.secret_key' "$AXIOM_PATH/accounts/$1.json")"
physical_region="$(jq -r '.physical_region' "$AXIOM_PATH/accounts/$1.json")"
region="$(jq -r '.region' "$AXIOM_PATH/accounts/$1.json")"
# Configure Scaleway CLI with credentials
echo -e "${BGreen}Configuring Scaleway CLI with credentials from $1.json...${Color_Off}"
scw config set access-key="$access_key" secret-key="$secret_key" default-region="$physical_region" default-zone="$region"
# Check if credentials are valid
if scw account project get &> /dev/null; then
echo -e "${BGreen}Scaleway account authenticated successfully.${Color_Off}"
else
echo -e "${Red}Scaleway account authentication failed. Please check your credentials.${Color_Off}"
bootstrap
fi
fi # end of scaleway
echo -e "${Green}Selected $1 successfully${Color_Off}"
else
echo -e "${Red}Account name not present${Color_Off}"
fi
else
echo -e "${BWhite}Available Accounts${Color_Off}"
echo -n -e "${BGreen}"
for account in $accounts
do
if [[ $account == $current ]]
then
echo "* $account"
else
echo " $account"
fi
done
echo -n -e "${Color_Off}"
fi