Skip to content

Commit e38bb63

Browse files
committed
Merge branch 'Release'
2 parents f3c8a93 + f2f1092 commit e38bb63

File tree

8 files changed

+94
-210
lines changed

8 files changed

+94
-210
lines changed

.github/workflows/main.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Dockerizing and Deployment
2+
on:
3+
push:
4+
branches:
5+
- "release"
6+
jobs:
7+
Docker:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Set up QEMU
11+
uses: docker/setup-qemu-action@v1
12+
- name: Set up Docker Buildx
13+
uses: docker/setup-buildx-action@v1
14+
- name: Login to DockerHub
15+
uses: docker/login-action@v1
16+
with:
17+
username: ${{ secrets.DOCKERHUB_USERNAME }}
18+
password: ${{ secrets.DOCKERHUB_TOKEN }}
19+
- name: Build and push
20+
uses: docker/build-push-action@v2
21+
with:
22+
push: true
23+
tags: devenes/boto3-api:${{github.run_number}}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
__pycache__/
22
env/
3+
logs/
34
app.log

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM python:3.8-slim-buster
2+
WORKDIR /app
3+
ADD requirements.txt .
4+
RUN pip3 install -r requirements.txt
5+
COPY . .
6+
RUN mkdir logs \
7+
&& touch ./logs/app.log \
8+
&& chmod 777 ./logs/app.log
9+
EXPOSE 8080
10+
CMD ["python", "./app.py"]

app.py

Lines changed: 58 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -11,97 +11,86 @@
1111
)
1212

1313

14+
def get_aws_client(request):
15+
aws_access_key_id = request.args.get("aws_access_key_id")
16+
aws_secret_access_key = request.args.get("aws_secret_access_key")
17+
region_name = request.args.get("region_name")
18+
return boto3.client('ec2',
19+
aws_access_key_id=aws_access_key_id,
20+
aws_secret_access_key=aws_secret_access_key,
21+
region_name=region_name
22+
)
23+
24+
1425
# Endpoint: http://<api_host>:<api_port>/ec2/list
15-
@app.route('/ec2/list', methods=['POST', 'GET'])
26+
@ app.route('/ec2/list', methods=['GET'])
1627
def aws_list():
1728
try:
18-
aws_access_key_id = request.args.get("aws_access_key_id")
19-
aws_secret_access_key = request.args.get("aws_secret_access_key")
20-
region_name = request.args.get("region_name")
21-
client = boto3.client('ec2',
22-
aws_access_key_id=aws_access_key_id,
23-
aws_secret_access_key=aws_secret_access_key,
24-
region_name=region_name
25-
)
29+
client = get_aws_client(request)
30+
31+
instances = client.describe_instances()
32+
output = []
33+
for reservation in instances['Reservations']:
34+
for instance in reservation['Instances']:
35+
output.append(
36+
{
37+
'id': instance['InstanceId'],
38+
"instance-type": instance['InstanceType'],
39+
"instance-state": instance['State']['Name'],
40+
"private-ip": instance['PrivateIpAddress'],
41+
"key-name": instance['KeyName'],
42+
"image-id": instance['ImageId'],
43+
"vpc-id": instance['VpcId'],
44+
"subnet-id": instance['SubnetId'],
45+
"security-group-ids": instance['SecurityGroups'],
46+
}
47+
)
48+
49+
print(output)
50+
return jsonify(output), 200
2651

2752
except Exception as error:
2853
print(str(error))
29-
return jsonify({'Error': "Unexpected Error occured"}), 500
30-
31-
instances = client.describe_instances()
32-
output = []
33-
for reservation in instances['Reservations']:
34-
output.extend([
35-
{
36-
'id': instance['InstanceId'],
37-
"instance-type": instance['InstanceType'],
38-
"instance-state": instance['State']['Name']
39-
}
40-
for instance in reservation['Instances']])
41-
42-
# print(output)
43-
return jsonify(output), 200
54+
return jsonify({'Message': "Unexpected Error occured", 'Error': str(error)}), 500
4455

