def _on_server(self, args): os.chdir(self.original_dir) info = get_info(os.path.abspath(args.path), args.format) self.logger.info( format_info( info, args.format, args.cols_width if args.cols_width else DEFAULT_COLS_WIDTH, _Dumper))
def _apitests(): # First check out if we have a process running on localhost:17010. # If we do, check out if it appears to belong to a server. # If it does, this is the server to test against. # Otherwise, the server's URL + credentials are read from environment variables. # If no env variables are present, API tests cannot run. conn_info = None server_path = None # We expect a process to be a Zato a server if it keeps open files containing these parts in their names. known_file_parts = ('logs/kvdb.log', 'logs/pubsub-overflown.log', 'logs/rbac.log') for item in psutil.net_connections(): if item.status == psutil.CONN_LISTEN and item.laddr[1] == 17010: proc = psutil.Process(pid=item.pid) known_found = 0 open_files = proc.open_files() for f in open_files: if any(part in f.path for part in known_file_parts): known_found += 1 if known_found == len(known_file_parts): server_path = open_files[0].path.split('/logs/')[0] break # We've got a path that for 99% is a server, but let's run zato info against it to be 100% sure. if server_path: try: info = get_info(server_path, None) except IOError: logger.warn('Path %s does not belong to a Zato server', server_path) else: if loads(info['component_details'] )['component'] == ZatoCommand.COMPONENTS.SERVER.code: conn_info = get_conn_info_from_path(server_path) # Still no connection info, try to obtain it from environment variables then. if not conn_info: conn_info = get_conn_info_from_env() # Ok, give up, we don't know how to connect to the target server if not conn_info: logger.warn( 'Cannot run API tests, could not obtain conn info from local path nor environment.' ) else: os.environ.update(**conn_info) apitest_cmd = 'apitest' tests_dir = os.path.join(curdir, 'apitest') run('{} run {}'.format(apitest_cmd, tests_dir))
def _apitests(): # First check out if we have a process running on localhost:17010. # If we do, check out if it appears to belong to a server. # If it does, this is the server to test against. # Otherwise, the server's URL + credentials are read from environment variables. # If no env variables are present, API tests cannot run. conn_info = None server_path = None # We expect a process to be a Zato a server if it keeps open files containing these parts in their names. known_file_parts = ('logs/kvdb.log', 'logs/pubsub-overflown.log', 'logs/rbac.log') for item in psutil.net_connections(): if item.status == psutil.CONN_LISTEN and item.laddr[1] == 17010: proc = psutil.Process(pid=item.pid) known_found = 0 open_files = proc.open_files() for f in open_files: if any(part in f.path for part in known_file_parts): known_found += 1 if known_found == len(known_file_parts): server_path = open_files[0].path.split('/logs/')[0] break # We've got a path that for 99% is a server, but let's run zato info against it to be 100% sure. if server_path: try: info = get_info(server_path, None) except IOError: logger.warn('Path %s does not belong to a Zato server', server_path) else: if loads(info['component_details'])['component'] == ZatoCommand.COMPONENTS.SERVER.code: conn_info = get_conn_info_from_path(server_path) # Still no connection info, try to obtain it from environment variables then. if not conn_info: conn_info = get_conn_info_from_env() # Ok, give up, we don't know how to connect to the target server if not conn_info: logger.warn('Cannot run API tests, could not obtain conn info from local path nor environment.') else: os.environ.update(**conn_info) apitest_cmd = 'apitest' tests_dir = os.path.join(curdir, 'apitest') run('{} run {}'.format(apitest_cmd, tests_dir))
def _on_server(self, args): # stdlib import os # yaml import yaml # Zato from zato.common.component_info import format_info, get_info class _Dumper(yaml.SafeDumper): def represent_none(self, data): return self.represent_scalar('tag:yaml.org,2002:null', '') os.chdir(self.original_dir) info = get_info(os.path.abspath(args.path), args.format) self.logger.info( format_info( info, args.format, args.cols_width if args.cols_width else DEFAULT_COLS_WIDTH, _Dumper))
def handle(self): self.response.content_type = 'application/json' self.response.payload.info = format_info(get_info(self.server.base_dir, INFO_FORMAT.JSON), INFO_FORMAT.JSON)
def ensure_no_pidfile(self, log_file_marker): pidfile = abspath(join(self.component_dir, 'pidfile')) # Pidfile exists .. if exists(pidfile): # .. but raise an error only if the PID it points to belongs # to an already running component. Otherwise, it must be a stale pidfile # that we can safely delete. pid = open(pidfile).read().strip() try: if pid: pid = int(pid) except ValueError: raise Exception( 'Could not parse pid value `{}` as an integer ({})'.format( pid, pidfile)) else: try: get_info(self.component_dir, INFO_FORMAT.DICT) except AccessDenied: # This could be another process /or/ it can be our own component started by another user, # so to be on the safe side, indicate an error instead of deleting the pidfile raise Exception( 'Access denied to PID `{}` found in `{}`'.format( pid, pidfile)) except NoSuchProcess: # This is fine, there is no process of that PID, # which means that this PID does not belong to our component # (because it doesn't belong to any process), so we may just delete this pidfile safely .. os.remove(pidfile) # .. but, if the component is load-balancer, we also need to delete its agent's pidfile. # The assumption is that if the load-balancer is not running then so isn't its agent. if log_file_marker == 'lb-agent': lb_agent_pidfile = abspath( join(self.component_dir, 'zato-lb-agent.pid')) os.remove(lb_agent_pidfile) else: # # This PID exists, but it still still possible that it belongs to another process # that took over a PID previously assigned to a Zato component, # in which case we can still delete the pidfile. # # We decide that a process is actually an already running Zato component if it has # opened log files that should belong that kind of component, as indicated by log_file_marker, # otherwise we assume this PID belongs to a completely different process and we can delete pidfile. # has_log = False has_lock = False log_path = abspath( join(self.component_dir, 'logs', '{}.log'.format(log_file_marker))) lock_path = abspath( join(self.component_dir, 'logs', '{}.lock'.format(log_file_marker))) if pid: for name in Process(pid).open_files(): if name.path == log_path: has_log = True elif name.path == lock_path: has_lock = True # Both files exist - this is our component and it's running so we cannot continue if has_log and has_lock: raise Exception( 'Cannot proceed, found pidfile `{}`'.format( pidfile)) # This must be an unrelated process, so we can delete pidfile .. os.remove(pidfile) # .. again, if the component is load-balancer, we also need to delete its agent's pidfile. # The assumption is that if the load-balancer is not running then so isn't its agent. if log_file_marker == 'lb-agent': lb_agent_pidfile = abspath( join(self.component_dir, 'zato-lb-agent.pid')) os.remove(lb_agent_pidfile) if self.show_output: self.logger.info('No such pidfile `%s`, OK', pidfile)
def handle(self): self.response.content_type = 'application/json' self.response.payload.info = format_info( get_info(self.server.base_dir, INFO_FORMAT.JSON), INFO_FORMAT.JSON)
def _on_server(self, args): os.chdir(self.original_dir) info = get_info(os.path.abspath(args.path), args.format) self.logger.info(format_info(info, args.format, args.cols_width if args.cols_width else DEFAULT_COLS_WIDTH, _Dumper))