-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_script.sh
172 lines (147 loc) · 5.38 KB
/
final_script.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
#!/bin/bash
# Default values for parameters
AWS_REGION="us-east-1"
IMAGE_TAG="latest"
CLUSTER_NAME="cherubgyre-dev"
TASK_DEFINITION_FAMILY="cherubgyre-dev"
SUBNETS='["subnet-0f52ad3011c093e0c"]'
SECURITY_GROUP="sg-0001d3ac435b86000"
# Parse command-line arguments
while getopts "d:u:" opt; do
case "$opt" in
d) ACTION="deploy"; REPOSITORY_NAME="$OPTARG";;
u) ACTION="undeploy"; REPOSITORY_NAME="$OPTARG";;
*) echo "Usage: $0 -d <repository_name> or -u <repository_name>"; exit 1;;
esac
done
# Check if a repository name was passed
if [ -z "$REPOSITORY_NAME" ]; then
echo "Repository name is required!"
exit 1
fi
# Define functions for deployment and undeployment
deploy() {
echo "Checking if repository exists..."
EXISTING_REPO=$(aws ecr describe-repositories --repository-names $REPOSITORY_NAME --region $AWS_REGION --query 'repositories[0].repositoryUri' --output text)
if [ "$EXISTING_REPO" == "None" ]; then
echo "Repository does not exist. Creating repository..."
REPOSITORY_URI=$(aws ecr create-repository --repository-name $REPOSITORY_NAME --region $AWS_REGION --query 'repository.repositoryUri' --output text)
else
echo "Repository already exists: $EXISTING_REPO"
REPOSITORY_URI=$EXISTING_REPO
fi
echo "Repository URI: $REPOSITORY_URI"
# Authenticate Docker with ECR
echo "Authenticating Docker with ECR..."
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $REPOSITORY_URI
# Clone the repository
echo "Cloning the repository..."
git clone https://github.com/davidemerson/cherubgyre
cd cherubgyre
# Build Docker Image
echo "Building Docker image..."
docker build -t $REPOSITORY_NAME .
# Tag Docker Image
echo "Tagging Docker image..."
docker tag $REPOSITORY_NAME:$IMAGE_TAG $REPOSITORY_URI:$IMAGE_TAG
# Push Docker Image to ECR
echo "Pushing Docker image to ECR..."
docker push $REPOSITORY_URI:$IMAGE_TAG
# Go back to initial directory
cd ..
# Create ECS Cluster
echo "Creating ECS cluster..."
aws ecs create-cluster --cluster-name $CLUSTER_NAME --region $AWS_REGION
# Register Task Definition
echo "Registering ECS Task Definition..."
cat <<EOF > task-definition.json
{
"containerDefinitions": [
{
"name": "$REPOSITORY_NAME",
"image": "$REPOSITORY_URI:$IMAGE_TAG",
"cpu": 0,
"portMappings": [
{
"name": "rust-api-container-8080-tcp",
"containerPort": 8080,
"hostPort": 8080,
"protocol": "tcp",
"appProtocol": "http"
}
],
"essential": true,
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/$CLUSTER_NAME",
"awslogs-region": "$AWS_REGION",
"awslogs-create-group": "true",
"awslogs-stream-prefix": "ecs"
}
}
}
],
"family": "$TASK_DEFINITION_FAMILY",
"executionRoleArn": "arn:aws:iam::545009831939:role/ecsTaskExecutionRole",
"networkMode": "awsvpc",
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "512",
"memory": "1024",
"runtimePlatform": {
"cpuArchitecture": "X86_64",
"operatingSystemFamily": "LINUX"
}
}
EOF
aws ecs register-task-definition --cli-input-json file://task-definition.json
# Run Task
echo "Running ECS Task..."
TASK_ARN=$(aws ecs run-task \
--cluster $CLUSTER_NAME \
--launch-type FARGATE \
--task-definition $TASK_DEFINITION_FAMILY \
--network-configuration "awsvpcConfiguration={subnets=$SUBNETS,securityGroups=[$SECURITY_GROUP],assignPublicIp='ENABLED'}" \
--query 'tasks[0].taskArn' --output text)
echo "Task ARN: $TASK_ARN"
# Wait for Task to Start
echo "Waiting for task to start..."
aws ecs wait tasks-running --cluster $CLUSTER_NAME --tasks $TASK_ARN
# Get Task Details
TASK_DETAILS=$(aws ecs describe-tasks --cluster $CLUSTER_NAME --tasks $TASK_ARN --query 'tasks[0].attachments[0].details')
PUBLIC_IP=$(echo $TASK_DETAILS | jq -r '.[] | select(.name=="networkInterfaceId") | .value' | xargs -I{} aws ec2 describe-network-interfaces --network-interface-ids {} --query 'NetworkInterfaces[0].Association.PublicIp' --output text)
echo "API is now running at http://$PUBLIC_IP:8080"
}
undeploy() {
echo "Stopping and deleting ECS task..."
TASK_ARN=$(aws ecs list-tasks --cluster $CLUSTER_NAME --query 'taskArns[0]' --output text)
if [ "$TASK_ARN" != "None" ]; then
aws ecs stop-task --cluster $CLUSTER_NAME --task $TASK_ARN
echo "Task stopped."
else
echo "No active task found to stop."
fi
echo "Deregistering ECS Task Definition..."
# Retrieve the task definition with the revision number
TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition $TASK_DEFINITION_FAMILY --query 'taskDefinition.taskDefinitionArn' --output text)
if [ "$TASK_DEFINITION" != "None" ]; then
aws ecs deregister-task-definition --task-definition $TASK_DEFINITION
echo "Task definition deregistered."
else
echo "No task definition found to deregister."
fi
echo "Deleting ECS Cluster..."
aws ecs delete-cluster --cluster $CLUSTER_NAME --region $AWS_REGION
echo "Undeployment completed."
}
# Execute based on action
if [ "$ACTION" == "deploy" ]; then
deploy
elif [ "$ACTION" == "undeploy" ]; then
undeploy
else
echo "Invalid action. Use -d for deployment or -ud for undeployment."
exit 1
fi