Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AMBARI-25854: Adapt Ambari 2.7.5 from python2 to python3.9 #3681

Open
wants to merge 11 commits into
base: branch-2.7
Choose a base branch
from

Conversation

wuzhuoming
Copy link

What changes were proposed in this pull request?

This PR show the necessary adaption for Ambari 2.7.5 to adapt python3.9.
related JIRA Issue: https://issues.apache.org/jira/browse/AMBARI-25854

How was this patch tested?

unit tests,
manual tests

OLD_RESOURCE_MANAGEMENT_DIR="/usr/lib/python3.9/site-packages/resource_management"
OLD_JINJA_DIR="/usr/lib/python3.9/site-packages/ambari_jinja2"
OLD_SIMPLEJSON_DIR="/usr/lib/python3.9/site-packages/ambari_simplejson"
OLD_AMBARI_AGENT_DIR="/usr/lib/python3.9/site-packages/ambari_agent"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract to one variable so that we can change the python version in one place..?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generate a build script build-ambari.sh to solve the problem. related link: 6e8082a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like Ambari should come with a venv for its own packages rather than pollute OS-level site-packages

@@ -314,7 +314,7 @@
<fileEncoding>utf-8</fileEncoding>
</postremoveScriptlet>

<needarch>x86_64</needarch>
<needarch>aarch64</needarch>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep x86_64 as default one and make it dynamic..?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generate a build script build-ambari.sh to solve the problem. related link: 6e8082a

@@ -200,27 +200,27 @@ def cache_dir(self):

@property
def cluster_cache_dir(self):
return os.path.join(self.cache_dir, FileCache.CLUSTER_CACHE_DIRECTORY)
return os.path.join('ambari_agent', 'dummy_files')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why all these are changed to "dummy_files"?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we try to test python3, we find that the corresponding path cannot be obtained. However, the default path obtained by python2 is this path. Therefore, the code is changed to dummy_files during the test of python3.

@himanshumaurya09876
Copy link
Contributor

I am trying to apply this patch on branch-2.7, but https://github.com/wuzhuoming/ambari-python3/blame/AMBARI-25854-release-2.7.5/ambari-agent/src/main/python/ambari_agent/listeners/ConfigurationEventListener.py#L53 from line 53 to 56 these changes are not present in branch-2.7, seems you haven't applied the changes on top of branch-2.7.
Can you tell me the branch on top of which you have done your changes?

@Hann0321
Copy link

I am trying to apply this patch on branch-2.7, but https://github.com/wuzhuoming/ambari-python3/blame/AMBARI-25854-release-2.7.5/ambari-agent/src/main/python/ambari_agent/listeners/ConfigurationEventListener.py#L53 from line 53 to 56 these changes are not present in branch-2.7, seems you haven't applied the changes on top of branch-2.7. Can you tell me the branch on top of which you have done your changes?

Can you tell me the branch on top of which you have done your changes?

Hello, here ambari 2.7.5 source code is downloaded from the official website of ambari. Link: https://archive.apache.org/dist/ambari/ambari-2.7.5/apache-ambari-2.7.5-src.tar.gz

@himanshumaurya09876
Copy link
Contributor

I am trying to apply this patch on branch-2.7, but https://github.com/wuzhuoming/ambari-python3/blame/AMBARI-25854-release-2.7.5/ambari-agent/src/main/python/ambari_agent/listeners/ConfigurationEventListener.py#L53 from line 53 to 56 these changes are not present in branch-2.7, seems you haven't applied the changes on top of branch-2.7. Can you tell me the branch on top of which you have done your changes?

Can you tell me the branch on top of which you have done your changes?

Hello, here ambari 2.7.5 source code is downloaded from the official website of ambari. Link: https://archive.apache.org/dist/ambari/ambari-2.7.5/apache-ambari-2.7.5-src.tar.gz

Please use the latest code from https://github.com/apache/ambari/tree/branch-2.7

@wuzhuoming
Copy link
Author

I am trying to apply this patch on branch-2.7, but https://github.com/wuzhuoming/ambari-python3/blame/AMBARI-25854-release-2.7.5/ambari-agent/src/main/python/ambari_agent/listeners/ConfigurationEventListener.py#L53 from line 53 to 56 these changes are not present in branch-2.7, seems you haven't applied the changes on top of branch-2.7. Can you tell me the branch on top of which you have done your changes?

Can you tell me the branch on top of which you have done your changes?

Hello, here ambari 2.7.5 source code is downloaded from the official website of ambari. Link: https://archive.apache.org/dist/ambari/ambari-2.7.5/apache-ambari-2.7.5-src.tar.gz

