From c811e534fb97189758433ecbfd9778a9777af41c Mon Sep 17 00:00:00 2001 From: Gabor Schulz <46921007+gaborschulz@users.noreply.github.com> Date: Sat, 3 Dec 2022 10:15:06 +0100 Subject: [PATCH] Alternative way to check if running in Docker (#1204) * Update setup.py * Update __init__.py * :bug: fix FileNotFoundError in running_in_docker (#1201) * Remove version constraints * Changed the way Docker is determined * Reference to ticket 1140 * Added documentation for ZAPPA_RUNNING_IN_DOCKER flag * Fixed indentation * Removed log file * Added strtobool and re-added MINIMUM_SUPPORTED_MIINOR_VERSION * updated version * Added #1201 to CHANGELOG.md, reformatted code * Reformatted by black due to line length * Removed unnecessary comment Co-authored-by: monkut --- CHANGELOG.md | 5 +++++ Makefile | 2 +- README.md | 10 ++++++++++ tests/tests.py | 4 ++-- zappa/__init__.py | 19 ++++++++----------- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7abf91d5d..b6e512987 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Zappa Changelog +## 0.56.1 + +* Fix FileNotFoundError in running_in_docker (#1201) +* Use ZAPPA_RUNNING_IN_DOCKER environment variable to use any Python version inside Docker container (#1140) + ## 0.56.0 * Not recognizing virtaulenv created with pyenv (#1132) * Remove six from zappa dependencies (#1164) diff --git a/Makefile b/Makefile index f1c2aa999..9ec1ebd8a 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ requirements: pipenv lock pipenv sync --dev -build: clean requirements-install +build: clean requirements python setup.py sdist python setup.py bdist_wheel diff --git a/README.md b/README.md index 1d5aaf13a..48db0eb85 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,16 @@ Update Example: Refer to [the blog post](https://ianwhitestone.work/zappa-serverless-docker/) for more details about how to leverage this functionality, and when you may want to. +If you are using a custom Docker image for your Lambda runtime (e.g. if you want to use a newer version of Python that is not yet supported by Lambda out of the box) and you would like to bypass the Python version check, you can set an environment variable to do so: + + $ export ZAPPA_RUNNING_IN_DOCKER=True + +You can also add this to your Dockerfile like this: + +``` +ENV ZAPPA_RUNNING_IN_DOCKER=True +``` + ### Rollback You can also `rollback` the deployed code to a previous version by supplying the number of revisions to return to. For instance, to rollback to the version deployed 3 versions ago: diff --git a/tests/tests.py b/tests/tests.py index f513e3c5f..5de4edee4 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -2604,7 +2604,7 @@ def test_unsupported_version_error(self, *_): reload(zappa) - @mock.patch("pathlib.Path.read_text", return_value="/docker/") + @mock.patch("os.getenv", return_value="True") @mock.patch("sys.version_info", new_callable=partial(get_sys_versioninfo, 6)) def test_minor_version_only_check_when_in_docker(self, *_): from importlib import reload @@ -2614,7 +2614,7 @@ def test_minor_version_only_check_when_in_docker(self, *_): reload(zappa) - @mock.patch("pathlib.Path.read_text", return_value="/docker/") + @mock.patch("os.getenv", return_value="True") @mock.patch("sys.version_info", new_callable=partial(get_sys_versioninfo, 7)) def test_no_runtimeerror_when_in_docker(self, *_): from importlib import reload diff --git a/zappa/__init__.py b/zappa/__init__.py index 9391d49c3..cd60f2701 100644 --- a/zappa/__init__.py +++ b/zappa/__init__.py @@ -1,5 +1,5 @@ +import os import sys -from pathlib import Path def running_in_docker() -> bool: @@ -7,10 +7,9 @@ def running_in_docker() -> bool: Determine if zappa is running in docker. - When docker is used allow usage of any python version """ - # https://stackoverflow.com/a/20012536/24718 - cgroup_content = Path("/proc/1/cgroup").read_text() - in_docker = "/docker/" in cgroup_content or "/lxc/" in cgroup_content - return in_docker + # https://stackoverflow.com/questions/63116419 + running_in_docker_flag = os.getenv("ZAPPA_RUNNING_IN_DOCKER", "False").lower() in {"y", "yes", "t", "true", "1"} + return running_in_docker_flag SUPPORTED_VERSIONS = [(3, 7), (3, 8), (3, 9)] @@ -19,10 +18,9 @@ def running_in_docker() -> bool: if not running_in_docker() and sys.version_info[:2] not in SUPPORTED_VERSIONS: print(running_in_docker()) formatted_supported_versions = ["{}.{}".format(*version) for version in SUPPORTED_VERSIONS] - err_msg = ( - f"This version of Python ({sys.version_info.major}.{sys.version_info.minor}) is not supported!\n" - f"Zappa (and AWS Lambda) support the following versions of Python: {formatted_supported_versions}" - ) + err_msg = "This version of Python ({}.{}) is not supported!\n".format( + *sys.version_info + ) + "Zappa (and AWS Lambda) support the following versions of Python: {}".format(formatted_supported_versions) raise RuntimeError(err_msg) elif running_in_docker() and sys.version_info.minor < MINIMUM_SUPPORTED_MINOR_VERSION: # when running in docker enforce minimum version only @@ -32,5 +30,4 @@ def running_in_docker() -> bool: ) raise RuntimeError(err_msg) - -__version__ = "0.56.0" +__version__ = "0.56.1"