ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jlun...@apache.org
Subject ambari git commit: AMBARI-15964: Add purge-list option for management packs (jluniya)
Date Tue, 19 Apr 2016 18:05:56 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 1fc8fe9fa -> d4ee11cc0


AMBARI-15964: Add purge-list option for management packs (jluniya)


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

Branch: refs/heads/trunk
Commit: d4ee11cc087254a26434d5f7cd9677c3096e8eba
Parents: 1fc8fe9
Author: Jayush Luniya <jluniya@hortonworks.com>
Authored: Tue Apr 19 11:05:47 2016 -0700
Committer: Jayush Luniya <jluniya@hortonworks.com>
Committed: Tue Apr 19 11:05:47 2016 -0700

----------------------------------------------------------------------
 ambari-server/src/main/python/ambari-server.py  | 17 ++++++--
 .../main/python/ambari_server/setupMpacks.py    | 25 +++++++----
 ambari-server/src/test/python/TestMpacks.py     | 44 +++++++++++++++++++-
 3 files changed, 75 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/d4ee11cc/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 e34467c..d211e9c 100755
--- a/ambari-server/src/main/python/ambari-server.py
+++ b/ambari-server/src/main/python/ambari-server.py
@@ -36,7 +36,8 @@ from ambari_server.serverUtils import is_server_runing, refresh_stack_hash
 from ambari_server.serverSetup import reset, setup, setup_jce_policy
 from ambari_server.serverUpgrade import upgrade, upgrade_stack, set_current
 from ambari_server.setupHttps import setup_https, setup_truststore
-from ambari_server.setupMpacks import install_mpack, upgrade_mpack
+from ambari_server.setupMpacks import install_mpack, upgrade_mpack, STACK_DEFINITIONS_RESOURCE_NAME,
\
+  SERVICE_DEFINITIONS_RESOURCE_NAME, MPACKS_RESOURCE_NAME
 from ambari_server.setupSso import setup_sso
 from ambari_server.dbCleanup import db_cleanup
 from ambari_server.hostUpdate import update_host_names
@@ -334,8 +335,13 @@ def init_parser_options(parser):
                     help="Specified the path for management pack to be installed/upgraded",
                     dest="mpack_path")
   parser.add_option('--purge', action="store_true", default=False,
-                    help="Purge existing stack definitions and previously installed management
packs",
+                    help="Purge existing resources specified in purge-list",
                     dest="purge")
+  purge_resources = ",".join([STACK_DEFINITIONS_RESOURCE_NAME, SERVICE_DEFINITIONS_RESOURCE_NAME,
MPACKS_RESOURCE_NAME])
+  default_purge_resources = ",".join([STACK_DEFINITIONS_RESOURCE_NAME, MPACKS_RESOURCE_NAME])
+  parser.add_option('--purge-list', default=default_purge_resources,
+                    help="Comma separated list of resources to purge ({0}). By default ({1})
will be purged.".format(purge_resources, default_purge_resources),
+                    dest="purge_list")
   parser.add_option('--force', action="store_true", default=False, help="Force install management
pack", dest="force")
   # -b and -i the remaining available short options
   # -h reserved for help
@@ -409,8 +415,13 @@ def init_parser_options(parser):
                     help="Specified the path for management pack to be installed/upgraded",
                     dest="mpack_path")
   parser.add_option('--purge', action="store_true", default=False,
-                    help="Purge existing stack definitions and previously installed management
packs",
+                    help="Purge existing resources specified in purge-list",
                     dest="purge")
