You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by tb...@apache.org on 2015/01/16 17:27:01 UTC

ambari git commit: AMBARI-9169 - Upgrade pack for Knox (tbeerbower)

Repository: ambari
Updated Branches:
  refs/heads/trunk e3763edde -> a71f942ed


AMBARI-9169 - Upgrade pack for Knox (tbeerbower)


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

Branch: refs/heads/trunk
Commit: a71f942eda00930e85f4c3b9451da2a58b58303f
Parents: e3763ed
Author: tbeerbower <tb...@hortonworks.com>
Authored: Fri Jan 16 10:19:44 2015 -0500
Committer: tbeerbower <tb...@hortonworks.com>
Committed: Fri Jan 16 11:26:51 2015 -0500

----------------------------------------------------------------------
 .../0.5.0.2.2/package/scripts/knox_gateway.py   | 14 +++-
 .../KNOX/0.5.0.2.2/package/scripts/params.py    |  6 ++
 .../KNOX/0.5.0.2.2/package/scripts/upgrade.py   | 71 ++++++++++++++++++++
 .../stacks/HDP/2.2/upgrades/upgrade-2.2.xml     | 17 ++++-
 .../python/stacks/2.2/KNOX/test_knox_gateway.py | 16 +++++
 5 files changed, 121 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/a71f942e/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/knox_gateway.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/knox_gateway.py b/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/knox_gateway.py
index 9332bff..7e5fe13 100644
--- a/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/knox_gateway.py
+++ b/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/knox_gateway.py
@@ -22,6 +22,7 @@ from resource_management.libraries.functions.security_commons import build_expec
   cached_kinit_executor, validate_security_config_properties, get_params_from_filesystem, \
   FILE_TYPE_XML
 import sys
+import upgrade
 
 from knox import knox
 from ldap import ldap
@@ -46,7 +47,16 @@ class KnoxGateway(Script):
     knox()
     ldap()
 
-  def start(self, env):
+  def pre_rolling_restart(self, env):
+    import params
+    env.set_params(params)
+
+    if params.version and compare_versions(format_hdp_stack_version(params.version), '2.2.0.0') >= 0:
+      upgrade.backup_data()
+      Execute(format("hdp-select set knox-server {version}"))
+
+
+  def start(self, env, rolling_restart=False):
     import params
     env.set_params(params)
     self.configure(env)
@@ -58,7 +68,7 @@ class KnoxGateway(Script):
             not_if=no_op_test
     )
 
-  def stop(self, env):
+  def stop(self, env, rolling_restart=False):
     import params
     env.set_params(params)
     self.configure(env)

http://git-wip-us.apache.org/repos/asf/ambari/blob/a71f942e/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/params.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/params.py b/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/params.py
index 3c91992..938d1d7 100644
--- a/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/params.py
+++ b/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/params.py
@@ -29,6 +29,8 @@ tmp_dir = Script.get_tmp_dir()
 
 stack_name = default("/hostLevelParams/stack_name", None)
 
+version = default("/commandParams/version", None)
+
 stack_version_unformatted = str(config['hostLevelParams']['stack_version'])
 hdp_stack_version = format_hdp_stack_version(stack_version_unformatted)
 
@@ -36,10 +38,14 @@ if hdp_stack_version != "" and compare_versions(hdp_stack_version, '2.2') >= 0:
   knox_bin = '/usr/hdp/current/knox-server/bin/gateway.sh'
   ldap_bin = '/usr/hdp/current/knox-server/bin/ldap.sh'
   knox_client_bin = '/usr/hdp/current/knox-server/bin/knoxcli.sh'
+  knox_data_dir = '/usr/hdp/current/knox-server/data'
+  knox_conf_dir = '/usr/hdp/current/knox-server/conf'
 else:
   knox_bin = '/usr/bin/gateway'
   ldap_bin = '/usr/lib/knox/bin/ldap.sh'
   knox_client_bin = '/usr/lib/knox/bin/knoxcli.sh'
+  knox_data_dir = '/usr/lib/knox/data'
+  knox_conf_dir = '/usr/lib/knox/conf'
 
 namenode_hosts = default("/clusterHostInfo/namenode_host", None)
 if type(namenode_hosts) is list:

