def _check_distribution(self): """Check distribution""" if self.__distribution not in self.__distributions_names \ and self.__distribution_alias not in self.__distributions_names: raise AnsibleError("{d} distribution is not supported".format( d=self.__distribution)) required_version = \ self.__distributions.get(self.__distribution, None) alias_required_version = \ self.__distributions.get(self.__distribution_alias, None) version_supported = False if required_version is not None \ and version_compare(self.__distribution_version, required_version, ">="): version_supported = True if alias_required_version is not None \ and version_compare(self.__distribution_version, alias_required_version, ">="): version_supported = True if not version_supported: raise AnsibleError( "{d} distribution version {v} is not supported".format( d=self.__distribution, v=self.__distribution_version))
def versioned_value(a, version): if not isinstance(a, dict) or not a: raise errors.AnsibleFilterError( "versioned_value expects to filter on not empty dict") if version in a: return a[version] temp = 'v0.0.0' for key in a: if (version_compare(key, version, 'lt', False) and version_compare(key, temp, 'gt', False)): temp = key if temp == 'v0.0.0': raise errors.AnsibleFilterError("version should be in dict or greater") else: return a[temp]
def _gather_virtualenv_status(self): """Gather virtualenv status""" if self.__family == "python" \ and self.__packages_virtualenv_exists is None: cmd = "{python} "\ "-c 'from sys import version_info ; print(version_info[0])'"\ .format(python=self.__python_interpreter) action = self._action(action="shell", args=dict(_raw_params=cmd, _uses_shell=False)) result = action.run(task_vars=self.__task_vars) virtualenv_status_error = result.get("stderr", None) virtualenv_status_major = result.get("stdout", None) self.__packages_virtualenv_exists = False if len(virtualenv_status_error) == 0: self.__packages_virtualenv_exists = True if len(virtualenv_status_major) > 0 \ and version_compare(virtualenv_status_major, "3", "<"): self.__packages_virtualenv_needs_upgrade = True else: self.__packages_virtualenv_needs_upgrade = False
def _gather_python_os_packages(self): """Gather python os packages""" if version_compare(self.__distro_version, "7", ">="): self.__python_os_package = "python3" self.__pip_os_package = "python3-pip" self.__virtualenv_os_package = "python3-virtualenv" self.__setup_tools_os_package = "python3-setuptools" else: self.__python_os_package = None self.__pip_os_package = None self.__virtualenv_os_package = None self.__setup_tools_os_package = None
def _gather_os_packages_groups(self): """Gather os packages groups""" self.__groups_present = \ self.__ansible_facts.get("_packages_groups_present", None) self.__debug_info["groups_gathered"] = False if self.__groups_present is None: self.__groups_present = list() if version_compare(self.__distro_version, "6", ">"): self.__debug_info["groups_gathered"] = True cmd = "LANGUAGE=en_US yum group list" action = self._action(action="shell", args=dict(_raw_params=cmd, _uses_shell=True)) result = action.run(task_vars=self.__task_vars) if "stdout_lines" in result: lines = result["stdout_lines"] regex_installed = re.compile("^Installed") regex_available = re.compile("^Available") regex_group = re.compile("^ ") gather_groups = False for line in lines: if regex_installed.match(line): gather_groups = True elif regex_available.match(line): gather_groups = False elif regex_group.match(line) and gather_groups: self.__groups_present.append("@" + line.strip()) self.__packages_os_present = \ self.__packages_os_present \ + self.__groups_present if self.__family == "os": self.__packages_present = \ self.__packages_present + self.__groups_present
def _setup_virtualenv(self): """Setup virtualenv""" self.__debug_info["virtualenv_created"] = False if self.__family == "python" \ and (not self.__packages_virtualenv_exists or self.__packages_virtualenv_needs_upgrade): if self.__packages_virtualenv_needs_upgrade: result = self._execute_module( module_name="file", module_args=dict( path=self.__packages_python_virtualenv, state="absent"), task_vars=self.__task_vars) if result.get("failed", False): raise AnsibleError("Failed to remove virtualenv") self.__packages_virtualenv_needs_upgrade = False if self.__packages_python_virtualenv_python is not None: python = self.__packages_python_virtualenv_python else: if version_compare(self.__distro_version, "7", "<"): python = "{path}/bin/python3".format( path=self.__packages_python_source_install_dir) else: python = "/usr/bin/python3" if self.__packages_python_virtualenv_site_packages: args = "--system-site-packages" else: args = "" if self.__packages_python_virtualenv_command is not None: virtualenv_cmd = self.__packages_python_virtualenv_command else: if version_compare(self.__distro_version, "7", "="): virtualenv_cmd = "/usr/bin/virtualenv-3" elif version_compare(self.__distro_version, "7", ">"): virtualenv_cmd = "/usr/bin/virtualenv" else: virtualenv_cmd = \ self.__packages_python_source_install_dir \ + "/bin/virtualenv" cmd = "{virtualenv_cmd} " \ "--python={python} {args} {virtualenv}" \ .format(virtualenv_cmd=virtualenv_cmd, python=python, args=args, virtualenv=self.__packages_python_virtualenv) action = self._action(action="shell", args=dict(_raw_params=cmd, _uses_shell=False)) task_vars = self.__task_vars.copy() task_vars["ansible_python_interpreter"] = \ self.__packages_python_virtualenv_python result = action.run(task_vars=task_vars) if result.get("failed", False): raise AnsibleError("Failed to setup virtualenv") self.__debug_info["virtualenv_created"] = True self.__packages_virtualenv_exists = True self.__changed = True