-
Notifications
You must be signed in to change notification settings - Fork 7
/
docker-registry-ui.py
executable file
·261 lines (213 loc) · 8.81 KB
/
docker-registry-ui.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
# -*- coding: utf-8 -*-
__author__ = 'arkii'
__email__ = '[email protected]'
__create__ = '10/30/14 19:48'
import os, re, tarfile
from urllib import urlencode
import configuration
from httplib import HTTPConnection
from flask import Flask
from flask import request
from flask import json
from flask import render_template as render
from flask import make_response
def add_http_header(object=None, key=None, value=None):
_response = make_response(object)
_response.headers[key] = value
return _response
class RegistryClass:
def __init__(self, server, port):
self.http_header = {
"Cache-Control": "no-cache",
"User-Agent": "DockerRegistryUI",
"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"
}
self.jsonde = json.JSONDecoder()
self.jsonen = json.JSONEncoder()
self.server = server
self.port = port
def ping(self, verbose=False):
try:
self.conn = HTTPConnection(self.server, port=self.port, timeout=3)
self.conn.request(method='GET', url='/v1/_ping', headers=self.http_header)
self.response = self.conn.getresponse()
self.data = self.response.read()
if verbose is True:
self.data = {'result': self.data}
self.status = self.response.status
self.message = self.response.reason
self.headers = dict(self.response.getheaders())
self.data['server_headers'] = self.headers
self.data['server_message'] = self.message
self.data['server_code'] = self.status
except IOError:
self.data = 'socket.error'
finally:
self.conn.close()
return self.data
def get(self, uri=None, verbose=False):
try:
self.conn = HTTPConnection(self.server, port=self.port, timeout=3)
self.action = 'GET'
self.conn.request(method=self.action, url=uri, headers=self.http_header)
self.response = self.conn.getresponse()
self.content = self.response.read() # here is str type
self.data = self.jsonde.decode(self.content) # convert str to dict
if verbose is True:
self.status = self.response.status
self.message = self.response.reason
self.headers = dict(self.response.getheaders())
self.data['server_headers'] = self.headers
self.data['server_message'] = self.message
self.data['server_code'] = self.status
except IOError:
self.data = 'socket.error'
finally:
self.conn.close()
return self.data
def act(self, action='GET', uri=None, verbose=False):
try:
self.conn = HTTPConnection(self.server, port=self.port, timeout=3)
self.method = action
self.conn.request(method=self.method, url=uri, headers=self.http_header)
self.response = self.conn.getresponse()
self.content = self.response.read() # here is str type
self.data = self.jsonde.decode(self.content) # convert str to dict
if verbose is True:
self.data = {'result': self.data}
self.status = self.response.status
self.message = self.response.reason
self.headers = dict(self.response.getheaders())
self.data['server_headers'] = self.headers
self.data['server_message'] = self.message
self.data['server_code'] = self.status
except IOError:
self.data = 'socket.error'
finally:
self.conn.close()
return self.data
def delete(self, action='DELETE', uri=None, verbose=False):
try:
self.conn = HTTPConnection(self.server, port=self.port, timeout=3)
self.method = action
self.conn.request(method=self.method, url=uri, headers=self.http_header)
self.response = self.conn.getresponse()
self.content = self.response.read() # here is str type
self.data = self.content
if verbose is True:
self.data = {'result': self.data}
self.status = self.response.status
self.message = self.response.reason
self.headers = dict(self.response.getheaders())
self.data['server_headers'] = self.headers
self.data['server_message'] = self.message
self.data['server_code'] = self.status
except IOError:
self.data = 'socket.error'
finally:
self.conn.close()
return self.data
def close(self):
self.conn.close()
registry = RegistryClass(server=configuration.server, port=configuration.port)
app = Flask(__name__)
@app.route('/')
def main_page():
_registryhost = configuration.server
_status = ping_server()
_t = registry.get('/v1/search?q=')
_imagenumber = ''
if isinstance(_t, dict): _imagenumber = _t['num_results']
return render('index.html', pagetitle='index', imagenumber=_imagenumber, hosts=_registryhost, status=_status)
@app.route('/images')
def images_page():
msg = registry.get('/v1/search?q=')
_tableheader = ['Name', 'Description', 'Actions']
return render('images.html', pagetitle='images', msg=msg, tableheader=_tableheader)
@app.route('/ping')
def ping_server():
return registry.ping()
@app.route('/find/', methods=['POST'])
@app.route('/find/<text>')
def find_image(text=None):
if request.method == 'POST':
uri = '/v1/search?q=' + request.values['name']
if text:
uri = '/v1/search?q=' + str(text)
msg = registry.get(uri=uri, verbose=True)
_tableheader = ['Name', 'Description', 'Actions']
return render('images.html', pagetitle='images', msg=msg, tableheader=_tableheader)
@app.route('/info/<id>')
def show_info(id=None):
_uri = '/v1/images/' + id + '/json'
_msg = registry.act(uri=_uri, verbose=True)
_msg['tableheader'] = ['Tag', 'ID']
_uri = '/v1/images/' + id + '/ancestry'
_ancestry = {}
_ancestry['tableheader'] = 'Ancestry'
_ancestry['data'] = registry.act(uri=_uri)
return render('info.html', pagetitle='images', msg=_msg, ancestry=_ancestry)
@app.route('/tags/', methods=['POST'])
@app.route('/tags/<namespace>/<repository>')
def show_tags(namespace=None, repository=None):
if repository is not None: _query = namespace + '/' + repository
if request.method == 'POST': _query = str(request.values['name'])
uri = '/v1/repositories/' + _query + '/tags'
msg = registry.act(uri=uri, verbose=True)
_tableheader = ['Name', 'ID', 'Actions']
_tree_json = '/json/' + _query
return render('tags.html', pagetitle='tags', tree_json=_tree_json, msg=msg, tableheader=_tableheader, reponame=_query)
@app.route('/rm/', methods=['POST'])
@app.route('/rm/<namespace>/<repository>')
@app.route('/rm/<namespace>/<repository>/<tag>')
def delete(namespace=None, repository=None, tag=None):
if request.method == 'POST':
uri = '/v1/repositories/' + str(request.values['name']) + '/'
else:
if namespace is not None: _query = namespace + '/'
if repository is not None: _query = namespace + '/' + repository + '/'
if tag is not None: _query = namespace + '/' + repository + '/tags/' + tag
uri = '/v1/repositories/' + _query
msg = registry.delete(action='DELETE', uri=uri)
# msg['tableheader'] = ['Tag', 'ID']
# return render('tags.html', pagetitle='images', msg=msg)
return str(msg)
@app.route('/plain', methods=['GET', 'POST', 'PUT', 'DELETE'])
def hello_world():
if request.method == 'GET':
return 'YOU GET ME !'
if request.method == 'POST':
return 'YOU POST ME !'
if request.method == 'PUT':
return 'YOU PUT ME !'
if request.method == 'DELETE':
return 'YOU DELETE ME !'
else:
return 'YOU DID NOTHING !'
@app.route('/test')
def test():
return render('test.html')
@app.route('/tree')
def tree():
_url = '/json'
return render('tree.html', tree_json=_url)
@app.route('/json/<namespace>/<repository>')
def output_json(namespace=None, repository=None):
if repository is not None: _query = namespace + '/' + repository
_uri = '/v1/repositories/' + _query + '/tags'
_ancestry = {}
_ancestry['tableheader'] = 'Ancestry'
_ancestry['data'] = registry.act(uri=_uri)
_tree_json = _ancestry['data']
_d = {'name':_query}
_c = []
for k, v in _tree_json.iteritems():
_c.append({'name':k, 'children':[{'name':v}]})
_d['children'] = _c
_data = json.JSONEncoder().encode(_d)
return add_http_header(_data,'Content-Type', 'application/json')
if __name__ == '__main__':
# Flask.debug = True
app.run(host='0.0.0.0')
app.run(debug=1)