-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelios
105 lines (92 loc) · 3.14 KB
/
helios
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
#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# Export environment variables
export HEL_SETTINGS_FILE="$(pwd)/helios.targets.json"
export TF_VAR_helios_workspace=$PWD
export TF_VAR_helios_runner=$PWD/scripts/runner.py
export TF_DIR=$(pwd)/iac/terraform
COMMAND=$1
ENV=$2
# Validate input arguments
if [[ -z "$1" ]]; then
echo "Error: Missing command argument. Must be one of 'plan', 'apply', 'init', 'burn', 'wipe' or 'bootstrap'."
exit 1
fi
# Handle 'helios burn state' command separately
if [[ "$1" == "burn" ]]; then
exec $PWD/scripts/linux/terraform-burn
echo "state was reset, reset your local kubernetes cluster to start fresh"
exit 1
fi
# Handle 'helios wipe' command separately
if [[ "$1" == "wipe" ]]; then
$PWD/helios burn && $PWD/helios bootstrap dev && $PWD/helios apply dev
echo "state was reset, reset your local kubernetes cluster to start fresh"
exit 1
fi
if [[ -z "$2" ]]; then
echo "Error: Missing environment argument. Must be one of 'local', 'dev', 'ci', 'prod'."
exit 1
fi
# Extract Kube context
export HELIOS_KUBE_CONTEXT=$(jq -r ".$2.kube.context" $HEL_SETTINGS_FILE)
if [ -z "$HELIOS_KUBE_CONTEXT" ]; then
echo "Error: HELIOS_KUBE_CONTEXT is not set. Ensure that the environment \"$2\" is correctly defined in $HEL_SETTINGS_FILE."
exit 1
fi
kubectl config use-context $HELIOS_KUBE_CONTEXT
echo "Using Kube Context: $HELIOS_KUBE_CONTEXT"
# Handle 'helios bootstrap' command separately
if [[ "$1" == "bootstrap" ]] && [[ "$2" =~ ^(dev|stage|prod)$ ]]; then
exec $PWD/scripts/linux/bootstrap-cluster $ENV
elif [[ ! "$1" =~ ^(plan|apply|init)$ ]]; then
echo "Error: Invalid command argument. Must be one of 'plan', 'apply', 'init' or 'bootstrap'."
exit 1
fi
# Determine the environment
case "$ENV" in
dev)
ENV_FILE="./iac/.env"
;;
ci)
ENV_FILE="./iac/.env.ci"
;;
next)
ENV_FILE="./iac/.env.next"
;;
prod)
ENV_FILE="./iac/.env.production"
;;
*)
echo "Error: Invalid environment argument. Must be one of 'dev', 'ci', 'prod', 'next'."
exit 1
;;
esac
# Read the helios.targets.json file
TF_MODULE=$(jq -r ".$2.module" $HEL_SETTINGS_FILE)
TF_CLUSTER_MODULE=$(jq -r ".$2.cluster_module" $HEL_SETTINGS_FILE)
if [ -z "$TF_MODULE" ]; then
echo "Error: The module for the environment \"$2\" does not exist in $HEL_SETTINGS_FILE."
exit 1
fi
# Echo the parameters that were read before running the final command
cat << EOF
Parameters:
Command: $1
Environment: $2
ENV_FILE: $ENV_FILE
HEL_SETTINGS_FILE: $HEL_SETTINGS_FILE
TF_DIR: $TF_DIR
TF_MODULE: $TF_MODULE
TF_CLUSTER_MODULE: $TF_MODULE
EOF
# Check tool installation
./scripts/linux/check-tool-installation
./scripts/linux/helios-health-check
./scripts/linux/helios-source-hash-to-json
# Execute terraform command
dotenv -e $ENV_FILE -- terraform -chdir=$TF_DIR init
dotenv -e $ENV_FILE -- terraform -chdir=$TF_DIR $1 -target=$TF_CLUSTER_MODULE -auto-approve
dotenv -e $ENV_FILE -- terraform -chdir=$TF_DIR $1 -target=$TF_MODULE -auto-approve
# dotenv -e $ENV_FILE -- terraform -chdir=$TF_DIR $1 -target=module.dev_env.module.kafka -auto-approve