-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·280 lines (239 loc) · 11.8 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
#!/usr/bin/python3
from gevent import monkey; monkey.patch_all()
import bottle
import click
import gevent
import logging
import pymysql
import time
import sys
import up
from datetime import timedelta
from DBUtils.PooledDB import PooledDB
from gevent.pool import Pool
from pymysql import Connection
from utils import log_exceptions, nice_shutdown
from utils.logging import configure_logging, wsgi_log_middleware
from up import construct_app, run_worker, td_format
from up.dao import UpDao, create_db
from up.session import TokenDecoder
CONTEXT_SETTINGS = {
'help_option_names': ['-h', '--help']
}
log = logging.getLogger(__name__)
# Use an unbounded pool to track gevent greenlets so we can
# wait for them to finish on shutdown.
gevent_pool = Pool()
@click.group(context_settings=CONTEXT_SETTINGS)
def main():
pass
@click.command()
@click.option('--mysql-host', default='localhost',
help='MySQL server host (default=localhost).')
@click.option('--mysql-port', default=3306,
help='MySQL server port (default=3306).')
@click.option('--mysql-user', default='up',
help='MySQL server user (default=up).')
@click.option('--mysql-password', default='',
help='MySQL server password (default=None).')
@click.option('--mysql-database', default='up',
help='MySQL server database (default=up).')
@click.option('--json', '-j', default=False, is_flag=True,
help='Log in json.')
@click.option('--verbose', '-v', default=False, is_flag=True,
help='Log debug messages.')
@log_exceptions(exit_on_exception=True)
def init(**options):
configure_logging(json=options['json'], verbose=options['verbose'])
connection = Connection(host=options['mysql_host'],
port=options['mysql_port'],
user=options['mysql_user'],
password=options['mysql_password'],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
create_db(connection, options['mysql_database'])
connection_pool = PooledDB(creator=pymysql,
mincached=1,
maxcached=10,
# max connections currently in use - doesn't
# include cached connections
maxconnections=50,
blocking=True,
host=options['mysql_host'],
port=options['mysql_port'],
user=options['mysql_user'],
password=options['mysql_password'],
database=options['mysql_database'],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
up_dao = UpDao(connection_pool)
up_dao.create_job_table()
@click.command()
@click.option('--tries', default=9,
help='Number of times to try a URL (default=9).')
@click.option('--initial-delay-minutes', default=15,
help='How long to wait before the first try of a URL (default=15).')
@click.option('--timeout-seconds', default=10,
help='Timeout when trying a URL (default=10).')
@click.option('--service-protocol', type=click.Choice(('https', 'http')),
default='https',
help='The protocol for the public service. (default=https)')
@click.option('--service-hostname', default='localhost',
help='The hostname for the public service. (default=localhost)')
@click.option('--service-port', default='',
help='The port for the public service, if non standard.')
@click.option('--service-path', default='',
help='The path prefix for the public service, if any.'
'Should start with a "/", but not end with one.')
@click.option('--oidc-name', default='Alias',
help='Name of the OpenID Connect provider to use for login.')
@click.option('--oidc-iss', required=True,
help='Issuer string of the OpenID Connect provider.')
@click.option('--oidc-about-url', required=True,
help='URL of an about page for the OpenID Connect provider.')
@click.option('--oidc-auth-endpoint', required=True,
help='URL of the authenticaiton endpoint of the OpenID Connect provider.')
@click.option('--oidc-token-endpoint', required=True,
help='URL of the token endpoint of the OpenID Connect provider.')
@click.option('--oidc-public-key-file', default='id_rsa.pub', type=click.File(mode='rb'),
help='Path to RSA256 public key file for the OpenID Connect provider. '
'(default=id_rsa.pub)')
@click.option('--oidc-client-id', required=True,
help='Client ID issued by the OpenID Connect provider.')
@click.option('--oidc-client-secret', required=True,
help='Client secret issued by the OpenID Connect provider.')
@click.option('--mysql-host', default='localhost',
help='MySQL server host (default=localhost).')
@click.option('--mysql-port', default=3306,
help='MySQL server port (default=3306).')
@click.option('--mysql-user', default='up',
help='MySQL server user (default=up).')
@click.option('--mysql-password', default='',
help='MySQL server password (default=None).')
@click.option('--mysql-database', default='up',
help='MySQL server database (default=up).')
@click.option('--testing-mode', default=False, is_flag=True,
help='Relax security to simplify testing, e.g. allow http cookies')
@click.option('--port', '-p', default=8080,
help='Port to serve on (default=8080).')
@click.option('--shutdown-sleep', default=10,
help='How many seconds to sleep during graceful shutdown. (default=10)')
@click.option('--shutdown-wait', default=10,
help='How many seconds to wait for active connections to close during graceful '
'shutdown (after sleeping). (default=10)')
@click.option('--json', '-j', default=False, is_flag=True,
help='Log in json.')
@click.option('--verbose', '-v', default=False, is_flag=True,
help='Log debug messages.')
@log_exceptions(exit_on_exception=True)
def server(**options):
def shutdown():
up.SERVER_READY = False
def wait():
# Sleep for a few seconds to allow for race conditions between sending
# the SIGTERM and load balancers stopping sending traffic here.
log.info('Shutdown: Sleeping %(sleep_s)s seconds.',
{'sleep_s': options['shutdown_sleep']})
time.sleep(options['shutdown_sleep'])
log.info('Shutdown: Waiting up to %(wait_s)s seconds for connections to close.',
{'wait_s': options['shutdown_sleep']})
gevent_pool.join(timeout=options['shutdown_wait'])
log.info('Shutdown: Exiting.')
sys.exit()
# Run in greenlet, as we can't block in a signal hander.
gevent.spawn(wait)
configure_logging(json=options['json'], verbose=options['verbose'])
connection_pool = PooledDB(creator=pymysql,
mincached=1,
maxcached=10,
# max connections currently in use - doesn't
# include cached connections
maxconnections=50,
blocking=True,
host=options['mysql_host'],
port=options['mysql_port'],
user=options['mysql_user'],
password=options['mysql_password'],
database=options['mysql_database'],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
up_dao = UpDao(connection_pool)
with options['oidc_public_key_file'] as file:
public_key = file.read()
token_decoder = TokenDecoder(public_key, options['oidc_iss'], options['oidc_client_id'])
app = construct_app(up_dao, token_decoder, **options)
app = wsgi_log_middleware(app)
with nice_shutdown(shutdown):
bottle.run(app,
host='0.0.0.0', port=options['port'],
server='gevent', spawn=gevent_pool,
# Disable default request logging - we're using middleware
quiet=True, error_log=None)
@click.command()
@click.option('--delay-multiplier', default=2,
help='Multiplier to apply to the delay after each try of a URL (default=2).')
@click.option('--timeout-seconds', default=10,
help='Timeout when trying a URL (default=10).')
@click.option('--oidc-token-endpoint', required=True,
help='URL of the token endpoint of the OpenID Connect provider.')
@click.option('--oidc-send-endpoint', required=True,
help='URL of the send message endpoint of the OpenID Connect provider.')
@click.option('--oidc-client-id', required=True,
help='Client ID issued by the OpenID Connect provider.')
@click.option('--oidc-client-secret', required=True,
help='Client secret issued by the OpenID Connect provider.')
@click.option('--mysql-host', default='localhost',
help='MySQL server host (default=localhost).')
@click.option('--mysql-port', default=3306,
help='MySQL server port (default=3306).')
@click.option('--mysql-user', default='up',
help='MySQL server user (default=up).')
@click.option('--mysql-password', default='',
help='MySQL server password (default=None).')
@click.option('--mysql-database', default='up',
help='MySQL server database (default=up).')
@click.option('--json', '-j', default=False, is_flag=True,
help='Log in json.')
@click.option('--verbose', '-v', default=False, is_flag=True,
help='Log debug messages.')
@log_exceptions(exit_on_exception=True)
def worker(**options):
configure_logging(json=options['json'], verbose=options['verbose'])
connection_pool = PooledDB(creator=pymysql,
mincached=1,
maxcached=1,
# max connections currently in use - doesn't
# include cached connections
maxconnections=1,
blocking=True,
host=options['mysql_host'],
port=options['mysql_port'],
user=options['mysql_user'],
password=options['mysql_password'],
database=options['mysql_database'],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
up_dao = UpDao(connection_pool)
with nice_shutdown():
run_worker(up_dao, **options)
@click.command()
@click.option('--tries', default=9,
help='Number of times to try a URL (default=9).')
@click.option('--initial-delay-minutes', default=15,
help='How long to wait before the first try of a URL (default=15).')
@click.option('--delay-multiplier', default=2,
help='Multiplier to apply to the delay after each try of a URL (default=2).')
def show_schedule(tries, initial_delay_minutes, delay_multiplier):
delay = timedelta(minutes=initial_delay_minutes)
total_delay = delay
for t in range(1, tries + 1):
print(f'{t:5}: {td_format(delay)}')
delay = delay * delay_multiplier
total_delay += delay
print(f"Total: {td_format(delay)}")
main.add_command(init)
main.add_command(server)
main.add_command(worker)
main.add_command(show_schedule)
if __name__ == '__main__':
main(auto_envvar_prefix='UP_OPT')