Skip to content

Commit c0cb58c

Browse files
DevdattaDevdatta
authored andcommitted
PaaS update: Added setup for Personal PaaS
1 parent 285ba08 commit c0cb58c

5 files changed

Lines changed: 202 additions & 0 deletions

File tree

‎PaaS/PersonalPaaS/myapp/Dockerfile‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM python:3.9-slim
2+
3+
WORKDIR /app
4+
5+
# Requirement 1: Copy requirements.txt
6+
7+
# Requirement 2: Install requirements.txt
8+
9+
# Copy code into the current working directory
10+
COPY . .
11+
12+
# Requirement 3: Add the CMD command

‎PaaS/PersonalPaaS/myapp/app.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)
4+
5+
@app.route("/")
6+
def hello():
7+
return "Hello from Personal PaaS!"
8+
9+
if __name__ == "__main__":
10+
app.run(host="0.0.0.0", port=5000)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flask

‎PaaS/PersonalPaaS/steps.txt‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
PersonalPaaS Detailed Steps
2+
3+
Assumptions used below:
4+
- AWS region: us-west-2
5+
- EC2 KeyPair name: assignment4
6+
- Private key file: assignment4.pem
7+
- Working directory: assignment5 (contains template.yaml, steps.txt, myapp/app.py, myapp/Dockerfile, myapp/requirements.txt)
8+
9+
1. Go to the assignment folder
10+
cd assignment5
11+
12+
2. Create EC2 VM for your personal PaaS
13+
aws cloudformation create-stack \
14+
--stack-name personal-paas \
15+
--template-body file://template.yaml \
16+
--parameters ParameterKey=KeyName,ParameterValue=assignment4 \
17+
--region us-west-2
18+
--capabilities CAPABILITY_NAMED_IAM
19+
20+
3. Wait for the Stack to complete
21+
aws cloudformation wait stack-create-complete \
22+
--stack-name personal-paas \
23+
--region us-west-2
24+
25+
4. Get the EC2 VM's public ip address
26+
EC2_IP=$(aws cloudformation describe-stacks \
27+
--stack-name personal-paas \
28+
--query "Stacks[0].Outputs[0].OutputValue" \
29+
--output text \
30+
--region us-west-2
31+
)
32+
echo "$EC2_IP"
33+
34+
5. Initialize the repo
35+
cd myapp
36+
git init -b main
37+
38+
# add files
39+
git add .
40+
git commit -m "initial commit"
41+
42+
6. Add the EC2 VM as a Git remote for your application
43+
git remote add paas ubuntu@"$EC2_IP":/home/ubuntu/app.git
44+
45+
7. Push to the remote to trigger deployment
46+
GIT_SSH_COMMAND='ssh -i "<Path to your assignment4.pem>" -o StrictHostKeyChecking=accept-new' \
47+
git push paas main
48+
49+
8. Verify deployment from browser or curl
50+
curl http://"$EC2_IP"
51+
# expected output: Hello from Personal PaaS!
52+
53+
9. Test update flow
54+
sed -i '' 's/Hello from Personal PaaS!/Hello from Personal PaaS v2!/' app.py
55+
git add app.py
56+
git commit -m "update greeting"
57+
GIT_SSH_COMMAND='ssh -i "<Path to your assignment4.pem>" -o StrictHostKeyChecking=accept-new' \
58+
git push paas main
59+
sleep 5
60+
curl http://"$EC2_IP"
61+
# expected output: Hello from Personal PaaS v2!
62+
63+
10. Optional debug commands (if deployment fails)
64+
ssh -i "<Path to your assignment4.pem>" ubuntu@"$EC2_IP" \
65+
"sudo docker ps -a; echo '---'; sudo tail -n 100 /var/log/paas-deploy.log"
66+
67+
11. Clean up AWS resources after testing
68+
aws cloudformation delete-stack \
69+
--stack-name personal-paas \
70+
--region us-west-2
71+
72+
aws cloudformation wait stack-delete-complete \
73+
--stack-name personal-paas \
74+
--region us-west-2