Please use the latest code from https://github.com/apache/ambari/tree/branch-2.7

Working on it! We are comparing the code between branch-2.7 and release-2.7.5 and making some modification to solve the conflit. Thank you for your comment!

@himanshumaurya09876
Copy link
Contributor

Do not delete .../.gitignore this file...

@himanshumaurya09876
Copy link
Contributor

Hi I am getting below error, while creating ambari rpms

07:46:07,345 [ERROR] error /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass: Command failed.
07:46:07,346 [ERROR] Exit code: 1
07:46:07,346 [ERROR] Command: sh
07:46:07,346 [ERROR] Arguments: -c node scripts/build.js
07:46:07,346 [ERROR] Directory: /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass
07:46:07,346 [ERROR] Output:
07:46:07,346 [ERROR] Building: /workspace/ambari/contrib/views/files/src/main/resources/ui/node/node /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
07:46:07,347 [ERROR] gyp info it worked if it ends with ok
07:46:07,347 [ERROR] gyp verb cli [ '/workspace/ambari/contrib/views/files/src/main/resources/ui/node/node',
07:46:07,347 [ERROR] gyp verb cli '/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass/node_modules/node-gyp/bin/node-gyp.js',
07:46:07,347 [ERROR] gyp verb cli 'rebuild',
07:46:07,347 [ERROR] gyp verb cli '--verbose',
07:46:07,347 [ERROR] gyp verb cli '--libsass_ext=',
07:46:07,347 [ERROR] gyp verb cli '--libsass_cflags=',
07:46:07,347 [ERROR] gyp verb cli '--libsass_ldflags=',
07:46:07,347 [ERROR] gyp verb cli '--libsass_library=' ]
07:46:07,347 [ERROR] gyp info using [email protected]
07:46:07,347 [ERROR] gyp info using [email protected] | linux | x64
07:46:07,347 [ERROR] gyp verb command rebuild []
07:46:07,347 [ERROR] gyp verb command clean []
07:46:07,347 [ERROR] gyp verb clean removing "build" directory
07:46:07,347 [ERROR] gyp verb command configure []
07:46:07,347 [ERROR] gyp verb check python checking for Python executable "python2" in the PATH
07:46:07,347 [ERROR] gyp verb which failed Error: not found: python2
07:46:07,347 [ERROR] gyp verb which failed at getNotFoundError (/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/which/which.js:13:12)
07:46:07,347 [ERROR] gyp verb which failed at F (/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/which/which.js:68:19)
07:46:07,347 [ERROR] gyp verb which failed at E (/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/which/which.js:80:29)
07:46:07,347 [ERROR] gyp verb which failed at /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/which/which.js:89:16
07:46:07,347 [ERROR] gyp verb which failed at /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/isexe/index.js:42:5
07:46:07,347 [ERROR] gyp verb which failed at /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/isexe/mode.js:8:5
07:46:07,346 [INFO] info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
07:46:07,347 [ERROR] gyp verb which failed at FSReqWrap.oncomplete (fs.js:82:15)
07:46:07,347 [ERROR] gyp verb which failed python2 { [Error: not found: python2] code: 'ENOENT' }
07:46:07,347 [ERROR] gyp verb check python checking for Python executable "python" in the PATH
07:46:07,347 [ERROR] gyp verb which succeeded python /usr/bin/python
07:46:07,347 [ERROR] gyp verb check python version /usr/bin/python -c "import platform; print(platform.python_version());" returned: "3.9.6\n"
07:46:07,347 [ERROR] gyp ERR! configure error
07:46:07,347 [ERROR] gyp ERR! stack Error: Python executable "/usr/bin/python" is v3.9.6, which is not supported by gyp.
07:46:07,347 [ERROR] gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.
07:46:07,347 [ERROR] gyp ERR! stack at Object.failPythonVersion (/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass/node_modules/node-gyp/lib/configure.js:491:19)
07:46:07,347 [ERROR] gyp ERR! stack at Object. (/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass/node_modules/node-gyp/lib/configure.js:473:14)
07:46:07,347 [ERROR] gyp ERR! stack at ChildProcess.exithandler (child_process.js:204:7)
07:46:07,347 [ERROR] gyp ERR! stack at emitTwo (events.js:87:13)
07:46:07,347 [ERROR] gyp ERR! stack at ChildProcess.emit (events.js:172:7)
07:46:07,347 [ERROR] gyp ERR! stack at maybeClose (internal/child_process.js:829:16)
07:46:07,347 [ERROR] gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
07:46:07,347 [ERROR] gyp ERR! System Linux 3.10.0-1160.88.1.el7.x86_64
07:46:07,347 [ERROR] gyp ERR! command "/workspace/ambari/contrib/views/files/src/main/resources/ui/node/node" "/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
07:46:07,347 [ERROR] gyp ERR! cwd /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass
07:46:07,348 [ERROR] gyp ERR! node -v v4.5.0
07:46:07,348 [ERROR] gyp ERR! node-gyp -v v3.6.0
07:46:07,348 [ERROR] gyp ERR! not ok

