浏览代码

AMBARI-1725. Check for iptables in ambari-server.py should use firewalld.service on Fedora 18+ (Trevor McKay via croberts)

Chad Roberts 11 年之前
父节点
当前提交
328c10e05d
共有 2 个文件被更改,包括 108 次插入40 次删除
  1. 92 29
      ambari-server/src/main/python/ambari-server.py
  2. 16 11
      ambari-server/src/test/python/TestAmbariServer.py

+ 92 - 29
ambari-server/src/main/python/ambari-server.py

@@ -46,8 +46,11 @@ SILENT = False
 SERVER_START_DEBUG = False
 
 # OS info
-OS = platform.dist()[0].lower()
+OS, OS_VERSION, _ = platform.linux_distribution() 
+OS = OS.lower().strip()
 OS_UBUNTU = 'ubuntu'
+OS_FEDORA = 'fedora'
+OS_OPENSUSE = 'opensuse'
 
 # action commands
 SETUP_ACTION = "setup"
@@ -231,15 +234,6 @@ PG_DEFAULT_PASSWORD = "bigdata"
 SERVICE_CMD = "/sbin/service"
 PG_SERVICE_NAME = "postgresql"
 PG_HBA_DIR = "/var/lib/pgsql/data"
-# iptables commands
-FIREWALL_SERVICE_NAME = "iptables"
-# on ubuntu iptables service is called ufw and other changes
-if OS == OS_UBUNTU:
-  FIREWALL_SERVICE_NAME = "ufw"
-  PG_HBA_DIR = '/etc/postgresql/8.4/main'
-  SERVICE_CMD = "/usr/sbin/service"
-
-IP_TBLS_STATUS_CMD = "%s %s status" % (SERVICE_CMD, FIREWALL_SERVICE_NAME)
 
 PG_ST_CMD = "%s %s status" % (SERVICE_CMD, PG_SERVICE_NAME)
 if os.path.isfile("/usr/bin/postgresql-setup"):
@@ -410,6 +404,94 @@ ASF_LICENSE_HEADER = '''
 # limitations under the License.
 '''
 
