Exemple #1
0
def get_config():
    option = lambda k, d: os.environ.get(k, d)
    config = {
        'app': option('SOVEREIGN_APP', None),
        'threads': int(option('SOVEREIGN_THREADS', 0)),
        'host': option('SOVEREIGN_HOST', '*'),
        'port': int(option('SOVEREIGN_PORT', 0)),
        'socket': int(option('SOVEREIGN_SOCKET', None)),
        'virtual_env': option('SOVEREIGN_VIRTUAL_ENV', None),
    }
    
    if config['socket']:
        config['socket'] = socket.fromfd( config['socket'], socket.AF_INET, socket.SOCK_STREAM )
    else:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((config['host'], config['port']))
        sock.listen(500)
        config['socket'] = sock
    
    if not config['app']:
        raise RuntimeError("App not specified in the environ.")
        
    if config['virtual_env']:
        activate_this = os.path.join( config['virtual_env'], 'bin', 'activate_this.py' )
        execfile(activate_this, dict(__file__=activate_this))
    
    return config 
 def testFromFd(self):
     # Testing fromfd()
     if not hasattr(socket, "fromfd"):
         return # On Windows, this doesn't exist
     fd = self.cli_conn.fileno()
     sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
     msg = sock.recv(1024)
     self.assertEqual(msg, MSG)
Exemple #3
0
 def create_socket(self, address=('', 0), reusable=True, listen=500):
     if (self._pipe):
         self.send('socket', address=address, reusable=reusable, listen=listen)
         fd = tradesocket.recv_fd(self._unix_socket.fileno())
         if (fd < 0):
             raise RuntimeError("Error in tradesocket.recv_fd()")
         return socket.fromfd( fd, socket.AF_INET, socket.SOCK_STREAM )
     else:
         return create_socket(address)
Exemple #4
0
 def __init__(self, **kw):
     if not self.socket:
         self.socket = socket.socket()
         self.socket.bind( tuple(kw.get('address', self.address)) )
         self.socket.listen(500)
     elif isinstance(self.socket, int):
         self.socket = socket.fromfd( self.socket, socket.AF_INET, socket.SOCK_STREAM )
     
     kw['address'] = self.socket.getsockname()
     super(ProcessProxyService, self).__init__(**kw)
 def create_socket(self, address):
     self.send('socket', address=address)
     fd = tradesocket.recv_fd(self._unix_socket.fileno())
     if (fd == -1):
         raise RuntimeError("Error in recv_fd(), message length is negative.")
     if (fd == -2):
         raise RuntimeError("Error in recv_fd(), cmessage is null.")
     if (fd == -3):
         raise RuntimeError("Error in recv_fd(), cmessage is il-formed.")
     return socket.fromfd( fd, socket.AF_INET, socket.SOCK_STREAM )
Exemple #6
0
def get_test_suite_socket():
    global DAISY_TEST_SOCKET_FD_STR
    if DAISY_TEST_SOCKET_FD_STR in os.environ:
        fd = int(os.environ[DAISY_TEST_SOCKET_FD_STR])
        sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
        sock = socket.SocketType(_sock=sock)
        sock.listen(CONF.backlog)
        del os.environ[DAISY_TEST_SOCKET_FD_STR]
        os.close(fd)
        return sock
    return None
Exemple #7
0
def get_test_suite_socket():
    global GLANCE_TEST_SOCKET_FD_STR
    if GLANCE_TEST_SOCKET_FD_STR in os.environ:
        fd = int(os.environ[GLANCE_TEST_SOCKET_FD_STR])
        sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
        sock = socket.SocketType(_sock=sock)
        sock.listen(CONF.backlog)
        del os.environ[GLANCE_TEST_SOCKET_FD_STR]
        os.close(fd)
        return sock
    return None
Exemple #8
0
def get_test_suite_socket():
    global SEARCHLIGHT_TEST_SOCKET_FD_STR
    if SEARCHLIGHT_TEST_SOCKET_FD_STR in os.environ:
        fd = int(os.environ[SEARCHLIGHT_TEST_SOCKET_FD_STR])
        sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
        if six.PY2:
            sock = socket.SocketType(_sock=sock)
        sock.listen(CONF.api.backlog)
        del os.environ[SEARCHLIGHT_TEST_SOCKET_FD_STR]
        os.close(fd)
        return sock
    return None
Exemple #9
0
def get_test_suite_socket():
    global SEARCHLIGHT_TEST_SOCKET_FD_STR
    if SEARCHLIGHT_TEST_SOCKET_FD_STR in os.environ:
        fd = int(os.environ[SEARCHLIGHT_TEST_SOCKET_FD_STR])
        sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
        if six.PY2:
            sock = socket.SocketType(_sock=sock)
        sock.listen(CONF.api.backlog)
        del os.environ[SEARCHLIGHT_TEST_SOCKET_FD_STR]
        os.close(fd)
        return sock
    return None
Exemple #10
0
 def _nfq_worker(self):
     s = socket.fromfd(self.nfq.fd, socket.AF_UNIX, socket.SOCK_STREAM)
     try:
         while True:
             LOG.debug('NFQ Recv')
             nfad = s.recv(self.NFQ_SOCKET_BUFFER_SIZE)
             self.nfq.handle_packet(nfad)
         LOG.error('NFQ Worker loop leave!')
     finally:
         LOG.error('NFQ Worker closing socket!')
         s.close()
         self.nfq.close()
         sys.exit(1)