4556

4657
# Endpoint: http://<api_host>:<api_port>/ec2/start
47-
@app.route("/ec2/start", methods=["POST", "GET"])
58+
@app.route("/ec2/start", methods=["POST"])
4859
def start_ec2_instances():
4960
try:
50-
aws_access_key_id = request.args.get("aws_access_key_id")
51-
aws_secret_access_key = request.args.get("aws_secret_access_key")
52-
region_name = request.args.get("region_name")
53-
InstanceIds = request.args.get("InstanceId")
54-
client = boto3.client('ec2',
55-
aws_access_key_id=aws_access_key_id,
56-
aws_secret_access_key=aws_secret_access_key,
57-
region_name=region_name,
58-
)
59-
60-
except Exception as error:
61-
print(str(error))
62-
return jsonify({'Error': "Unexpected Error occured"}), 500
61+
client = get_aws_client(request)
62+
InstanceId = request.args.get("InstanceId")
6363

64-
response = client.describe_instances()
65-
InstanceIds = []
66-
for instance in response["Reservations"][0]["Instances"]:
67-
InstanceIds.append(instance["InstanceId"])
64+
response = client.start_instances(
65+
InstanceIds=[InstanceId]
66+
)
6867

69-
response = client.start_instances(
70-
InstanceIds=InstanceIds
71-
)
68+
return jsonify(response["StartingInstances"][0]), 200
7269

73-
return jsonify(response["StartingInstances"][0]), 200
70+
except Exception as error:
71+
print(str(error))
72+
return jsonify({'Message': "Unexpected Error occured", 'Error': str(error)}), 500
7473

7574

7675
# Endpoint: http://<api_host>:<api_port>/ec2/stop
77-
@app.route("/ec2/stop", methods=["POST", "GET"])
76+
@app.route("/ec2/stop", methods=["POST"])
7877
def stop_ec2_instances():
7978
try:
80-
aws_access_key_id = request.args.get("aws_access_key_id")
81-
aws_secret_access_key = request.args.get("aws_secret_access_key")
82-
region_name = request.args.get("region_name")
83-
InstanceIds = request.args.get("InstanceId")
84-
client = boto3.client('ec2',
85-
aws_access_key_id=aws_access_key_id,
86-
aws_secret_access_key=aws_secret_access_key,
87-
region_name=region_name,
88-
)
79+
client = get_aws_client(request)
80+
InstanceId = request.args.get("InstanceId")
8981

90-
except Exception as error:
91-
print(str(error))
92-
return jsonify({'Error': "Unexpected Error occured"}), 500
82+
response = client.stop_instances(
83+
InstanceIds=[InstanceId]
84+
)
9385

94-
response = client.describe_instances()
95-
InstanceIds = []
96-
for instance in response["Reservations"][0]["Instances"]:
97-
InstanceIds.append(instance["InstanceId"])
86+
return jsonify(response["StoppingInstances"][0]), 200
9887

99-
response = client.stop_instances(
100-
InstanceIds=InstanceIds
101-
)
102-
103-
return jsonify(response["StoppingInstances"][0]), 200
88+
except Exception as error:
89+
print(str(error))
90+
return jsonify({'Message': "Unexpected Error occured", 'Error': str(error)}), 500
10491

10592

10693
if __name__ == "__main__":
107-
app.run(host=config["host"], port=config["port"], debug=True)
94+
app.run(host=config["host"],
95+
port=config["port"],
96+
debug=False)

config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
config = {
22
"host": "0.0.0.0",
33
"port": 8080,
4-
"log_file": "app.log",
5-
"log_level": "INFO"
4+
"log_file": "logs/app.log",
5+
"log_level": "DEBUG",
66
}

list.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

start.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

stop.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)