forked from docker-archive/docker-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
131 lines (108 loc) · 3.8 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
# -*- coding: utf-8 -*-
import logging
import logging.handlers
import os
import sys
try:
import bugsnag
import bugsnag.flask
except ImportError as e:
_bugsnag_import_error = e
bugsnag = None
from . import toolkit
from .lib import config
from .server import __version__
import flask
# configure logging prior to subsequent imports which assume
# logging has been configured
cfg = config.load()
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
level=getattr(logging, cfg.loglevel.upper()),
datefmt="%d/%b/%Y:%H:%M:%S %z")
from .lib import mirroring # noqa
app = flask.Flask('docker-registry')
@app.route('/_ping')
@app.route('/v1/_ping')
def ping():
headers = {'X-Docker-Registry-Standalone': cfg.standalone is True}
if mirroring.is_mirror():
headers['X-Docker-Registry-Standalone'] = 'mirror'
return toolkit.response(headers=headers)
@app.route('/_versions')
@app.route('/v1/_versions')
def versions():
"""Return a JSON object ({"package-name": "package-version", ...}).
This is an unofficial endpoint for debugging your docker-registry
install. If you're running a publicly-accessible endpoint, it's
probably best to disable this endpoint to avoid leaking
implementation details.
"""
versions = {}
if cfg.debug_versions:
for name, module in sys.modules.items():
if name.startswith('_'):
continue
try:
version = module.__version__
except AttributeError:
continue
versions[name] = version
versions['python'] = sys.version
return toolkit.response(versions)
@app.route('/')
def root():
return toolkit.response('docker-registry server ({0}) (v{1})'
.format(cfg.flavor, __version__))
@app.after_request
def after_request(response):
response.headers['X-Docker-Registry-Version'] = __version__
response.headers['X-Docker-Registry-Config'] = cfg.flavor
return response
def init():
# Configure the email exceptions
info = cfg.email_exceptions
if info and info.smtp_host:
mailhost = info.smtp_host
mailport = info.smtp_port
if mailport:
mailhost = (mailhost, mailport)
smtp_secure = info.smtp_secure
secure_args = _adapt_smtp_secure(smtp_secure)
mail_handler = logging.handlers.SMTPHandler(
mailhost=mailhost,
fromaddr=info.from_addr,
toaddrs=[info.to_addr],
subject='Docker registry exception',
credentials=(info.smtp_login,
info.smtp_password),
secure=secure_args)
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
# Configure bugsnag
info = cfg.bugsnag
if info:
if not bugsnag:
raise _bugsnag_import_error
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
'..'))
bugsnag.configure(api_key=info,
project_root=root_path,
release_stage=cfg.flavor,
notify_release_stages=[cfg.flavor],
app_version=__version__
)
bugsnag.flask.handle_exceptions(app)
def _adapt_smtp_secure(value):
"""Adapt the value to arguments of ``SMTP.starttls()``
.. seealso:: <http://docs.python.org/2/library/smtplib.html\
#smtplib.SMTP.starttls>
"""
if isinstance(value, basestring):
# a string - wrap it in the tuple
return (value,)
if isinstance(value, config.Config):
assert set(value.keys()) <= set(['keyfile', 'certfile'])
return (value.keyfile, value.certfile)
if value:
return ()
init()