@Hann0321
Copy link

Hi I am getting below error, while creating ambari rpms

07:46:07,345 [ERROR] error /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass: Command failed. 07:46:07,346 [ERROR] Exit code: 1 07:46:07,346 [ERROR] Command: sh 07:46:07,346 [ERROR] Arguments: -c node scripts/build.js 07:46:07,346 [ERROR] Directory: /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass 07:46:07,346 [ERROR] Output: 07:46:07,346 [ERROR] Building: /workspace/ambari/contrib/views/files/src/main/resources/ui/node/node /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library= 07:46:07,347 [ERROR] gyp info it worked if it ends with ok 07:46:07,347 [ERROR] gyp verb cli [ '/workspace/ambari/contrib/views/files/src/main/resources/ui/node/node', 07:46:07,347 [ERROR] gyp verb cli '/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass/node_modules/node-gyp/bin/node-gyp.js', 07:46:07,347 [ERROR] gyp verb cli 'rebuild', 07:46:07,347 [ERROR] gyp verb cli '--verbose', 07:46:07,347 [ERROR] gyp verb cli '--libsass_ext=', 07:46:07,347 [ERROR] gyp verb cli '--libsass_cflags=', 07:46:07,347 [ERROR] gyp verb cli '--libsass_ldflags=', 07:46:07,347 [ERROR] gyp verb cli '--libsass_library=' ] 07:46:07,347 [ERROR] gyp info using [email protected] 07:46:07,347 [ERROR] gyp info using [email protected] | linux | x64 07:46:07,347 [ERROR] gyp verb command rebuild [] 07:46:07,347 [ERROR] gyp verb command clean [] 07:46:07,347 [ERROR] gyp verb clean removing "build" directory 07:46:07,347 [ERROR] gyp verb command configure [] 07:46:07,347 [ERROR] gyp verb check python checking for Python executable "python2" in the PATH 07:46:07,347 [ERROR] gyp verb which failed Error: not found: python2 07:46:07,347 [ERROR] gyp verb which failed at getNotFoundError (/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/which/which.js:13:12) 07:46:07,347 [ERROR] gyp verb which failed at F (/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/which/which.js:68:19) 07:46:07,347 [ERROR] gyp verb which failed at E (/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/which/which.js:80:29) 07:46:07,347 [ERROR] gyp verb which failed at /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/which/which.js:89:16 07:46:07,347 [ERROR] gyp verb which failed at /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/isexe/index.js:42:5 07:46:07,347 [ERROR] gyp verb which failed at /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/isexe/mode.js:8:5 07:46:07,346 [INFO] info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command. 07:46:07,347 [ERROR] gyp verb which failed at FSReqWrap.oncomplete (fs.js:82:15) 07:46:07,347 [ERROR] gyp verb which failed python2 { [Error: not found: python2] code: 'ENOENT' } 07:46:07,347 [ERROR] gyp verb check python checking for Python executable "python" in the PATH 07:46:07,347 [ERROR] gyp verb which succeeded python /usr/bin/python 07:46:07,347 [ERROR] gyp verb check python version /usr/bin/python -c "import platform; print(platform.python_version());" returned: "3.9.6\n" 07:46:07,347 [ERROR] gyp ERR! configure error 07:46:07,347 [ERROR] gyp ERR! stack Error: Python executable "/usr/bin/python" is v3.9.6, which is not supported by gyp. 07:46:07,347 [ERROR] gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0. 07:46:07,347 [ERROR] gyp ERR! stack at Object.failPythonVersion (/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass/node_modules/node-gyp/lib/configure.js:491:19) 07:46:07,347 [ERROR] gyp ERR! stack at Object. (/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass/node_modules/node-gyp/lib/configure.js:473:14) 07:46:07,347 [ERROR] gyp ERR! stack at ChildProcess.exithandler (child_process.js:204:7) 07:46:07,347 [ERROR] gyp ERR! stack at emitTwo (events.js:87:13) 07:46:07,347 [ERROR] gyp ERR! stack at ChildProcess.emit (events.js:172:7) 07:46:07,347 [ERROR] gyp ERR! stack at maybeClose (internal/child_process.js:829:16) 07:46:07,347 [ERROR] gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5) 07:46:07,347 [ERROR] gyp ERR! System Linux 3.10.0-1160.88.1.el7.x86_64 07:46:07,347 [ERROR] gyp ERR! command "/workspace/ambari/contrib/views/files/src/main/resources/ui/node/node" "/workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" 07:46:07,347 [ERROR] gyp ERR! cwd /workspace/ambari/contrib/views/files/src/main/resources/ui/node_modules/node-sass 07:46:07,348 [ERROR] gyp ERR! node -v v4.5.0 07:46:07,348 [ERROR] gyp ERR! node-gyp -v v3.6.0 07:46:07,348 [ERROR] gyp ERR! not ok

