[88e7f2f] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import os,sys |
---|
| 4 | |
---|
| 5 | from optparse import OptionParser |
---|
| 6 | |
---|
| 7 | from federation import config_parser |
---|
| 8 | from federation.server import server, xmlrpc_handler, soap_handler |
---|
| 9 | from federation.util import fedd_ssl_context |
---|
| 10 | from federation.deter_impl import new_feddservice |
---|
| 11 | |
---|
| 12 | from socket import error as socket_error |
---|
| 13 | from threading import Lock, Thread |
---|
| 14 | from signal import signal, pause, SIGINT, SIGTERM |
---|
| 15 | from select import select, error |
---|
| 16 | from time import sleep |
---|
| 17 | |
---|
| 18 | import logging |
---|
| 19 | import M2Crypto |
---|
| 20 | |
---|
| 21 | class fedd_opts(OptionParser): |
---|
| 22 | """Encapsulate option processing in this class, rather than in main""" |
---|
| 23 | def __init__(self): |
---|
| 24 | OptionParser.__init__(self, usage="%prog [opts] (--help for details)", |
---|
| 25 | version="0.1") |
---|
| 26 | |
---|
| 27 | self.set_defaults(logfile=None, debug=0) |
---|
| 28 | |
---|
| 29 | self.add_option("-d", "--debug", action="count", dest="debug", |
---|
| 30 | help="Set debug. Repeat for more information") |
---|
| 31 | self.add_option("-f", "--configfile", action="store", |
---|
| 32 | default="/usr/local/etc/fedd.conf", |
---|
| 33 | dest="configfile", help="Configuration file (required)") |
---|
| 34 | self.add_option("-l", "--logfile", action="store", dest="logfile", |
---|
| 35 | help="File to send log messages to") |
---|
| 36 | self.add_option("--trace", action="store_const", dest="tracefile", |
---|
| 37 | const=sys.stderr, help="Print SOAP exchange to stderr") |
---|
| 38 | |
---|
| 39 | servers_active = True # Sub-servers run while this is True |
---|
| 40 | servers = [ ] # server instances instantiated from services |
---|
| 41 | servers_lock = Lock() # Lock to manipulate servers from sub-server threads |
---|
| 42 | |
---|
| 43 | def shutdown(sig, frame): |
---|
| 44 | """ |
---|
| 45 | On a signal, stop running sub-servers. |
---|
| 46 | |
---|
| 47 | This is connected to signals below |
---|
| 48 | """ |
---|
| 49 | global servers_active, flog |
---|
| 50 | |
---|
| 51 | servers_active = False |
---|
| 52 | flog.info("Received signal %d, shutting down" % sig); |
---|
| 53 | |
---|
| 54 | def run_server(s): |
---|
| 55 | """ |
---|
| 56 | Operate a subserver, shutting down when servers_active is false. |
---|
| 57 | |
---|
| 58 | Each server (that is host/port/transport triple) has a thread running this |
---|
| 59 | function, so each can handle requests independently. They all call in to |
---|
| 60 | the same implementation, which must manage its own synchronization. |
---|
| 61 | """ |
---|
| 62 | global servers_active # Not strictly needed: servers_active is only read |
---|
| 63 | global servers # List of active servers |
---|
| 64 | global servers_lock # Lock to manipulate servers |
---|
| 65 | |
---|
| 66 | while servers_active: |
---|
| 67 | try: |
---|
| 68 | i, o, e = select((s,), (), (), 1.0) |
---|
| 69 | if s in i: s.handle_request() |
---|
| 70 | except error: |
---|
| 71 | # The select call seems to get interrupted by signals as well as |
---|
| 72 | # the main thread. This essentially ignores signals in this |
---|
| 73 | # thread. |
---|
| 74 | pass |
---|
| 75 | |
---|
| 76 | # Done. Remove us from the list |
---|
| 77 | servers_lock.acquire() |
---|
| 78 | servers.remove(s) |
---|
| 79 | servers_lock.release() |
---|
| 80 | |
---|
| 81 | M2Crypto.threading.init() |
---|
| 82 | opts, args = fedd_opts().parse_args() |
---|
| 83 | |
---|
| 84 | # Logging setup |
---|
| 85 | flog = logging.getLogger("fedd") |
---|
| 86 | ffmt = logging.Formatter("%(asctime)s %(name)s %(message)s", |
---|
| 87 | '%d %b %y %H:%M:%S') |
---|
| 88 | |
---|
| 89 | if opts.logfile: fh = logging.FileHandler(opts.logfile) |
---|
| 90 | else: fh = logging.StreamHandler(sys.stdout) |
---|
| 91 | |
---|
| 92 | # The handler will print anything, setting the logger level will affect what |
---|
| 93 | # gets recorded. |
---|
| 94 | fh.setLevel(logging.DEBUG) |
---|
| 95 | |
---|
| 96 | if opts.debug: flog.setLevel(logging.DEBUG) |
---|
| 97 | else: flog.setLevel(logging.INFO) |
---|
| 98 | |
---|
| 99 | fh.setFormatter(ffmt) |
---|
| 100 | flog.addHandler(fh) |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | if not os.access(opts.configfile, os.R_OK): |
---|
| 104 | sys.exit("Can't read config file %s" % opts.configfile) |
---|
| 105 | |
---|
| 106 | # Initialize the implementation |
---|
| 107 | try: |
---|
| 108 | config= config_parser() |
---|
| 109 | config.read(opts.configfile) |
---|
| 110 | except Exception, e: |
---|
| 111 | sys.exit("Cannot parse config file %s: %s" % (opts.configfile, e)) |
---|
| 112 | |
---|
| 113 | try: |
---|
| 114 | impl = new_feddservice(config) |
---|
| 115 | except RuntimeError, e: |
---|
| 116 | str = getattr(e, 'desc', None) or getattr(e,'message', None) or \ |
---|
| 117 | "No message" |
---|
| 118 | sys.exit("Error configuring fedd: %s" % str) |
---|
| 119 | |
---|
| 120 | if impl.cert_file: |
---|
| 121 | if not os.access(impl.cert_file, os.R_OK): |
---|
| 122 | sys.exit("Cannot read certificate file: %s" % impl.cert_file) |
---|
| 123 | else: |
---|
| 124 | sys.exit("Must supply certificate file (probably in config)") |
---|
| 125 | |
---|
| 126 | if impl.trusted_certs: |
---|
| 127 | if not os.access(impl.trusted_certs, os.R_OK): |
---|
| 128 | sys.exit("Cannot read trusted certificate file: %s" % \ |
---|
| 129 | impl.trusted_certs) |
---|
| 130 | |
---|
| 131 | # Create the SSL credentials |
---|
| 132 | ctx = None |
---|
| 133 | while ctx == None: |
---|
| 134 | try: |
---|
| 135 | ctx = fedd_ssl_context(impl.cert_file, impl.trusted_certs, |
---|
| 136 | password=impl.cert_pwd) |
---|
| 137 | except Exception, e: |
---|
| 138 | if str(e) != "bad decrypt" or impl.cert_pwd != None: |
---|
| 139 | raise |
---|
| 140 | |
---|
| 141 | services = config.get("globals", "services", "23235") |
---|
| 142 | |
---|
| 143 | for s in services.split(","): |
---|
| 144 | s = s.strip() |
---|
| 145 | colons = s.count(":") |
---|
| 146 | try: |
---|
| 147 | if colons == 0: |
---|
| 148 | p = int(s) |
---|
| 149 | h = '' |
---|
| 150 | t = 'soap' |
---|
| 151 | elif colons == 1: |
---|
| 152 | p, t = s.split(":") |
---|
| 153 | p = int(p) |
---|
| 154 | h = '' |
---|
| 155 | elif colons == 2: |
---|
| 156 | h, p, t = s.split(":") |
---|
| 157 | p = int(p) |
---|
| 158 | else: |
---|
| 159 | flog.error("Invalid service specification %s ignored." % s) |
---|
| 160 | continue |
---|
| 161 | except ValueError: |
---|
| 162 | flog.error("Error converting port to integer in %s: spec ignored" % s) |
---|
| 163 | continue |
---|
| 164 | |
---|
| 165 | t = t.lower() |
---|
| 166 | sdebug = (opts.debug > 0) |
---|
| 167 | try: |
---|
| 168 | if t == 'soap': |
---|
| 169 | servers.append(server((h, p), soap_handler, ctx, impl, sdebug)) |
---|
| 170 | elif t == 'xmlrpc': |
---|
| 171 | servers.append(server((h, p), xmlrpc_handler, ctx, impl, sdebug)) |
---|
| 172 | else: |
---|
| 173 | flog.error("Invalid transport specification (%s) in service %s" % \ |
---|
| 174 | (t, s)) |
---|
| 175 | continue |
---|
| 176 | except socket_error, e: |
---|
| 177 | flog.error("Cannot create server for %s: %s" % (s, e[1])) |
---|
| 178 | continue |
---|
| 179 | |
---|
| 180 | # Make sure that there are no malformed servers in the list |
---|
| 181 | servers = [ s for s in servers if s ] |
---|
| 182 | |
---|
| 183 | # Catch signals |
---|
| 184 | signal(SIGINT, shutdown) |
---|
| 185 | signal(SIGTERM, shutdown) |
---|
| 186 | |
---|
| 187 | # Start the servers |
---|
| 188 | for s in servers: |
---|
| 189 | Thread(target=run_server, args=(s,)).start() |
---|
| 190 | |
---|
| 191 | # Main thread waits for signals |
---|
| 192 | while servers_active: |
---|
| 193 | sleep(1.0) |
---|
| 194 | |
---|
| 195 | #Once shutdown starts wait for all the servers to terminate. |
---|
| 196 | while True: |
---|
| 197 | servers_lock.acquire() |
---|
| 198 | if len(servers) == 0: |
---|
| 199 | servers_lock.release() |
---|
| 200 | flog.info("All servers exited. Terminating") |
---|
| 201 | sys.exit(0) |
---|
| 202 | servers_lock.release() |
---|
| 203 | sleep(1) |
---|
| 204 | |
---|