def select(component, version): """ Executes <stack-selector-tool> on the specific component and version. Some global variables that are imported via params/status_params/params_linux will need to be recalcuated after the <stack-selector-tool>. However, python does not re-import existing modules. The only way to ensure that the configuration variables are recalculated is to call reload(...) on each module that has global parameters. After invoking <stack-selector-tool>, this function will also reload params, status_params, and params_linux. :param component: the <stack-selector-tool> component, such as oozie-server. If "all", then all components will be updated. :param version: the version to set the component to, such as 2.2.0.0-1234 """ stack_selector_path = stack_tools.get_stack_tool_path(stack_tools.STACK_SELECTOR_NAME) command = (STACK_SELECT_PREFIX, stack_selector_path, "set", component, version) Execute(command, sudo=True) # don't trust the ordering of modules: # 1) status_params # 2) params_linux # 3) params modules = sys.modules param_modules = "status_params", "params_linux", "params" for moduleName in param_modules: if moduleName in modules: module = modules.get(moduleName) reload(module) Logger.info("After {0}, reloaded module {1}".format(command, moduleName))
def actionexecute(self, env): config = Script.get_config() version = default('/commandParams/version', None) stack_name = default('/hostLevelParams/stack_name', "") if not version: raise Fail("Value is required for '/commandParams/version'") # other os? if OSCheck.is_redhat_family(): cmd = ('/usr/bin/yum', 'clean', 'all') code, out = shell.call(cmd, sudo=True) real_ver = format_stack_version(version) if real_ver and check_stack_feature(StackFeature.ROLLING_UPGRADE, real_ver): stack_selector_path = stack_tools.get_stack_tool_path(stack_tools.STACK_SELECTOR_NAME) cmd = ('ambari-python-wrap', stack_selector_path, 'set', 'all', version) code, out = shell.call(cmd, sudo=True) if code != 0: raise Exception("Command '{0}' exit code is nonzero".format(cmd)) if real_ver and check_stack_feature(StackFeature.CONFIG_VERSIONING, real_ver): # backup the old and symlink /etc/[component]/conf to <stack-root>/current/[component] for k, v in conf_select.get_package_dirs().iteritems(): for dir_def in v: link_config(dir_def['conf_dir'], dir_def['current_dir'])
def actionexecute(self, env): config = Script.get_config() version = default('/commandParams/version', None) stack_name = default('/hostLevelParams/stack_name', "") if not version: raise Fail("Value is required for '/commandParams/version'") # other os? if OSCheck.is_redhat_family(): cmd = ('/usr/bin/yum', 'clean', 'all') code, out = shell.call(cmd, sudo=True) min_ver = format_stack_version("2.2") real_ver = format_stack_version(version) if stack_name == "HDP": if compare_versions(real_ver, min_ver) >= 0: stack_selector_path = stack_tools.get_stack_tool_path( stack_tools.STACK_SELECTOR_NAME) cmd = ('ambari-python-wrap', stack_selector_path, 'set', 'all', version) code, out = shell.call(cmd, sudo=True) if compare_versions(real_ver, format_stack_version("2.3")) >= 0: # backup the old and symlink /etc/[component]/conf to <stack-root>/current/[component] for k, v in conf_select.get_package_dirs().iteritems(): for dir_def in v: link_config(dir_def['conf_dir'], dir_def['current_dir'])
def actionexecute(self, env): version = default('/commandParams/version', None) if not version: raise Fail("Value is required for '/commandParams/version'") # other os? if OSCheck.is_redhat_family(): cmd = ('/usr/bin/yum', 'clean', 'all') code, out = shell.call(cmd, sudo=True) formatted_version = format_stack_version(version) if not formatted_version: raise Fail("Unable to determine a properly formatted stack version from {0}".format(version)) stack_selector_path = stack_tools.get_stack_tool_path(stack_tools.STACK_SELECTOR_NAME) # this script runs on all hosts; if this host doesn't have stack components, # then don't invoke the stack tool # (no need to log that it's skipped - the function will do that) if is_host_skippable(stack_selector_path, formatted_version): return # invoke "set all" cmd = ('ambari-python-wrap', stack_selector_path, 'set', 'all', version) code, out = shell.call(cmd, sudo=True) if code != 0: raise Exception("Command '{0}' exit code is nonzero".format(cmd)) if check_stack_feature(StackFeature.CONFIG_VERSIONING, formatted_version): # backup the old and symlink /etc/[component]/conf to <stack-root>/current/[component] for k, v in conf_select.get_package_dirs().iteritems(): for dir_def in v: link_config(dir_def['conf_dir'], dir_def['current_dir'])
def unsafe_get_stack_versions(): """ Gets list of stack versions installed on the host. By default a call to <stack-selector-tool> versions is made to get the list of installed stack versions. DO NOT use a fall-back since this function is called by alerts in order to find potential errors. :return: Returns a tuple of (exit code, output, list of installed stack versions). """ stack_selector_path = stack_tools.get_stack_tool_path(stack_tools.STACK_SELECTOR_NAME) code, out = call((STACK_SELECT_PREFIX, stack_selector_path, 'versions')) versions = [] if 0 == code: for line in out.splitlines(): versions.append(line.rstrip('\n')) return (code, out, versions)
def get_supported_packages(): """ Parses the output from <stack-select> packages and returns an array of the various packages. :return: and array of packages support by <stack-select> """ stack_selector_path = stack_tools.get_stack_tool_path(stack_tools.STACK_SELECTOR_NAME) command = (STACK_SELECT_PREFIX, stack_selector_path, "packages") code, stdout = shell.call(command, sudo = True, quiet = True) if code != 0 or stdout is None: raise Fail("Unable to query for supported packages using {0}".format(stack_selector_path)) # turn the output into lines, stripping each line return [line.strip() for line in stdout.splitlines()]
def get_stack_versions(stack_root): """ Gets list of stack versions installed on the host. By default a call to <stack-selector-tool> versions is made to get the list of installed stack versions. As a fallback list of installed versions is collected from stack version directories in stack install root. :param stack_root: Stack install root :return: Returns list of installed stack versions. """ stack_selector_path = stack_tools.get_stack_tool_path(stack_tools.STACK_SELECTOR_NAME) code, out = call((STACK_SELECT_PREFIX, stack_selector_path, 'versions')) versions = [] if 0 == code: for line in out.splitlines(): versions.append(line.rstrip('\n')) if not versions: versions = get_versions_from_stack_root(stack_root) return versions
def get_stack_versions(stack_root): """ Gets list of stack versions installed on the host. Be default a call to <stack-selector-tool> versions is made to get the list of installed stack versions. As a fallback list of installed versions is collected from stack version directories in stack install root. :param stack_root: Stack install root :return: Returns list of installed stack versions. """ stack_selector_path = stack_tools.get_stack_tool_path(stack_tools.STACK_SELECTOR_NAME) code, out = call((STACK_SELECT_PREFIX, stack_selector_path, 'versions')) versions = [] if 0 == code: for line in out.splitlines(): versions.append(line.rstrip('\n')) if not versions: versions = get_versions_from_stack_root(stack_root) return versions
def get_stack_version(package_name): """ @param package_name, name of the package, from which, function will try to get stack version """ stack_selector_path = stack_tools.get_stack_tool_path(stack_tools.STACK_SELECTOR_NAME) if not os.path.exists(stack_selector_path): Logger.info('Skipping get_stack_version since " + stack_selector_tool + " is not yet available') return None # lazy fail try: command = 'ambari-python-wrap {stack_selector_path} status {package_name}'.format( stack_selector_path=stack_selector_path, package_name=package_name) return_code, stack_output = shell.call(command, timeout=20) except Exception, e: Logger.error(str(e)) raise Fail('Unable to execute " + stack_selector_path + " command to retrieve the version.')
def actionexecute(self, env): summary = upgrade_summary.get_upgrade_summary() if summary is None: Logger.warning("There is no upgrade in progress") return if summary.associated_version is None: Logger.warning( "There is no version associated with the upgrade in progress") return if summary.orchestration != "STANDARD": Logger.warning( "The 'stack-select set all' command can only be invoked during STANDARD upgrades" ) return if summary.direction.lower( ) != Direction.UPGRADE or summary.is_downgrade_allowed or summary.is_revert: Logger.warning( "The 'stack-select set all' command can only be invoked during an UPGRADE which cannot be downgraded" ) return # other os? if OSCheck.is_redhat_family(): cmd = ('/usr/bin/yum', 'clean', 'all') code, out = shell.call(cmd, sudo=True) stack_selector_path = stack_tools.get_stack_tool_path( stack_tools.STACK_SELECTOR_NAME) # this script runs on all hosts; if this host doesn't have stack components, # then don't invoke the stack tool # (no need to log that it's skipped - the function will do that) if is_host_skippable(stack_selector_path, summary.associated_version): return # invoke "set all" cmd = ('ambari-python-wrap', stack_selector_path, 'set', 'all', summary.associated_version) code, out = shell.call(cmd, sudo=True) if code != 0: raise Exception("Command '{0}' exit code is nonzero".format(cmd))
def _get_single_version_from_stack_select(): """ Call "<stack-selector> versions" and return the version string if only one version is available. :return: Returns a version string if successful, and None otherwise. """ # Ubuntu returns: "stdin: is not a tty", as subprocess output, so must use a temporary file to store the output. tmpfile = tempfile.NamedTemporaryFile() tmp_dir = Script.get_tmp_dir() tmp_file = os.path.join(tmp_dir, "copy_tarball_out.txt") stack_version = None out = None stack_selector_path = stack_tools.get_stack_tool_path(stack_tools.STACK_SELECTOR_NAME) get_stack_versions_cmd = "{0} versions > {1}".format(stack_selector_path, tmp_file) try: code, stdoutdata = shell.call(get_stack_versions_cmd, logoutput=True) with open(tmp_file, 'r+') as file: out = file.read() except Exception, e: Logger.logger.exception("Could not parse output of {0}. Error: {1}".format(str(tmp_file), str(e)))
def _get_cmd(command, package, version): conf_selector_path = stack_tools.get_stack_tool_path(stack_tools.CONF_SELECTOR_NAME) return ('ambari-python-wrap', conf_selector_path, command, '--package', package, '--stack-version', version, '--conf-version', '0')