Let rename old podman varlink package as podman1

This allows to import old varlink based podman client
ase podman1 while keeping safe to install the new
client podman-py package as podman.

To archive such result it first temporarly rename existing podman
dir, then install podman package on its place, renames it as
podman1, and finally restore pre-existing podman directory
if any initially found.

Change-Id: If834cc383f11e656f17e49d5e6fa401ac8bee829
This commit is contained in:
Federico Ressi 2021-05-14 14:38:30 +02:00
parent a655a71c7d
commit 6a56f0f0fb
7 changed files with 105 additions and 11 deletions

View File

@ -2,7 +2,6 @@
ansi2html # LGPLv3+
pandas # BSD
podman # Apache-2.0
pytest-cov # MIT
pytest-rerunfailures # MPL-2.0
pytest-timeout # MIT

View File

@ -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):

View File

@ -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))

View File

@ -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)

36
tobiko/podman/config.py → tools/ensure_podman1.py Normal file → Executable file
View File

@ -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()

View File

@ -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()

View File

@ -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}}