def _get_validator_pid(self, node_name): pid_file = os.path.join(self._state_dir, "{}.pid".format(node_name)) max_attempts = 3 attempts = 0 while attempts < max_attempts: if os.path.exists(pid_file): with open(pid_file, 'r') as fd: try: return int(fd.readline()) except ValueError: if attempts >= max_attempts: raise ManagementError( "invalid pid file: {}".format(pid_file)) time.sleep(1) attempts = attempts + 1 raise ManagementError("no such file: {}".format(pid_file))
def is_running(self, node_name): pid_file = os.path.join(self._state_dir, "{}.pid".format(node_name)) if os.path.exists(pid_file): with open(pid_file, 'r') as fd: try: pid = int(fd.readline()) except ValueError: raise ManagementError( "invalid pid file: {}".format(pid_file)) if psutil.pid_exists(pid): return True return False
def get_executable_script(script_name): ''' Searches PATH environmental variable to find the information needed to execute a script. Args: script_name: the name of the 'executable' script Returns: ret_val (list<str>): A list containing the python executable, and the full path to the script. Includes sys.executable, because certain operating systems cannot execute scripts directly. ''' ret_val = None if 'PATH' not in os.environ: raise ManagementError('no PATH environmental variable') search_path = os.environ['PATH'] for directory in search_path.split(os.pathsep): if os.path.exists(os.path.join(directory, script_name)): ret_val = os.path.join(directory, script_name) break if ret_val is not None: ret_val = [sys.executable, ret_val] else: raise ManagementError("could not locate %s" % (script_name)) return ret_val