ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hapyles...@apache.org
Subject [3/8] ambari git commit: AMBARI-21398 Backend need to distinguish repo_version records by using the Stack Name (dgrinenko)
Date Thu, 17 Aug 2017 10:04:16 GMT
http://git-wip-us.apache.org/repos/asf/ambari/blob/3f6c4ca0/ambari-common/src/main/python/resource_management/libraries/functions/packages_analyzer.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/resource_management/libraries/functions/packages_analyzer.py
b/ambari-common/src/main/python/resource_management/libraries/functions/packages_analyzer.py
deleted file mode 100644
index e290adb..0000000
--- a/ambari-common/src/main/python/resource_management/libraries/functions/packages_analyzer.py
+++ /dev/null
@@ -1,356 +0,0 @@
-#!/usr/bin/env python
-
-"""
-Licensed to the Apache Software Foundation (ASF) under one
-or more contributor license agreements.  See the NOTICE file
-distributed with this work for additional information
-regarding copyright ownership.  The ASF licenses this file
-to you under the Apache License, Version 2.0 (the
-"License"); you may not use this file except in compliance
-with the License.  You may obtain a copy of the License at
-
-    http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-"""
-
-import re
-import sys
-import logging
-import subprocess
-from threading import Thread
-import threading
-from ambari_commons import OSCheck, OSConst
-from ambari_commons import shell
-from ambari_commons.constants import AMBARI_SUDO_BINARY
-from resource_management.core.logger import Logger
-from resource_management.core import shell as rmf_shell
-from resource_management.core.exceptions import Fail
-
-__all__ = ["installedPkgsByName", "allInstalledPackages", "allAvailablePackages", "nameMatch",
-           "getInstalledRepos", "getInstalledPkgsByRepo", "getInstalledPkgsByNames", "getPackageDetails"]
-
-LIST_INSTALLED_PACKAGES_UBUNTU = "COLUMNS=9999 ; for i in $(dpkg -l |grep ^ii |awk -F' '
'{print $2}'); do      apt-cache showpkg \"$i\"|head -3|grep -v '^Versions'| tr -d '()' |
awk '{ print $1\" \"$2 }'|sed -e 's/^Package: //;' | paste -d ' ' - -;  done"
-LIST_AVAILABLE_PACKAGES_UBUNTU = "packages=`for  i in $(ls -1 /var/lib/apt/lists  | grep
%s ) ; do grep ^Package: /var/lib/apt/lists/$i |  awk '{print $2}' ; done` ; for i in $packages;
do      apt-cache showpkg \"$i\"|head -3|grep -v '^Versions'| tr -d '()' | awk '{ print $1\"
\"$2 }'|sed -e 's/^Package: //;' | paste -d ' ' - -;  done"
-GREP_REPO_EXCLUDE_SYSTEM = "-v \"ubuntu.com\""
-logger = logging.getLogger()
-
-# default timeout for async invoked processes
-TIMEOUT_SECONDS = 40
-
-
-def _launch_subprocess(command):
-  isShell = not isinstance(command, (list, tuple))
-  return subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=isShell,
close_fds=True)
-
-
-def subprocessWithTimeout(command):
-  event = threading.Event()
-
-  def watchdog_func(command):
-    event.wait(TIMEOUT_SECONDS)
-    if command.returncode is None:
-      logger.error("Task timed out and will be killed")
-      shell.kill_process_with_children(command.pid)
-    pass
-
-  osStat = _launch_subprocess(command)
-  logger.debug("Launching watchdog thread")
-
-  event.clear()
-
-  thread = Thread(target=watchdog_func, args=(osStat, ))
-  thread.start()
-
-  out, err = osStat.communicate()
-  result = {}
-  result['out'] = out
-  result['err'] = err
-  result['retCode'] = osStat.returncode
-
-  event.set()
-  thread.join()
-  return result
-
-
-def installedPkgsByName(allInstalledPackages,
-                        pkgName, installedPkgs):
-  """
-  Get all installed package whose name starts with the
-  strings contained in pkgName
-  """
-  for item in allInstalledPackages:
-    if item[0].find(pkgName) == 0:
-      installedPkgs.append(item[0])
-
-
-def allInstalledPackages(allInstalledPackages):
-  """
-  All installed packages in system
-  """
-  if OSCheck.is_suse_family():
-    return _lookUpZypperPackages(
-      [AMBARI_SUDO_BINARY, "zypper", "--no-gpg-checks", "search", "--installed-only", "--details"],
-      allInstalledPackages)
-  elif OSCheck.is_redhat_family():
-    return _lookUpYumPackages(
-      [AMBARI_SUDO_BINARY, "yum", "list", "installed"],
-      'Installed Packages',
-      allInstalledPackages)
-  elif OSCheck.is_ubuntu_family():
-     return _lookUpAptPackages(
-      LIST_INSTALLED_PACKAGES_UBUNTU,
-      allInstalledPackages)
-
-
-def get_available_packages_in_repos(repositories):
-  """
-  Gets all (both installed and available) packages that are available at given repositories.
-  :param repositories: from command configs like config['repositoryFile']['repositories']
-  :return: installed and available packages from these repositories
-  """
-
-  available_packages = []
-  installed_packages = []
-  available_packages_in_repos = []
-  repo_ids = [repository['repoId'] for repository in repositories]
-  if OSCheck.is_ubuntu_family():
-    allInstalledPackages(installed_packages)
-    repo_urls = [repository['baseUrl'] for repository in repositories]
-    repo_urls = [repo_url.replace("http://","") for repo_url in repo_urls]
-    repo_urls = [repo_url.replace("/","_") for repo_url in repo_urls]
-    for url in repo_urls:
-      _lookUpAptPackages(
-        LIST_AVAILABLE_PACKAGES_UBUNTU % url,
-        available_packages)
-      for package in installed_packages:
-        if url in package[2]:
-          available_packages_in_repos.append(package[0])
-    for package in available_packages:
-      available_packages_in_repos.append(package[0])
-  elif OSCheck.is_suse_family():
-    for repo in repo_ids:
-      _lookUpZypperPackages([AMBARI_SUDO_BINARY, "zypper", "--no-gpg-checks", "search", "--details",
"--repo", repo],
-                            available_packages)
-    available_packages_in_repos += [package[0] for package in available_packages]
-  elif OSCheck.is_redhat_family():
-    for repo in repo_ids:
-      _lookUpYumPackages([AMBARI_SUDO_BINARY, "yum", "list", "available", "--disablerepo=*",
"--enablerepo=" + repo],
-                         'Available Packages', available_packages)
-      _lookUpYumPackages([AMBARI_SUDO_BINARY, "yum", "list", "installed", "--disablerepo=*",
"--enablerepo=" + repo],
-                         'Installed Packages', installed_packages)
-    available_packages_in_repos += [package[0] for package in available_packages + installed_packages]
-  return available_packages_in_repos
-
-
-def allAvailablePackages(allAvailablePackages):
-  if OSCheck.is_suse_family():
-    return _lookUpZypperPackages(
-      [AMBARI_SUDO_BINARY, "zypper", "--no-gpg-checks", "search", "--uninstalled-only", "--details"],
-      allAvailablePackages)
-  elif OSCheck.is_redhat_family():
-    return _lookUpYumPackages(
-      [AMBARI_SUDO_BINARY, "yum", "list", "available"],
-      'Available Packages',
-      allAvailablePackages)
-  elif OSCheck.is_ubuntu_family():
-     return _lookUpAptPackages(
-       LIST_AVAILABLE_PACKAGES_UBUNTU % GREP_REPO_EXCLUDE_SYSTEM,
-      allAvailablePackages)
-
-# ToDo: add execution via sudo for ubuntu (currently Ubuntu is not supported)
-def _lookUpAptPackages(command, allPackages):
-  try:
-    result = subprocessWithTimeout(command)
-    if 0 == result['retCode']:
-      for x in result['out'].split('\n'):
-        if x.strip():
-          allPackages.append(x.split(' '))
-
-  except:
-    logger.error("Unexpected error:", sys.exc_info()[0])
-
-
-def _lookUpYumPackages(command, skipTill, allPackages):
-  try:
-    result = subprocessWithTimeout(command)
-    if 0 == result['retCode']:
-      lines = result['out'].split('\n')
-      lines = [line.strip() for line in lines]
-      items = []
-      skipIndex = 3
-      for index in range(len(lines)):
-        if skipTill in lines[index]:
-          skipIndex = index + 1
-          break
-
-      for line in lines[skipIndex:]:
-        items = items + line.strip(' \t\n\r').split()
-
-      for i in range(0, len(items), 3):
-        if '.' in items[i]:
-          items[i] = items[i][:items[i].rindex('.')]
-        if items[i + 2].find('@') == 0:
-          items[i + 2] = items[i + 2][1:]
-        allPackages.append(items[i:i + 3])
-  except:
-    logger.error("Unexpected error:", sys.exc_info()[0])
-
-
-def _lookUpZypperPackages(command, allPackages):
-  try:
-    result = subprocessWithTimeout(command)
-    if 0 == result['retCode']:
-      lines = result['out'].split('\n')
-      lines = [line.strip() for line in lines]
-      for index in range(len(lines)):
-        if "--+--" in lines[index]:
-          skipIndex = index + 1
-          break
-
-      for line in lines[skipIndex:]:
-        items = line.strip(' \t\n\r').split('|')
-        allPackages.append([items[1].strip(), items[3].strip(), items[5].strip()])
-  except:
-    logger.error("Unexpected error:", sys.exc_info()[0])
-
-
-def nameMatch(lookupName, actualName):
-  tokens = actualName.strip().split()
-  for token in tokens:
-    if lookupName.lower() in token.lower():
-      return True
-  return False
-
-
-def getInstalledRepos(hintPackages, allPackages, ignoreRepos, repoList):
-  """
-  Gets all installed repos by name based on repos that provide any package
-  contained in hintPackages
-  Repos starting with value in ignoreRepos will not be returned
-  hintPackages must be regexps.
-  """
-  allRepos = []
-  for hintPackage in hintPackages:
-    for item in allPackages:
-      if re.match(hintPackage, item[0]) and not item[2] in allRepos:
-        allRepos.append(item[2])
-
-  for repo in allRepos:
-    ignore = False
-    for ignoredRepo in ignoreRepos:
-      if nameMatch(ignoredRepo, repo):
-        ignore = True
-    if not ignore:
-      repoList.append(repo)
-
-
-def getInstalledPkgsByRepo(repos, ignorePackages, installedPackages):
-  """
-  Get all the installed packages from the repos listed in repos
-  """
-  packagesFromRepo = []
-  packagesToRemove = []
-  for repo in repos:
-    subResult = []
-    for item in installedPackages:
-      if repo == item[2]:
-        subResult.append(item[0])
-    packagesFromRepo = list(set(packagesFromRepo + subResult))
-
-  for package in packagesFromRepo:
-    keepPackage = True
-    for ignorePackage in ignorePackages:
-      if nameMatch(ignorePackage, package):
-        keepPackage = False
-        break
-    if keepPackage:
-      packagesToRemove.append(package)
-  return packagesToRemove
-
-
-def getInstalledPkgsByNames(pkgNames, installedPackages):
-  """
-  Gets all installed packages that start with names in pkgNames
-  """
-  packages = []
-  for pkgName in pkgNames:
-    subResult = []
-    installedPkgsByName(installedPackages, pkgName, subResult)
-    packages = list(set(packages + subResult))
-  return packages
-
-
-def getPackageDetails(installedPackages, foundPackages):
-  """
-  Gets the name, version, and repoName for the packages
-  """
-  packageDetails = []
-  for package in foundPackages:
-    pkgDetail = {}
-    for installedPackage in installedPackages:
-      if package == installedPackage[0]:
-        pkgDetail['name'] = installedPackage[0]
-        pkgDetail['version'] = installedPackage[1]
-        pkgDetail['repoName'] = installedPackage[2]
-    packageDetails.append(pkgDetail)
-  return packageDetails
-
-def getReposToRemove(repos, ignoreList):
-  reposToRemove = []
-  for repo in repos:
-    addToRemoveList = True
-    for ignoreRepo in ignoreList:
-      if nameMatch(ignoreRepo, repo):
-        addToRemoveList = False
-        continue
-    if addToRemoveList:
-      reposToRemove.append(repo)
-  return reposToRemove
-
-def getInstalledPackageVersion(package_name):
-  if OSCheck.is_ubuntu_family():
-    code, out = rmf_shell.checked_call("dpkg -s {0} | grep Version | awk '{{print $2}}'".format(package_name))
-  else:
-    code, out = rmf_shell.checked_call("rpm -q --queryformat '%{{version}}-%{{release}}'
{0} | sed -e 's/\.el[0-9]//g'".format(package_name))
-
-  return out
-
-
-def verifyDependencies():
-  """
-  Verify that we have no dependency issues in package manager. Dependency issues could appear
because of aborted or terminated
-  package installation process or invalid packages state after manual modification of packages
list on the host
-   :return True if no dependency issues found, False if dependency issue present
-  :rtype bool
-  """
-  check_str = None
-  cmd = None
-
-  if OSCheck.is_redhat_family():
-    cmd = ['/usr/bin/yum', '-d', '0', '-e', '0', 'check', 'dependencies']
-    check_str = "has missing requires|Error:"
-  elif OSCheck.is_suse_family():
-    cmd = ['/usr/bin/zypper', '--quiet', '--non-interactive', 'verify', '--dry-run']
-    check_str = "\d+ new package(s)? to install"
-  elif OSCheck.is_ubuntu_family():
-    cmd = ['/usr/bin/apt-get', '-qq', 'check']
-    check_str = "has missing dependency|E:"
-
-  if check_str is None or cmd is None:
-    raise Fail("Unsupported OSFamily on the Agent Host")
-
-  code, out = rmf_shell.checked_call(cmd, sudo=True)
-
-  output_regex = re.compile(check_str)
-
-  if code or (out and output_regex.search(out)):
-    err_msg = Logger.filter_text("Failed to verify package dependencies. Execution of '%s'
returned %s. %s" % (cmd, code, out))
-    Logger.error(err_msg)
-    return False
-
-  return True

http://git-wip-us.apache.org/repos/asf/ambari/blob/3f6c4ca0/ambari-common/src/main/python/resource_management/libraries/script/script.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/resource_management/libraries/script/script.py
b/ambari-common/src/main/python/resource_management/libraries/script/script.py
index 63b55ff..90337f4 100644
--- a/ambari-common/src/main/python/resource_management/libraries/script/script.py
+++ b/ambari-common/src/main/python/resource_management/libraries/script/script.py
@@ -52,13 +52,13 @@ from resource_management.libraries.functions.version import compare_versions
 from resource_management.libraries.functions.version import format_stack_version
 from resource_management.libraries.functions import stack_tools
 from resource_management.libraries.functions.constants import Direction
-from resource_management.libraries.functions import packages_analyzer
 from resource_management.libraries.script.config_dictionary import ConfigDictionary, UnknownConfiguration
 from resource_management.core.resources.system import Execute
 from contextlib import closing
 from resource_management.libraries.functions.stack_features import check_stack_feature
 from resource_management.libraries.functions.constants import StackFeature
 from resource_management.libraries.functions.show_logs import show_logs
+from resource_management.core.providers import get_provider
 from resource_management.libraries.functions.fcntl_based_process_lock import FcntlBasedProcessLock
 
 import ambari_simplejson as json # simplejson is much faster comparing to Python 2.6 json
module and has the same functions set.
@@ -455,7 +455,9 @@ class Script(object):
     if not Script.stack_version_from_distro_select or '*' in Script.stack_version_from_distro_select:
       # FIXME: this method is not reliable to get stack-selector-version
       # as if there are multiple versions installed with different <stack-selector-tool>,
we won't detect the older one (if needed).
-      Script.stack_version_from_distro_select = packages_analyzer.getInstalledPackageVersion(
+      pkg_provider = get_provider("Package")
+
+      Script.stack_version_from_distro_select = pkg_provider.get_installed_package_version(
               stack_tools.get_stack_tool_package(stack_tools.STACK_SELECTOR_NAME))
 
     return Script.stack_version_from_distro_select
@@ -476,12 +478,19 @@ class Script(object):
         return package
 
 
-  def format_package_name(self, name):
+  def format_package_name(self, name, repo_version=None):
     from resource_management.libraries.functions.default import default
     """
     This function replaces ${stack_version} placeholder with actual version.  If the package
     version is passed from the server, use that as an absolute truth.
+    
+    :param name name of the package
+    :param repo_version actual version of the repo currently installing
     """
+    stack_version_package_formatted = ""
+
+    if not repo_version:
+      repo_version = self.get_stack_version_before_packages_installed()
 
     package_delimiter = '-' if OSCheck.is_ubuntu_family() else '_'
 
@@ -517,7 +526,7 @@ class Script(object):
 
     # Wildcards cause a lot of troubles with installing packages, if the version contains
wildcards we try to specify it.
     if not package_version or '*' in package_version:
-      stack_version_package_formatted = self.get_stack_version_before_packages_installed().replace('.',
package_delimiter).replace('-', package_delimiter) if STACK_VERSION_PLACEHOLDER in name else
name
+      stack_version_package_formatted = repo_version.replace('.', package_delimiter).replace('-',
package_delimiter) if STACK_VERSION_PLACEHOLDER in name else name
 
     package_name = name.replace(STACK_VERSION_PLACEHOLDER, stack_version_package_formatted)
 
@@ -723,7 +732,7 @@ class Script(object):
 
     if 'host_sys_prepped' in config['hostLevelParams']:
       # do not install anything on sys-prepped host
-      if config['hostLevelParams']['host_sys_prepped'] == True:
+      if config['hostLevelParams']['host_sys_prepped'] is True:
         Logger.info("Node has all packages pre-installed. Skipping.")
         return
       pass
@@ -731,8 +740,9 @@ class Script(object):
       package_list_str = config['hostLevelParams']['package_list']
       agent_stack_retry_on_unavailability = bool(config['hostLevelParams']['agent_stack_retry_on_unavailability'])
       agent_stack_retry_count = int(config['hostLevelParams']['agent_stack_retry_count'])
+      pkg_provider = get_provider("Package")
       try:
-        available_packages_in_repos = packages_analyzer.get_available_packages_in_repos(config['repositoryFile']['repositories'])
+        available_packages_in_repos = pkg_provider.get_available_packages_in_repos(config['repositoryFile']['repositories'])
       except Exception as err:
         available_packages_in_repos = []
       if isinstance(package_list_str, basestring) and len(package_list_str) > 0:

http://git-wip-us.apache.org/repos/asf/ambari/blob/3f6c4ca0/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
index b812476..394e9f9 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
@@ -98,8 +98,8 @@ public class StackVersionListener {
       if (null != rve) {
         String currentRepoVersion = rve.getVersion();
         if (!StringUtils.equals(currentRepoVersion, newVersion)) {
-          rve.setVersion(newVersion);
-          repositoryVersionDAO.merge(rve);
+            rve.setVersion(newVersion);
+            repositoryVersionDAO.merge(rve);
         }
       }
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/3f6c4ca0/ambari-server/src/main/resources/custom_actions/scripts/check_host.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/custom_actions/scripts/check_host.py b/ambari-server/src/main/resources/custom_actions/scripts/check_host.py
index 358c104..a485415 100644
--- a/ambari-server/src/main/resources/custom_actions/scripts/check_host.py
+++ b/ambari-server/src/main/resources/custom_actions/scripts/check_host.py
@@ -27,7 +27,6 @@ import socket
 import getpass
 import tempfile
 
-from resource_management.libraries.functions import packages_analyzer
 from resource_management.libraries.functions.default import default
 from ambari_commons import os_utils
 from ambari_commons.os_check import OSCheck, OSConst
@@ -40,6 +39,7 @@ from resource_management.core.exceptions import Fail
 from ambari_commons.constants import AMBARI_SUDO_BINARY
 from resource_management.core import shell
 from resource_management.core.logger import Logger
+from resource_management.core.providers import get_provider
 
 # WARNING. If you are adding a new host check that is used by cleanup, add it to BEFORE_CLEANUP_HOST_CHECKS
 # It is used by HostCleanup.py
@@ -113,6 +113,7 @@ class CheckHost(Script):
   
   def __init__(self):
     self.reportFileHandler = HostCheckReportFileHandler()
+    self.pkg_provider = get_provider("Package")
   
   def actionexecute(self, env):
     Logger.info("Host checks started.")
@@ -229,21 +230,18 @@ class CheckHost(Script):
   def execute_existing_repos_and_installed_packages_check(self, config):
       Logger.info("Installed packages and existing repos checks started.")
 
-      installedPackages = []
-      availablePackages = []
-      packages_analyzer.allInstalledPackages(installedPackages)
-      packages_analyzer.allAvailablePackages(availablePackages)
-
-      repos = []
-      packages_analyzer.getInstalledRepos(self.PACKAGES, installedPackages + availablePackages,
-                                      self.IGNORE_PACKAGES_FROM_REPOS, repos)
-      packagesInstalled = packages_analyzer.getInstalledPkgsByRepo(repos, self.IGNORE_PACKAGES,
installedPackages)
-      additionalPkgsInstalled = packages_analyzer.getInstalledPkgsByNames(
-        self.ADDITIONAL_PACKAGES, installedPackages)
+      installedPackages = self.pkg_provider.all_installed_packages()
+      availablePackages = self.pkg_provider.all_available_packages()
+
+      repos = self.pkg_provider.get_installed_repos(self.PACKAGES, installedPackages + availablePackages,
+                                                    self.IGNORE_PACKAGES_FROM_REPOS)
+
+      packagesInstalled = self.pkg_provider.get_installed_pkgs_by_repo(repos, self.IGNORE_PACKAGES,
installedPackages)
+      additionalPkgsInstalled = self.pkg_provider.get_installed_pkgs_by_names(self.ADDITIONAL_PACKAGES,
installedPackages)
       allPackages = list(set(packagesInstalled + additionalPkgsInstalled))
       
-      installedPackages = packages_analyzer.getPackageDetails(installedPackages, allPackages)
-      repos = packages_analyzer.getReposToRemove(repos, self.IGNORE_REPOS)
+      installedPackages = self.pkg_provider.get_package_details(installedPackages, allPackages)
+      repos = self.pkg_provider.get_repos_to_remove(repos, self.IGNORE_REPOS)
 
       Logger.info("Installed packages and existing repos checks completed.")
       return installedPackages, repos

http://git-wip-us.apache.org/repos/asf/ambari/blob/3f6c4ca0/ambari-server/src/main/resources/custom_actions/scripts/install_packages.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/custom_actions/scripts/install_packages.py b/ambari-server/src/main/resources/custom_actions/scripts/install_packages.py
index f24b811..6a80a43 100644
--- a/ambari-server/src/main/resources/custom_actions/scripts/install_packages.py
+++ b/ambari-server/src/main/resources/custom_actions/scripts/install_packages.py
@@ -28,20 +28,21 @@ from ambari_commons.str_utils import cbool, cint
 from resource_management.core.exceptions import Fail
 from resource_management.core.logger import Logger
 from resource_management.core.resources import Package
-from resource_management.libraries.functions.packages_analyzer import allInstalledPackages,
verifyDependencies
 from resource_management.libraries.functions import conf_select
 from resource_management.libraries.functions import stack_tools
 from resource_management.libraries.functions.stack_select import get_stack_versions
 from resource_management.libraries.functions.version import format_stack_version
 from resource_management.libraries.functions.repo_version_history \
     import read_actual_version_from_history_file, write_actual_version_to_history_file, REPO_VERSION_HISTORY_FILE
+from resource_management.core.providers import get_provider
 from resource_management.core.resources.system import Link
 from resource_management.libraries.functions import StackFeature
-from resource_management.libraries.functions import packages_analyzer
 from resource_management.libraries.functions.repository_util import create_repo_files, CommandRepository
 from resource_management.libraries.functions.stack_features import check_stack_feature
 from resource_management.libraries.resources.repository import Repository
 from resource_management.libraries.script.script import Script
+from resource_management.core import sudo
+
 
 class InstallPackages(Script):
   """
@@ -53,6 +54,11 @@ class InstallPackages(Script):
 
   UBUNTU_REPO_COMPONENTS_POSTFIX = ["main"]
 
+  def __init__(self):
+    super(InstallPackages, self).__init__()
+
+    self.pkg_provider = get_provider("Package")
+
   def actionexecute(self, env):
     num_errors = 0
 
@@ -95,7 +101,7 @@ class InstallPackages(Script):
     self.repository_version = self.repository_version.strip()
 
     try:
-      if 0 == len(command_repository.repositories):
+      if not command_repository.repositories:
         Logger.warning(
           "Repository list is empty. Ambari may not be managing the repositories for {0}.".format(
             self.repository_version))
@@ -167,13 +173,12 @@ class InstallPackages(Script):
 
     # After upgrading hdf-select package from HDF-2.X to HDF-3.Y, we need to create this
symlink
     if self.stack_name.upper() == "HDF" \
-            and not os.path.exists("/usr/bin/conf-select") and os.path.exists("/usr/bin/hdfconf-select"):
-      Link("/usr/bin/conf-select", to = "/usr/bin/hdfconf-select")
+            and not sudo.path_exists("/usr/bin/conf-select") and sudo.path_exists("/usr/bin/hdfconf-select"):
+      Link("/usr/bin/conf-select", to="/usr/bin/hdfconf-select")
 
     for package_name, directories in conf_select.get_package_dirs().iteritems():
       conf_select.select(self.stack_name, package_name, stack_version, ignore_errors = True)
 
-
   def compute_actual_version(self):
     """
     After packages are installed, determine what the new actual version is.
@@ -309,6 +314,7 @@ class InstallPackages(Script):
 
     # Install packages
     packages_were_checked = False
+    packages_installed_before = []
     stack_selector_package = stack_tools.get_stack_tool_package(stack_tools.STACK_SELECTOR_NAME)
     try:
       Package(stack_selector_package,
@@ -317,13 +323,12 @@ class InstallPackages(Script):
               retry_count=agent_stack_retry_count
       )
       
-      packages_installed_before = []
-      allInstalledPackages(packages_installed_before)
+      packages_installed_before = self.pkg_provider.all_installed_packages()
       packages_installed_before = [package[0] for package in packages_installed_before]
       packages_were_checked = True
       filtered_package_list = self.filter_package_list(package_list)
       try:
-        available_packages_in_repos = packages_analyzer.get_available_packages_in_repos(config['repositoryFile']['repositories'])
+        available_packages_in_repos = self.pkg_provider.get_available_packages_in_repos(config['repositoryFile']['repositories'])
       except Exception:
         available_packages_in_repos = []
       for package in filtered_package_list:
@@ -339,8 +344,7 @@ class InstallPackages(Script):
 
       # Remove already installed packages in case of fail
       if packages_were_checked and packages_installed_before:
-        packages_installed_after = []
-        allInstalledPackages(packages_installed_after)
+        packages_installed_after = self.pkg_provider.all_installed_packages()
         packages_installed_after = [package[0] for package in packages_installed_after]
         packages_installed_before = set(packages_installed_before)
         new_packages_installed = [package for package in packages_installed_after if package
not in packages_installed_before]
@@ -355,7 +359,7 @@ class InstallPackages(Script):
           if package_version_string and (package_version_string in package):
             Package(package, action="remove")
 
-    if not verifyDependencies():
+    if not self.pkg_provider.verify_dependencies():
       ret_code = 1
       Logger.logger.error("Failure while verifying dependencies")
       Logger.logger.error("*******************************************************************************")
@@ -373,36 +377,6 @@ class InstallPackages(Script):
       Logger.logger.exception("Failure while computing actual version. Error: {0}".format(str(err)))
     return ret_code
 
-  def install_repository(self, url_info, append_to_file, template):
-
-    repo = {
-      'repoName': "{0}-{1}".format(url_info['name'], self.repository_version)
-    }
-
-    if not 'baseUrl' in url_info:
-      repo['baseurl'] = None
-    else:
-      repo['baseurl'] = url_info['baseUrl']
-
-    if not 'mirrorsList' in url_info:
-      repo['mirrorsList'] = None
-    else:
-      repo['mirrorsList'] = url_info['mirrorsList']
-
-    ubuntu_components = [url_info['name']] + self.UBUNTU_REPO_COMPONENTS_POSTFIX
-    file_name = self.stack_name + "-" + self.repository_version
-
-    Repository(repo['repoName'],
-      action = "create",
-      base_url = repo['baseurl'],
-      mirror_list = repo['mirrorsList'],
-      repo_file_name = file_name,
-      repo_template = template,
-      append_to_file = append_to_file,
-      components = ubuntu_components,  # ubuntu specific
-    )
-    return repo['repoName'], file_name
-
   def abort_handler(self, signum, frame):
     Logger.error("Caught signal {0}, will handle it gracefully. Compute the actual version
if possible before exiting.".format(signum))
     self.check_partial_install()

http://git-wip-us.apache.org/repos/asf/ambari/blob/3f6c4ca0/ambari-server/src/main/resources/custom_actions/scripts/remove_previous_stacks.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/custom_actions/scripts/remove_previous_stacks.py
b/ambari-server/src/main/resources/custom_actions/scripts/remove_previous_stacks.py
index 958b800..2b50171 100644
--- a/ambari-server/src/main/resources/custom_actions/scripts/remove_previous_stacks.py
+++ b/ambari-server/src/main/resources/custom_actions/scripts/remove_previous_stacks.py
@@ -25,8 +25,8 @@ import os
 from resource_management import Script, format, Package, Execute, Fail
 from resource_management.core.logger import Logger
 from resource_management.libraries.functions import stack_tools
-from resource_management.libraries.functions.packages_analyzer import allInstalledPackages
 from resource_management.libraries.functions.stack_select import get_stack_versions
+from resource_management.core.providers import get_provider
 
 CURRENT_ = "/current/"
 stack_root = Script.get_stack_root()
@@ -43,6 +43,7 @@ class RemovePreviousStacks(Script):
     self.stack_tool_package = stack_tools.get_stack_tool_package(stack_tools.STACK_SELECTOR_NAME)
 
     versions_to_remove = self.get_lower_versions(version)
+    self.pkg_provider = get_provider("Package")
 
     for low_version in versions_to_remove:
       self.remove_stack_version(structured_output, low_version)
@@ -73,8 +74,8 @@ class RemovePreviousStacks(Script):
   def get_packages_to_remove(self, version):
     packages = []
     formated_version = version.replace('.', '_').replace('-', '_')
-    all_installed_packages = []
-    allInstalledPackages(all_installed_packages)
+    all_installed_packages = self.pkg_provider.all_installed_packages()
+
     all_installed_packages = [package[0] for package in all_installed_packages]
     for package in all_installed_packages:
       if formated_version in package and self.stack_tool_package not in package:


Mime
View raw message