Advertisement
Guest User

asgard.py

a guest
Nov 24th, 2011
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. #!/usr/bin/local/python3
  2. class Asgard:
  3.     children = []
  4. #    config = None
  5.     abs_path = None
  6.     socket = None
  7.     process_queue = {}
  8.    
  9.     @staticmethod
  10.     def initialize():
  11.         Asgard.abs_path = __file__[:-len(os.path.basename(__file__))]
  12.         os.chdir(Asgard.abs_path)
  13.         # Set the cwd to [...]/Asgard/
  14.         file = open(Asgard.abs_path + "config/asgard.yaml", "r")
  15.         Asgard.config = yaml.load(file)
  16.         Asgard.socket_start()
  17.         Asgard.spawn_children()
  18.    
  19.     @staticmethod
  20.     def spawn_children():
  21.         from library.childcontainer import Child
  22.         Asgard.socket.settimeout(Asgard.config['asgard']['socket']['accept_timeout'])
  23.         for name, child in Asgard.config['asgard']['resources'].items():
  24.             child['name'] = name
  25.             Asgard.spawn_child(child)
  26.         Asgard.socket.listen(5)
  27.         try:
  28.             while True:
  29.                 client = Asgard.socket.accept()[0] # Disregard the address,
  30.                 Child(client)                      # It will be b'' on UNIX sockets
  31.         except socket.error:
  32.             print("SOCKET ERROR") # do verify
  33.            
  34.        
  35.    
  36.     @staticmethod
  37.     def spawn_child(child):
  38.         process = Popen(
  39.               [Asgard.abs_path + child['bin'].strip('/'),
  40.               Asgard.config['asgard']['socket']['domain'],
  41.               Asgard.config['asgard']['socket']['family'],
  42.               Asgard.config['asgard']['socket']['type']]
  43.         )
  44.         Asgard.process_queue[process.pid] = process
  45.    
  46.     @staticmethod
  47.     def socket_start():
  48.         Asgard.socket = socket.socket(
  49.             getattr(socket, Asgard.config['asgard']['socket']['family']),
  50.             getattr(socket, Asgard.config['asgard']['socket']['type']))        
  51.         Asgard.socket.bind(Asgard.abs_path + Asgard.config['asgard']['socket']['domain'])
  52.         Asgard.socket.listen(1)
  53.  
  54. if __name__ == "__main__":
  55.     import socket, os, yaml
  56.     from select import epoll, EPOLLIN #@UnresolvedImport
  57.     from subprocess import Popen
  58.     Asgard.initialize()
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement