forked from aws-samples/eks-cluster-upgrades-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·181 lines (141 loc) · 6.78 KB
/
install.sh
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
#!/bin/bash
# Function to prompt for yes/no input
prompt_yes_no() {
local question=$1
local default=$2
local answer
# Set default value if provided
if [[ $default == "yes" ]]; then
question+=" [Y/n]: "
elif [[ $default == "no" ]]; then
question+=" [y/N]: "
else
question+=" [y/n]: "
fi
# Prompt for input
read -p "$question" answer
# Set default value if input is empty
if [[ -z $answer ]]; then
answer=$default
fi
# Convert input to lowercase using 'tr'
answer=$(echo "$answer" | tr '[:upper:]' '[:lower:]')
# Check if answer is valid
if [[ $answer == "y" || $answer == "yes" ]]; then
echo "yes"
else
echo "no"
fi
}
# Function to prompt for sensitive input
prompt_sensitive() {
local question=$1
local answer
# Prompt for input (hide input)
read -sp "$question" answer
echo "$answer"
}
# Function to replace variables in files using sed
replace_variables() {
local file=$1
local replacements=$2
# Perform the replacements using sed
for key in "${!replacements[@]}"; do
sed -i'' -e "s|$key|${replacements[$key]}|g" "$file"
done
}
# Function to build the git_url
build_git_url() {
local base_url="https://github.com/aws-samples/eks-cluster-upgrades-workshop"
local username=$1
local git_url="${base_url/aws-samples/$username}"
echo "$git_url"
}
# Prompt for fork creation
fork_created=$(prompt_yes_no "Have you already created your fork?" "no")
# Prompt for personal access token
token_created=$(prompt_yes_no "Have you already created your personal access token?" "no")
# Check if both answers are "yes"
if [[ $fork_created == "yes" && $token_created == "yes" ]]; then
# Prompt for git username
read -p "Enter your git username: " git_username
read -p "Enter your git branch (leave blank to set to main): " git_branch
# create an if to validate the branch and set main as default if not provided by user
if [[ -z $git_branch ]]; then
git_branch="main"
echo "Branch set to main."
fi
# Prompt for GitHub personal access token (same as git password)
git_password=$(prompt_sensitive "Enter your GitHub personal access token: ")
# Prompt for AWS region
read -p "Enter your AWS region: " aws_region
# Build the git_url
git_url=$(build_git_url "$git_username")
# Output the collected inputs
echo -e "\nFork created: $fork_created"
echo "Token created: $token_created"
echo "Git username: $git_username"
echo "AWS region: $aws_region"
echo "Git URL: $git_url"
# Proceed with further actions
echo "Proceeding with further actions... Cloning your forked GitHub Repository"
git clone "$git_url" -b $git_branch
read -p "Enter the tf_state path (leave blank to generate infrastructure from scratch): " tf_state_path
if [[ -n $tf_state_path ]]; then
# Copy tf_state file to current directory
cp "$tf_state_path" eks-cluster-upgrades-workshop/terraform/clusters/terraform.tfstate
echo "Terraform state file copied to current directory."
fi
cd eks-cluster-upgrades-workshop/terraform/clusters
echo "Configuring Terraform"
terraform init
terraform plan -var="git_password=$git_password" -var="git_username=$git_username" -var="git_url=$git_url" -var="git_branch=$git_branch" -var="aws_region=$aws_region"
# TODO: add aws_region on terraform apply command
terraform apply -var="git_password=$git_password" -var="git_username=$git_username" -var="git_url=$git_url" -var="git_branch=$git_branch" -var="aws_region=$aws_region" --auto-approve
sleep 5
# TODO: add aws_region on terraform apply command
terraform apply -var="git_password=$git_password" -var="git_username=$git_username" -var="git_url=$git_url" -var="git_branch=$git_branch" -var="aws_region=$aws_region" --auto-approve
aws eks --region $aws_region update-kubeconfig --name eks-upgrades-workshop
echo "Change needed variables on template for GitOps"
# Retrieve Terraform outputs and set them as environment variables
ARGO_WORKFLOWS_BUCKET_ARN=$(terraform output -raw argo_workflows_bucket_arn)
ARGO_WORKFLOWS_BUCKET_NAME=$(terraform output -raw argo_workflows_bucket_name)
ARGO_WORKFLOWS_IRSA=$(terraform output -raw argo_workflows_irsa)
AWS_REGION=$(terraform output -raw aws_region)
AWS_VPC_ID=$(terraform output -raw aws_vpc_id)
CLUSTER_ENDPOINT=$(terraform output -raw cluster_endpoint)
CLUSTER_IAM_ROLE_NAME=$(terraform output -raw cluster_iam_role_name)
CLUSTER_SECURITY_GROUP_ID=$(terraform output -raw cluster_primary_security_group_id)
KARPENTER_INSTANCE_PROFILE=$(terraform output -raw karpenter_instance_profile)
KARPENTER_IRSA=$(terraform output -raw karpenter_irsa)
pwd
# Define the file paths
karpenter_file="../../gitops/add-ons/02-karpenter.yaml"
argo_workflows_file="../../gitops/add-ons/03-argo-workflows.yaml"
upgrades_workflow_file="../../upgrades-workflows/upgrade-validate-workflow.yaml"
# Perform the replacements using sed (macOS)
sed -i'' -e "s|ARGO_WORKFLOWS_BUCKET_ARN|$ARGO_WORKFLOWS_BUCKET_ARN|g" "$karpenter_file"
sed -i'' -e "s|ARGO_WORKFLOWS_BUCKET_NAME|$ARGO_WORKFLOWS_BUCKET_NAME|g" "$karpenter_file"
sed -i'' -e "s|ARGO_WORKFLOWS_IRSA|$ARGO_WORKFLOWS_IRSA|g" "$karpenter_file"
sed -i'' -e "s|CLUSTER_ENDPOINT|$CLUSTER_ENDPOINT|g" "$karpenter_file"
sed -i'' -e "s|KARPENTER_INSTANCE_PROFILE|$KARPENTER_INSTANCE_PROFILE|g" "$karpenter_file"
sed -i'' -e "s|KARPENTER_IRSA|$KARPENTER_IRSA|g" "$karpenter_file"
sed -i'' -e "s|AWS_REGION|$AWS_REGION|g" "$karpenter_file"
sed -i'' -e "s|ARGO_WORKFLOWS_BUCKET_ARN|$ARGO_WORKFLOWS_BUCKET_ARN|g" "$argo_workflows_file"
sed -i'' -e "s|ARGO_WORKFLOWS_BUCKET_NAME|$ARGO_WORKFLOWS_BUCKET_NAME|g" "$argo_workflows_file"
sed -i'' -e "s|ARGO_WORKFLOWS_IRSA|$ARGO_WORKFLOWS_IRSA|g" "$argo_workflows_file"
sed -i'' -e "s|CLUSTER_ENDPOINT|$CLUSTER_ENDPOINT|g" "$argo_workflows_file"
sed -i'' -e "s|KARPENTER_INSTANCE_PROFILE|$KARPENTER_INSTANCE_PROFILE|g" "$argo_workflows_file"
sed -i'' -e "s|KARPENTER_IRSA|$KARPENTER_IRSA|g" "$argo_workflows_file"
sed -i'' -e "s|AWS_REGION|$AWS_REGION|g" "$argo_workflows_file"
sed -i'' -e "s|AWS_VPC_ID|$AWS_VPC_ID|g" "$upgrades_workflow_file"
sed -i'' -e "s|REGION_AWS|$AWS_REGION|g" "$upgrades_workflow_file"
sed -i'' -e "s|AWS_CLUSTER_IAM_ROLE_NAME|$CLUSTER_IAM_ROLE_NAME|g" "$upgrades_workflow_file"
sed -i'' -e "s|CLUSTER_SECURITY_GROUP_ID|$CLUSTER_SECURITY_GROUP_ID|g" "$upgrades_workflow_file"
# Applying deprecated manifests
kubectl apply -f ../../gitops/applications/deprecated-manifests
echo
echo "Now proceed to flux module"
else
echo "You need to create your fork and personal access token before proceeding."
fi