Browse Source

osp16.2 >= icorporate podman-py api usage podman_ver3, where needed

Change-Id: I5be8be3f95a0cb2a09a4988c451bdfa0d6580b2f
changes/85/788685/32
r 1 month ago
parent
commit
3fa7adcab1
4 changed files with 104 additions and 47 deletions
  1. +2
    -1
      extra-requirements.txt
  2. +1
    -1
      tobiko/podman/__init__.py
  3. +76
    -37
      tobiko/podman/_client.py
  4. +25
    -8
      tobiko/tripleo/containers.py

+ 2
- 1
extra-requirements.txt View File

@ -2,7 +2,8 @@
ansi2html # LGPLv3+
pandas # BSD
podman-py # Apache-2.0
pytest-cov # MIT
pytest-rerunfailures # MPL-2.0
pytest-timeout # MIT
validations-libs # APACHE-2.0
validations-libs # Apache-2.0

+ 1
- 1
tobiko/podman/__init__.py View File

@ -19,7 +19,7 @@ from tobiko.podman import _client
from tobiko.podman import _shell
from tobiko.podman import _exception
Podman_Version_3 = _client.podman_version_3
PodmanClientFixture = _client.PodmanClientFixture
get_podman_client = _client.get_podman_client
list_podman_containers = _client.list_podman_containers


+ 76
- 37
tobiko/podman/_client.py View File

