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 object_url(*args): # Prefer an environmental variable for getting the endpoint if available. url = os.environ.get('HIL_ENDPOINT') if url is None: config.setup() url = cfg.get('client', 'endpoint') for arg in args: url += '/' + urllib.quote(arg, '') return url
def object_url(*args): """Return a url with a prefix of the HIL endpoint, and args as the (remaining) segments of the path. TODO: This function's name is no longer very accurate. As soon as it is safe, we should change it to something more generic. """ # Prefer an environmental variable for getting the endpoint if available. url = os.environ.get('HIL_ENDPOINT') if url is None: config.setup() url = config.cfg.get('client', 'endpoint') for arg in args: url += '/' + urllib.quote(arg, '') return url
def object_url(*args): """Return a url with a prefix of the HIL endpoint, and args as the (remaining) segments of the path. TODO: This function's name is no longer very accurate. As soon as it is safe, we should change it to something more generic. """ # Prefer an environmental variable for getting the endpoint if available. url = os.environ.get('HIL_ENDPOINT') if url is None: config.setup() url = cfg.get('client', 'endpoint') for arg in args: url += '/' + urllib.quote(arg, '') return url
def create_admin_user(username, password): """Create an admin user. Only valid for the database auth backend. This must be run on the HIL API server, with access to hil.cfg and the database. It will create an user named <username> with password <password>, who will have administrator privileges. This command should only be used for bootstrapping the system; once you have an initial admin, you can (and should) create additional users via the API. """ config.setup() if not config.cfg.has_option('extensions', 'hil.ext.auth.database'): sys.exit("'make_inital_admin' is only valid with the database auth" " backend.") from hil import model from hil.model import db from hil.ext.auth.database import User model.init_db() db.session.add(User(label=username, password=password, is_admin=True)) db.session.commit()
def main(): """Entry point to the CLI. There is a script located at ${source_tree}/scripts/hil, which invokes this function. """ ensure_not_root() config.setup() if len(sys.argv) < 2 or sys.argv[1] not in command_dict: # Display usage for all commands help() sys.exit(1) else: setup_http_client() try: command_dict[sys.argv[1]](*sys.argv[2:]) except FailedAPICallException as e: sys.exit('Error: %s\n' % e.message) except InvalidAPIArgumentsException as e: sys.exit('Error: %s\n' % e.message) except Exception as e: sys.exit('Unexpected error: %s\n' % e.message)
def main(): """Entrypoint for the hil-admin command.""" ensure_not_root() config.setup() model.init_db() manager.run()