You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ds...@apache.org on 2014/05/30 16:22:27 UTC

git commit: AMBARI-5911 OS constants should be located in one place (dsen)

Repository: ambari
Updated Branches:
  refs/heads/trunk 4d4b62f80 -> 3d66c8efc


AMBARI-5911 OS constants should be located in one place (dsen)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/3d66c8ef
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/3d66c8ef
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/3d66c8ef

Branch: refs/heads/trunk
Commit: 3d66c8efc5bf40bf2e825648513b24b32fad8245
Parents: 4d4b62f
Author: Dmitry Sen <ds...@hortonworks.com>
Authored: Fri May 30 17:21:50 2014 +0300
Committer: Dmitry Sen <ds...@hortonworks.com>
Committed: Fri May 30 17:22:10 2014 +0300

----------------------------------------------------------------------
 .../src/main/python/ambari_agent/HostInfo.py    | 26 +++-----
 .../main/python/common_functions/__init__.py    |  3 +-
 .../main/python/common_functions/os_check.py    | 69 ++++++++++++++++----
 ambari-server/src/main/python/ambari-server.py  | 12 ++--
 .../src/main/python/ambari_server/utils.py      | 12 +---
 ambari-server/src/main/python/bootstrap.py      | 11 +---
 ambari-server/src/test/python/TestOSCheck.py    |  2 +-
 7 files changed, 81 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/3d66c8ef/ambari-agent/src/main/python/ambari_agent/HostInfo.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/HostInfo.py b/ambari-agent/src/main/python/ambari_agent/HostInfo.py
index 1e74dea..3dcc8f2 100644
--- a/ambari-agent/src/main/python/ambari_agent/HostInfo.py
+++ b/ambari-agent/src/main/python/ambari_agent/HostInfo.py
@@ -31,7 +31,7 @@ import platform
 from PackagesAnalyzer import PackagesAnalyzer
 from HostCheckReportFileHandler import HostCheckReportFileHandler
 from Hardware import Hardware
-from common_functions import OSCheck
+from common_functions import OSCheck, OSConst
 import socket
 
 logger = logging.getLogger()
@@ -41,21 +41,14 @@ OS_VERSION = OSCheck().get_os_major_version()
 OS_TYPE = OSCheck.get_os_type()
 OS_FAMILY = OSCheck.get_os_family()
 
-# OS constants
-OS_UBUNTU_DEBIAN = 'debian'
-OS_UBUNTU = 'ubuntu'
-OS_FEDORA = 'fedora'
-OS_OPENSUSE = 'opensuse'
-OS_SUSE = 'suse'
-OS_SUSE_ENTERPRISE = 'sles'
-
 # service cmd
 SERVICE_CMD = "/sbin/service"
 
 # on ubuntu iptables service is called ufw
-if OS_FAMILY == OS_UBUNTU_DEBIAN:
+if OS_FAMILY == OSConst.DEBIAN_FAMILY:
   SERVICE_CMD = "/usr/sbin/service"
 
