You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by cr...@apache.org on 2013/12/09 15:41:23 UTC
git commit: AMBARI-1725. Check for iptables in ambari-server.py
should use firewalld.service on Fedora 18+ (Trevor McKay via croberts)
Updated Branches:
refs/heads/trunk 98081d63a -> 328c10e05
AMBARI-1725. Check for iptables in ambari-server.py should use firewalld.service on Fedora 18+ (Trevor McKay via croberts)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/328c10e0
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/328c10e0
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/328c10e0
Branch: refs/heads/trunk
Commit: 328c10e05d1826a6512781a669a902129717c13c
Parents: 98081d6
Author: Chad Roberts <cr...@redhat.com>
Authored: Mon Dec 9 09:40:22 2013 -0500
Committer: Chad Roberts <cr...@redhat.com>
Committed: Mon Dec 9 09:40:22 2013 -0500
----------------------------------------------------------------------
ambari-server/src/main/python/ambari-server.py | 121 ++++++++++++++-----
.../src/test/python/TestAmbariServer.py | 27 +++--
2 files changed, 108 insertions(+), 40 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/328c10e0/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 f0f287a..e51f074 100755
--- a/ambari-server/src/main/python/ambari-server.py
+++ b/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 ###
http://git-wip-us.apache.org/repos/asf/ambari/blob/328c10e0/ambari-server/src/test/python/TestAmbariServer.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestAmbariServer.py b/ambari-server/src/test/python/TestAmbariServer.py
index b3fc5eb..36fb6df 100644
--- a/ambari-server/src/test/python/TestAmbariServer.py
+++ b/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):