This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
load-balancer-servo
executable file
·124 lines (107 loc) · 3.32 KB
/
load-balancer-servo
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
#!/usr/bin/python -tt
#
# Copyright 2009-2013 Eucalyptus Systems, Inc.
#
# 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; version 3 of the License.
#
# 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/.
#
# Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta
# CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need
# additional information or have any questions.
import os
import sys
import getopt
import servo
import servo.config as config
def daemonize(func=None):
# Fork process
procid = os.fork()
if procid < 0:
sys.exit(1)
elif procid > 0:
sys.exit(0)
procid = os.setsid()
if procid == -1:
sys.exit(1)
# Close open file descriptors
for fd in (0, 1, 2):
try:
os.close(fd)
except OSError:
pass
# Point descriptors to /dev/null
os.open("/dev/null", os.O_RDWR)
os.dup(0)
os.dup(0)
os.umask(027)
os.chdir("/")
# Import here since these are only necessary if we daemonize
import fcntl
import signal
import atexit
# Create pidfile
try:
f = open(config.pidfile, "w")
fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
f.write(str(os.getpid()))
f.flush()
except IOError:
servo.log.error("Failed to write pidfile %s", config.pidfile)
sys.exit(1)
signal.signal(signal.SIGTERM, cleanpid)
atexit.register(cleanpid)
if func:
servo.log.info("Starting daemonized servo process")
func()
def cleanpid(signum=None, action=None):
os.remove(config.pidfile)
sys.exit(0)
def usage():
print """
%s [-d | --daemonize] [-p | --pidfile]
""" % (os.path.basename(sys.argv[0]))
sys.exit(1)
if __name__ == "__main__":
daemon = False
try:
longopts = [
"--help",
"--daemonize",
"--pidfile",
"--log-level",
"--boto-config",
]
opts, _ = getopt.getopt(sys.argv[1:], "hdp:l:b:c:", longopts)
for arg, value in opts:
if arg == "-d" or arg == "--daemonize":
daemon = True
elif arg == "-p" or arg == "--pidfile":
servo.set_pidfile(value)
elif arg == "-l" or arg == "--log-level":
servo.set_loglevel(value)
elif arg == "-b" or arg == "--boto-log-level":
servo.set_boto_loglevel(value)
elif arg == "-c" or arg == "--boto-config":
servo.set_boto_config(value)
elif arg == "-h" or arg == "--help":
usage()
except getopt.GetoptError:
usage()
if daemon:
daemonize(servo.start_all)
else:
try:
servo.log.info("Starting Loadbalancer Servo")
servo.start_all()
except KeyboardInterrupt:
servo.log.info("Interrupted By User")
sys.exit(0)