ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aonis...@apache.org
Subject [1/2] ambari git commit: AMBARI-16809. Agent host registration uses sudo even if sudo is not available. (aonishuk)
Date Mon, 23 May 2016 14:41:46 GMT
Repository: ambari
Updated Branches:
  refs/heads/branch-2.4 fe83cee08 -> e7d0acb95
  refs/heads/trunk 79ec4ce20 -> e3f2ddfc2


AMBARI-16809. Agent host registration uses sudo even if sudo is not available. (aonishuk)


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

Branch: refs/heads/trunk
Commit: e3f2ddfc2be4c275f50f71c7df26657cca3aa2e4
Parents: 79ec4ce
Author: Andrew Onishuk <aonishuk@hortonworks.com>
Authored: Mon May 23 17:41:43 2016 +0300
Committer: Andrew Onishuk <aonishuk@hortonworks.com>
Committed: Mon May 23 17:41:43 2016 +0300

----------------------------------------------------------------------
 ambari-server/src/main/python/bootstrap.py     | 33 ++++++++++++++++-----
 ambari-server/src/test/python/TestBootstrap.py | 26 ++++++++--------
 2 files changed, 38 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/e3f2ddfc/ambari-server/src/main/python/bootstrap.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/bootstrap.py b/ambari-server/src/main/python/bootstrap.py
index 24755ae..e576fc8 100755
--- a/ambari-server/src/main/python/bootstrap.py
+++ b/ambari-server/src/main/python/bootstrap.py
@@ -53,6 +53,8 @@ DEFAULT_AGENT_TEMP_FOLDER = "/var/lib/ambari-agent/tmp"
 DEFAULT_AGENT_DATA_FOLDER = "/var/lib/ambari-agent/data"
 DEFAULT_AGENT_LIB_FOLDER = "/var/lib/ambari-agent"
 PYTHON_ENV="env PYTHONPATH=$PYTHONPATH:" + DEFAULT_AGENT_TEMP_FOLDER
+SERVER_AMBARI_SUDO = os.getenv('ROOT','/').rstrip('/') + "/var/lib/ambari-server/ambari-sudo.sh"
+AMBARI_SUDO = os.path.join(DEFAULT_AGENT_TEMP_FOLDER, 'ambari-sudo.sh')
 
 class HostLog:
   """ Provides per-host logging. """
@@ -450,6 +452,19 @@ class BootstrapDefault(Bootstrap):
   def hasPassword(self):
     password_file = self.shared_state.password_file
     return password_file is not None and password_file != 'null'
+  
+  def copyAmbariSudo(self):
+    # Copying the os check script file
+    fileToCopy = SERVER_AMBARI_SUDO
+    target = self.TEMP_FOLDER
+    params = self.shared_state
+    self.host_log.write("==========================\n")
+    self.host_log.write("Copying ambari sudo script...")
+    scp = SCP(params.user, params.sshPort, params.sshkey_file, self.host, fileToCopy,
+              target, params.bootdir, self.host_log)
+    result = scp.run()
+    self.host_log.write("\n")
+    return result
 
   def copyOsCheckScript(self):
     # Copying the os check script file
@@ -479,12 +494,12 @@ class BootstrapDefault(Bootstrap):
     return result
 
   def getMoveRepoFileWithPasswordCommand(self, targetDir):
-    return "sudo -S mv " + str(self.getRemoteName(self.AMBARI_REPO_FILENAME)) \
+    return "{sudo} -S mv ".format(sudo=AMBARI_SUDO) + str(self.getRemoteName(self.AMBARI_REPO_FILENAME))
\
            + " " + os.path.join(str(targetDir), self.AMBARI_REPO_FILENAME) + \
            " < " + str(self.getPasswordFile())
 
   def getMoveRepoFileWithoutPasswordCommand(self, targetDir):
-    return "sudo mv " + str(self.getRemoteName(self.AMBARI_REPO_FILENAME)) \
+    return "{sudo} mv ".format(sudo=AMBARI_SUDO) + str(self.getRemoteName(self.AMBARI_REPO_FILENAME))
\
            + " " + os.path.join(str(targetDir), self.AMBARI_REPO_FILENAME)
 
   def getMoveRepoFileCommand(self, targetDir):
@@ -494,11 +509,11 @@ class BootstrapDefault(Bootstrap):
       return self.getMoveRepoFileWithoutPasswordCommand(targetDir)
 
   def getAptUpdateCommand(self):
-    return "sudo apt-get update -o Dir::Etc::sourcelist=\"%s/%s\" -o API::Get::List-Cleanup=\"0\"
--no-list-cleanup" %\
-          ("sources.list.d", self.AMBARI_REPO_FILENAME)
+    return "%s apt-get update -o Dir::Etc::sourcelist=\"%s/%s\" -o API::Get::List-Cleanup=\"0\"
--no-list-cleanup" %\
+          (AMBARI_SUDO, "sources.list.d", self.AMBARI_REPO_FILENAME)
           
   def getRepoFileChmodCommand(self):
