def _maintain(project, node, node_label): """Helper function to execute maintenance tasks. Powers off the node, checks for the existence of maintenance pool config options, and posts to the maintenance URL if they exist.""" logger = logging.getLogger(__name__) if (cfg.has_option('maintenance', 'maintenance_project') and cfg.has_option('maintenance', 'url')): maintenance_proj = get_or_404( model.Project, cfg.get('maintenance', 'maintenance_project')) if (project == maintenance_proj): # Already in maintenance pool return elif (cfg.has_option('maintenance', 'maintenance_project')): raise errors.NotFoundError("Maintenance URL not in hil.cfg.") elif (cfg.has_option('maintenance', 'url')): raise errors.NotFoundError("Maintenance project not in hil.cfg.") else: return if (cfg.has_option('maintenance', 'shutdown')): node.obm.power_off() maintenance_proj.nodes.append(node) url = cfg.get('maintenance', 'url') payload = json.dumps({'node': node_label}) try: response = requests.post(url, headers={'Content-Type': 'application/json'}, data=payload) except requests.ConnectionError: logger.warn('POST to maintenance service' ' failed: connection failed') if (not 200 <= response < 300): logger.warn('POST to maintenance service' ' failed with response: %s', response.text)
def serve_networks(): """Start the HIL networking server""" from hil import model, deferred from time import sleep config.setup() server.init() server.register_drivers() server.validate_state() model.init_db() migrations.check_db_schema() # Check if config contains usable sleep_time if (cfg.has_section('network-daemon') and cfg.has_option('network-daemon', 'sleep_time')): try: sleep_time = cfg.getfloat('network-daemon', 'sleep_time') except (ValueError): sys.exit("Error: sleep_time set to non-float value") if sleep_time <= 0 or sleep_time >= 3600: sys.exit("Error: sleep_time not within bounds " "0 < sleep_time < 3600") if sleep_time > 60: logger.warn('sleep_time greater than 1 minute.') else: sleep_time = 2 while True: # Empty the journal until it's empty; then delay so we don't tight # loop. while deferred.apply_networking(): pass sleep(sleep_time)
def serve(port): """Run a development api server. Don't use this in production.""" try: port = schema.And( schema.Use(int), lambda n: MIN_PORT_NUMBER <= n <= MAX_PORT_NUMBER).validate(port) except schema.SchemaError: raise InvalidAPIArgumentsException( 'Error: Invaid port. Must be in the range 1-65535.') except Exception as e: sys.exit('Unxpected Error!!! \n %s' % e) """Start the HIL API server""" config.setup() if cfg.has_option('devel', 'debug'): debug = cfg.getboolean('devel', 'debug') else: debug = False # We need to import api here so that the functions within it get registered # (via `rest_call`), though we don't use it directly: # pylint: disable=unused-variable from hil import api, rest server.init() migrations.check_db_schema() server.stop_orphan_consoles() rest.serve(port, debug=debug)
def should_save(switch_obj): """checks the config file to see if switch should save or not""" switch_ext = switch_obj.__class__.__module__ if cfg.has_option(switch_ext, 'save'): if not cfg.getboolean(switch_ext, 'save'): return False return True
def should_save(switch_obj): """checks the config file to see if switch should save or not""" switch_ext = switch_obj.__class__.__module__ if cfg.has_option(switch_ext, 'save'): if not cfg.getboolean(switch_ext, 'save'): return False return True
def _should_save(self, switch_type): """checks the config file to see if switch should save or not""" switch_ext = 'hil.ext.switches.' + switch_type if cfg.has_option(switch_ext, 'save'): if not cfg.getboolean(switch_ext, 'save'): return False return True
def _should_save(self, switch_type): """checks the config file to see if switch should save or not""" switch_ext = 'hil.ext.switches.' + switch_type if cfg.has_option(switch_ext, 'save'): if not cfg.getboolean(switch_ext, 'save'): return False return True
def init_auth(): ok = auth.get_auth_backend().authenticate() if cfg.has_option('auth', 'require_authentication'): require_auth = cfg.getboolean('auth', 'require_authentication') else: require_auth = True if not ok and require_auth: raise AuthorizationError("Authentication failed. Authentication " "is required to use this service.")
def _maintain(project, node, node_label): """Helper function to execute maintenance tasks. Powers off the node, checks for the existence of maintenance pool config options, and posts to the maintenance URL if they exist.""" logger = logging.getLogger(__name__) if (cfg.has_option('maintenance', 'maintenance_project') and cfg.has_option('maintenance', 'url')): maintenance_proj = get_or_404( model.Project, cfg.get('maintenance', 'maintenance_project') ) if (project == maintenance_proj): # Already in maintenance pool return elif (cfg.has_option('maintenance', 'maintenance_project')): raise errors.NotFoundError("Maintenance URL not in hil.cfg.") elif (cfg.has_option('maintenance', 'url')): raise errors.NotFoundError("Maintenance project not in hil.cfg.") else: return if (cfg.has_option('maintenance', 'shutdown')): node.obm.power_off() maintenance_proj.nodes.append(node) url = cfg.get('maintenance', 'url') payload = json.dumps({'node': node_label}) try: response = requests.post(url, headers={'Content-Type': 'application/json'}, data=payload) except requests.ConnectionError: logger.warn('POST to maintenance service' ' failed: connection failed') if (not 200 <= response < 300): logger.warn('POST to maintenance service' ' failed with response: %s', response.text)
def init_auth(): """Process authentication. This invokes the auth backend. If HIL is configured to *require* authentication, and authentication fails, it raises an AuthorizationError. """ ok = auth.get_auth_backend().authenticate() if cfg.has_option('auth', 'require_authentication'): require_auth = cfg.getboolean('auth', 'require_authentication') else: require_auth = True if not ok and require_auth: raise AuthorizationError("Authentication failed. Authentication " "is required to use this service.")
def init_auth(): """Process authentication. This invokes the auth backend. If HIL is configured to *require* authentication, and authentication fails, it raises an AuthorizationError. """ ok = auth.get_auth_backend().authenticate() if cfg.has_option('auth', 'require_authentication'): require_auth = cfg.getboolean('auth', 'require_authentication') else: require_auth = True if not ok and require_auth: raise AuthorizationError("Authentication failed. Authentication " "is required to use this service.")