9
9
from argparse import ArgumentParser # noqa
10
10
from argparse import RawTextHelpFormatter # noqa
11
11
import distutils .spawn
12
+ import getpass
13
+ import logging
12
14
import os
13
15
import sys
14
16
21
23
from .search import * # noqa
22
24
23
25
cfg = config .load ()
24
- if cfg .standalone is True :
25
- # If standalone mode is enabled (default) , load the fake Index routes
26
+ if cfg .standalone :
27
+ # If standalone mode is enabled, load the fake Index routes
26
28
from .index import * # noqa
27
29
28
30
31
+ logger = logging .getLogger (__name__ )
32
+
29
33
DESCRIPTION = """run the docker-registry with gunicorn, honoring the following
30
34
environment variables:
31
-
35
+ REGISTRY_HOST: TCP host or ip to bind to; default is 0.0.0.0
36
+ REGISTRY_PORT: TCP port to bind to; default is 5000
32
37
GUNICORN_WORKERS: number of worker processes gunicorn should start
33
- REGISTRY_PORT: TCP port to bind to on all ipv4 addresses; default is 5000
34
38
GUNICORN_GRACEFUL_TIMEOUT: timeout in seconds for graceful worker restart
35
39
GUNiCORN_SILENT_TIMEOUT: timeout in seconds for restarting silent workers
40
+ GUNiCORN_USER: unix user to downgrade priviledges to
41
+ GUNiCORN_GROUP: unix group to downgrade priviledges to
36
42
"""
37
43
38
44
@@ -48,20 +54,45 @@ def run_gunicorn():
48
54
formatter_class = RawTextHelpFormatter )
49
55
parser .parse_args ()
50
56
51
- workers = str (env .source ('GUNICORN_WORKERS' ))
52
- host = env .source ('REGISTRY_HOST' )
53
- port = env .source ('REGISTRY_PORT' )
54
- graceful_timeout = str (env .source ('GUNICORN_GRACEFUL_TIMEOUT' ))
55
- silent_timeout = str (env .source ('GUNICORN_SILENT_TIMEOUT' ))
56
-
57
- address = '%s:%s' % (host , port )
58
-
59
57
gunicorn_path = distutils .spawn .find_executable ('gunicorn' )
60
- if gunicorn_path is None :
58
+ if not gunicorn_path :
61
59
print ('error: gunicorn executable not found' , file = sys .stderr )
62
60
sys .exit (1 )
63
61
64
- os .execl (gunicorn_path , 'gunicorn' , '--access-logfile' , '-' , '--debug' ,
65
- '--max-requests' , '100' , '--graceful-timeout' , graceful_timeout ,
66
- '-t' , silent_timeout , '-k' , 'gevent' , '-b' , address ,
67
- '-w' , workers , 'docker_registry.wsgi:application' )
62
+ address = '%s:%s' % (
63
+ env .source ('REGISTRY_HOST' ),
64
+ env .source ('REGISTRY_PORT' )
65
+ )
66
+
67
+ args = [
68
+ gunicorn_path , 'gunicorn' ,
69
+ '--access-logfile' , '-' , '--debug' ,
70
+ '--max-requests' , '100' ,
71
+ '-k' , 'gevent' ,
72
+ '--graceful-timeout' , env .source ('GUNICORN_GRACEFUL_TIMEOUT' ),
73
+ '-t' , env .source ('GUNICORN_SILENT_TIMEOUT' ),
74
+ '-w' , env .source ('GUNICORN_WORKERS' ),
75
+ '-b' , address ,
76
+ 'docker_registry.wsgi:application'
77
+ ]
78
+
79
+ user = env .source ('GUNICORN_USER' )
80
+ group = env .source ('GUNICORN_GROUP' )
81
+ if user or group :
82
+ if getpass .getuser () == 'root' :
83
+ if user :
84
+ logger .info ('Downgrading privs to user %s' % user )
85
+ args .append ('-u' )
86
+ args .append (user )
87
+
88
+ if group :
89
+ logger .info ('Downgrading privs to group %s' % user )
90
+ args .append ('-g' )
91
+ args .append (group )
92
+ else :
93
+ logger .warn ('You asked we drop priviledges, but we are not root!' )
94
+
95
+ # Stringify all args
96
+ for (k , v ) in enumerate (args ):
97
+ args [k ] = str (v )
98
+ os .execl (* args )
0 commit comments