+  purge_resources = ",".join([STACK_DEFINITIONS_RESOURCE_NAME, SERVICE_DEFINITIONS_RESOURCE_NAME,
MPACKS_RESOURCE_NAME])
+  default_purge_resources = ",".join([STACK_DEFINITIONS_RESOURCE_NAME, MPACKS_RESOURCE_NAME])
+  parser.add_option('--purge-list', default=default_purge_resources,
+                    help="Comma separated list of resources to purge ({0}). By default ({1})
will be purged.".format(purge_resources, default_purge_resources),
+                    dest="purge_list")
   parser.add_option('--force', action="store_true", default=False, help="Force install management
pack", dest="force")
 
   parser.add_option('--ldap-url', default=None, help="Primary url for LDAP", dest="ldap_url")

http://git-wip-us.apache.org/repos/asf/ambari/blob/d4ee11cc/ambari-server/src/main/python/ambari_server/setupMpacks.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/ambari_server/setupMpacks.py b/ambari-server/src/main/python/ambari_server/setupMpacks.py
index 3aa071f..6994cbd 100644
--- a/ambari-server/src/main/python/ambari_server/setupMpacks.py
+++ b/ambari-server/src/main/python/ambari_server/setupMpacks.py
@@ -38,6 +38,9 @@ from ambari_server.setupActions import INSTALL_MPACK_ACTION, UPGRADE_MPACK_ACTIO
 
 MPACKS_REPLAY_LOG_FILENAME = "mpacks_replay.log"
 MPACKS_CACHE_DIRNAME = "cache"
+STACK_DEFINITIONS_RESOURCE_NAME = "stack-definitions"
+SERVICE_DEFINITIONS_RESOURCE_NAME = "service-definitions"
+MPACKS_RESOURCE_NAME = "mpacks"
 
 class _named_dict(dict):
   """
@@ -180,7 +183,7 @@ def remove_symlinks(stack_location, service_definitions_location, staged_mpack_d
           print_info_msg("Removing symlink {0}".format(dir))
           sudo.unlink(dir)
 
-def purge_stacks_and_mpacks(replay_mode=False):
+def purge_stacks_and_mpacks(purge_list, replay_mode=False):
   """
   Purge all stacks and management packs
   :param replay_mode: Flag to indicate if purging in replay mode
@@ -190,16 +193,24 @@ def purge_stacks_and_mpacks(replay_mode=False):
 
   print_info_msg("Purging existing stack definitions and management packs")
 
-  if os.path.exists(stack_location):
+  if not purge_list:
+    print_info_msg("Nothing to purge")
+    return
+
+  # Don't delete default stack_advisor.py (stacks/stack_advisor.py)
+  if STACK_DEFINITIONS_RESOURCE_NAME in purge_list and os.path.exists(stack_location):
     print_info_msg("Purging stack location: " + stack_location)
-    sudo.rmtree(stack_location)
+    for file in sorted(os.listdir(stack_location)):
+      path = os.path.join(stack_location, file)
+      if(os.path.isdir(path)):
+        sudo.rmtree(path)
 
-  if os.path.exists(service_definitions_location):
+  if SERVICE_DEFINITIONS_RESOURCE_NAME in purge_list and os.path.exists(service_definitions_location):
     print_info_msg("Purging service definitions location: " + service_definitions_location)
     sudo.rmtree(service_definitions_location)
 
   # Don't purge mpacks staging directory in replay mode
-  if os.path.exists(mpacks_staging_location) and not replay_mode:
+  if MPACKS_RESOURCE_NAME in purge_list and not replay_mode and os.path.exists(mpacks_staging_location):
     print_info_msg("Purging mpacks staging location: " + mpacks_staging_location)
     sudo.rmtree(mpacks_staging_location)
     sudo.makedir(mpacks_staging_location, 0755)
@@ -537,8 +548,8 @@ def _install_mpack(options, replay_mode=False):
     validate_mpack_prerequisites(mpack_metadata)
 
   # Purge previously installed stacks and management packs
-  if options.purge:
-    purge_stacks_and_mpacks(replay_mode)
+  if options.purge and options.purge_list:
+    purge_stacks_and_mpacks(options.purge_list.split(","), replay_mode)
 
   # Get ambari mpack properties
   stack_location, service_definitions_location, mpacks_staging_location = get_mpack_properties()

http://git-wip-us.apache.org/repos/asf/ambari/blob/d4ee11cc/ambari-server/src/test/python/TestMpacks.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestMpacks.py b/ambari-server/src/test/python/TestMpacks.py
index 68e00ee..63dd643 100644
--- a/ambari-server/src/test/python/TestMpacks.py
+++ b/ambari-server/src/test/python/TestMpacks.py
@@ -18,7 +18,8 @@ limitations under the License.
 import os
 from mock.mock import patch, MagicMock, call
 from ambari_commons.exceptions import FatalException
-from ambari_server.setupMpacks import install_mpack, upgrade_mpack, replay_mpack_logs
+from ambari_server.setupMpacks import install_mpack, upgrade_mpack, replay_mpack_logs, purge_stacks_and_mpacks,
\
+  STACK_DEFINITIONS_RESOURCE_NAME, SERVICE_DEFINITIONS_RESOURCE_NAME, MPACKS_RESOURCE_NAME
 from unittest import TestCase
 from ambari_server.serverConfiguration import STACK_LOCATION_KEY, COMMON_SERVICES_PATH_PROPERTY,
MPACKS_STAGING_PATH_PROPERTY
 
@@ -65,6 +66,45 @@ class TestMpacks(TestCase):
     self.assertTrue(fail)
 
   @patch("os.path.exists")
+  @patch("ambari_server.setupMpacks.get_ambari_properties")
+  def test_purge_stacks_and_mpacks(self, get_ambari_version_mock, os_path_exists_mock):
+    options = self._create_empty_options_mock()
+    get_ambari_version_mock.return_value = configs
+    stacks_directory = configs[STACK_LOCATION_KEY]
+    common_services_directory = configs[COMMON_SERVICES_PATH_PROPERTY]
+    mpacks_directory = configs[MPACKS_STAGING_PATH_PROPERTY]
+    os_path_exists_mock.return_value = False
+
+    purge_stacks_and_mpacks(None)
+    os_path_exists_calls = []
+    os_path_exists_mock.assert_has_calls(os_path_exists_calls)
+
+    purge_stacks_and_mpacks(options.purge_list.split(","))
+    os_path_exists_calls = [
+      call(stacks_directory),
+      call(mpacks_directory)
+    ]
+    os_path_exists_mock.assert_has_calls(os_path_exists_calls)
+
+    options.purge_list = ",".join([STACK_DEFINITIONS_RESOURCE_NAME, SERVICE_DEFINITIONS_RESOURCE_NAME,
MPACKS_RESOURCE_NAME])
+    purge_stacks_and_mpacks(options.purge_list.split(","))
+    os_path_exists_calls = [
+      call(stacks_directory),
+      call(common_services_directory),
+      call(mpacks_directory)
+    ]
+    os_path_exists_mock.assert_has_calls(os_path_exists_calls)
+
+    options.purge_list = ",".join([STACK_DEFINITIONS_RESOURCE_NAME, SERVICE_DEFINITIONS_RESOURCE_NAME,
MPACKS_RESOURCE_NAME])
+    options.replay_mode = True
+    purge_stacks_and_mpacks(options.purge_list.split(","))
+    os_path_exists_calls = [
+      call(stacks_directory),
+      call(common_services_directory)
+    ]
+    os_path_exists_mock.assert_has_calls(os_path_exists_calls)
+
+  @patch("os.path.exists")
   @patch("ambari_server.setupMpacks.extract_archive")
   @patch("ambari_server.setupMpacks.get_archive_root_dir")
   @patch("ambari_server.setupMpacks.download_mpack")
@@ -443,6 +483,8 @@ class TestMpacks(TestCase):
     options = MagicMock()
     options.mpack_path = None
     options.purge = None
+    options.purge_list = ",".join([STACK_DEFINITIONS_RESOURCE_NAME, MPACKS_RESOURCE_NAME])
     options.force = None
     options.verbose = None
+    options.replay_mode = False
     return options
\ No newline at end of file


Mime
View raw message