-
Notifications
You must be signed in to change notification settings - Fork 11
/
nameserver
executable file
·128 lines (104 loc) · 4.16 KB
/
nameserver
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2012, 2014 Martin Raspaud
# Author(s):
# Martin Raspaud <martin.raspaud@smhi.se>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""The nameserver. Port 5555 (hardcoded) is used for communications.
"""
# TODO: make port configurable.
from posttroll.ns import NameServer
import logging
logger = logging.getLogger(__name__)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--daemon", help="Run as a daemon",
choices=["start", "stop", "status", "restart"])
parser.add_argument("-l", "--log", help="File to log to (defaults to stdout)",
default=None)
parser.add_argument("-v", "--verbose", help="print debug messages too",
action="store_true")
opts = parser.parse_args()
if opts.log:
import logging.handlers
handler = logging.handlers.TimedRotatingFileHandler(opts.log,
"midnight",
backupCount=7)
else:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("[%(levelname)s: %(asctime)s :"
" %(name)s] %(message)s",
'%Y-%m-%d %H:%M:%S'))
if opts.verbose:
loglevel = logging.DEBUG
else:
loglevel = logging.INFO
handler.setLevel(loglevel)
logging.getLogger('').setLevel(loglevel)
logging.getLogger('').addHandler(handler)
logger = logging.getLogger("nameserver")
ns = NameServer()
if opts.daemon is None:
try:
ns.run()
except KeyboardInterrupt:
pass
except:
logger.exception("Something wrong happened...")
raise
finally:
print ("Thanks for using pytroll/nameserver. "
"See you soon on www.pytroll.org!")
else: # Running as a daemon
if opts.daemon == "status":
import os
import sys
if os.path.exists("/tmp/nameserver.pid"):
with open("/tmp/nameserver.pid") as fd_:
pid = int(fd_.read())
try:
os.kill(pid, 0)
except OSError:
sys.exit(1)
else:
sys.exit(0)
else:
sys.exit(1)
try:
import daemon.runner
import signal
import sys
class App(object):
"""App object for running the nameserver as daemon.
"""
stdin_path = "/dev/null"
stdout_path = "/dev/null"
stderr_path = "/dev/null"
run = ns.run
pidfile_path = "/tmp/nameserver.pid"
pidfile_timeout = 90
def _terminate(*args):
"""terminate the nameserver.
"""
del args
ns.stop()
signal.signal(signal.SIGTERM, _terminate)
APP = App()
sys.argv = [sys.argv[0], opts.daemon]
angel = daemon.runner.DaemonRunner(APP)
if opts.log:
angel.daemon_context.files_preserve = [handler.stream]
angel.parse_args([sys.argv[0], opts.daemon])
sys.exit(angel.do_action())
except ImportError:
print "Cannot run as a daemon, you need python-daemon installed."