<s> import sys <EOL> from adapter import DockerAdapter <EOL> class HubController : <EOL> def __init__ ( self ) : <EOL> self . hub = GridHub ( ) <EOL> @ staticmethod <EOL> def is_running ( ) : <EOL> hub = GridHub ( ) <EOL> return hub . is_running ( ) <EOL> @ staticmethod <EOL> def start ( ) : <EOL> """<STR_LIT>""" <EOL> hub = GridHub ( ) <EOL> try : <EOL> hub . start ( ) <EOL> return True <EOL> except Exception : <EOL> return False <EOL> @ staticmethod <EOL> def is_valid_browser ( browser ) : <EOL> if browser in GridHub . VALID_BROWSERS : <EOL> return True <EOL> return False <EOL> @ staticmethod <EOL> def add ( browser ) : <EOL> """<STR_LIT>""" <EOL> hub = GridHub ( ) <EOL> return hub . add_node ( browser ) <EOL> @ staticmethod <EOL> def get_status ( ) : <EOL> hub = GridHub ( ) <EOL> ff_count = <NUM_LIT:0> <EOL> ph_count = <NUM_LIT:0> <EOL> ch_count = <NUM_LIT:0> <EOL> nodes = hub . get_nodes ( ) <EOL> for node in nodes : <EOL> if "<STR_LIT>" in node [ '<STR_LIT>' ] : <EOL> ff_count += <NUM_LIT:1> <EOL> elif "<STR_LIT>" in node [ '<STR_LIT>' ] : <EOL> ph_count += <NUM_LIT:1> <EOL> elif "<STR_LIT>" in node [ '<STR_LIT>' ] : <EOL> ch_count += <NUM_LIT:1> <EOL> status = { <EOL> "<STR_LIT>" : hub . get_ip ( ) , <EOL> "<STR_LIT>" : ff_count , <EOL> "<STR_LIT>" : ph_count , <EOL> "<STR_LIT>" : ch_count <EOL> } <EOL> return status <EOL> @ staticmethod <EOL> def restart_nodes ( browser = None ) : <EOL> hub = GridHub ( ) <EOL> return hub . restart_nodes ( browser ) <EOL> @ staticmethod <EOL> def stop_nodes ( ) : <EOL> hub = GridHub ( ) <EOL> return hub . stop_nodes ( ) <EOL> @ staticmethod <EOL> def shutdown ( ) : <EOL> hub = GridHub ( ) <EOL> hub . shutdown ( ) <EOL> class GridHub : <EOL> VALID_BROWSERS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> self . adapter = DockerAdapter ( ) <EOL> def get_container_info ( self ) : <EOL> container = self . adapter . find_container ( '<STR_LIT>' ) <EOL> if not container : <EOL> return False <EOL> container_info = self . adapter . inspect ( container [ '<STR_LIT>' ] ) <EOL> container = dict ( container . items ( ) + container_info . items ( ) ) <EOL> return container <EOL> def get_nodes ( self ) : <EOL> nodes = [ ] <EOL> containers = self . adapter . client . containers ( ) <EOL> for container in containers : <EOL> if "<STR_LIT>" in container [ '<STR_LIT>' ] : <EOL> nodes . append ( container ) <EOL> return nodes <EOL> def is_running ( self ) : <EOL> container = self . get_container_info ( ) <EOL> if not container : <EOL> return False <EOL> return "<STR_LIT>" in container [ '<STR_LIT>' ] <EOL> def start ( self ) : <EOL> response = self . adapter . client . create_container ( '<STR_LIT>' , ports = { "<STR_LIT>" : { } } ) <EOL> if response [ '<STR_LIT>' ] : <EOL> self . adapter . client . start ( response , None , { '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> def get_ip ( self ) : <EOL> container = self . get_container_info ( ) <EOL> return container [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def add_node ( self , browser ) : <EOL> if not browser in self . VALID_BROWSERS : <EOL> return False <EOL> ip = self . get_ip ( ) <EOL> response = self . adapter . client . create_container ( '<STR_LIT>' + browser + '<STR_LIT>' , [ ] , environment = { "<STR_LIT>" : ip } ) <EOL> if response [ '<STR_LIT>' ] : <EOL> self . adapter . client . start ( response ) <EOL> return True <EOL> def restart_nodes ( self , browser = None ) : <EOL> containers = self . get_nodes ( ) <EOL> nodes_to_stop = [ ] <EOL> for container in containers : <EOL> if browser is not None and browser not in container [ '<STR_LIT>' ] : <EOL> continue <EOL> nodes_to_stop . append ( container ) <EOL> if len ( nodes_to_stop ) == <NUM_LIT:0> : <EOL> return False <EOL> for container in nodes_to_stop : <EOL> self . adapter . restart ( container ) <EOL> return True <EOL> def stop_nodes ( self , by_browser = None ) : <EOL> if by_browser is not None and not by_browser in self . VALID_BROWSERS : <EOL> return False <EOL> browser = '<STR_LIT>' <EOL> if by_browser in self . VALID_BROWSERS : <EOL> browser = by_browser + '<STR_LIT:->' <EOL> containers = self . adapter . client . containers ( ) <EOL> for container in containers : <EOL> if browser + "<STR_LIT>" in container [ '<STR_LIT>' ] : <EOL> self . adapter . client . stop ( container [ '<STR_LIT>' ] ) <EOL> self . adapter . client . remove_container ( container [ '<STR_LIT>' ] ) <EOL> return True <EOL> def shutdown ( self ) : <EOL> self . stop_nodes ( ) <EOL> container = self . adapter . find_container ( '<STR_LIT>' ) <EOL> if not container : <EOL> return False <EOL> self . adapter . client . stop ( container ) <EOL> self . adapter . client . remove_container ( container ) </s>
