Skip to content

Commit 41b3ca8

Browse files
author
neeleshdodda44
committed
implement remove repo
1 parent f58b0c9 commit 41b3ca8

File tree

2 files changed

+120
-2
lines changed

2 files changed

+120
-2
lines changed

app.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@
1111
from boto3.dynamodb.conditions import Key
1212
from flask import Flask, request, jsonify
1313

14-
from deploy import run_deploy_routine
14+
from deploy import run_deploy_routine, run_delete_routine
1515

1616

1717
JWT_SECRET = "datajbsnmd5h84rbewvzx6*cax^jgmqw@m3$ds_%z-4*qy0n44fjr5shark"
1818
JWT_ALGO = "HS256"
1919

20+
APPLICATION_NAME = "cloud-node"
2021
app = Flask(__name__)
2122
CORS(app)
2223

24+
@app.route("/")
25+
def home():
26+
return "This is the dashboard api homepage!"
27+
2328
@app.route("/userdata", methods=["GET"])
2429
def get_user_data():
2530
"""
@@ -102,6 +107,36 @@ def create_new_repo():
102107
}
103108
})
104109

110+
@app.route("/delete/<repo_id>", methods=["POST"])
111+
def delete_new_repo(repo_id):
112+
"""
113+
Deletes a repo under the authenticated user.
114+
115+
Example HTTP POST Request body (JSON format):
116+
{
117+
"RepoID": "repo_id",
118+
}
119+
"""
120+
# Check authorization
121+
claims = authorize_user(request)
122+
if claims is None: return jsonify(make_unauthorized_error()), 400
123+
124+
user_id = claims["pk"]
125+
repo_name = re.sub('[^a-zA-Z0-9-]', '-', repo_name)
126+
try:
127+
run_delete_routine(repo_id)
128+
except Exception as e:
129+
# TODO: Revert things.
130+
return jsonify(make_error(str(e))), 400
131+
132+
return jsonify({
133+
"Error": False,
134+
"Results": {
135+
"RepoId": repo_id,
136+
"TrueApiKey": true_api_key
137+
}
138+
})
139+
105140
@app.route("/repos", methods=["GET"])
106141
def get_all_repos():
107142
"""
@@ -399,6 +434,28 @@ def _create_new_cloud_node(repo_id, api_key):
399434
except Exception as e:
400435
raise Exception("Error while creating new cloud node.")
401436

437+
def _delete_cloud_node(repo_id):
438+
"""
439+
Deletes cloud node
440+
"""
441+
try:
442+
client = boto3.client('elasticbeanstalk')
443+
except ClientError as err:
444+
print("Failed to create boto3 client.\n" + str(err))
445+
return False
446+
447+
try:
448+
response = client.delete_environment_configuration(
449+
ApplicationName=APPLICATION_NAME,
450+
EnvironmentName=repo_id
451+
)
452+
except ClientError as err:
453+
print("Failed to delete environment.\n" + str(err))
454+
return False
455+
456+
print(response)
457+
458+
402459
def _create_presigned_url(bucket_name, object_name, expiration=3600):
403460
"""Generate a presigned URL to share an S3 object
404461

deploy.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ def create_new_version():
9292
'S3Bucket': S3_BUCKET,
9393
'S3Key': BUCKET_KEY
9494
},
95-
Process=True
95+
Process=True,
96+
Tags=[
97+
{
98+
'Key': 'type',
99+
'Value': 'cloud'
100+
},
101+
]
96102
)
97103
except ClientError as err:
98104
print("Failed to create application version.\n" + str(err))
@@ -236,3 +242,58 @@ def run_deploy_routine(repo_id):
236242
# _ = zip_server_directory()
237243
_ = deploy_cloud_node(repo_id)
238244
_ = add_codepipeline_deploy_step(repo_id)
245+
246+
def _delete_cloud_node(repo_id):
247+
"""
248+
Deletes cloud node
249+
"""
250+
try:
251+
client = boto3.client('elasticbeanstalk')
252+
except ClientError as err:
253+
print("Failed to create boto3 client.\n" + str(err))
254+
return False
255+
256+
try:
257+
response = client.terminate_environment(
258+
EnvironmentName=repo_id,
259+
TerminateResources=True,
260+
)
261+
except ClientError as err:
262+
print("Failed to delete environment.\n" + str(err))
263+
return False
264+
265+
print(response)
266+
267+
def _remove_codepipeline_deploy_step(env_name):
268+
"""
269+
Removes Cloud Node from the AWS CodePipeline pipeline.
270+
"""
271+
try:
272+
target_name = DEPLOYMENT_NAME.format(env_name)
273+
client = boto3.client('codepipeline')
274+
pipeline_response = client.get_pipeline(
275+
name=PIPELINE_NAME,
276+
)
277+
278+
pipeline_data = pipeline_response['pipeline']
279+
for stage in pipeline_data['stages']:
280+
if stage['name'] == 'Deploy':
281+
stage['actions'] = [action for action in stage['actions'] \
282+
if action['name'] != target_name]
283+
_ = client.update_pipeline(
284+
pipeline=pipeline_data
285+
)
286+
print("Updated CodeDeploy pipeline.")
287+
except Exception as e:
288+
# Does not raise an exception since this is not crucial.
289+
# TODO: Log an error and alert developers because this could break things.
290+
print("Error: " + str(e))
291+
292+
return True
293+
294+
def run_delete_routine(repo_id):
295+
"""
296+
Runs delete cloud node routine
297+
"""
298+
_ = _delete_cloud_node(repo_id)
299+
_ = _remove_codepipeline_deploy_step(repo_id)

0 commit comments

Comments
 (0)