diff --git a/extra-requirements.txt b/extra-requirements.txt index 067e40cf3..97ea79292 100644 --- a/extra-requirements.txt +++ b/extra-requirements.txt @@ -2,7 +2,6 @@ ansi2html # LGPLv3+ pandas # BSD -podman # Apache-2.0 pytest-cov # MIT pytest-rerunfailures # MPL-2.0 pytest-timeout # MIT diff --git a/tobiko/podman/_client.py b/tobiko/podman/_client.py index e2414e4cc..f11ff5a2e 100644 --- a/tobiko/podman/_client.py +++ b/tobiko/podman/_client.py @@ -15,7 +15,7 @@ # under the License. from __future__ import absolute_import -import podman +import podman1 import tobiko from tobiko.podman import _exception @@ -42,7 +42,7 @@ def podman_client(obj=None): obj = get_podman_client() if tobiko.is_fixture(obj): obj = tobiko.setup_fixture(obj).client - if isinstance(obj, podman.Client): + if isinstance(obj, podman1.Client): return obj raise TypeError('Cannot obtain a Podman client from {!r}'.format(obj)) @@ -118,9 +118,9 @@ class PodmanClientFixture(tobiko.SharedFixture): host=host, socket=socket) - client = podman.Client(uri=podman_remote_socket_uri, - remote_uri=remote_uri, - identity_file='~/.ssh/id_rsa') + client = podman1.Client(uri=podman_remote_socket_uri, + remote_uri=remote_uri, + identity_file='~/.ssh/id_rsa') client.system.ping() return client except (ConnectionRefusedError, ConnectionResetError): diff --git a/tobiko/tripleo/containers.py b/tobiko/tripleo/containers.py index f9ace81d1..7bb981fab 100644 --- a/tobiko/tripleo/containers.py +++ b/tobiko/tripleo/containers.py @@ -6,7 +6,7 @@ import functools from oslo_log import log import pandas -import podman as podmanlib +import podman1 import docker as dockerlib import tobiko @@ -376,7 +376,7 @@ def action_on_container(action, # we get the specified action as function from podman lib if container_runtime_module == podman: container_function = getattr( - podmanlib.libs.containers.Container, '{}'.format(action)) + podman1.libs.containers.Container, '{}'.format(action)) else: container_function = getattr( dockerlib.models.containers.Container, '{}'.format(action)) diff --git a/tools/common.py b/tools/common.py index 186a31f3d..74e529ed9 100644 --- a/tools/common.py +++ b/tools/common.py @@ -13,6 +13,7 @@ # under the License. from __future__ import absolute_import +import contextlib import logging import os import shlex @@ -127,3 +128,21 @@ def remove_file(filename): return True else: return False + + +@contextlib.contextmanager +def stash_dir(*target_dirs: str): + stashed_dirs = [] + for target_dir in target_dirs: + if os.path.isdir(target_dir): + stashed_dir = target_dir + '.stash' + LOG.info(f"Renaming directory: {target_dir} -> {stashed_dir}") + os.rename(target_dir, stashed_dir) + stashed_dirs.append((stashed_dir, target_dir)) + + yield + + for stashed_dir, target_dir in stashed_dirs: + if os.path.isdir(stashed_dir): + LOG.info(f"Restoring directory: {stashed_dir} -> {target_dir}") + os.rename(stashed_dir, target_dir) diff --git a/tobiko/podman/config.py b/tools/ensure_podman1.py old mode 100644 new mode 100755 similarity index 52% rename from tobiko/podman/config.py rename to tools/ensure_podman1.py index 7af0b1e60..c90bc10db --- a/tobiko/podman/config.py +++ b/tools/ensure_podman1.py @@ -1,6 +1,5 @@ -# Copyright (c) 2019 Red Hat, Inc. -# -# All Rights Reserved. +#!/usr/bin/env python3 +# Copyright 2018 Red Hat # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -14,3 +13,34 @@ # License for the specific language governing permissions and limitations # under the License. from __future__ import absolute_import + +import os +import sys + +TOP_DIR = os.path.realpath(os.path.dirname(os.path.dirname(__file__))) + +if TOP_DIR not in sys.path: + sys.path.insert(0, TOP_DIR) + +from tools import common # noqa +from tools import install # noqa + + +LOG = common.get_logger(__name__) + + +def main(): + common.setup_logging() + ensure_podman1() + + +def ensure_podman1(): + try: + import podman1 + except ImportError: + install.install_podman1() + import podman1 + + +if __name__ == '__main__': + main() diff --git a/tools/install.py b/tools/install.py index da9ced08a..99e7bce51 100755 --- a/tools/install.py +++ b/tools/install.py @@ -15,7 +15,10 @@ from __future__ import absolute_import import os +import site import sys +import typing + TOP_DIR = os.path.realpath(os.path.dirname(os.path.dirname(__file__))) @@ -41,6 +44,7 @@ def main(): common.setup_logging() install_tox() install_bindeps() + install_podman1() install_tobiko() @@ -61,11 +65,51 @@ def install_tobiko(): pip_install(f"-e '{TOP_DIR}'") +def install_podman1(version=''): + pip_unisntall('podman') + + LOG.info(f"Installing Podman... (version: {version})") + + site_dirs = {os.path.dirname(os.path.realpath(site_dir)) + for site_dir in site.getsitepackages() + if os.path.isdir(site_dir)} + more_site_dirs = {os.path.join(site_dir, 'site-packages') + for site_dir in site_dirs + if os.path.isdir(os.path.join(site_dir, 'site-packages'))} + site_dirs.update(more_site_dirs) + LOG.debug(f"Site packages dirs: {site_dirs}") + + # Must ensure pre-existing podman directories are restored + # after installation + podman_dirs = [os.path.join(site_dir, 'podman') + for site_dir in sorted(site_dirs)] + LOG.debug(f"Possible podman directories: {podman_dirs}") + with common.stash_dir(*podman_dirs): + for podman_dir in podman_dirs: + assert not os.path.exists(podman_dir) + pip_install(f"'podman{version}'") + for podman_dir in podman_dirs: + if os.path.isdir(podman_dir): + # Rename podman directory to podman1 + os.rename(podman_dir, podman_dir + '1') + break + else: + raise RuntimeError("Podman directory not found!") + for podman_dir in podman_dirs: + assert not os.path.exists(podman_dir) + + def pip_install(args): LOG.debug(f"Installing packages: {args}...") common.execute_python(f"-m pip install {TOX_CONSTRAINTS} {args}", capture_stdout=False) +def pip_unisntall(args): + LOG.debug(f"Uninstalling packages: {args}...") + common.execute_python(f"-m pip uninstall -y {args}", + capture_stdout=False) + + if __name__ == '__main__': main() diff --git a/tox.ini b/tox.ini index fbef8ae74..f21e01360 100644 --- a/tox.ini +++ b/tox.ini @@ -39,6 +39,8 @@ setenv = TOX_CONSTRAINTS_FILE = {env:TOX_CONSTRAINTS_FILE:https://opendev.org/openstack/requirements/raw/branch/master/upper-constraints.txt} TOX_EXTRA_REQUIREMENTS = {env:TOX_EXTRA_REQUIREMENTS:-r{toxinidir}/extra-requirements.txt} VIRTUAL_ENV = {envdir} +commands_pre = + {envpython} {toxinidir}/tools/ensure_podman1.py commands = {envpython} {toxinidir}/tools/run_tests.py {posargs:{env:RUN_TESTS_EXTRA_ARGS}}