-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudcontrol.py
More file actions
140 lines (127 loc) · 5.26 KB
/
cloudcontrol.py
File metadata and controls
140 lines (127 loc) · 5.26 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from flask import Flask, render_template, json, request, Response
from flask_caching import Cache
from subprocess import Popen
import os
import boto3
import config
app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
#
# The Home Page
#
# This function simply passes on the appropriate data
# in the form of a dicionary, to index.html jinja2 template
#
@app.route('/', methods=['GET', 'POST'])
def index():
# The data dictionary that will
# be passed to the jinja2 template
render_data={}
# Pass on the configurations that are needed by the template
render_data['dep_order'] = config.APP_DEPENDENCY_ORDER
render_data['data_refresh'] = config.DATA_REFRESH
render_data['banner'] = config.BANNER
# If some thing was posted,
if request.method == 'POST':
# First of all make sure there are no locks
# Add the alert in the render_data if a lock is there
if os.path.isfile( config.ENV_START_LOCK ):
render_data['alert'] = 'Please wait, the environment is currently booting up'
elif os.path.isfile( config.ENV_STOP_LOCK ):
render_data['alert'] = 'Please wait, the environment is currently shutting down'
else:
if 'startins' in request.form:
ec2_res = boto3.Session(
aws_access_key_id = config.AWS_ACCESS_KEY_ID,
aws_secret_access_key = config.AWS_SECRET_ACCESS_KEY,
region_name = config.AWS_REGION
).resource( 'ec2' ).Instance(id=request.form['startins']).start()
render_data['info'] = 'Starting Instance: ' + request.form['insname'] + '(' + request.form['startins'] + ')'
elif 'stopins' in request.form:
ec2_res = boto3.Session(
aws_access_key_id = config.AWS_ACCESS_KEY_ID,
aws_secret_access_key = config.AWS_SECRET_ACCESS_KEY,
region_name = config.AWS_REGION
).resource( 'ec2' ).Instance(id=request.form['stopins']).stop(Force=False)
render_data['info'] = 'Stoping Instance: ' + request.form['insname'] + '(' + request.form['stopins'] + ')'
# If startenv was posted,
# Call the script with start argument
# And pass the success notification in render_data
elif 'startenv' in request.form and 'env' in request.form:
Popen( [ 'nohup', config.ENV_START_STOP, 'start', request.form['env'] ],
preexec_fn=os.setpgrp )
render_data['info'] = 'Starting the environment: ' + request.form['env']
# If stopenv was posted
# Call the script with stop argument
# And pass the success notification in render_data
elif 'stopenv' in request.form and 'env' in request.form:
Popen( [ 'nohup', config.ENV_START_STOP, 'stop', request.form['env'] ],
preexec_fn=os.setpgrp )
render_data['info'] = 'Stoping the environment: ' + request.form['env']
# Some thing unknow was posted,
# Pass the Invalid Request alert in render_data
else:
render_data['alert'] = 'Invalid Request'
# Pass render_data to the jinja2 template
return render_template('index.html', data=render_data)
#
# The Instances View
#
# We will cache the result of this view for 5 seconds
# This function will be called directly and frequently
# by bootstrap-tables hence caching it is crucial.
# The volumes function inside this view is cached for
# even greater time, as the volume info rarely changes
# and is very time consuming to calculate on each call
#
@app.route("/instances", methods=['GET'])
@cache.cached( timeout = config.INSTANCES_CACHE )
def instances():
# ec2 service resource
ec2_res = boto3.Session(
aws_access_key_id = config.AWS_ACCESS_KEY_ID,
aws_secret_access_key = config.AWS_SECRET_ACCESS_KEY,
region_name = config.AWS_REGION
).resource( 'ec2' )
# filter the instances
ec2 = ec2_res.instances.filter( Filters=config.INSTANCE_FILTER )
# volumes is a separate funtion that will return the ebs
# volumes attached to each instance.
# This was made a separate function so it can be cached separately
@cache.cached( timeout = config.VOLUMES_CACHE, key_prefix='volumes')
def volumes():
return { i.id : [ str(ec2_res.Volume( id=bdm['Ebs']['VolumeId']).size)+'GB ' for bdm in i.block_device_mappings ] for i in ec2.all() }
vols = volumes()
# We iterate over all the instances and collect the data
# in instances array and finally output it in json
instances = []
for i in ec2.all():
iid = i.id
istate = i.state['Name']
itype = i.instance_type
iaz = i.placement['AvailabilityZone']
iname = ( [ it['Value'] for it in i.tags if it['Key'] == 'Name' ] or [''] )[0]
ienv = ( [ it['Value'] for it in i.tags if it['Key'] == 'Env' ] or [''] )[0]
ipvip = i.private_ip_address
ivols = vols[ iid ]
instances.append ( { 'name': iname, 'id': iid, 'az': iaz, 'env': ienv, 'type': itype, 'vols': ivols, 'privip': ipvip, 'state': istate } )
return Response( json.dumps(instances), mimetype='application/json' )
#
# The Environment Status view
#
# This view will simply return the content of the status file
# present in the application root. This status file is suppose
# be updated by the script that starts/stops the environment.
# Bootstrap will call this view frequently to show the status on the homepage
#
@app.route("/envstatus", methods=['GET'])
def envstatus():
content = ''
with open(config.ENV_STATUS_OUT, "r") as f:
#for line in f:
# content += line
# content += ('<br />')
content = f.read().replace( '\n', '<br />' )
return(content)
if __name__ == "__main__":
app.run( host="0.0.0.0", debug=True)