Repository: ambari
Updated Branches:
  refs/heads/trunk 891b1205d -> fd8754d25


AMBARI-6221. Ambari Server reset show wrong commands for DB manipulation. 
(mpapirkovskyy)


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

Branch: refs/heads/trunk
Commit: fd8754d252cdc9c7285a0f2842cb28140f41395e
Parents: 891b120
Author: Myroslav Papirkovskyy <mpapyrkovs...@hortonworks.com>
Authored: Mon Jun 23 19:03:27 2014 +0300
Committer: Myroslav Papirkovskyy <mpapyrkovs...@hortonworks.com>
Committed: Mon Jun 23 19:03:50 2014 +0300

----------------------------------------------------------------------
 ambari-server/src/main/python/ambari-server.py  | 53 ++++++++++++--------
 .../src/test/python/TestAmbariServer.py         | 31 +++++++++---
 2 files changed, 58 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/fd8754d2/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 e0d3b34..d011ce7 100755
--- a/ambari-server/src/main/python/ambari-server.py
+++ b/ambari-server/src/main/python/ambari-server.py
@@ -1396,7 +1396,6 @@ def get_db_cli_tool(args):
 
   return None
 
-
 #execute SQL script on remote database: Deprecated
 def execute_remote_script(args, scriptPath):
   print_warning_msg("Deprecated method called.")
@@ -1405,19 +1404,31 @@ def execute_remote_script(args, scriptPath):
     # args.warnings.append('{0} not found. Please, run DDL script 
