-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
158 lines (142 loc) · 5.54 KB
/
run.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
#!/bin/bash
gather_parameters_deploy() {
read -p "Deployment identifier [posit-sce]: " stack_name
stack_name=${stack_name:-posit-sce}
echo "Please provide the IAM Role that is assigned to the current authenticated user."
echo "This role will be granted Admin rights on the EKS cluster for the setup."
read -p "ARN: " role
export CURRENT_ROLE_ARN=$role
export ssl=false
export domain=false
echo "Do you want to use a custom domain name?"
if read_yes_no; then
read -p "What is the FQDN: " domain
export domain=$domain
echo "POSIT will be setup for usage with $domain"
echo "Please follow the documentation in the README file to configure your own domain"
else
echo "Do you want to enable HTTPS with a generated SSL cert?"
if read_yes_no; then
export ssl=true
fi
fi
}
gather_parameters_destroy() {
export CURRENT_ROLE_ARN="Not Applicable"
read -p "Deployment identifier [posit-sce]: " stack_name
stack_name=${stack_name:-posit-sce}
}
read_yes_no() {
local response
while true; do
read -p "Please enter y/n: " response
case "$response" in
[Yy]) return 0 ;;
[Nn]) return 1 ;;
*) echo "Invalid input. Please enter y or n." ;;
esac
done
}
welcome() {
clear
echo " ____ ____ _____ __________ ___ _ _______ ________ _______ "
echo " / __ \/ __ \/ ___// _/_ __/ ____ ____ / | | / / ___/ / ____/ //_/ ___/ "
echo " / /_/ / / / /\__ \ / / / / / __ \/ __ \ / /| | | /| / /\__ \ / __/ / ,< \__ \ "
echo " / ____/ /_/ /___/ // / / / / /_/ / / / / / ___ | |/ |/ /___/ / / /___/ /| |___/ / "
echo "/_/ \____//____/___/ /_/ \____/_/ /_/ /_/ |_|__/|__//____/ /_____/_/ |_/____/ "
echo " "
echo "Welcome to the POSIT on AWS EKS installer!"
echo "Please follow the guided setup which will deploy the POSIT suite into AWS EKS."
echo "References to any details can be found on the blogpost:"
echo "https://blog.amazonaws.com/hcls/posit-on-aws-eks"
echo " "
}
check_command() {
local command_to_check="$1"
if ! command -v "$command_to_check" &> /dev/null
then
echo "Error: $command_to_check is not installed." >&2
exit 1
fi
}
check_aws_authentication() {
# Try to get the AWS identity information
aws_identity=$(aws sts get-caller-identity --output text --query 'Account' 2>&1)
current_region=$(aws ec2 describe-availability-zones --output text --query 'AvailabilityZones[0].[RegionName]')
if [[ $? -eq 0 ]]; then
echo "AWS CLI is authenticated."
echo "Admin Role: $CURRENT_ROLE_ARN"
echo "Account Number: $aws_identity"
echo "The selected AWS Region is: $current_region"
else
echo "Error: AWS CLI is not authenticated or AWS STS service is not reachable." >&2
echo "Details: $aws_identity"
exit 1
fi
# Ask for user confirmation to proceed
read -p "Do you want to proceed? (y/n) " -n 1 -r
echo # Move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo "Exiting...."
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle both script and sourcing
fi
export AWS_REGION=$current_region
}
if [ $# -ne 1 ]; then
echo "Usage: $0 [deploy|destroy]"
exit 1
fi
if [ "$1" != "deploy" ] && [ "$1" != "destroy" ]; then
echo "Invalid action. Usage: $0 [deploy|destroy]"
exit 1
fi
# Set the action
action=$1
welcome
check_command "xargs"
check_command "kubectl"
check_command "aws"
check_command "helm"
check_command "envsubst"
check_command "jq"
check_command "cdk"
# Gather parameter information based on the action
if [ "$action" == "deploy" ]; then
echo "You have chosen to deploy POSIT on AWS EKS."
echo "This script will run through the following tasks to deploy the solution:"
echo "1. Deploy all infrastructure using infrastructure as code with the AWS CDK"
echo "2. Prep POSIT and AWS EKS install"
echo "3. Configure POSIT and networking so its ready for use"
echo " "
gather_parameters_deploy
check_aws_authentication
echo " "
#Deploy the CDK stack
#Ensure the region is bootstrapped
echo "[ ] 1.1 Bootstrapping AWS CDK... [Takes up to 3 min]"
cdk bootstrap >> /dev/null
echo "[ ] 1.2 Deploying infrastructure using AWS CDK... [Takes up to 30 min]"
cdk deploy --require-approval never --context clusterName=$stack_name --context stackName=$stack_name
echo "[ ] 2. Configuring POSIT and networking... [Takes up to 1 min]"
source ./scripts/post-cdk-hook.sh $stack_name
echo "[ ] 3. Deploying POSIT containers adn configuration [Takes up to 5 min]"
source ./scripts/posit-install.sh
echo "[ ] 4. Configure domain / SSL if needed [Takes up to 1 min]"
if $ssl; then
source ./scripts/cert-install.sh
fi
elif [ "$action" == "destroy" ]; then
echo "You have chosen to destroy your current installation of POSIT on AWS EKS."
gather_parameters_destroy
check_aws_authentication
#Empty the kube resources.
echo "[ ] 1. Cleaning out all EKS deployments... [Takes up to 1 min]"
source ./scripts/kube-reset.sh
echo "Please confirm you want to destroy the infrastructure, this is irreversible"
if read_yes_no; then
#Delete the CDK stack
echo "[ ] 2. Destroying infrastructure using AWS CDK... [Takes up to 30 min]"
cdk destroy --force --context stackName=$stack_name
fi
fi