forked from francescou/docker-compose-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
247 lines (214 loc) · 6.31 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
"""
Docker Compose UI, flask based application
"""
from flask import Flask, jsonify, request
from scripts.bridge import ps_, get_project, get_container_from_id
from scripts.find_yml import find_yml_files
from scripts.requires_auth import requires_auth, authentication_enabled, \
disable_authentication, set_authentication
from json import loads
import logging
import requests
import docker
import os
import traceback
# 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/<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,
links=container.links(),
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
"""
name = loads(request.data)["id"]
get_project_with_name(name).build()
return jsonify(command='build')
@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 == 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)