@ -1,13 +1,3 @@
# Copyright (c) 2019 Red Hat, Inc.
#
# All Rights Reserved.
#
# 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
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@ -15,14 +5,23 @@
# under the License.
from __future__ import absolute_import
import subprocess
import os
from oslo_log import log
import podman
import podman1
import tobiko
from tobiko.podman import _exception
from tobiko.podman import _shell
from tobiko.shell import ssh
from tobiko.shell import sh
LOG = log.getLogger(__name__)
def get_podman_client(ssh_client=None):
return PodmanClientFixture(ssh_client=ssh_client)
@ -37,16 +36,31 @@ def list_podman_containers(client=None, **kwargs):
return tobiko.select(containers)
PODMAN_CLIENT_CLASSES = \
podman1.Client, podman.PodmanClient # pylint: disable=E1101
def podman_client(obj=None):
if obj is None:
obj = get_podman_client()
if tobiko.is_fixture(obj):
obj = tobiko.setup_fixture(obj).client
if isinstance(obj, podman1.Client):
if isinstance(obj, PODMAN_CLIENT_CLASSES):
return obj
raise TypeError('Cannot obtain a Podman client from {!r}'.format(obj))
def podman_version_3():
podman_ver = sh.execute('rpm -q podman').stdout.split('-')[1].split('.')[0]
if int(podman_ver) >= 3:
return True
else:
return False
class PodmanClientFixture(tobiko.SharedFixture):
client = None
@ -70,30 +84,37 @@ class PodmanClientFixture(tobiko.SharedFixture):
return ssh_client
def setup_client(self):
# setup podman access via varlink
# podman ver3 (osp>=16.2) has different service / socket paths
if podman_version_3():
podman_service = 'podman.socket'
podman_socket_file = '/run/podman/podman.sock'
else:
podman_service = 'io.podman.socket'
podman_socket_file = '/run/podman/io.podman'
podman_client_setup_cmds = \
"sudo test -f /var/varlink_client_access_setup || \
f"""sudo test -f /var/podman_client_access_setup || \
(sudo groupadd -f podman && \
sudo usermod -a -G podman heat-admin && \
sudo chmod -R o=wxr /etc/tmpfiles.d && \
sudo echo 'd /run/podman 0770 root heat-admin' > \
/etc/tmpfiles.d/podman.conf && \
sudo cp /lib/systemd/system/io.podman.socket \
/etc/systemd/system/io.podman.socket && \
sudo crudini --set /etc/systemd/system/io.podman.socket Socket \
sudo cp /lib/systemd/system/{podman_service} \
/etc/systemd/system/{podman_service} && \
sudo crudini --set /etc/systemd/system/{podman_service} Socket \
SocketMode 0660 && \
sudo crudini --set /etc/systemd/system/io.podman.socket Socket \
sudo crudini --set /etc/systemd/system/{podman_service} Socket \
SocketGroup podman && \
sudo systemctl daemon-reload && \
sudo systemd-tmpfiles --create && \
sudo systemctl enable --now io.podman.socket && \
sudo systemctl enable --now {podman_service} && \
sudo chmod 777 /run/podman && \
sudo chown -R root: /run/podman && \
sudo chmod g+rw /run/podman/io.podman && \
sudo chmod 777 /run/podman/io.podman && \
sudo chmod g+rw {podman_socket_file} && \
sudo chmod 777 {podman_socket_file} && \
sudo setenforce 0 && \
sudo systemctl start io.podman.socket && \
sudo touch /var/varlink_client_access_setup)"
sudo systemctl start {podman_service} && \
sudo touch /var/podman_client_access_setup)"""
sh.execute(podman_client_setup_cmds, ssh_client=self.ssh_client)
@ -102,26 +123,44 @@ class PodmanClientFixture(tobiko.SharedFixture):
self.client = client = self.create_client()
return client
def create_client(self):
for _ in range(360):
def create_client(self): # noqa: C901
for _ in tobiko.retry(timeout=60., interval=5.):
try:
podman_remote_socket = self.discover_podman_socket()
username = self.ssh_client.connect_parameters['username']
host = self.ssh_client.connect_parameters["hostname"]
socket = podman_remote_socket
podman_remote_socket_uri = \
'unix:/tmp/podman.sock_{}'.format(host)
remote_uri = 'ssh://{username}@{host}{socket}'.format(
username=username,
host=host,
socket=socket)
client = podman1.Client(uri=podman_remote_socket_uri,
remote_uri=remote_uri,
identity_file='~/.ssh/id_rsa')
client.system.ping()
podman_remote_socket_uri = f'unix:/tmp/podman.sock_{host}'
remote_uri = f'ssh://{username}@{host}{socket}'
if podman_version_3():
# check if a ssh tunnel exists, if not create one
psall = str(subprocess.check_output(('ps', '-ef')))
if f'ssh -L /tmp/podman.sock_{host}' not in psall:
if os.path.exists(f"/tmp/podman.sock_{host}"):
subprocess.call(
['rm', '-f', f'/tmp/podman.sock_{host}'])
# start a background ssh tunnel with the remote host
subprocess.call(['ssh', '-L',
f'/tmp/podman.sock_{host}:'
f'/run/podman/podman.sock',
host, '-N', '-f'])
for _ in tobiko.retry(timeout=60., interval=1.):
if os.path.exists(f'/tmp/podman.sock_{host}'):
break
client = podman.PodmanClient(
base_url=podman_remote_socket_uri)
if client.ping():
LOG.info('container_client is online')
else:
client = podman1.Client( # pylint: disable=E1101
uri=podman_remote_socket_uri,
remote_uri=remote_uri,
identity_file='~/.ssh/id_rsa')
if client.system.ping():
LOG.info('container_client is online')
return client
except (ConnectionRefusedError, ConnectionResetError):
# retry


+ 25
- 8
tobiko/tripleo/containers.py View File

@ -305,15 +305,31 @@ def comparable_container_keys(container, include_container_objects=False):
"""returns the tuple : 'container_host','container_name',
'container_state, container object if specified'
"""
if container_runtime_module == podman and include_container_objects:
return (tripleo_topology.ip_to_hostname(
# Differenciate between podman_ver3 with podman-py from earlier api
if podman.Podman_Version_3():
con_host_name_stat_obj_tuple = (tripleo_topology.ip_to_hostname(
container.client.base_url.rsplit('_')[1]), container.attrs[
'Names'][0], container.attrs['State'], container)
con_host_name_stat_tuple = (tripleo_topology.ip_to_hostname(
container.client.base_url.rsplit('_')[1]), container.attrs[
'Names'][0], container.attrs['State'])
else:
con_host_name_stat_obj_tuple = (tripleo_topology.ip_to_hostname(
container._client._context.hostname), # pylint: disable=W0212
container.data['names'], container.data['status'],
container)
elif container_runtime_module == podman:
return (tripleo_topology.ip_to_hostname(
container.data['names'], container.data['status'], container)
con_host_name_stat_tuple = (tripleo_topology.ip_to_hostname(
container._client._context.hostname), # pylint: disable=W0212
container.data['names'], container.data['status'])
container.data['names'], container.data['status'])
if container_runtime_module == podman and include_container_objects:
return con_host_name_stat_obj_tuple
elif container_runtime_module == podman:
return con_host_name_stat_tuple
elif container_runtime_module == docker and include_container_objects:
return (container.attrs['Config']['Hostname'],
@ -376,7 +392,8 @@ def action_on_container(action,
# we get the specified action as function from podman lib
if container_runtime_module == podman:
container_function = getattr(
podman1.libs.containers.Container, '{}'.format(action))
podman1.libs.containers.Container, # pylint: disable=E1101
'{}'.format(action))
else:
container_function = getattr(
dockerlib.models.containers.Container, '{}'.format(action))


Loading…
Cancel
Save