-    return "sudo chmod 644 {0}".format(self.getRepoFile())
+    return "{0} chmod 644 {1}".format(AMBARI_SUDO, self.getRepoFile())
 
   def copyNeededFiles(self):
     # get the params
@@ -580,7 +595,7 @@ class BootstrapDefault(Bootstrap):
     version = self.getAmbariVersion()
     port = self.getAmbariPort()
     passwordFile = self.getPasswordFile()
-    return "sudo -S python " + str(setupFile) + " " + str(expected_hostname) + \
+    return "{sudo} -S python ".format(sudo=AMBARI_SUDO) + str(setupFile) + " " + str(expected_hostname)
+ \
            " " + str(passphrase) + " " + str(server)+ " " + quote_bash_args(str(user_run_as))
+ " " + str(version) + \
            " " + str(port) + " < " + str(passwordFile)
 
@@ -591,7 +606,7 @@ class BootstrapDefault(Bootstrap):
     user_run_as = self.shared_state.user_run_as
     version=self.getAmbariVersion()
     port=self.getAmbariPort()
-    return "sudo python " + str(setupFile) + " " + str(expected_hostname) + \
+    return "{sudo} python ".format(sudo=AMBARI_SUDO) + str(setupFile) + " " + str(expected_hostname)
+ \
            " " + str(passphrase) + " " + str(server)+ " " + quote_bash_args(str(user_run_as))
+ " " + str(version) + \
            " " + str(port)
 
@@ -619,6 +634,7 @@ class BootstrapDefault(Bootstrap):
       command = "dpkg --get-selections|grep -e '^sudo\s*install'"
     else:
       command = "rpm -qa | grep -e '^sudo\-'"
