-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject2.md
More file actions
119 lines (103 loc) · 3.24 KB
/
project2.md
File metadata and controls
119 lines (103 loc) · 3.24 KB
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
#!/bin/bash
# Function: Clone Django app
code_clone() {
echo "Cloning the Django app..."
git clone https://github.com/LondheShubham153/django-notes-app.git
}
# Function: Install dependencies
install_requirements() {
echo "Installing dependencies..."
sudo apt-get update -y
sudo apt-get install docker.io nginx -y
}
# Function: Build docker image
build_docker() {
echo "Building docker image..."
docker build -t django-notes-app:latest ./django-notes-app
}
# Function: Run docker container
run_docker() {
echo "Running docker container..."
docker run -d -p 8000:8000 --name notes-app-container django-notes-app:latest
}
# Function: Push docker image to Docker Hub
push_docker() {
echo "Pushing docker image to DockerHub..."
docker tag django-notes-app:latest <your-dockerhub-username>/django-notes-app:latest
docker push <your-dockerhub-username>/django-notes-app:latest
}
# Function: Check AWS CLI
check_awscli() {
if ! command -v aws &>/dev/null; then
echo "AWS CLI is not installed. Please install it first."
exit 1
fi
}
# Function: Create EC2 instance
create_instance() {
echo "Creating EC2 instance..."
INSTANCE_ID=$(aws ec2 run-instances \
--image-id ami-053b0d53c279acc90 \
--count 1 \
--instance-type t2.micro \
--key-name <your-key-pair> \
--security-group-ids <your-sg-id> \
--subnet-id <your-subnet-id> \
--query 'Instances[0].InstanceId' \
--output text)
if [ -z "$INSTANCE_ID" ]; then
echo "Failed to create EC2 instance."
exit 1
else
echo "Instance $INSTANCE_ID created successfully."
fi
}
# Function: Wait until instance is running
wait_for_instance() {
echo "Waiting for instance $INSTANCE_ID to be in running state..."
aws ec2 wait instance-running --instance-ids $INSTANCE_ID
echo "Instance $INSTANCE_ID is now running."
}
# Function: Deploy Dockerized app to EC2
deploy_app() {
echo "Deploying app to EC2 instance..."
PUBLIC_IP=$(aws ec2 describe-instances \
--instance-ids $INSTANCE_ID \
--query "Reservations[0].Instances[0].PublicIpAddress" \
--output text)
ssh -o StrictHostKeyChecking=no -i "<your-key.pem>" ubuntu@$PUBLIC_IP << EOF
sudo apt-get update -y
sudo apt-get install docker.io -y
docker pull <your-dockerhub-username>/django-notes-app:latest
docker run -d -p 8000:8000 <your-dockerhub-username>/django-notes-app:latest
EOF
echo "App deployed successfully at http://$PUBLIC_IP:8000"
}
# Main Menu
menu() {
echo "Select an option:"
echo "1. Clone Code"
echo "2. Install Requirements"
echo "3. Build Docker Image"
echo "4. Run Docker Container"
echo "5. Push Docker Image to DockerHub"
echo "6. Create EC2 Instance"
echo "7. Deploy App to EC2"
echo "8. Exit"
read -p "Enter choice: " choice
case $choice in
1) code_clone ;;
2) install_requirements ;;
3) build_docker ;;
4) run_docker ;;
5) push_docker ;;
6) check_awscli; create_instance; wait_for_instance ;;
7) deploy_app ;;
8) exit 0 ;;
*) echo "Invalid choice. Try again." ;;
esac
}
# Infinite loop for menu
while true; do
menu
done