You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ma...@apache.org on 2013/05/31 17:03:51 UTC

svn commit: r1488264 - in /incubator/ambari/trunk/ambari-server/src: main/python/ambari-server.py test/python/TestAmbaryServer.py

Author: mahadev
Date: Fri May 31 15:03:50 2013
New Revision: 1488264

URL: http://svn.apache.org/r1488264
Log:
AMBARI-2243. Automate updating ambari.properties as a part of ambari-server upgrade. (Dmitry Sen via mahadev)

Modified:
    incubator/ambari/trunk/ambari-server/src/main/python/ambari-server.py
    incubator/ambari/trunk/ambari-server/src/test/python/TestAmbaryServer.py

Modified: incubator/ambari/trunk/ambari-server/src/main/python/ambari-server.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/python/ambari-server.py?rev=1488264&r1=1488263&r2=1488264&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/python/ambari-server.py (original)
+++ incubator/ambari/trunk/ambari-server/src/main/python/ambari-server.py Fri May 31 15:03:50 2013
@@ -34,6 +34,8 @@ import fileinput
 import urllib2
 import time
 import getpass
+import datetime
+
 # debug settings
 VERBOSE = False
 SILENT = False
@@ -99,6 +101,7 @@ JAVA_HOME="JAVA_HOME"
 PID_DIR="/var/run/ambari-server"
 PID_NAME="ambari-server.pid"
 AMBARI_PROPERTIES_FILE="ambari.properties"
+AMBARI_PROPERTIES_RPMSAVE_FILE="ambari.properties.rpmsave"
 
 SETUP_DB_CMD = ['su', '-', 'postgres',
         '--command=psql -f {0} -v username=\'"{1}"\' -v password="\'{2}\'"']
@@ -149,6 +152,27 @@ JCE_POLICY_FILENAME = "jce_policy-6.zip"
 JCE_DOWNLOAD_CMD = "curl -o {0} {1}"
 JCE_MIN_FILESIZE = 5000
 
+#Apache License Header
+ASF_LICENSE_HEADER = '''
+# Copyright 2011 The Apache Software Foundation
+#
+# 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.
+'''
+
 def configure_pg_hba_ambaridb_users():
   args = optparse.Values()
   configure_postgres_username_password(args)
@@ -768,7 +792,49 @@ def check_postgre_up():
     retcode, out, err = run_os_command(PG_START_CMD)
     return retcode
 
+def update_ambari_properties():
+  prev_conf_file = search_file(AMBARI_PROPERTIES_RPMSAVE_FILE, get_conf_dir())
+  conf_file = search_file(AMBARI_PROPERTIES_FILE, get_conf_dir())
+  old_properties = None
+  new_properties = None
+
+  prop_dict = {JDBC_USER_NAME_PROPERTY : None,
+               JDBC_PASSWORD_FILE_PROPERTY : None,
+               JAVA_HOME_PROPERTY : None,
+               OS_TYPE_PROPERTY : None}
 
+  try:
+    old_properties = Properties()
+    old_properties.load(open(prev_conf_file))
+  except (Exception), e:
+    print 'Could not read "%s": %s' % (prev_conf_file, e)
+    return -1
+
+  for prop_key in prop_dict.keys():
+    value = old_properties.get_property(prop_key)
+    if value:
+      prop_dict[prop_key] = value
+
+  try:
+    new_properties = Properties()
+    new_properties.load(open(conf_file))
+
+    for prop_key,prop_value in prop_dict.items():
+      if not prop_value is None:
+        new_properties.process_pair(prop_key,prop_value)
+
+    new_properties.store(open(conf_file,'w'))
+
+  except (Exception), e:
+    print 'Could not write "%s": %s' % (conf_file, e)
+    return -1
+
+  timestamp = datetime.datetime.now()
+  format = '%Y%m%d%H%M%S'
+  os.rename(AMBARI_PROPERTIES_RPMSAVE_FILE, AMBARI_PROPERTIES_RPMSAVE_FILE +
+    '.' + timestamp.strftime(format))
+
+  return 0
 
 #
 # Configures the OS settings in ambari properties.
@@ -1025,6 +1091,13 @@ def stop(args):
 # Upgrades the Ambari Server.
 #
 def upgrade(args):
+
+  print 'Updating properties in ' + AMBARI_PROPERTIES_FILE + ' ...'
+  retcode = update_ambari_properties()
+  if not retcode == 0:
+    printErrorMsg(AMBARI_PROPERTIES_FILE + ' file can\'t be updated. Exiting')
+    sys.exit(retcode)
+
   print 'Checking PostgreSQL...'
   retcode = check_postgre_up()
   if not retcode == 0:
@@ -1437,6 +1510,24 @@ class Properties(object):
       if hasattr(self._props, name):
         return getattr(self._props, name)
 
+  def store(self, out, header=""):
+    """ Write the properties list to the stream 'out' along
+    with the optional 'header' """
+    if out.mode[0] != 'w':
+      raise ValueError,'Steam should be opened in write mode!'
+    try:
+      out.write(''.join(('#', ASF_LICENSE_HEADER, '\n')))
+      out.write(''.join(('#',header,'\n')))
+      # Write timestamp
+      tstamp = time.strftime('%a %b %d %H:%M:%S %Z %Y', time.localtime())
+      out.write(''.join(('#',tstamp,'\n')))
+      # Write properties from the pristine dictionary
+      for prop, val in self._origprops.items():
+        if val is not None:
+          out.write(''.join((prop,'=',val,'\n')))
+      out.close()
+    except IOError, e:
+      raise
 
 if __name__ == "__main__":
   main()

Modified: incubator/ambari/trunk/ambari-server/src/test/python/TestAmbaryServer.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/python/TestAmbaryServer.py?rev=1488264&r1=1488263&r2=1488264&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/python/TestAmbaryServer.py (original)
+++ incubator/ambari/trunk/ambari-server/src/test/python/TestAmbaryServer.py Fri May 31 15:03:50 2013
@@ -1188,7 +1188,36 @@ class TestAmbariServer(TestCase):
     self.assertTrue(get_choice_string_input_mock.called)
     self.assertEqual(4, len(get_choice_string_input_mock.call_args_list[0][0]))
 
+  @patch.object(ambari_server, "get_conf_dir")
+  def test_update_ambari_properties(self, get_conf_dir_mock):
 
+    properties = ["server.jdbc.user.name=ambari-server\n",
+      "server.jdbc.user.passwd=/etc/ambari-server/conf/password.dat\n",
+      "java.home=/usr/jdk64/jdk1.6.0_31\n",
+      "server.os_type=redhat6\n"]
+
+    get_conf_dir_mock.return_value = ""
+
+    tf1 = tempfile.NamedTemporaryFile()
+    tf2 = tempfile.NamedTemporaryFile()
+    ambari_server.AMBARI_PROPERTIES_RPMSAVE_FILE = tf1.name
+    ambari_server.AMBARI_PROPERTIES_FILE = tf2.name
+
+    with open(ambari_server.AMBARI_PROPERTIES_RPMSAVE_FILE, 'w') as f:
+      for line in properties:
+        f.write(line)
+
+    #Call tested method
+    ambari_server.update_ambari_properties()
+
+    with open(ambari_server.AMBARI_PROPERTIES_FILE, 'r') as f:
+      ambari_properties_content = f.readlines()
+
+    for line in properties:
+      if not line in ambari_properties_content:
+        self.fail()
+
+    pass
 
   def get_sample(self, sample):
     """