Exemple #11
0
 def setup_backend(cls, dct=None):
     """
     This is called by a backend (child) python process to setup the
     environment and create a simple object representing the service
     settings gleamed from the os.environ key SERVICE_JSON.
     """
     if dct is None:
         dct = fix_unicode_keys( json.loads(os.environ['SERVICE_JSON']) )
     
     dct['socket'] = socket.fromfd( dct['socket'], socket.AF_INET, socket.SOCK_STREAM )
     
     if "VIRTUAL_ENV" in os.environ:
         activate_this = os.path.join( os.environ['VIRTUAL_ENV'], 'bin', 'activate_this.py' )
         execfile(activate_this, dict(__file__=activate_this))
     
     return dct
Exemple #12
0
    def _sock_listen(self):
        address_str = self.container.config.get(WEB_SERVER_CONFIG_KEY, '')
        family = socket.AF_INET
        if address_str.startswith('fd://'):
            fd = int(address_str.split('://')[1])
            try:
                sock = socket.fromfd(fd, family, socket.SOCK_STREAM)
                if sys.platform[:3] != "win":
                    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                if hasattr(socket, 'SO_REUSEPORT'):
                    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
                return sock
            except socket.error:
                pass

        return eventlet.listen(self.bind_addr, family, backlog=2048)
if (args.output_tls == 'true' or args.output_tls == 'True' or args.output_tls == '1'):
    args.output_tls = True
else:
    args.output_tls = False

if os.access(args.key, os.R_OK) == False:
    print("Error: private key %s not readable" % args.key)
    sys.exit(1)

if os.access(args.cert, os.R_OK) == False:
    print("Error: certificate %s not readable" % args.cert)
    sys.exit(1)


# This allows our app to get into a network namespace other than the default.
# to do so, open /var/run/netns/<file>, and then have @ it with the fd using
# the setns(2) call. E.g. f=open('/var/run/netns/x'); setns(f)
prctl.cap_permitted.sys_admin = True
prctl.cap_effective.sys_admin = True

fd = int(1)
source = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
source.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

gp = eventlet.greenpool.GreenPool()
gp.spawn(route,source,gp,args)
gp.waitall()


Exemple #14
0
    sock = None

    if options.restart_args:
        restart_args = json.loads(options.restart_args)
        factory = restart_args['factory']
        factory_args = restart_args['factory_args']

        start_delay = restart_args.get('start_delay')
        if start_delay is not None:
            factory_args['start_delay'] = start_delay
            print "(%s) delaying startup by %s" % (os.getpid(), start_delay)
            time.sleep(start_delay)

        fd = restart_args.get('fd')
        if fd is not None:
            sock = socket.fromfd(restart_args['fd'], socket.AF_INET,
                                 socket.SOCK_STREAM)
            ## socket.fromfd doesn't result in a socket object that has the same fd.
            ## The old fd is still open however, so we close it so we don't leak.
            os.close(restart_args['fd'])
        return start_controller(sock, factory, factory_args)

    ## We're starting up for the first time.
    if options.daemonize:
        # Do the daemon dance. Note that this isn't what is considered good
        # daemonization, because frankly it's convenient to keep the file
        # descriptiors open (especially when there are prints scattered all
        # over the codebase.)
        # What we do instead is fork off, create a new session, fork again.
        # This leaves the process group in a state without a session
        # leader.
        pid = os.fork()
    sock = None

    if options.restart_args:
        restart_args = json.loads(options.restart_args)
        factory = restart_args['factory']
        factory_args = restart_args['factory_args']

        start_delay = restart_args.get('start_delay')
        if start_delay is not None:
            factory_args['start_delay'] = start_delay
            print "(%s) delaying startup by %s" % (os.getpid(), start_delay)
            time.sleep(start_delay)

        fd = restart_args.get('fd')
        if fd is not None:
            sock = socket.fromfd(restart_args['fd'], socket.AF_INET, socket.SOCK_STREAM)
            ## socket.fromfd doesn't result in a socket object that has the same fd.
            ## The old fd is still open however, so we close it so we don't leak.
            os.close(restart_args['fd'])
        return start_controller(sock, factory, factory_args)

    ## We're starting up for the first time.
    if options.daemonize:
        # Do the daemon dance. Note that this isn't what is considered good
        # daemonization, because frankly it's convenient to keep the file
        # descriptiors open (especially when there are prints scattered all
        # over the codebase.)
        # What we do instead is fork off, create a new session, fork again.
        # This leaves the process group in a state without a session
        # leader.
        pid = os.fork()
Exemple #16
0
args = parser.parse_args()


if (args.output_tls == 'true' or args.output_tls == 'True' or args.output_tls == '1'):
    args.output_tls = True
else:
    args.output_tls = False

if os.access(args.key, os.R_OK) == False:
    print("Error: private key %s not readable" % args.key)
    sys.exit(1)

if os.access(args.cert, os.R_OK) == False:
    print("Error: certificate %s not readable" % args.cert)
    sys.exit(1)


# This allows our app to get into a network namespace other than the default.
# to do so, open /var/run/netns/<file>, and then have @ it with the fd using
# the setns(2) call. E.g. f=open('/var/run/netns/x'); setns(f)
prctl.cap_permitted.sys_admin = True
prctl.cap_effective.sys_admin = True

fd = int(1)
source = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
source.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

gp = eventlet.greenpool.GreenPool()
gp.spawn(route,source,gp,args)
gp.waitall()