http://git-wip-us.apache.org/repos/asf/ambari/blob/a71f942e/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/upgrade.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/upgrade.py b/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/upgrade.py
new file mode 100644
index 0000000..9976cb2
--- /dev/null
+++ b/ambari-server/src/main/resources/common-services/KNOX/0.5.0.2.2/package/scripts/upgrade.py
@@ -0,0 +1,71 @@
+
+#!/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 os
+import tarfile
+import tempfile
+
+from resource_management.core.logger import Logger
+from resource_management.core.exceptions import Fail
+
+BACKUP_TEMP_DIR = "knox-upgrade-backup"
+BACKUP_DATA_ARCHIVE = "knox-data-backup.tar"
+BACKUP_CONF_ARCHIVE = "knox-conf-backup.tar"
+
+def backup_data():
+  """
+  Backs up the knox data as part of the upgrade process.
+  :return:
+  """
+  Logger.info('Backing up Knox data directory before upgrade...')
+  directoryMappings = _get_directory_mappings()
+
+  absolute_backup_dir = os.path.join(tempfile.gettempdir(), BACKUP_TEMP_DIR)
+  if not os.path.isdir(absolute_backup_dir):
+    os.makedirs(absolute_backup_dir)
+
+  for directory in directoryMappings:
+    if not os.path.isdir(directory):
+      raise Fail("Unable to backup missing directory {0}".format(directory))
+
+    archive = os.path.join(absolute_backup_dir, directoryMappings[directory])
+    Logger.info('Compressing {0} to {1}'.format(directory, archive))
+
+    if os.path.exists(archive):
+      os.remove(archive)
+
+    tarball = None
+    try:
+      tarball = tarfile.open(archive, "w")
+      tarball.add(directory, arcname=os.path.basename(directory))
+    finally:
+      if tarball:
+        tarball.close()
+
+def _get_directory_mappings():
+  """
+  Gets a dictionary of directory to archive name that represents the
+  directories that need to be backed up and their output tarball archive targets
+  :return:  the dictionary of directory to tarball mappings
+  """
+  import params
+
+  return { params.knox_data_dir : BACKUP_DATA_ARCHIVE, params.knox_conf_dir : BACKUP_CONF_ARCHIVE }
+

http://git-wip-us.apache.org/repos/asf/ambari/blob/a71f942e/ambari-server/src/main/resources/stacks/HDP/2.2/upgrades/upgrade-2.2.xml
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/stacks/HDP/2.2/upgrades/upgrade-2.2.xml b/ambari-server/src/main/resources/stacks/HDP/2.2/upgrades/upgrade-2.2.xml
index 15aa1f5..75730f2 100644
--- a/ambari-server/src/main/resources/stacks/HDP/2.2/upgrades/upgrade-2.2.xml
+++ b/ambari-server/src/main/resources/stacks/HDP/2.2/upgrades/upgrade-2.2.xml
@@ -98,7 +98,14 @@
       <service name="KAFKA">
         <component>KAFKA_BROKER</component>
       </service>
-   </group>
+    </group>
+
+    <group name="KNOX" title="Knox">
+      <skippable>true</skippable>
+      <service name="KNOX">
+        <component>KNOX_GATEWAY</component>
+      </service>
+    </group>
 
     <group name="STORM" title="Storm">
       <skippable>true</skippable>
@@ -425,6 +432,14 @@
       </component>
     </service>
 
+    <service name="KNOX">
+      <component name="KNOX_GATEWAY">
+        <upgrade>
+          <task xsi:type="restart" />
+        </upgrade>
+      </component>
+    </service>
+
     <service name="STORM">
       <component name="NIMBUS">
         <pre-upgrade>

http://git-wip-us.apache.org/repos/asf/ambari/blob/a71f942e/ambari-server/src/test/python/stacks/2.2/KNOX/test_knox_gateway.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/stacks/2.2/KNOX/test_knox_gateway.py b/ambari-server/src/test/python/stacks/2.2/KNOX/test_knox_gateway.py
index debd77e..3d0ca6e 100644
--- a/ambari-server/src/test/python/stacks/2.2/KNOX/test_knox_gateway.py
+++ b/ambari-server/src/test/python/stacks/2.2/KNOX/test_knox_gateway.py
@@ -197,3 +197,19 @@ class TestKnoxGateway(RMFTestCase):
                        target = RMFTestCase.TARGET_COMMON_SERVICES
     )
     put_structured_out_mock.assert_called_with({"securityState": "UNSECURED"})
+
+  @patch("tarfile.open")
+  @patch("os.path.isdir")
+  def test_pre_rolling_restart(self, isdir_mock, tarfile_open_mock):
+    isdir_mock.return_value = True
+
+    self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/knox_gateway.py",
+                       classname = "KnoxGateway",
+                       command = "pre_rolling_restart",
+                       config_file="default.json",
+                       hdp_stack_version = self.STACK_VERSION,
+                       target = RMFTestCase.TARGET_COMMON_SERVICES)
+
+    self.assertTrue(tarfile_open_mock.called)
+
+    self.assertResourceCalled("Execute", "hdp-select set knox-server 2.2.1.0-2067")