forked from aws-samples/pcluster-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
184 lines (149 loc) · 4.67 KB
/
app.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
# with the License. A copy of the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and
# limitations under the License.
import datetime
from flask import Flask, Response, request, send_from_directory
from flask.json import JSONEncoder
from flask_cors import CORS # comment this on deployment
from flask_restful import Api
from werkzeug.routing import BaseConverter
import api.utils as utils
from api.PclusterApiHandler import (
PclusterApiHandler,
authenticate,
authenticated,
cancel_job,
create_user,
delete_user,
ec2_action,
get_aws_config,
get_cluster_config,
get_custom_image_config,
get_dcv_session,
get_identity,
get_version,
get_instance_types,
list_users,
login,
logout,
price_estimate,
queue_status,
sacct,
scontrol_job,
set_user_role,
submit_job,
)
class RegexConverter(BaseConverter):
def __init__(self, url_map, *items):
super(RegexConverter, self).__init__(url_map)
self.regex = items[0]
class PClusterJSONEncoder(JSONEncoder):
"""Make the model objects JSON serializable."""
include_nulls = False
def default(self, obj):
if isinstance(obj, datetime.date):
return utils.to_iso_timestr(obj)
return JSONEncoder.default(self, obj)
def run():
app = utils.build_flask_app(__name__)
app.json_encoder = PClusterJSONEncoder
app.url_map.converters["regex"] = RegexConverter
CORS(app) # comment this on deployment
api = Api(app)
@app.errorhandler(401)
def custom_401(_error):
return Response(
"You are not authorized to perform this action.", 401
)
@app.route("/", defaults={"path": ""})
@app.route('/<path:path>')
def serve(path):
return utils.serve_frontend(app, path)
@app.route("/manager/ec2_action", methods=["POST"])
@authenticated()
def ec2_action_():
return ec2_action()
@app.route("/manager/get_cluster_configuration")
@authenticated()
def get_cluster_config_():
return get_cluster_config()
@app.route("/manager/get_custom_image_configuration")
@authenticated()
def get_custom_image_config_():
return get_custom_image_config()
@app.route("/manager/get_aws_configuration")
@authenticated()
def get_aws_config_():
return get_aws_config()
@app.route("/manager/get_instance_types")
@authenticated()
def get_instance_types_():
return get_instance_types()
@app.route("/manager/get_dcv_session")
@authenticated()
def get_dcv_session_():
return get_dcv_session()
@app.route("/manager/get_identity")
@authenticated("guest")
def get_identity_():
return get_identity()
@app.route("/manager/get_version")
def get_version_():
return get_version()
@app.route("/manager/list_users")
@authenticated("admin")
def list_users_():
return list_users()
@app.route("/manager/create_user", methods=["POST"])
@authenticated("admin")
def create_user_():
return create_user()
@app.route("/manager/delete_user", methods=["DELETE"])
@authenticated("admin")
def delete_user_():
return delete_user()
@app.route("/manager/set_user_role", methods=["PUT"])
@authenticated("admin")
def set_user_role_():
return set_user_role()
@app.route("/manager/queue_status")
@authenticated()
def queue_status_():
return queue_status()
@app.route("/manager/cancel_job")
@authenticated()
def cancel_job_():
return cancel_job()
@app.route("/manager/price_estimate")
@authenticated()
def price_estimate_():
return price_estimate()
@app.route("/manager/submit_job", methods=["POST"])
@authenticated()
def submit_job_():
return submit_job()
@app.route("/manager/sacct", methods=["POST"])
@authenticated()
def sacct_():
return sacct()
@app.route("/manager/scontrol_job")
@authenticated()
def scontrol_job_():
return scontrol_job()
@app.route("/login")
def login_():
return login()
@app.route("/logout")
def logout_():
return logout()
api.add_resource(PclusterApiHandler, "/api")
return app
if __name__ == "__main__":
run()