def _create_host(_host): if not auth: raise exceptions.ConfigurationError( 'Authentication not provided ' 'for host: {0}'.format(_host)) if not port: raise exceptions.ConfigurationError( 'Port not provided for host: {0}'.format(_host)) return { 'auth': auth, 'port': port, 'host': _host, 'public_address': public_address }
def setup(): global app, backend # initialize flask application app = Flask(__name__) Api(app) # load application configuration file is exists config_file_path = os.environ.get('HOST_POOL_SERVICE_CONFIG_PATH') if config_file_path: utils.write_to_log('service.setup', "config_file_path {0}".format(config_file_path)) with open(config_file_path) as f: yaml_conf = yaml.load(f.read()) config.configure(yaml_conf) utils.write_to_log('service.setup', "config_file_path {0} after configure".format(config_file_path)) else: utils.write_to_log('service.setup', "Failed loading application: " \ "HOST_POOL_SERVICE_CONFIG_PATH environment variable is not defined. " \ "Use this variable to point to the application configuration file ") raise exceptions.ConfigurationError( 'Failed loading application: ' 'HOST_POOL_SERVICE_CONFIG_PATH environment ' 'variable is not defined. Use this variable to ' 'point to the application configuration file ') # initialize application backend backend = rest_backend.RestBackend(pool=config.get().pool)
def _get_subnet_and_mask(ip_range): regex = re.compile(CIDR_REGEX) result = regex.findall(ip_range) if len(result) != 1: raise exceptions.ConfigurationError( '{0} is not a legal CIDR notation'.format(ip_range)) subnet, mask = ip_range.split('/') return subnet, mask
def _load(pool): if isinstance(pool, str): with open(pool, 'r') as config_file: return yaml.load(config_file) elif isinstance(pool, dict): return pool else: raise exceptions.ConfigurationError('Unexpected configuration ' 'type: {0}'.format(type(pool)))
def _get_auth(self, host): default_auth = self.default.get('auth', {}) auth = copy.deepcopy(default_auth) auth.update(host.get('auth', {})) keyfile = auth.get('keyfile') if keyfile and not os.access(keyfile, os.R_OK): raise exceptions.ConfigurationError( 'Key file {0} does not exist or does not have ' 'the proper permissions'.format(keyfile)) return auth
def _get_auth(self, host): utils.write_to_log('get_auth', "Starting...") default_auth = self.default.get('auth', {}) auth = copy.deepcopy(default_auth) auth.update(host.get('auth', {})) keyfile = auth.get('keyfile') utils.write_to_log('get_auth', "keyfile is {0}".format(keyfile)) if keyfile and not os.access(keyfile, os.R_OK): utils.write_to_log('get_auth', "keyfile {0} no access".format(keyfile)) raise exceptions.ConfigurationError( 'Key file {0} does not exist or does not have ' 'the proper permissions'.format(keyfile)) return auth
def _create_host(_host): if not auth: utils.write_to_log('_create_host', 'Authentication not provided for host: {0}'.format(_host)) raise exceptions.ConfigurationError( 'Authentication not provided ' 'for host: {0}'.format(_host)) if not port: utils.write_to_log('_create_host', 'Port not provided for host: {0}'.format(_host)) raise exceptions.ConfigurationError( 'Port not provided for host: {0}' .format(_host)) utils.write_to_log('_create_host', " auth {0}".format(str(auth))) utils.write_to_log('_create_host', " port {0}".format(port)) utils.write_to_log('_create_host', " _host {0}".format(str(_host))) utils.write_to_log('_create_host', " public address {0}".format(public_address)) return { 'auth': auth, 'port': port, 'host': _host, 'public_address': public_address }
def load(self): def _create_host(_host): if not auth: utils.write_to_log('_create_host', 'Authentication not provided for host: {0}'.format(_host)) raise exceptions.ConfigurationError( 'Authentication not provided ' 'for host: {0}'.format(_host)) if not port: utils.write_to_log('_create_host', 'Port not provided for host: {0}'.format(_host)) raise exceptions.ConfigurationError( 'Port not provided for host: {0}' .format(_host)) utils.write_to_log('_create_host', " auth {0}".format(str(auth))) utils.write_to_log('_create_host', " port {0}".format(port)) utils.write_to_log('_create_host', " _host {0}".format(str(_host))) utils.write_to_log('_create_host', " public address {0}".format(public_address)) return { 'auth': auth, 'port': port, 'host': _host, 'public_address': public_address } for host in self.hosts: port = self._get_port(host) auth = self._get_auth(host) public_address = host.get('public_address') if 'host' in host: utils.write_to_log('_create_host', "'host' is in host") # an explicit address is configured for this host yield _create_host(host['host']) elif 'ip_range' in host: # ip range was specified. in this case we create a host # dictionary for each ip address separately. subnet, mask = _get_subnet_and_mask(host['ip_range']) for host_ip in _get_subnet_hosts(subnet, mask): yield _create_host(host_ip) else: utils.write_to_log('_create_host', "A host must define either the 'host' or the 'ip_range' key") raise exceptions.ConfigurationError( "A host must define either the " "'host' or the 'ip_range' key")
def _load(pool): if isinstance(pool, str): utils.write_to_log('_load', "isinstance") with open(pool, 'r') as config_file: utils.write_to_log('_load', "open pool {0} config_file".format(pool)) return yaml.load(config_file) elif isinstance(pool, dict): utils.write_to_log('_load', "isinstance pool dict") return pool else: utils.write_to_log('_load', "Unexpected pool configuration type: '{0}'".format(type(pool))) raise exceptions.ConfigurationError( 'Unexpected pool configuration ' 'type: {0}'.format(type(pool)))
def write_to_log(caller_string, message_text, log_file=None): try: if log_file is None: log_file = get_log_filename() lock = LockFile(log_file) lock.acquire() current_message = "{0}: {1}\n".format(caller_string, message_text) with open(log_file, 'a') as f: f.write(current_message) f.close() except: err_message = "{0}: Failures while locking or using {1}".format( caller_string, log_file) lock.release() raise exceptions.ConfigurationError(err_message) lock.release()
def get_log_file_content(log_file=None): try: if log_file is None: log_file = get_log_filename() lock = LockFile(log_file) lock.acquire() with open(log_file, 'r') as f: lines = f.read().splitlines() f.close() lock.release() file_content = "" for line in lines: file_content += "{0}<br/>".format(line) return file_content except: err_message = "Failures while locking or using {0}".format(log_file) lock.release() raise exceptions.ConfigurationError(err_message)
def setup(): global app, backend # initialize flask application app = Flask(__name__) Api(app) # load application configuration file is exists config_file_path = os.environ.get('HOST_POOL_SERVICE_CONFIG_PATH') if config_file_path: with open(config_file_path) as f: yaml_conf = yaml.load(f.read()) config.configure(yaml_conf) else: raise exceptions.ConfigurationError( 'Failed loading application: ' 'HOST_POOL_SERVICE_CONFIG_PATH environment ' 'variable is not defined. Use this variable to ' 'point to the application configuration file ') # initialize application backend backend = rest_backend.RestBackend(pool=config.get().pool)
def _validate(config): if 'hosts' not in config: raise exceptions.ConfigurationError('Pool configuration ' 'is missing a hosts section')
def _validate(config): if 'hosts' not in config: utils.write_to_log('_validate', "Pool configuration is missing a hosts section") raise exceptions.ConfigurationError( 'Pool configuration ' 'is missing a hosts section')