This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
61 lines (48 loc) · 2.57 KB
/
run.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
import argparse
import signal
import time
import faas
from faas.utils import is_crash_loop_set, get_crash_loop_file_identifier, get_application_tokens, ExpandPath, is_dir
def ignore_signal(signum, frame):
print("Ignoring signal: {}".format(signum))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--port", type=int, required=True, help="Port to listen on")
parser.add_argument("--config-file", type=argparse.FileType(mode='r'),
help="Application config file")
parser.add_argument("--log-directory", default='.', type=is_dir, action=ExpandPath,
help="Application log directory")
parser.add_argument("--host", default="0.0.0.0", help="Host to listen on")
parser.add_argument("--debug", action='store_true', help="Print debug info")
parser.add_argument("--start-delay-seconds", default=0, type=int,
help="Start application after N seconds")
parser.add_argument("--crash", action='store_true',
help="Crash application before starting socket")
parser.add_argument("--ignore-signal", action='append', type=int,
help="Specify OS signal to be ignored in numeric format.")
parser.add_argument("--require-authentication", action='store_true',
help="Enforce token base authentication")
args = parser.parse_args()
faas.require_authentication = args.require_authentication
if faas.require_authentication:
if not args.config_file:
parser.error('The --require-authentication argument requires the --config-file')
faas.application_tokens = get_application_tokens(args.config_file)
faas.log_directory = args.log_directory
options = {"threaded": True, "debug": True, "use_reloader": False}
time.sleep(args.start_delay_seconds)
if args.ignore_signal:
# Using debug prevents from setting SIGTERM trap, therefore we need to disable it.
options["debug"] = False
for signal_to_ignore in args.ignore_signal:
print("Setting trap for signal: {}".format(signal_to_ignore))
signal.signal(signal_to_ignore, ignore_signal)
crash_loop_set = is_crash_loop_set(get_crash_loop_file_identifier())
if args.crash or crash_loop_set:
raise Exception("Crash argument was set (crash loop: {}). Crashing!".format(crash_loop_set))
faas.app.run(args.host, args.port, **options)
while True:
print "I'm still standing, which means FaaS app was terminated."
time.sleep(1)
if __name__ == "__main__":
main()