Sorry, the Ambari adapts to the ARM architecture and Python3 based on the openEuler system. Currently, the code of the x86 architecture branch is not verified or adapted. Our team is considering whether to proceed with this task.

@himanshumaurya09876
Copy link
Contributor

Hi I am getting below error for both ambari-agent as well as ambari-server, while adding zookeeper service

Traceback (most recent call last):
File "/var/lib/ambari-agent/cache/stack-hooks/before-INSTALL/scripts/hook.py", line 37, in
BeforeInstallHook().execute()
File "/usr/lib/ambari-agent/lib/resource_management/libraries/script/script.py", line 302, in execute
logging_level_str = logging._levelToName[self.logging_level]
KeyError: 'INFO'

@Hann0321
Copy link

Hann0321 commented Apr 24, 2023

@himanshumaurya09876

Hi,what your system environment and architecture is? And how did you add the Zookeeper service?

@himanshumaurya09876
Copy link
Contributor

@himanshumaurya09876

Hi,what your system environment and architecture is? And how did you add the Zookeeper service?

I am using RHEL 8 machines. I have installed ambari-agent, ambari-server and through Ambari UI install wizard I am adding Zookeeper service

@Hann0321
Copy link

@himanshumaurya09876

Hi,what your system environment and architecture is? And how did you add the Zookeeper service?

I am using RHEL 8 machines. I have installed ambari-agent, ambari-server and through Ambari UI install wizard I am adding Zookeeper service

You can create an issue and attach the error log or image. I'm not sure where you went wrong.

@himanshumaurya09876
Copy link
Contributor

Hi I am getting below error while adding service trough Ambari UI Install wizard

File "/usr/lib/ambari-agent/lib/resource_management/core/providers/packaging.py", line 30, in action_install     self._pkg_manager.install_package(package_name, self.__create_context())   
File "/usr/lib/ambari-agent/lib/ambari_commons/repo_manager/yum_manager.py", line 211, in install_package     elif context.is_upgrade or context.use_repos or not self._check_existence(name):   
File "/usr/lib/ambari-agent/lib/ambari_commons/repo_manager/yum_manager.py", line 278, in _check_existence     return self.yum_check_package_available(name)   
File "/usr/lib/ambari-agent/lib/ambari_commons/repo_manager/yum_manager.py", line 287, in yum_check_package_available     import yum  # Python Yum API is much faster then other check methods. (even then "import rpm")
ModuleNotFoundError: No module named 'yum'

While analyzing this I found that yum module is not available in python3, have you also observed same error?

@Hann0321
Copy link

Hi I am getting below error while adding service trough Ambari UI Install wizard

File "/usr/lib/ambari-agent/lib/resource_management/core/providers/packaging.py", line 30, in action_install     self._pkg_manager.install_package(package_name, self.__create_context())    File "/usr/lib/ambari-agent/lib/ambari_commons/repo_manager/yum_manager.py", line 211, in install_package     elif context.is_upgrade or context.use_repos or not self._check_existence(name):    File "/usr/lib/ambari-agent/lib/ambari_commons/repo_manager/yum_manager.py", line 278, in _check_existence     return self.yum_check_package_available(name)    File "/usr/lib/ambari-agent/lib/ambari_commons/repo_manager/yum_manager.py", line 287, in yum_check_package_available     import yum  # Python Yum API is much faster then other check methods. (even then "import rpm") ModuleNotFoundError: No module named 'yum'

While analyzing this I found that yum module is not available in python3, have you also observed same error?

@himanshumaurya09876
Hi
Python3 has deprecated the use of the yum module. I think we can abandon the call to yum_check_package_available and use the call to rpm_check_package_available.

image

@almeidajeff
Copy link

almeidajeff commented Apr 25, 2023

Evaluating the rpm_check_package_available method, it uses the python rpm library which is also discontinued for Python3, as a suggestion we can use the os module to execute the rpm command.