manually'.format(DATABASE_CLI_TOOLS[DATABASE_INDEX]))
     if VERBOSE:
       print_warning_msg('{0} not 
found'.format(DATABASE_CLI_TOOLS[DATABASE_INDEX]))
-    return -1, "Client wasn't found", "Client wasn't found"
+    return -1, "Client wasn't found", "Client wasn't found"  
+  CMD = get_remote_script_line(args, scriptPath, False)
+  if CMD:
+    retcode, out, err = run_in_shell(CMD)
+    return retcode, out, err
+  else:
+    return -2, "Wrong database", "Wrong database"
 
-  if args.dbms == "postgres":
 
+def get_remote_script_line(args, scriptPath, forPrint=True):
+  tool = get_db_cli_tool(args)
+  if not tool:
+    # args.warnings.append('{0} not found. Please, run DDL script 
manually'.format(DATABASE_CLI_TOOLS[DATABASE_INDEX]))
+    if VERBOSE or args.persistence_type == "remote":
+      print_warning_msg('{0} not 
found'.format(DATABASE_CLI_TOOLS[DATABASE_INDEX]))  
+    return None
+  if args.dbms == "postgres":
     os.environ["PGPASSWORD"] = args.database_password
-    retcode, out, err = run_in_shell('{0} {1}'.format(tool, 
POSTGRES_EXEC_ARGS.format(
+    return '{0} {1}'.format(tool, POSTGRES_EXEC_ARGS.format(
       args.database_host,
       args.database_port,
       args.database_name,
       args.database_username,
       scriptPath
-    )))
-    return retcode, out, err
+    ))
   elif args.dbms == "oracle":
     sid_or_sname = "sid"
     if (hasattr(args, 'sid_or_sname') and args.sid_or_sname == "sname") or \
@@ -1425,29 +1436,27 @@ def execute_remote_script(args, scriptPath):
       print_info_msg("using SERVICE_NAME instead of SID for Oracle")
       sid_or_sname = "service_name"
 
-    retcode, out, err = run_in_shell('{0} {1}'.format(tool, 
ORACLE_EXEC_ARGS.format(
+    return '{0} {1}'.format(tool, ORACLE_EXEC_ARGS.format(
       args.database_username,
-      args.database_password,
+      args.database_password if not forPrint else BLIND_PASSWORD,
       args.database_host,
       args.database_port,
       args.database_name,
       scriptPath,
       sid_or_sname
-    )))
-    return retcode, out, err
+    ))
   elif args.dbms == "mysql":
     MYSQL_EXEC_ARGS = MYSQL_EXEC_ARGS_WO_USER_VARS if MYSQL_INIT_SCRIPT == 
scriptPath else MYSQL_EXEC_ARGS_WITH_USER_VARS
-    retcode, out, err = run_in_shell('{0} {1}'.format(tool, 
MYSQL_EXEC_ARGS.format(
+    return '{0} {1}'.format(tool, MYSQL_EXEC_ARGS.format(
       args.database_host,
       args.database_port,
       args.database_username,
-      args.database_password,
+      args.database_password if not forPrint else BLIND_PASSWORD,
       args.database_name,
       scriptPath
-    )))
-    return retcode, out, err
+    ))
 
-  return -2, "Wrong database", "Wrong database"
+  return None
 
 
 def configure_database_password(showDefault=True):
@@ -2383,12 +2392,12 @@ def reset(args):
 
   check_database_name_property()
   parse_properties_file(args)
-
+  
   if args.persistence_type == "remote":
-    client_usage_cmd_drop = 
DATABASE_CLI_TOOLS_USAGE[DATABASE_INDEX].format(DATABASE_DROP_SCRIPTS[DATABASE_INDEX],
 args.database_username,
-                                                     BLIND_PASSWORD, 
args.database_name)
-    client_usage_cmd_init = 
DATABASE_CLI_TOOLS_USAGE[DATABASE_INDEX].format(DATABASE_INIT_SCRIPTS[DATABASE_INDEX],
 args.database_username,
-                                                     BLIND_PASSWORD, 
args.database_name)
+    client_usage_cmd_drop = get_remote_script_line(args, 
DATABASE_DROP_SCRIPTS[DATABASE_INDEX])
+    client_usage_cmd_init = get_remote_script_line(args, 
DATABASE_INIT_SCRIPTS[DATABASE_INDEX])
+    if not client_usage_cmd_drop or not client_usage_cmd_init:
+      raise NonFatalException("Could`t create command lines for {0} 
DB".format(args.dbms))
 
     print_warning_msg('To reset Ambari Server schema ' +
                       'you must run the following DDL against the database to '
@@ -2396,6 +2405,10 @@ def reset(args):
                       + os.linesep + 'Then you must run the following DDL ' +
                       'against the database to create the schema: ' + 
os.linesep +
                       client_usage_cmd_init + os.linesep)
+    if args.dbms == "postgres":
+      raise NonFatalException("Please set DB password to PGPASSWORD env 
variable before running DDL`s!")
+    else:
+      raise NonFatalException("Please replace '*' symbols with password before 
running DDL`s!")
   else:
     # Run automatic reset only for embedded DB
     okToRun = get_YN_input("Confirm server reset [yes/no]({0})? 
".format(default), SILENT)

http://git-wip-us.apache.org/repos/asf/ambari/blob/fd8754d2/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 0c254b9..dbbefab 100644
--- a/ambari-server/src/test/python/TestAmbariServer.py
+++ b/ambari-server/src/test/python/TestAmbariServer.py
@@ -2405,6 +2405,7 @@ 
MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
     self.assertFalse(check_selinux_mock.called)
     self.assertFalse(check_ambari_user_mock.called)
 
+  @patch.object(ambari_server, 'get_remote_script_line')
   @patch.object(ambari_server, 'is_server_runing')
   @patch.object(ambari_server, "get_YN_input")
   @patch.object(ambari_server, "setup_db")
@@ -2417,7 +2418,8 @@ 
MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
   def test_reset(self, check_database_name_property_mock, is_root_mock,
                  parse_properties_file_mock, 
configure_database_username_password_mock,
                  run_os_command_mock, print_info_msg_mock,
-                 setup_db_mock, get_YN_inputMock, is_server_running_mock):
+                 setup_db_mock, get_YN_inputMock, is_server_running_mock,
+                 get_remote_script_line_mock):
 
     parse_properties_file_mock.return_value = 0
     args = MagicMock()
@@ -2460,7 +2462,7 @@ 
MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
     self.assertTrue(setup_db_mock.called)
 
     # Database errors cases
-    is_server_running_mock.side_effect = [(True, 123), (False, 0), (False, 0)]
+    is_server_running_mock.side_effect = [(True, 123), (False, 0), (False, 0), 
(False, 0), (False, 0)]
 
     try:
       ambari_server.reset(args)
@@ -2476,10 +2478,27 @@ 
MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
       # Expected
       pass
 
-    #remote db case
-    args.persistence_type = "remote"
-    rcode = ambari_server.reset(args)
-    self.assertEqual(None, rcode)
+    get_remote_script_line_mock.return_value = None
+    try:
+      #remote db case
+      args.persistence_type = "remote"
+      ambari_server.reset(args)
+      self.fail("Should throw exception")
+    except NonFatalException:
+      # Expected
+      pass
+
+    get_remote_script_line_mock.reset_mock()
+    get_remote_script_line_mock.side_effect = ["drop", "create"]
+    try:
+      #remote db case (not Postgres)
+      args.persistence_type = "remote"
+      rcode = ambari_server.reset(args)
+      self.fail("Should throw exception")
+    except NonFatalException:
+      # Expected
+      self.assertTrue(get_remote_script_line_mock.called)
+      pass
 
   @patch.object(ambari_server, "get_YN_input")
   @patch("__builtin__.raw_input")

Reply via email to