‎PaaS/PersonalPaaS/template.yaml‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Description: Personal PaaS VM (Git-push-to-deploy)
3+
4+
Parameters:
5+
KeyName:
6+
Type: AWS::EC2::KeyPair::KeyName
7+
Description: EC2 KeyPair for SSH access
8+
9+
Resources:
10+
PaaSEC2Instance:
11+
Type: AWS::EC2::Instance
12+
Properties:
13+
InstanceType: t2.micro
14+
KeyName: "" # Requirement 4: Inject the KeyName provided as a parameter
15+
ImageId: "" # Requirement 5: Use Ubuntu 24.04 image for us-west-2
16+
17+
SecurityGroupIds:
18+
- !Ref InstanceSecurityGroup
19+
20+
UserData:
21+
Fn::Base64: !Sub |
22+
#!/bin/bash
23+
set -e
24+
25+
apt-get update -y
26+
apt-get install -y docker.io git
27+
28+
systemctl start docker
29+
systemctl enable docker
30+
31+
# Directories for app + logs
32+
mkdir -p /home/ubuntu/app
33+
mkdir -p /home/ubuntu/logs
34+
chown -R ubuntu:ubuntu /home/ubuntu/app /home/ubuntu/logs
35+
36+
# Bare repo
37+
sudo -u ubuntu git init --bare /home/ubuntu/app.git
38+
sudo -u ubuntu git --git-dir=/home/ubuntu/app.git symbolic-ref HEAD refs/heads/main
39+
40+
# Create the deploy script
41+
cat << 'EOF' > /usr/local/bin/deploy.sh
42+
#!/bin/bash
43+
set -e
44+
45+
APP_DIR=/home/ubuntu/app
46+
REPO_DIR=/home/ubuntu/app.git
47+
LOG_FILE=/var/log/paas-deploy.log # Deployment logs
48+
APP_LOG_FILE=/home/ubuntu/logs/app.log # App logs
49+
50+
exec >> $LOG_FILE 2>&1 # Replace the current shell and capture all the log output lines
51+
52+
echo "==== Deploy started at $(date) ===="
53+
54+
rm -rf $APP_DIR
55+
mkdir -p $APP_DIR
56+
57+
git --work-tree=$APP_DIR --git-dir=$REPO_DIR checkout -f
58+
cd $APP_DIR
59+
60+
# Requirement 6: Build the docker container. Tag it myapp
61+
62+
docker stop myapp-container || true
63+
docker rm myapp-container || true
64+
65+
pkill -f "docker logs -f myapp-container" || true
66+
67+
# Requirement 7: Run the Docker container. Map the application port to port 80. Name the container myapp-container
68+
69+
mkdir -p /home/ubuntu/logs
70+
touch $APP_LOG_FILE
71+
chown -R ubuntu:ubuntu /home/ubuntu/logs
72+
73+
docker logs -f myapp-container >> $APP_LOG_FILE 2>&1 &
74+
75+
echo "==== Deploy finished ===="
76+
EOF
77+
78+
chmod +x /usr/local/bin/deploy.sh
79+
echo "ubuntu ALL=(ALL) NOPASSWD: /usr/local/bin/deploy.sh" > /etc/sudoers.d/deploy
80+
chmod 440 /etc/sudoers.d/deploy
81+
82+
# Git post-receive hook
83+
cat << 'EOF' > /home/ubuntu/app.git/hooks/post-receive
84+
#!/bin/bash
85+
sudo /usr/local/bin/deploy.sh
86+
EOF
87+
88+
chmod +x /home/ubuntu/app.git/hooks/post-receive
89+
chown -R ubuntu:ubuntu /home/ubuntu/app.git
90+
91+
InstanceSecurityGroup:
92+
Type: AWS::EC2::SecurityGroup
93+
Properties:
94+
GroupDescription: Allow SSH and HTTP
95+
SecurityGroupIngress:
96+
- IpProtocol: tcp
97+
FromPort: 22
98+
ToPort: 22
99+
CidrIp: 0.0.0.0/0
100+
101+
# Requirement 8: Add the security group for Port 80 to allow traffic from anywhere.
102+
103+
Outputs:
104+
PublicIP:
105+
Value: !GetAtt PaaSEC2Instance.PublicIp

0 commit comments

Comments
 (0)