Here is a suggestion of how the method would look:

def rpm_check_package_available(self, name):
    import os
    packages = os.popen("rpm -qa --queryformat '%{name} '").read().split()

    name_regex = re.escape(name).replace("\\?", ".").replace("\\*", ".*") + '$'
    regex = re.compile(name_regex)
    
    return any(regex.match(package) for package in packages)

@himanshumaurya09876
Copy link
Contributor

himanshumaurya09876 commented Apr 29, 2023

Hi, I am getting below error while adding Ambari-Metrics to my cluster

Traceback (most recent call last):
File "/AMBARI_METRICS/package/scripts/metrics_collector.py", line 117, in
AmsCollector().execute()
File "/usr/lib/ambari-agent/lib/resource_management/libraries/script/script.py", line 361, in execute
method(env)
File "/AMBARI_METRICS/package/scripts/metrics_collector.py", line 38, in install
self.install_packages(env)
File "/usr/lib/ambari-agent/lib/resource_management/libraries/script/script.py", line 863, in install_packages
retry_count=agent_stack_retry_count)
File "/usr/lib/ambari-agent/lib/resource_management/core/base.py", line 164, in init
self.env.run()
File "/usr/lib/ambari-agent/lib/resource_management/core/environment.py", line 169, in run
self.run_action(resource, action)
File "/usr/lib/ambari-agent/lib/resource_management/core/environment.py", line 133, in run_action
provider_action()
File "/usr/lib/ambari-agent/lib/resource_management/core/providers/packaging.py", line 30, in action_install
self._pkg_manager.install_package(package_name, self.__create_context())
File "/usr/lib/ambari-agent/lib/ambari_commons/repo_manager/yum_manager.py", line 219, in install_package
shell.repository_manager_executor(cmd, self.properties, context)
File "/usr/lib/ambari-agent/lib/ambari_commons/shell.py", line 744, in repository_manager_executor
call_result = subprocess_executor(cmd, timeout=-1, env=env)
File "/usr/lib/ambari-agent/lib/ambari_commons/shell.py", line 446, in subprocess_executor
lines = [line for line in output]
File "/usr/lib64/python3.6/contextlib.py", line 92, in exit
raise RuntimeError("generator didn't stop")
RuntimeError: generator didn't stop

@OneCricketeer
Copy link

OneCricketeer commented Apr 29, 2023

File "/usr/lib64/python3.6/contextlib.py", line 92, in exit
raise RuntimeError("generator didn't stop")

This PR seemed to be trying to use Python 3.9, not 3.6. What happens when you upgrade that? @himanshumaurya09876

Also, use code fences for formatting, please

@himanshumaurya09876
Copy link
Contributor

File "/usr/lib64/python3.6/contextlib.py", line 92, in exit
raise RuntimeError("generator didn't stop")

This PR seemed to be trying to use Python 3.9, not 3.6. What happens when you upgrade that? @himanshumaurya09876

Also, use code fences for formatting, please

Hi, I have tried both python 3.6 as well as 3.9. It's giving same error in both cases.

@Hann0321
Copy link

Hann0321 commented May 4, 2023

File "/usr/lib64/python3.6/contextlib.py", line 92, in exit
raise RuntimeError("generator didn't stop")

This PR seemed to be trying to use Python 3.9, not 3.6. What happens when you upgrade that? @himanshumaurya09876
Also, use code fences for formatting, please

Hi, I have tried both python 3.6 as well as 3.9. It's giving same error in both cases.

@himanshumaurya09876
Hi, thank you for your reply. You are advised to use Python 3.7 or later for verification.

@@ -317,42 +317,42 @@ def run(self):
self.cmd_status()

def cmd_start(self):
print "Starting %d host(s)" % len(self.hosts)
print("Starting %d host(s)" % len(self.hosts))
for host in self.hosts:
cmd = "ambari-agent start --home %s" % (host.home_dir)
os.environ['AMBARI_AGENT_CONF_DIR'] = os.path.join(host.home_dir, "etc/ambari-agent/conf")
subprocess32.call(cmd, shell=True, env=os.environ)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update subprocess32 to subprocess ?

for host in self.hosts:
cmd = "ambari-agent start --home %s" % (host.home_dir)
os.environ['AMBARI_AGENT_CONF_DIR'] = os.path.join(host.home_dir, "etc/ambari-agent/conf")
subprocess32.call(cmd, shell=True, env=os.environ)

