def __init__(self, serviceToPidDict, pidPathVars, globalConfig, servicesToLinuxUser): self.serToPidDict = serviceToPidDict.copy() self.pidPathVars = pidPathVars self.pidPathes = [] self.sh = shellRunner() self.pidFilesDict = {} self.globalConfig = globalConfig self.servicesToLinuxUser = servicesToLinuxUser self.fillDirValues() for pidPath in self.pidPathes: self.listFiles(pidPath) for service, pid in self.serToPidDict.items(): if self.servicesToLinuxUser.has_key(service): linuxUserKey = self.servicesToLinuxUser[service] if self.globalConfig.has_key(linuxUserKey): self.serToPidDict[service] = string.replace( pid, self.USER_PATTERN, self.globalConfig[linuxUserKey]) else: if self.USER_PATTERN in pid: logger.error( 'There is no linux user mapping for component: ' + service) if StatusCheck.firstInit: logger.info('Service to pid dictionary: ' + str(self.serToPidDict)) StatusCheck.firstInit = False else: logger.debug('Service to pid dictionary: ' + str(self.serToPidDict))
def _check_existence(self, name, context): cmd = CHECK_CMD[context.log_output] + [name] runner = shellRunner() res = runner.run(cmd) if name in res['output']: return True return False
def _check_existence(self, name): cmd = CHECK_CMD[self.get_logoutput()] + [name] runner = shellRunner() res = runner.run(cmd) if name in res['output']: return True return False
def stop_agent(): # stop existing Ambari agent pid = -1 runner = shellRunner() try: with open(ProcessHelper.pidfile, 'r') as f: pid = f.read() pid = int(pid) runner.run([AMBARI_SUDO_BINARY, 'kill', '-15', str(pid)]) for i in range(GRACEFUL_STOP_TRIES): result = runner.run([AMBARI_SUDO_BINARY, 'kill', '-0', str(pid)]) if result['exitCode'] != 0: logger.info("Agent died gracefully, exiting.") sys.exit(0) time.sleep(GRACEFUL_STOP_TRIES_SLEEP) logger.info("Agent not going to die gracefully, going to execute kill -9") raise Exception("Agent is running") except Exception, err: #raise if pid == -1: print ("Agent process is not running") else: res = runner.run([AMBARI_SUDO_BINARY, 'kill', '-9', str(pid)]) if res['exitCode'] != 0: raise Exception("Error while performing agent stop. " + res['error'] + res['output']) sys.exit(0)
def __init__(self, serviceToPidDict, pidPathVars, globalConfig, servicesToLinuxUser): self.serToPidDict = serviceToPidDict.copy() self.pidPathVars = pidPathVars self.pidPathes = [] self.sh = shellRunner() self.pidFilesDict = {} self.globalConfig = globalConfig self.servicesToLinuxUser = servicesToLinuxUser self.fillDirValues() for pidPath in self.pidPathes: self.listFiles(pidPath) for service, pid in self.serToPidDict.items(): if self.servicesToLinuxUser.has_key(service): linuxUserKey = self.servicesToLinuxUser[service] if self.globalConfig.has_key(linuxUserKey): self.serToPidDict[service] = string.replace(pid, self.USER_PATTERN, self.globalConfig[linuxUserKey]) else: if self.USER_PATTERN in pid: logger.error('There is no linux user mapping for component: ' + service) if StatusCheck.firstInit: logger.info('Service to pid dictionary: ' + str(self.serToPidDict)) StatusCheck.firstInit = False else: logger.debug('Service to pid dictionary: ' + str(self.serToPidDict))
def check_sudo(): # don't need to check sudo for root. if os.geteuid() == 0: return runner = shellRunner() test_command = [AMBARI_SUDO_BINARY, '/usr/bin/test', '/'] test_command_str = ' '.join(test_command) start_time = time.time() res = runner.run(test_command) end_time = time.time() run_time = end_time - start_time if res['exitCode'] != 0: raise Exception("Please check your sudo configurations.\n" + test_command_str + " failed with " + res['error'] + res['output']) # bad sudo configurations if run_time > 2: logger.warn(( "Sudo commands on this host are running slowly ('{0}' took {1} seconds).\n" + "This will create a significant slow down for ambari-agent service tasks." ).format(test_command_str, run_time))
def stop_agent(): # stop existing Ambari agent pid = -1 runner = shellRunner() try: with open(agent_pidfile, 'r') as f: pid = f.read() pid = int(pid) runner.run([AMBARI_SUDO_BINARY, 'kill', '-15', str(pid)]) for i in range(GRACEFUL_STOP_TRIES): result = runner.run([AMBARI_SUDO_BINARY, 'kill', '-0', str(pid)]) if result['exitCode'] != 0: logger.info("Agent died gracefully, exiting.") sys.exit(0) time.sleep(GRACEFUL_STOP_TRIES_SLEEP) logger.info( "Agent not going to die gracefully, going to execute kill -9") raise Exception("Agent is running") except Exception, err: #raise if pid == -1: print("Agent process is not running") else: res = runner.run([AMBARI_SUDO_BINARY, 'kill', '-9', str(pid)]) if res['exitCode'] != 0: raise Exception("Error while performing agent stop. " + res['error'] + res['output']) else: logger.info("Agent stopped successfully by kill -9, exiting.") sys.exit(0)
def getUptimeSeconds(self): try: runner = shellRunner() result = runner.runPowershell(script_block=FacterWindows.GET_UPTIME_CMD).output.replace('\n', '').replace('\r', '') return int(result) except: log.warn("Can not get SwapFree") return 0
def getSwapSize(self): try: runner = shellRunner() result = runner.runPowershell(script_block=FacterWindows.GET_PAGE_FILE_INFO).output.split(" ")[0].replace('\n', '').replace( '\r', '') return result except: log.warn("Can not get SwapFree") return 0
def test_shellRunner_run(self, getpwnamMock): sh = shellRunner() result = sh.run(['echo']) self.assertEquals(result['exitCode'], 0) self.assertEquals(result['error'], '') getpwnamMock.return_value = [os.getuid(), os.getuid(), os.getuid()] result = sh.run(['echo'], 'non_exist_user_name') self.assertEquals(result['exitCode'], 0) self.assertEquals(result['error'], '')
def getMemorySize(self): try: runner = shellRunner() result = runner.runPowershell(script_block=FacterWindows.GET_MEMORY_CMD).output.split(" ")[-1].replace('\n', '').replace( '\r', '') return result except: log.warn("Can not get MemorySize") return 0
def remove_package(self, name): if self._check_existence(name): cmd = REMOVE_CMD[self.get_logoutput()] + [name] cmdString = " ".join(cmd) Logger.info("Removing package %s ('%s')" % (name, " ".join(cmd))) runner = shellRunner() res = runner.run(cmd) if res['exitCode'] != 0: raise Exception("Error while upgrading choco package " + name + ". " + res['error'] + res['output']) else: Logger.info("Skipping removal of non-existing package %s" % (name))
def remove_package(self, name, ignore_dependencies = False): if self._check_existence(name): cmd = REMOVE_CMD[self.get_logoutput()] + [name] cmdString = " ".join(cmd) Logger.info("Removing package %s ('%s')" % (name, " ".join(cmd))) runner = shellRunner() res = runner.run(cmd) if res['exitCode'] != 0: raise Exception("Error while upgrading choco package " + name + ". " + res['error'] + res['output']) else: Logger.info("Skipping removal of non-existing package %s" % (name))
def upgrade_package(self, name, use_repos=[], skip_repos=[]): cmd = UPGRADE_CMD[self.get_logoutput()] if use_repos: enable_repo_option = '-s' + ",".join(use_repos) cmd = cmd + [enable_repo_option] cmd = cmd + [name] cmdString = " ".join(cmd) Logger.info("Upgrading package %s ('%s')" % (name, cmdString)) runner = shellRunner() res = runner.run(cmd) if res['exitCode'] != 0: raise Exception("Error while upgrading choco package " + name + ". " + res['error'] + res['output'])
def on_failure(self, pythonCommand, result): """ Log some useful information after task failure. """ logger.info("Command " + pprint.pformat(pythonCommand) + " failed with exitcode=" + str(result['exitcode'])) cmd_list = ["ps faux", "netstat -tulpn"] shell_runner = shellRunner() for cmd in cmd_list: ret = shell_runner.run(cmd) logger.info("Command '{0}' returned {1}. {2}{3}".format( cmd, ret["exitCode"], ret["error"], ret["output"]))
def on_failure(self, pythonCommand, result): """ Log some useful information after task failure. """ logger.info("Command " + pprint.pformat(pythonCommand) + " failed with exitcode=" + str(result['exitcode'])) if OSCheck.is_windows_family(): cmd_list = ["WMIC path win32_process get Caption,Processid,Commandline", "netstat -an"] else: cmd_list = ["ps faux", "netstat -tulpn"] shell_runner = shellRunner() for cmd in cmd_list: ret = shell_runner.run(cmd) logger.info("Command '{0}' returned {1}. {2}{3}".format(cmd, ret["exitCode"], ret["error"], ret["output"]))
def install_package(self, name, use_repos=[], skip_repos=[]): if not self._check_existence(name) or use_repos: cmd = INSTALL_CMD[self.get_logoutput()] if use_repos: enable_repo_option = '-s' + ",".join(use_repos) cmd = cmd + [enable_repo_option] cmd = cmd + [name] cmdString = " ".join(cmd) Logger.info("Installing package %s ('%s')" % (name, cmdString)) runner = shellRunner() res = runner.run(cmd) if res['exitCode'] != 0: raise Exception("Error while installing choco package " + name + ". " + res['error'] + res['output']) else: Logger.info("Skipping installation of existing package %s" % (name))
def osdisks(): mounts = [] runner = shellRunner() command_result = runner.runPowershell(script_block=Hardware.WINDOWS_GET_DRIVES_CMD) if command_result.exitCode != 0: return mounts else: for drive in [line for line in command_result.output.split(os.linesep) if line != '']: available, used, percent, size, type, mountpoint = drive.split(" ") mounts.append({"available": available, "used": used, "percent": percent, "size": size, "type": type, "mountpoint": mountpoint}) return mounts
def osdisks(self): mounts = [] runner = shellRunner() command_result = runner.runPowershell(script_block=Hardware.WINDOWS_GET_DRIVES_CMD) if command_result.exitCode != 0: return mounts else: for drive in [line for line in command_result.output.split(os.linesep) if line != '']: available, used, percent, size, fs_type, mountpoint = drive.split(" ") mounts.append({"available": available, "used": used, "percent": percent, "size": size, "type": fs_type, "mountpoint": mountpoint}) return mounts
def log_process_information(logger): """ Check if certain configuration sent from the server has been received. """ if OSCheck.is_windows_family(): cmd_list = [ "WMIC path win32_process get Caption,Processid,Commandline", "netstat -an" ] else: cmd_list = ["export COLUMNS=9999 ; ps faux", "netstat -tulpn"] shell_runner = shellRunner() for cmd in cmd_list: ret = shell_runner.run(cmd) logger.info("Command '{0}' returned {1}. {2}{3}".format( cmd, ret["exitCode"], ret["error"], ret["output"]))
def remove_package(self, name, context, ignore_dependencies=False): """ Remove package :type name str :type context ambari_commons.shell.RepoCallContext :type ignore_dependencies bool """ if self._check_existence(name, context): cmd = REMOVE_CMD[context.log_output] + [name] cmdString = " ".join(cmd) Logger.info("Removing package %s ('%s')" % (name, " ".join(cmd))) runner = shellRunner() res = runner.run(cmd) if res['exitCode'] != 0: raise Exception("Error while upgrading choco package " + name + ". " + res['error'] + res['output']) else: Logger.info("Skipping removal of non-existing package %s" % (name))
def upgrade_package(self, name, context): """ Install package :type name str :type context ambari_commons.shell.RepoCallContext """ cmd = UPGRADE_CMD[context.log_output] if context.use_repos: enable_repo_option = '-s' + ",".join( sorted(context.use_repos.keys())) cmd = cmd + [enable_repo_option] cmd = cmd + [name] cmdString = " ".join(cmd) Logger.info("Upgrading package %s ('%s')" % (name, cmdString)) runner = shellRunner() res = runner.run(cmd) if res['exitCode'] != 0: raise Exception("Error while upgrading choco package " + name + ". " + res['error'] + res['output'])
def check_sudo(): # don't need to check sudo for root. if os.geteuid() == 0: return runner = shellRunner() test_command = [AMBARI_SUDO_BINARY, '/usr/bin/test', '/'] test_command_str = ' '.join(test_command) start_time = time.time() res = runner.run(test_command) end_time = time.time() run_time = end_time - start_time if res['exitCode'] != 0: raise Exception("Please check your sudo configurations.\n" + test_command_str + " failed with " + res['error'] + res['output']) # bad sudo configurations if run_time > 2: logger.warn(("Sudo commands on this host are running slowly ('{0}' took {1} seconds).\n" + "This will create a significant slow down for ambari-agent service tasks.").format(test_command_str, run_time))
def stop_agent(): # stop existing Ambari agent pid = -1 try: f = open(ProcessHelper.pidfile, 'r') pid = f.read() pid = int(pid) f.close() runner = shellRunner() runner.run([AMBARI_SUDO_BINARY, 'kill', '-15', str(pid)]) time.sleep(5) if os.path.exists(ProcessHelper.pidfile): raise Exception("PID file still exists.") sys.exit(0) except Exception, err: if pid == -1: print ("Agent process is not running") else: res = runner.run([AMBARI_SUDO_BINARY, 'kill', '-9', str(pid)]) if res['exitCode'] != 0: raise Exception("Error while performing agent stop. " + res['error'] + res['output']) sys.exit(1)
def stop_agent(): # stop existing Ambari agent pid = -1 runner = shellRunner() try: f = open(ProcessHelper.pidfile, 'r') pid = f.read() pid = int(pid) f.close() runner.run([AMBARI_SUDO_BINARY, 'kill', '-15', str(pid)]) time.sleep(5) if os.path.exists(ProcessHelper.pidfile): raise Exception("PID file still exists.") sys.exit(0) except Exception, err: if pid == -1: print ("Agent process is not running") else: res = runner.run([AMBARI_SUDO_BINARY, 'kill', '-9', str(pid)]) if res['exitCode'] != 0: raise Exception("Error while performing agent stop. " + res['error'] + res['output']) sys.exit(1)
def install_package(self, name, context): """ Install package :type name str :type context ambari_commons.shell.RepoCallContext """ if not self._check_existence(name) or context.use_repos: cmd = INSTALL_CMD[context.log_output] if context.use_repos: enable_repo_option = '-s' + ",".join( sorted(context.use_repos.keys())) cmd = cmd + [enable_repo_option] cmd = cmd + [name] cmdString = " ".join(cmd) Logger.info("Installing package %s ('%s')" % (name, cmdString)) runner = shellRunner() res = runner.run(cmd) if res['exitCode'] != 0: raise Exception("Error while installing choco package " + name + ". " + res['error'] + res['output']) else: Logger.info("Skipping installation of existing package %s" % (name))