forked from francescou/docker-compose-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
358 lines (306 loc) · 9.21 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"""
Docker Compose UI, flask based application
"""
from compose.service import ImageType
from json import loads
import logging
import os
import traceback
import docker
import requests
from flask import Flask, jsonify, request
from scripts.bridge import ps_, get_project, get_container_from_id, get_yml_path
from scripts.find_yml import find_yml_files
from scripts.requires_auth import requires_auth, authentication_enabled, \
disable_authentication, set_authentication
# Flask Application
API_V1 = '/api/v1/'
YML_PATH = '/opt/docker-compose-projects'
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__, static_url_path='')
# load project definitions
projects = find_yml_files(YML_PATH)
logging.debug(projects)
def get_project_with_name(name):
"""
get docker compose project given a project name
"""
path = projects[name]
return get_project(path)
# REST endpoints
@app.route(API_V1 + "projects", methods=['GET'])
def list_projects():
"""
List docker compose projects
"""
global projects
projects = find_yml_files(YML_PATH)
return jsonify(projects=projects)
@app.route(API_V1 + "projects/<name>", methods=['GET'])
def project_containers(name):
"""
get project details
"""
project = get_project_with_name(name)
containers = ps_(project)
return jsonify(containers=containers)
@app.route(API_V1 + "projects/<project>/<service_id>", methods=['POST'])
@requires_auth
def run_service(project, service_id):
"""
docker-compose run service
"""
json = loads(request.data)
service = get_project_with_name(project).get_service(service_id)
command = json["command"] if 'command' in json else service.options.get('command')
container = service \
.create_container(one_off=True, command=command)
container.start()
return jsonify(\
command='run %s/%s' % (project, service_id), \
name=container.name, \
id=container.id \
)
@app.route(API_V1 + "projects/yml/<name>", methods=['GET'])
def project_yml(name):
"""
get yml content
"""
path = get_yml_path(projects[name])
with open(path) as data_file:
return jsonify(yml=data_file.read())
@app.route(API_V1 + "projects/<name>/<container_id>", methods=['GET'])
def project_container(name, container_id):
"""
get container details
"""
project = get_project_with_name(name)
container = get_container_from_id(project.client, container_id)
return jsonify(
id=container.id,
short_id=container.short_id,
human_readable_command=container.human_readable_command,
name=container.name,
name_without_project=container.name_without_project,
number=container.number,
ports=container.ports,
ip=container.get('NetworkSettings.IPAddress'),
labels=container.labels,
log_config=container.log_config,
image=container.image,
environment=container.environment
)
@app.route(API_V1 + "projects/<name>", methods=['DELETE'])
@requires_auth
def kill(name):
"""
docker-compose kill
"""
get_project_with_name(name).kill()
return jsonify(command='kill')
@app.route(API_V1 + "projects", methods=['PUT'])
@requires_auth
def pull():
"""
docker-compose pull
"""
name = loads(request.data)["id"]
get_project_with_name(name).pull()
return jsonify(command='pull')
@app.route(API_V1 + "services", methods=['PUT'])
@requires_auth
def scale():
"""
docker-compose scale
"""
req = loads(request.data)
name = req['project']
service_name = req['service']
num = req['num']
project = get_project_with_name(name)
project.get_service(service_name).scale(desired_num=int(num))
return jsonify(command='scale')
@app.route(API_V1 + "projects", methods=['POST'])
@requires_auth
def up_():
"""
docker-compose up
"""
name = loads(request.data)["id"]
containers = get_project_with_name(name).up()
logging.debug(containers)
return jsonify(
{
'command': 'up',
'containers': map(lambda container: container.name, containers)
})
@app.route(API_V1 + "build", methods=['POST'])
@requires_auth
def build():
"""
docker-compose build
"""
json = loads(request.data)
name = json["id"]
dic = dict(no_cache=json["no_cache"] if "no_cache" in json \
else None, pull=json["pull"] if "pull" in json else None)
get_project_with_name(name).build(**dic)
return jsonify(command='build')
@app.route(API_V1 + "create", methods=['POST'])
@requires_auth
def create():
"""
create new project
"""
data = loads(request.data)
directory = YML_PATH + '/' + data["name"]
os.makedirs(directory)
file_path = directory + "/docker-compose.yml"
out_file = open(file_path, "w")
out_file.write(data["yml"])
out_file.close()
return jsonify(path=file_path)
@app.route(API_V1 + "search", methods=['POST'])
def search():
"""
search for a project on www.composeregistry.com
"""
query = loads(request.data)['query']
response = requests.get('https://www.composeregistry.com/api/v1/search', \
params={'query': query}, headers={'x-key': 'default'})
if response.status_code == 200:
return jsonify(response.json())
else:
result = jsonify(response.json())
result.status_code = response.status_code
return result
@app.route(API_V1 + "yml", methods=['POST'])
def yml():
"""
get yml content from www.composeregistry.com
"""
item_id = loads(request.data)['id']
response = requests.get('https://www.composeregistry.com/api/v1/yml', \
params={'id': item_id}, headers={'x-key': 'default'})
return jsonify(response.json())
@app.route(API_V1 + "start", methods=['POST'])
@requires_auth
def start():
"""
docker-compose start
"""
name = loads(request.data)["id"]
get_project_with_name(name).start()
return jsonify(command='start')
@app.route(API_V1 + "stop", methods=['POST'])
@requires_auth
def stop():
"""
docker-compose stop
"""
name = loads(request.data)["id"]
get_project_with_name(name).stop()
return jsonify(command='stop')
@app.route(API_V1 + "down", methods=['POST'])
@requires_auth
def down():
"""
docker-compose down
"""
name = loads(request.data)["id"]
get_project_with_name(name).down(ImageType.none, None)
return jsonify(command='down')
@app.route(API_V1 + "logs/<name>", defaults={'limit': "all"}, methods=['GET'])
@app.route(API_V1 + "logs/<name>/<int:limit>", methods=['GET'])
def logs(name, limit):
"""
docker-compose logs
"""
lines = {}
for k in get_project_with_name(name).containers(stopped=True):
lines[k.name] = k.logs(timestamps=True, tail=limit).split('\n')
return jsonify(logs=lines)
@app.route(API_V1 + "logs/<name>/<container_id>", defaults={'limit': "all"}, methods=['GET'])
@app.route(API_V1 + "logs/<name>/<container_id>/<int:limit>", methods=['GET'])
def container_logs(name, container_id, limit):
"""
docker-compose logs of a specific container
"""
project = get_project_with_name(name)
container = get_container_from_id(project.client, container_id)
lines = container.logs(timestamps=True, tail=limit).split('\n')
return jsonify(logs=lines)
@app.route(API_V1 + "host", methods=['GET'])
def host():
"""
docker host info
"""
host_value = os.getenv('DOCKER_HOST')
return jsonify(host=host_value)
@app.route(API_V1 + "host", methods=['POST'])
@requires_auth
def set_host():
"""
set docker host
"""
new_host = loads(request.data)["id"]
if new_host is None:
if os.environ.has_key('DOCKER_HOST'):
del os.environ['DOCKER_HOST']
return jsonify()
else:
os.environ['DOCKER_HOST'] = new_host
return jsonify(host=new_host)
@app.route(API_V1 + "authentication", methods=['GET'])
def authentication():
"""
check if basic authentication is enabled
"""
return jsonify(enabled=authentication_enabled())
@app.route(API_V1 + "authentication", methods=['DELETE'])
@requires_auth
def disable_basic_authentication():
"""
disable basic authentication
"""
disable_authentication()
return jsonify(enabled=False)
@app.route(API_V1 + "authentication", methods=['POST'])
@requires_auth
def enable_basic_authentication():
"""
set up basic authentication
"""
data = loads(request.data)
set_authentication(data["username"], data["password"])
return jsonify(enabled=True)
# static resources
@app.route("/")
def index():
"""
index.html
"""
return app.send_static_file('index.html')
## basic exception handling
@app.errorhandler(requests.exceptions.ConnectionError)
def handle_connection_error(err):
"""
connection exception handler
"""
return 'docker host not found: ' + str(err), 500
@app.errorhandler(docker.errors.DockerException)
def handle_docker_error(err):
"""
docker exception handler
"""
return 'docker exception: ' + str(err), 500
@app.errorhandler(Exception)
def handle_generic_error(err):
"""
default exception handler
"""
traceback.print_exc()
return 'error: ' + str(err), 500
# run app
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True, threaded=True)