def cmd_stop(self):
print "Stopping %d host(s)" % len(self.hosts)
print("Stopping %d host(s)" % len(self.hosts))
for host in self.hosts:
cmd = "ambari-agent stop --home %s" % (host.home_dir)
os.environ['AMBARI_AGENT_CONF_DIR'] = os.path.join(host.home_dir, "etc/ambari-agent/conf")
subprocess32.call(cmd, shell=True, env=os.environ)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update subprocess32 to subprocess ?

for host in self.hosts:
cmd = "ambari-agent stop --home %s" % (host.home_dir)
os.environ['AMBARI_AGENT_CONF_DIR'] = os.path.join(host.home_dir, "etc/ambari-agent/conf")
subprocess32.call(cmd, shell=True, env=os.environ)

def cmd_restart(self):
print "Restarting %d host(s)" % len(self.hosts)
print("Restarting %d host(s)" % len(self.hosts))
for host in self.hosts:
cmd = "ambari-agent restart --home %s" % (host.home_dir)
os.environ['AMBARI_AGENT_CONF_DIR'] = os.path.join(host.home_dir, "etc/ambari-agent/conf")
subprocess32.call(cmd, shell=True, env=os.environ)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update subprocess32 to subprocess ?

@@ -226,7 +226,7 @@ def create_host_name_script(self, host_name, host_name_script):
"echo HOSTNAME"
with open(str(host_name_script), "w+") as f:
f.writelines(template.replace("HOSTNAME", host_name))
subprocess32.call("chmod +x %s" % host_name_script, shell=True)
subprocess.call("chmod +x %s" % host_name_script, shell=True)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any security considerations here ? can the subprocess call be used with shell=False ?

Example - subprocess.call(["chmod" + " +x " + host_name_script], shell=False)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lingarajg

