This repository has been archived by the owner on Feb 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
captaind
executable file
·105 lines (82 loc) · 2.87 KB
/
captaind
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
#!/usr/bin/env python
import os
import sys
import daemon
import logging
import optparse
import eventlet
import contextlib
import logging.handlers
from daemon.pidlockfile import PIDLockFile
from captain.server import Server
def main():
parser = optparse.OptionParser()
def architectures_callback(option, opt, value, parser):
setattr(
parser.values,
option.dest,
[x for x in value.split(',') if x],
)
parser.add_option('--logfile', dest='log_file')
parser.add_option('--pidfile', dest='pid_file', default='')
parser.add_option('--verbosity', dest='verbosity', default=2, type='int')
parser.add_option('--archive-root', dest='archive_root', default='./root')
parser.add_option('--gpg-home', dest='gpg_home', default='./root/gpghome')
parser.add_option('--archive', dest='archive', default='stable')
parser.add_option('--origin', dest='origin', default='captain')
parser.add_option('--max-versions', dest='max_versions', default=2,
type='int')
parser.add_option('--architectures', dest='architectures', type='string',
action='callback', default=('i386', 'amd64'),
callback=architectures_callback)
parser.add_option('--port', dest='port', default=3333, type='int')
parser.add_option('--bind', dest='bind', default='127.0.0.1')
options, args = parser.parse_args()
if not options.architectures:
parser.error("Must specify at least one architecture.")
context = noop()
if options.pid_file:
context = daemon.DaemonContext(
pidfile=PIDLockFile(options.pid_file),
files_preserve=range(daemon.daemon.get_maximum_file_descriptors()),
)
for x in ('archive_root', 'gpg_home'):
# Ensure paths are absolute
setattr(options, x, os.path.abspath(getattr(options, x)))
val = getattr(options, x)
if not os.path.isdir(val):
print "E: %s is not a directory" % val
return 1
eventlet.monkey_patch()
# Setup logging
log = logging.getLogger()
log.setLevel({
0: logging.WARNING,
1: logging.INFO,
2: logging.DEBUG,
}[options.verbosity])
if options.log_file:
handler = logging.handlers.RotatingFileHandler(
filename=options.log_file,
mode='a+',
maxBytes=2 ** 20,
backupCount=20,
)
else:
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(
logging.Formatter('%(asctime).19s %(levelname).1s %(message)s')
)
log.addHandler(handler)
# Open sockets, etc. before daemonising
server = Server(log, options)
# Try and create files with 644 (rw-r--r--) permissions
os.umask(022)
with context:
server.run()
return 0
@contextlib.contextmanager
def noop():
yield
if __name__ == '__main__':
sys.exit(main())