+if OS == OS_UBUNTU:
+  PG_HBA_DIR = '/etc/postgresql/8.4/main'
+
+class FirewallChecks(object):
+  def __init__(self):
+
+    self.FIREWALL_SERVICE_NAME = "iptables"
+    self.SERVICE_CMD = SERVICE_CMD
+    self.SERVICE_SUBCMD = "status"
+
+  def get_command(self):
+    return "%s %s %s" % (self.SERVICE_CMD, self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
+
+  def check_result(self, retcode, out, err):
+      return retcode == 0
+
+  def check_iptables(self):
+    retcode, out, err = run_os_command(self.get_command())
+    if err and len(err) > 0:
+      print err
+    if self.check_result(retcode, out, err):
+      print_warning_msg("%s is running. Confirm the necessary Ambari ports are accessible. " %
+                        self.FIREWALL_SERVICE_NAME +
+                        "Refer to the Ambari documentation for more details on ports.")
+      ok = get_YN_input("OK to continue [y/n] (y)? ", True)
+      if not ok:
+        raise FatalException(1, None)
+
+  def get_running_result(self):
+    # To support test code.  Expected ouput from run_os_command.
+    return (0, "", "")
+
+  def get_stopped_result(self):
+    # To support test code.  Expected output from run_os_command.
+    return (3, "", "")
+
+class UbuntuFirewallChecks(FirewallChecks):
+  def __init__(self):
+    super(UbuntuFirewallChecks, self).__init__()
+
+    self.FIREWALL_SERVICE_NAME = "ufw"
+    self.SERVICE_CMD = "/usr/sbin/service"
+
+  def check_result(self, retcode, out, err):
+    # On ubuntu, the status command returns 0 whether running or not
+    return out and len(out) > 0 and out.strip() != "ufw stop/waiting"
+
+  def get_running_result(self):
+    # To support test code.  Expected ouput from run_os_command.
+    return (0, "ufw start/running", "")
+
+  def get_stopped_result(self):
+    # To support test code.  Expected output from run_os_command.
+    return (0, "ufw stop/waiting", "")
+
+class Fedora18FirewallChecks(FirewallChecks):
+  def __init__(self):
+    self.FIREWALL_SERVICE_NAME = "firewalld.service"
+
+  def get_command(self):
+    return "systemctl is-active firewalld.service"
+
+class OpenSuseFirewallChecks(FirewallChecks):
+  def __init__(self):
+    self.FIREWALL_SERVICE_NAME = "SuSEfirewall2"
+
+  def get_command(self):
+    return "/sbin/SuSEfirewall2 status"
+
+def get_firewall_object():
+  if OS == OS_UBUNTU:
+    return UbuntuFirewallChecks()
+  elif OS == OS_FEDORA and int(OS_VERSION) >= 18:
+    return Fedora18FirewallChecks()
+  elif OS == OS_OPENSUSE:
+    return OpenSuseFirewallChecks()
+  else:
+    return FirewallChecks()
+
+def get_firewall_object_types():
+  # To support test code, so tests can loop through the types
+  return (FirewallChecks, 
+          UbuntuFirewallChecks, 
+          Fedora18FirewallChecks,
+          OpenSuseFirewallChecks)
+
+def check_iptables():
+  return get_firewall_object().check_iptables()
 
 def get_conf_dir():
   try:
@@ -724,25 +806,6 @@ def check_ambari_user():
     return 1
   return 0
 
-
-
-#
-# Checks iptables
-#
-def check_iptables():
-  retcode, out, err = run_os_command(IP_TBLS_STATUS_CMD)
-
-  if err and len(err) > 0:
-    print err
-
-  if retcode == 0:
-    print_warning_msg("%s is running. Confirm the necessary Ambari ports are accessible. " % FIREWALL_SERVICE_NAME +
-      "Refer to the Ambari documentation for more details on ports.")
-    ok = get_YN_input("OK to continue [y/n] (y)? ", True)
-    if not ok:
-      raise FatalException(1, None)
-
-
 ### Postgres ###
 
 

+ 16 - 11
ambari-server/src/test/python/TestAmbariServer.py

@@ -1005,21 +1005,26 @@ class TestAmbariServer(TestCase):
   @patch.object(ambari_server, "print_warning_msg")
   @patch.object(ambari_server, "get_YN_input")
   def test_check_iptables_is_running(self, get_YN_input_mock, print_warning_msg, run_os_command_mock):
-    run_os_command_mock.return_value = (0, "", "")
-    get_YN_input_mock.side_effect = [True]
-    ambari_server.check_iptables()
-    self.assertEqual(print_warning_msg.call_args_list[0][0][0],
-      "%s is running. Confirm the necessary Ambari ports are accessible. " % ambari_server.FIREWALL_SERVICE_NAME +
-      "Refer to the Ambari documentation for more details on ports." 
-    )
+    counter = 0
+    for fwo_type in ambari_server.get_firewall_object_types():
+      fwo = fwo_type()
+      run_os_command_mock.return_value = fwo.get_running_result()
+      get_YN_input_mock.side_effect = [True]
+      fwo.check_iptables()
+      self.assertEqual(len(print_warning_msg.call_args_list), counter+1)
+      self.assertEqual(print_warning_msg.call_args_list[counter][0][0],
+        "%s is running. Confirm the necessary Ambari ports are accessible. " % fwo.FIREWALL_SERVICE_NAME +
+        "Refer to the Ambari documentation for more details on ports.")
+      counter += 1
 
   @patch.object(ambari_server, "run_os_command")
   @patch.object(ambari_server, "print_warning_msg")
   def test_check_iptables_is_not_running(self, print_warning_msg, run_os_command_mock):
-    run_os_command_mock.return_value = (3, "", "")
-    ambari_server.check_iptables()
-
-    self.assertFalse(print_warning_msg.called)
+    for fwo_type in ambari_server.get_firewall_object_types():
+      fwo = fwo_type()
+      run_os_command_mock.return_value = fwo.get_stopped_result()
+      fwo.check_iptables()
+      self.assertFalse(print_warning_msg.called)
 
   def test_dlprogress(self):