I believe that the subprocess32 module is a copy (maybe there is some customization but I don't know) of Python's standard subprocess module, so the most viable thing would be to maintain the use of the module that is already standard in the language and not maintain a "separate" module, in my opinion it would not make much sense.

As for the use of shell=True, it should be avoided taking into account the issue of security and the possibility of executing some arbitrary code.
shell=True It is sometimes convenient to make use of shell-specific features such as word splitting or parameter expansion.

We would have a syntax change at all points where shell=True is executed, example:

of this

subprocess.Popen("command -with -options 'like this' and\\ an\\ argument", shell=True)

For that
subprocess.Popen(['command', '-with','-options', 'like this', 'and an argument'])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you that Python introduces true concurrency through the use of multiple processes. However, Python 2 doesn't have the subprocess module by default. Therefore, we copied the subprocess module from Python 3's standard library and made compatibility modifications to maintain it within the Ambari codebase. So, if we upgrade to Python 3, we won't need to maintain our own version of the subprocess module anymore and can directly use the one provided by Python 3.

@timyuer
Copy link
Contributor

timyuer commented May 22, 2023

@wuzhuoming
Please change the PR title to trigger the unit test like this,

AMBARI-25854: XXX

Thx.

@himanshumaurya09876
Copy link
Contributor

Hi I am getting failures for below UT

`======================================================================
FAIL: test_count_hash_sum (TestResourceFilesKeeper.TestResourceFilesKeeper)

Traceback (most recent call last):
File "ambari-server/src/test/python/TestResourceFilesKeeper.py", line 349, in test_count_hash_sum
self.assertEqual(hash_sum, self.DUMMY_UNCHANGEABLE_PACKAGE_HASH)
AssertionError: 'a3a6ed30f109be83e02a4281d427ee237947248a' != 'c83bb4e4e34213300af27bf48dabf2b7568a174b'

  • a3a6ed30f109be83e02a4281d427ee237947248a
  • c83bb4e4e34213300af27bf48dabf2b7568a174b

======================================================================
FAIL: test_zip_directory (TestResourceFilesKeeper.TestResourceFilesKeeper)

Traceback (most recent call last):
File "ambari-common/src/test/python/mock/mock.py", line 1208, in patched
return func(*args, **keywargs)
File "ambari-server/src/test/python/TestResourceFilesKeeper.py", line 434, in test_zip_directory
self.assertEqual(hash_val, self.DUMMY_UNCHANGEABLE_PACKAGE_HASH)
AssertionError: 'a3a6ed30f109be83e02a4281d427ee237947248a' != 'c83bb4e4e34213300af27bf48dabf2b7568a174b'

  • a3a6ed30f109be83e02a4281d427ee237947248a
  • c83bb4e4e34213300af27bf48dabf2b7568a174b

======================================================================
FAIL: test_collect_zookeeper (TestPortAlert.TestPortAlert)

Traceback (most recent call last):
File "/workspace/ambari/ambari-common/src/test/python/mock/mock.py", line 1208, in patched
return func(*args, **keywargs)
File "/workspace/ambari/ambari-agent/src/test/python/ambari_agent/TestPortAlert.py", line 408, in test_collect_zookeeper
alert.collect()
File "/workspace/ambari/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py", line 189, in collect
self.collector.put(self.cluster_name, data)
File "/workspace/ambari/ambari-common/src/test/python/mock/mock.py", line 962, in call
return _mock_self._mock_call(*args, **kwargs)
File "/workspace/ambari/ambari-common/src/test/python/mock/mock.py", line 1025, in _mock_call
ret_val = effect(*args, **kwargs)
File "/workspace/ambari/ambari-agent/src/test/python/ambari_agent/TestPortAlert.py", line 400, in collector_side_effect
self.assertEqual(data['state'], expected_state)
AssertionError: 'CRITICAL' != 'OK'

  • CRITICAL
  • OK

======================================================================
FAIL: test_collect_zookeeper_connectionTimeout (TestPortAlert.TestPortAlert)

Traceback (most recent call last):
File "/workspace/ambari/ambari-common/src/test/python/mock/mock.py", line 1208, in patched
return func(*args, **keywargs)
File "/workspace/ambari/ambari-agent/src/test/python/ambari_agent/TestPortAlert.py", line 525, in test_collect_zookeeper_connectionTimeout
alert.collect()
File "/workspace/ambari/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py", line 189, in collect
self.collector.put(self.cluster_name, data)
File "/workspace/ambari/ambari-common/src/test/python/mock/mock.py", line 962, in call
return _mock_self._mock_call(*args, **kwargs)
File "/workspace/ambari/ambari-common/src/test/python/mock/mock.py", line 1025, in _mock_call
ret_val = effect(*args, **kwargs)
File "/workspace/ambari/ambari-agent/src/test/python/ambari_agent/TestPortAlert.py", line 518, in collector_side_effect
self.assertEqual(data['text'], expected_text)
AssertionError: "Connection failed: 'str' object has no attribute 'decode' to 192.168.0.1:2181" != 'Connection failed: Socket Timeout to 192.168.0.1:2181'

  • Connection failed: 'str' object has no attribute 'decode' to 192.168.0.1:2181
  • Connection failed: Socket Timeout to 192.168.0.1:2181

======================================================================
FAIL: test_collect_zookeeper_warning (TestPortAlert.TestPortAlert)

Traceback (most recent call last):
File "/workspace/ambari/ambari-common/src/test/python/mock/mock.py", line 1208, in patched
return func(*args, **keywargs)
File "/workspace/ambari/ambari-agent/src/test/python/ambari_agent/TestPortAlert.py", line 466, in test_collect_zookeeper_warning
alert.collect()
File "/workspace/ambari/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py", line 189, in collect
self.collector.put(self.cluster_name, data)
File "/workspace/ambari/ambari-common/src/test/python/mock/mock.py", line 962, in call
return _mock_self._mock_call(*args, **kwargs)
File "/workspace/ambari/ambari-common/src/test/python/mock/mock.py", line 1025, in _mock_call
ret_val = effect(*args, **kwargs)
File "/workspace/ambari/ambari-agent/src/test/python/ambari_agent/TestPortAlert.py", line 458, in collector_side_effect
self.assertEqual(data['state'], expected_state)
AssertionError: 'CRITICAL' != 'WARNING'

  • CRITICAL
  • WARNING

======================================================================
FAIL: test_status_commands_clear_structured_out (TestScript.TestScript)

Traceback (most recent call last):
File "/workspace/ambari/ambari-common/src/test/python/mock/mock.py", line 1208, in patched
return func(*args, **keywargs)
File "/workspace/ambari/ambari-agent/src/test/python/resource_management/TestScript.py", line 85, in test_status_commands_clear_structured_out
self.assertFalse(open_mock.called)
AssertionError: True is not false


`

@wuzhuoming wuzhuoming changed the title Adapt Ambari 2.7.5 from python2 to python3.9[AMBARI-25854] AMBARI-25854: Adapt Ambari 2.7.5 from python2 to python3.9 Aug 2, 2023
wenwj0 added 4 commits August 2, 2023 14:02
# Conflicts:
#	ambari-agent/src/main/python/ambari_agent/CommandStatusDict.py
#	ambari-agent/src/main/python/ambari_agent/HostInfo.py
#	ambari-agent/src/main/python/ambari_agent/listeners/ConfigurationEventListener.py
#	ambari-agent/src/main/python/ambari_agent/listeners/HostLevelParamsEventListener.py
#	ambari-server/src/main/python/ambari_server/dbCleanup.py
#	ambari-server/src/test/python/TestAmbariServer.py
#	ambari-server/src/test/python/TestMpacks.py
#	ambari-server/src/test/python/TestSetupSso.py
#	ambari-server/src/test/python/TestSetupTrustedProxy.py
#	ambari-server/src/test/python/custom_actions/TestCheckHost.py
@wenwj0 wenwj0 force-pushed the AMBARI-25854-release-2.7.5 branch from c5641b7 to f02e68c Compare August 7, 2023 09:18
Copy link
Contributor

@himanshumaurya09876 himanshumaurya09876 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to above changes stderr:/var/lib/ambari-agent/data/errors.txt is filled with below logs
WARNING:root:An exception occurred when calling the hasattr method...... WARNING:root:An exception occurred when calling the hasattr method...... WARNING:root:An exception occurred when calling the hasattr method......

@bhavikpatel9977
Copy link
Contributor

@wuzhuoming ** wuzhuoming **

@wuzhuoming are you observing the same?

@wenwj0
Copy link
Contributor

wenwj0 commented Aug 17, 2023

@himanshumaurya09876 @bhavikpatel9977 Thank for pointing out the issue, we're trying to reproduce this problem locally.

BTW, can someone tell us what os the CI/CD uses, we need to figure out which python version is supported.

@bhavikpatel9977
Copy link
Contributor

BTW, can someone tell us what os the CI/CD uses, we need to figure out which python version is supported.

Good point. Are we also planning to have new branch for python-3 or we will merge it in the current 2.7.5 branch?

@wenwj0
Copy link
Contributor

wenwj0 commented Aug 17, 2023

Good point. Are we also planning to have new branch for python-3 or we will merge it in the current 2.7.5 branch?

I think we better merge this feature into a new branch, trunk or 2.8 or others.

@bhavikpatel9977
Copy link
Contributor

I think we better merge this feature into a new branch, trunk or 2.8 or others.
OK.

When are you guys planning to complete the validation?

@wenwj0
Copy link
Contributor

wenwj0 commented Aug 17, 2023

When are you guys planning to complete the validation?

we may need one more week to fix some issues and we also welcome more contributors to join for fully validation on different os or scenary.

@bhavikpatel9977
Copy link
Contributor

we may need one more week to fix some issues and we also welcome more contributors to join for fully validation on different os or scenary.

Sounds good!! Let's connect on slack channel, we will also contribute on this.

@JiaLiangC
Copy link
Contributor

I think we better merge this feature into a new branch, trunk or 2.8 or others.

I also think that merging it in version 2.8 or later is a good suggestion. 2.8 is a completely new version that is separate from HDP and is currently under development. If we merge it in, we can perform extensive validation testing during the subsequent development. Additionally, version 2.7 has already become closed source within the HDP stack and is currently in a maintenance state, making it unsuitable for major changes or new features.

@@ -107,8 +107,9 @@ def run(self):
scpstat = subprocess.Popen(scpcommand, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
log = scpstat.communicate()
errorMsg = log[1]
log = log[0] + "\n" + log[1]
log_value0 = log[0].decode()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the check if it is instance of bytes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the check if it is instance of bytes?

In python3, Subprocesses output bytes, not characters when calling communicate(), so we need to add universal_newlines=True if we want str.

https://docs.python.org/3.9/library/subprocess.html

@bhavikpatel9977
Copy link
Contributor

Due to above changes stderr:/var/lib/ambari-agent/data/errors.txt is filled with below logs WARNING:root:An exception occurred when calling the hasattr method...... WARNING:root:An exception occurred when calling the hasattr method...... WARNING:root:An exception occurred when calling the hasattr method......

@wenwj0 have you observed the same behaviour and fixed the same?

@wenwj0
Copy link
Contributor

wenwj0 commented Aug 18, 2023

Due to above changes stderr:/var/lib/ambari-agent/data/errors.txt is filled with below logs WARNING:root:An exception occurred when calling the hasattr method...... WARNING:root:An exception occurred when calling the hasattr method...... WARNING:root:An exception occurred when calling the hasattr method......

We have fixed this issue now and you guys can try to test it. @bhavikpatel9977 @himanshumaurya09876

This problem occurs when we try hasattr(xxx) or getattr(xxx), if we want to set a parameter which is not in configuration dictionary, python3 will throw an exception while python2 will not throw exception.

@wenwj0
Copy link
Contributor

wenwj0 commented Sep 4, 2023

Hi everyone, we create a new PR (#3751) to the trunk branch.

Please take a look :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.