+    command = "[ \"$EUID\" -eq 0 ] || " + command
     ssh = SSH(params.user, params.sshPort, params.sshkey_file, self.host, command,
               params.bootdir, self.host_log,
               errorMessage="Error: Sudo command is not available. "
@@ -676,7 +692,7 @@ class BootstrapDefault(Bootstrap):
     params = self.shared_state
     user = params.user
 
-    command = "sudo mkdir -p {0} ; sudo chown -R {1} {0} ; sudo chmod 755 {3} ; sudo chmod
755 {2} ; sudo chmod 1777 {0}".format(
+    command = "SUDO=$([ \"$EUID\" -eq 0 ] && echo || echo sudo) ; $SUDO mkdir -p
{0} ; $SUDO chown -R {1} {0} ; $SUDO chmod 755 {3} ; $SUDO chmod 755 {2} ; $SUDO chmod 1777
{0}".format(
       self.TEMP_FOLDER, quote_bash_args(params.user), DEFAULT_AGENT_DATA_FOLDER, DEFAULT_AGENT_LIB_FOLDER)
 
     ssh = SSH(params.user, params.sshPort, params.sshkey_file, self.host, command,
@@ -707,6 +723,7 @@ class BootstrapDefault(Bootstrap):
     self.status["start_time"] = time.time()
     # Population of action queue
     action_queue = [self.createTargetDir,
+                    self.copyAmbariSudo,
                     self.copyCommonFunctions,
                     self.copyOsCheckScript,
                     self.runOsCheckScript,

http://git-wip-us.apache.org/repos/asf/ambari/blob/e3f2ddfc/ambari-server/src/test/python/TestBootstrap.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestBootstrap.py b/ambari-server/src/test/python/TestBootstrap.py
index 9888507..8356f91 100644
--- a/ambari-server/src/test/python/TestBootstrap.py
+++ b/ambari-server/src/test/python/TestBootstrap.py
@@ -112,7 +112,7 @@ class TestBootstrap(TestCase):
     utime = 1234
     bootstrap_obj.getUtime = MagicMock(return_value=utime)
     ret = bootstrap_obj.getRunSetupWithPasswordCommand("hostname")
-    expected = "sudo -S python /var/lib/ambari-agent/tmp/setupAgent{0}.py hostname TEST_PASSPHRASE
" \
+    expected = "/var/lib/ambari-agent/tmp/ambari-sudo.sh -S python /var/lib/ambari-agent/tmp/setupAgent{0}.py
hostname TEST_PASSPHRASE " \
                "ambariServer root  8440 < /var/lib/ambari-agent/tmp/host_pass{0}".format(utime)
     self.assertEquals(ret, expected)
 
@@ -353,11 +353,11 @@ class TestBootstrap(TestCase):
     self.assertEquals(res, expected)
     command = str(init_mock.call_args[0][4])
     self.assertEqual(command,
-                     "sudo mkdir -p /var/lib/ambari-agent/tmp ; "
-                     "sudo chown -R root /var/lib/ambari-agent/tmp ; "
-                     "sudo chmod 755 /var/lib/ambari-agent ; "
-                     "sudo chmod 755 /var/lib/ambari-agent/data ; "
-                     "sudo chmod 1777 /var/lib/ambari-agent/tmp")
+                     "SUDO=$([ \"$EUID\" -eq 0 ] && echo || echo sudo) ; $SUDO mkdir
-p /var/lib/ambari-agent/tmp ; "
+                     "$SUDO chown -R root /var/lib/ambari-agent/tmp ; "
+                     "$SUDO chmod 755 /var/lib/ambari-agent ; "
+                     "$SUDO chmod 755 /var/lib/ambari-agent/data ; "
+                     "$SUDO chmod 1777 /var/lib/ambari-agent/tmp")
 
   @patch.object(BootstrapDefault, "getOsCheckScript")
   @patch.object(BootstrapDefault, "getOsCheckScriptRemoteLocation")
@@ -400,12 +400,12 @@ class TestBootstrap(TestCase):
     hasPassword_mock.return_value = False
     getRemoteName_mock.return_value = "RemoteName"
     rf = bootstrap_obj.getMoveRepoFileCommand("target")
-    self.assertEquals(rf, "sudo mv RemoteName target/ambari.repo")
+    self.assertEquals(rf, "/var/lib/ambari-agent/tmp/ambari-sudo.sh mv RemoteName target/ambari.repo")
     # With password
     hasPassword_mock.return_value = True
     getRemoteName_mock.return_value = "RemoteName"
     rf = bootstrap_obj.getMoveRepoFileCommand("target")
-    self.assertEquals(rf, "sudo -S mv RemoteName target/ambari.repo < RemoteName")
+    self.assertEquals(rf, "/var/lib/ambari-agent/tmp/ambari-sudo.sh -S mv RemoteName target/ambari.repo
< RemoteName")
 
   @patch("os.path.exists")
   @patch.object(OSCheck, "is_suse_family")
@@ -464,7 +464,7 @@ class TestBootstrap(TestCase):
     self.assertEqual(input_file, "setupAgentFile")
     self.assertEqual(remote_file, "RemoteName")
     command = str(ssh_init_mock.call_args[0][4])
-    self.assertEqual(command, "sudo chmod 644 RepoFile")
+    self.assertEqual(command, "/var/lib/ambari-agent/tmp/ambari-sudo.sh chmod 644 RepoFile")
     # Another order
     expected1 = {"exitstatus": 0, "log": "log0", "errormsg": "errorMsg"}
     expected2 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"}
@@ -600,7 +600,7 @@ class TestBootstrap(TestCase):
     res = bootstrap_obj.checkSudoPackage()
     self.assertEquals(res, expected)
     command = str(init_mock.call_args[0][4])
-    self.assertEqual(command, "rpm -qa | grep -e '^sudo\-'")
+    self.assertEqual(command, "[ \"$EUID\" -eq 0 ] || rpm -qa | grep -e '^sudo\-'")
 
   @patch.object(OSCheck, "is_suse_family")
   @patch.object(OSCheck, "is_ubuntu_family")
@@ -623,7 +623,7 @@ class TestBootstrap(TestCase):
     res = bootstrap_obj.checkSudoPackage()
     self.assertEquals(res, expected)
     command = str(init_mock.call_args[0][4])
-    self.assertEqual(command, "dpkg --get-selections|grep -e '^sudo\s*install'")
+    self.assertEqual(command, "[ \"$EUID\" -eq 0 ] || dpkg --get-selections|grep -e '^sudo\s*install'")
 
 
   @patch.object(SSH, "__init__")
@@ -747,7 +747,7 @@ class TestBootstrap(TestCase):
     hasPassword_mock.return_value = False
     try_to_execute_mock.return_value = {"exitstatus": 0, "log":"log0", "errormsg":"errormsg0"}
     bootstrap_obj.run()
-    self.assertEqual(try_to_execute_mock.call_count, 7) # <- Adjust if changed
+    self.assertEqual(try_to_execute_mock.call_count, 8) # <- Adjust if changed
     self.assertTrue(createDoneFile_mock.called)
     self.assertEqual(bootstrap_obj.getStatus()["return_code"], 0)
 
@@ -758,7 +758,7 @@ class TestBootstrap(TestCase):
     hasPassword_mock.return_value = True
     try_to_execute_mock.return_value = {"exitstatus": 0, "log":"log0", "errormsg":"errormsg0"}
     bootstrap_obj.run()
-    self.assertEqual(try_to_execute_mock.call_count, 10) # <- Adjust if changed
+    self.assertEqual(try_to_execute_mock.call_count, 11) # <- Adjust if changed
     self.assertTrue(createDoneFile_mock.called)
     self.assertEqual(bootstrap_obj.getStatus()["return_code"], 0)
 


Mime
View raw message