+
 class HostInfo:
   # List of project names to be used to find alternatives folders etc.
   DEFAULT_PROJECT_NAMES = [
@@ -68,7 +61,7 @@ class HostInfo:
 
   # List of live services checked for on the host, takes a map of plan strings
   DEFAULT_LIVE_SERVICES = [
-    {"redhat":"ntpd", "suse":"ntp", "debian":"ntp"}
+    {OSConst.REDHAT_FAMILY: "ntpd", OSConst.SUSE_FAMILY: "ntp", OSConst.DEBIAN_FAMILY: "ntp"}
   ]
 
   # Set of default users (need to be replaced with the configured user names)
@@ -134,7 +127,6 @@ class HostInfo:
     self.packages = PackagesAnalyzer()
     self.reportFileHandler = HostCheckReportFileHandler(config)
 
-
   def dirType(self, path):
     if not os.path.exists(path):
       return 'not_exist'
@@ -212,9 +204,9 @@ class HostInfo:
         homeDir = fields[5]
         result['name'] = fields[0]
         result['homeDir'] = fields[5]
-        result['status'] = "Available";
+        result['status'] = "Available"
         if not os.path.exists(homeDir):
-          result['status'] = "Invalid home directory";
+          result['status'] = "Invalid home directory"
         results.append(result)
 
   def osdiskAvailableSpace(self, path):
@@ -288,11 +280,11 @@ class HostInfo:
      return self.current_umask
 
   def getFirewallObject(self):
-    if OS_TYPE == OS_UBUNTU:
+    if OS_TYPE == OSConst.OS_UBUNTU:
       return UbuntuFirewallChecks()
-    elif OS_TYPE == OS_FEDORA and int(OS_VERSION) >= 18:
+    elif OS_TYPE == OSConst.OS_FEDORA and int(OS_VERSION) >= 18:
       return Fedora18FirewallChecks()
-    elif OS_TYPE == OS_SUSE or OS_TYPE == OS_OPENSUSE or OS_TYPE == OS_SUSE_ENTERPRISE:
+    elif OS_FAMILY == OSConst.SUSE_FAMILY:
       return SuseFirewallChecks()
     else:
       return FirewallChecks()

http://git-wip-us.apache.org/repos/asf/ambari/blob/3d66c8ef/ambari-common/src/main/python/common_functions/__init__.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/common_functions/__init__.py b/ambari-common/src/main/python/common_functions/__init__.py
index 50baaba..0b936d4 100644
--- a/ambari-common/src/main/python/common_functions/__init__.py
+++ b/ambari-common/src/main/python/common_functions/__init__.py
@@ -18,8 +18,9 @@ See the License for the specific language governing permissions and
 limitations under the License.
 '''
 
-from common_functions.os_check import OSCheck
+from common_functions.os_check import OSCheck, OSConst
 
 __all__ = [
   'OSCheck',
+  'OSConst',
 ]

http://git-wip-us.apache.org/repos/asf/ambari/blob/3d66c8ef/ambari-common/src/main/python/common_functions/os_check.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/common_functions/os_check.py b/ambari-common/src/main/python/common_functions/os_check.py
index 79aaa75..ebdaf06 100644
--- a/ambari-common/src/main/python/common_functions/os_check.py
+++ b/ambari-common/src/main/python/common_functions/os_check.py
@@ -33,6 +33,55 @@ def linux_distribution():
 
   return linux_distribution
 
+
+class OS_CONST_TYPE(type):
+  # os families
+  REDHAT_FAMILY = 'redhat'
+  DEBIAN_FAMILY = 'debian'
+  SUSE_FAMILY = 'suse'
+
+  # Declare here os type mapping
+  OS_FAMILY_COLLECTION = [
+                            {'name': REDHAT_FAMILY,
+                             'os_list':
+                                ['redhat', 'fedora', 'centos', 'oraclelinux',
+                                 'ascendos', 'amazon', 'xenserver', 'oel', 'ovs',
+                                 'cloudlinux', 'slc', 'scientific', 'psbm',
+                                 'centos linux']
+                             },
+                            {'name': DEBIAN_FAMILY,
+                             'os_list': ['ubuntu', 'debian']
+                             },
+                            {'name': SUSE_FAMILY,
+                             'os_list': ['sles', 'sled', 'opensuse', 'suse']
+                             }
+                           ]
+  # Would be generated from Family collection definition
+  OS_COLLECTION = []
+
+  def __init__(cls, name, bases, dct):
+    for item in cls.OS_FAMILY_COLLECTION:
+      cls.OS_COLLECTION += item['os_list']
+
+  def __getattr__(cls, name):
+    """
+      Added support of class.OS_<os_type> properties defined in OS_COLLECTION
+      Example:
+              OSConst.OS_CENTOS would return centos
+              OSConst.OS_OTHEROS would triger an error, coz
+               that os is not present in OS_FAMILY_COLLECTION map
+    """
+    name = name.lower()
+    if "os_" in name and name[3:] in cls.OS_COLLECTION:
+      return name[3:]
+    else:
+      raise Exception("Unknown class property '%s'" % name)
+
+
+class OSConst:
+  __metaclass__ = OS_CONST_TYPE
+
+
 class OSCheck:
 
   @staticmethod
@@ -72,15 +121,11 @@ class OSCheck:
     In case cannot detect raises exception( from self.get_operating_system_type() ).
     """
     os_family = OSCheck.get_os_type()
-    if os_family in ['redhat', 'fedora', 'centos', 'oraclelinux', 'ascendos',
-                     'amazon', 'xenserver', 'oel', 'ovs', 'cloudlinux',
-                     'slc', 'scientific', 'psbm', 'centos linux']:
-      os_family = 'RedHat'
-    elif os_family in ['ubuntu', 'debian']:
-      os_family = 'Debian'
-    elif os_family in ['sles', 'sled', 'opensuse', 'suse']:
-      os_family = 'Suse'
-    #else:  os_family = OSCheck.get_os_type()
+    for os_family_item in OSConst.OS_FAMILY_COLLECTION:
+      if os_family in os_family_item['os_list']:
+        os_family = os_family_item['name']
+        break
+
     return os_family.lower()
 
   @staticmethod
@@ -134,7 +179,7 @@ class OSCheck:
      This is safe check for debian family, doesn't generate exception
     """
     try:
-      if OSCheck.get_os_family() == "debian":
+      if OSCheck.get_os_family() == OSConst.DEBIAN_FAMILY:
         return True
     except Exception:
       pass
@@ -148,7 +193,7 @@ class OSCheck:
      This is safe check for suse family, doesn't generate exception
     """
     try:
-      if OSCheck.get_os_family() == "suse":
+      if OSCheck.get_os_family() == OSConst.SUSE_FAMILY:
         return True
     except Exception:
       pass
@@ -162,7 +207,7 @@ class OSCheck:
      This is safe check for redhat family, doesn't generate exception
     """
     try:
-      if OSCheck.get_os_family() == "redhat":
+      if OSCheck.get_os_family() == OSConst.REDHAT_FAMILY:
         return True
     except Exception:
       pass

http://git-wip-us.apache.org/repos/asf/ambari/blob/3d66c8ef/ambari-server/src/main/python/ambari-server.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/ambari-server.py b/ambari-server/src/main/python/ambari-server.py
index 304ebdd..6ad94d5 100755
--- a/ambari-server/src/main/python/ambari-server.py
+++ b/ambari-server/src/main/python/ambari-server.py
@@ -41,7 +41,7 @@ import random
 import pwd
 from ambari_server.resourceFilesKeeper import ResourceFilesKeeper, KeeperException
 import json
-from common_functions import OSCheck
+from common_functions import OSCheck, OSConst
 from ambari_server import utils
 
 # debug settings
@@ -494,11 +494,11 @@ class OpenSuseFirewallChecks(FirewallChecks):
 
 
 def get_firewall_object():
-  if OS_TYPE == utils.OS_UBUNTU:
+  if OS_TYPE == OSConst.OS_UBUNTU:
     return UbuntuFirewallChecks()
-  elif OS_TYPE == utils.OS_FEDORA and int(OS_VERSION) >= 18:
+  elif OS_TYPE == OSConst.OS_FEDORA and int(OS_VERSION) >= 18:
     return Fedora18FirewallChecks()
-  elif OS_TYPE == utils.OS_OPENSUSE:
+  elif OS_TYPE == OSConst.OS_OPENSUSE:
     return OpenSuseFirewallChecks()
   else:
     return FirewallChecks()
@@ -1078,7 +1078,7 @@ def check_postgre_up():
     return 0
   else:
     # run initdb only on non ubuntu systems as ubuntu does not have initdb cmd.
-    if OS_TYPE != utils.OS_UBUNTU:
+    if OS_TYPE != OSConst.OS_UBUNTU:
       print "Running initdb: This may take upto a minute."
       retcode, out, err = run_os_command(PG_INITDB_CMD)
       if retcode == 0:
@@ -1090,7 +1090,7 @@ def check_postgre_up():
                                  stdin=subprocess.PIPE,
                                  stderr=subprocess.PIPE
                                  )
-      if OS_TYPE == utils.OS_SUSE:
+      if OS_TYPE == OSConst.OS_SUSE:
         time.sleep(20)
         result = process.poll()
         print_info_msg("Result of postgres start cmd: " + str(result))

http://git-wip-us.apache.org/repos/asf/ambari/blob/3d66c8ef/ambari-server/src/main/python/ambari_server/utils.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/ambari_server/utils.py b/ambari-server/src/main/python/ambari_server/utils.py
index c132f50..6f5be59 100644
--- a/ambari-server/src/main/python/ambari_server/utils.py
+++ b/ambari-server/src/main/python/ambari_server/utils.py
@@ -18,13 +18,7 @@ See the License for the specific language governing permissions and
 limitations under the License.
 '''
 import os
-
-#OS constants
-OS_UBUNTU = 'ubuntu'
-OS_FEDORA = 'fedora'
-OS_OPENSUSE = 'opensuse'
-OS_SUSE = 'suse'
-OS_SUSE_ENTERPRISE = 'sles'
+from common_functions import OSConst
 
 #PostgreSQL settings
 UBUNTU_PG_HBA_ROOT = "/etc/postgresql"
@@ -65,7 +59,7 @@ def get_ubuntu_pg_version():
 
 def get_postgre_hba_dir(OS):
   """Return postgre hba dir location depends on OS"""
-  if OS == OS_UBUNTU:
+  if OS == OSConst.OS_UBUNTU:
     return "%s/%s/main" % (UBUNTU_PG_HBA_ROOT, get_ubuntu_pg_version())
   else:
     return PG_HBA_ROOT_DEFAULT
@@ -73,7 +67,7 @@ def get_postgre_hba_dir(OS):
 
 def get_postgre_running_status(OS):
   """Return postgre running status indicator"""
-  if OS == OS_UBUNTU:
+  if OS == OSConst.OS_UBUNTU:
     return "%s/main" % get_ubuntu_pg_version()
   else:
     return PG_STATUS_RUNNING_DEFAULT

http://git-wip-us.apache.org/repos/asf/ambari/blob/3d66c8ef/ambari-server/src/main/python/bootstrap.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/bootstrap.py b/ambari-server/src/main/python/bootstrap.py
index 650751f..b32d8fe 100755
--- a/ambari-server/src/main/python/bootstrap.py
+++ b/ambari-server/src/main/python/bootstrap.py
@@ -388,7 +388,6 @@ class Bootstrap(threading.Thread):
     self.host_log.write("\n")
     return retcode
 
-
   def runSetupAgent(self):
     params = self.shared_state
     self.host_log.write("==========================\n")
@@ -400,7 +399,6 @@ class Bootstrap(threading.Thread):
     self.host_log.write("\n")
     return retcode
 
-
   def createDoneFile(self, retcode):
     """ Creates .done file for current host. These files are later read from Java code.
     If .done file for any host is not created, the bootstrap will hang or fail due to timeout"""
@@ -414,7 +412,7 @@ class Bootstrap(threading.Thread):
   def getServerFamily(self):
     '''Return server OS family and version'''
     cot = re.search("([^\d]+)([\d]*)", self.shared_state.cluster_os_type)
-    return cot.group(1).lower(),cot.group(2).lower()
+    return cot.group(1).lower(), cot.group(2).lower()
 
   def checkSudoPackage(self):
     """ Checking 'sudo' package on remote host """
@@ -427,13 +425,12 @@ class Bootstrap(threading.Thread):
       command = "rpm -qa | grep -e '^sudo\-'"
     ssh = SSH(params.user, params.sshkey_file, self.host, command,
               params.bootdir, self.host_log,
-              errorMessage="Error: Sudo command is not available. " \
+              errorMessage="Error: Sudo command is not available. "
                            "Please install the sudo command.")
     retcode = ssh.run()
     self.host_log.write("\n")
     return retcode
 
-
   def copyPasswordFile(self):
     # Copy the password file
     self.host_log.write("Copying password file to 'tmp' folder...")
@@ -454,7 +451,6 @@ class Bootstrap(threading.Thread):
     self.host_log.write("Copying password file finished")
     return max(retcode1["exitstatus"], retcode2["exitstatus"])
 
-
   def changePasswordFileModeOnHost(self):
     # Change password file mode to 600
     self.host_log.write("Changing password file mode...")
@@ -466,7 +462,6 @@ class Bootstrap(threading.Thread):
     self.host_log.write("Change password file mode on host finished")
     return retcode
 
-
   def deletePasswordFile(self):
     # Deleting the password file
     self.host_log.write("Deleting password file...")
@@ -486,7 +481,7 @@ class Bootstrap(threading.Thread):
         last_retcode["exitstatus"] = retcode
       else:
         last_retcode = retcode
-    except Exception, e:
+    except Exception:
       self.host_log.write("Traceback: " + traceback.format_exc())
     return last_retcode
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3d66c8ef/ambari-server/src/test/python/TestOSCheck.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestOSCheck.py b/ambari-server/src/test/python/TestOSCheck.py
index f420095..7a29ec3 100644
--- a/ambari-server/src/test/python/TestOSCheck.py
+++ b/ambari-server/src/test/python/TestOSCheck.py
@@ -27,7 +27,7 @@ import sys
 from unittest import TestCase
 from mock.mock import patch
 
-from common_functions import OSCheck
+from common_functions import OSCheck, OSConst
 import os_check_type
 
 with patch("platform.linux_distribution", return_value=('Suse', '11', 'Final')):