AMBARI-17494. Falcon start fails non-root + umask 027 (aonishuk)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/c26dfced Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/c26dfced Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/c26dfced Branch: refs/heads/branch-2.4 Commit: c26dfced74b2fbf9dfe63342e0a232401afb5e4c Parents: c02c565 Author: Andrew Onishuk <[email protected]> Authored: Thu Jun 30 13:18:08 2016 +0300 Committer: Andrew Onishuk <[email protected]> Committed: Thu Jun 30 13:18:08 2016 +0300 ---------------------------------------------------------------------- .../python/resource_management/core/sudo.py | 44 +++++++++++--------- .../libraries/providers/hdfs_resource.py | 5 ++- 2 files changed, 28 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/c26dfced/ambari-common/src/main/python/resource_management/core/sudo.py ---------------------------------------------------------------------- diff --git a/ambari-common/src/main/python/resource_management/core/sudo.py b/ambari-common/src/main/python/resource_management/core/sudo.py index 4ea44bc..d6fd71f 100644 --- a/ambari-common/src/main/python/resource_management/core/sudo.py +++ b/ambari-common/src/main/python/resource_management/core/sudo.py @@ -27,9 +27,7 @@ import stat import errno import random from resource_management.core import shell -from resource_management.core.logger import Logger from resource_management.core.exceptions import Fail -from ambari_commons.os_check import OSCheck import subprocess @@ -144,6 +142,8 @@ if os.geteuid() == 0: def kill(pid, signal): os.kill(pid, signal) + def listdir(path): + return os.listdir(path) else: # os.chown replacement @@ -197,7 +197,19 @@ else: # fp.write replacement def create_file(filename, content, encoding=None): - return _create_file(filename, content, True, encoding) + """ + if content is None, create empty file + """ + content = content if content else "" + content = content.encode(encoding) if encoding else content + + tmpf_name = tempfile.gettempdir() + os.sep + tempfile.template + str(time.time()) + "_" + str(random.randint(0, 1000)) + try: + with open(tmpf_name, "wb") as fp: + fp.write(content) + shell.checked_call(["cp", "-f", tmpf_name, filename], sudo=True) + finally: + os.unlink(tmpf_name) # fp.read replacement def read_file(filename, encoding=None): @@ -255,7 +267,17 @@ else: # shutil.copy replacement def copy(src, dst): shell.checked_call(["sudo", "cp", "-r", src, dst], sudo=True) + + # os.listdir replacement + def listdir(path): + if not path_isdir(path): + raise Fail("{0} is not a directory. Cannot list files of it.".format(path)) + code, out, err = shell.checked_call(["ls", path], sudo=True, stderr=subprocess.PIPE) + files = out.splitlines() + return files + + def chmod_recursive(path, recursive_mode_flags, recursion_follow_links): find_flags = [] if recursion_follow_links: @@ -263,19 +285,3 @@ def chmod_recursive(path, recursive_mode_flags, recursion_follow_links): for key, flags in recursive_mode_flags.iteritems(): shell.checked_call(["find"] + find_flags + [path, "-type", key, "-exec" , "chmod", flags ,"{}" ,";"]) - -# fp.write replacement -def _create_file(filename, content, withSudo, encoding=None): - """ - if content is None, create empty file - """ - content = content if content else "" - content = content.encode(encoding) if encoding else content - - tmpf_name = tempfile.gettempdir() + os.sep + tempfile.template + str(time.time()) + "_" + str(random.randint(0, 1000)) - try: - with open(tmpf_name, "wb") as fp: - fp.write(content) - shell.checked_call(["cp", "-f", tmpf_name, filename], sudo=withSudo) - finally: - os.unlink(tmpf_name) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/c26dfced/ambari-common/src/main/python/resource_management/libraries/providers/hdfs_resource.py ---------------------------------------------------------------------- diff --git a/ambari-common/src/main/python/resource_management/libraries/providers/hdfs_resource.py b/ambari-common/src/main/python/resource_management/libraries/providers/hdfs_resource.py index 12d9ee4..7abdf5c 100644 --- a/ambari-common/src/main/python/resource_management/libraries/providers/hdfs_resource.py +++ b/ambari-common/src/main/python/resource_management/libraries/providers/hdfs_resource.py @@ -29,6 +29,7 @@ from resource_management.core.resources.system import File from resource_management.core.providers import Provider from resource_management.core.logger import Logger from resource_management.core import shell +from resource_management.core import sudo from resource_management.libraries.script import Script from resource_management.libraries.functions import format from resource_management.libraries.functions.get_user_call_output import get_user_call_output @@ -271,10 +272,10 @@ class HdfsResourceWebHDFS: self._copy_from_local_directory(self.main_resource.resource.target, self.main_resource.resource.source) def _copy_from_local_directory(self, target, source): - for next_path_part in os.listdir(source): + for next_path_part in sudo.listdir(source): new_source = os.path.join(source, next_path_part) new_target = format("{target}/{next_path_part}") - if os.path.isdir(new_source): + if sudo.path_isdir(new_source): Logger.info(format("Creating DFS directory {new_target}")) self._create_directory(new_target) self._copy_from_local_directory(new_target, new_source)
