osp16.2 >= icorporate podman-py api usage podman_ver3, where needed
Change-Id: I5be8be3f95a0cb2a09a4988c451bdfa0d6580b2f
This commit is contained in:
parent
2e9db6ed31
commit
3fa7adcab1
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
ansi2html # LGPLv3+
|
ansi2html # LGPLv3+
|
||||||
pandas # BSD
|
pandas # BSD
|
||||||
|
podman-py # Apache-2.0
|
||||||
pytest-cov # MIT
|
pytest-cov # MIT
|
||||||
pytest-rerunfailures # MPL-2.0
|
pytest-rerunfailures # MPL-2.0
|
||||||
pytest-timeout # MIT
|
pytest-timeout # MIT
|
||||||
validations-libs # APACHE-2.0
|
validations-libs # Apache-2.0
|
||||||
|
@ -19,7 +19,7 @@ from tobiko.podman import _client
|
|||||||
from tobiko.podman import _shell
|
from tobiko.podman import _shell
|
||||||
from tobiko.podman import _exception
|
from tobiko.podman import _exception
|
||||||
|
|
||||||
|
Podman_Version_3 = _client.podman_version_3
|
||||||
PodmanClientFixture = _client.PodmanClientFixture
|
PodmanClientFixture = _client.PodmanClientFixture
|
||||||
get_podman_client = _client.get_podman_client
|
get_podman_client = _client.get_podman_client
|
||||||
list_podman_containers = _client.list_podman_containers
|
list_podman_containers = _client.list_podman_containers
|
||||||
|
@ -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
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
@ -15,14 +5,23 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
import os
|
||||||
|
|
||||||
|
from oslo_log import log
|
||||||
|
import podman
|
||||||
import podman1
|
import podman1
|
||||||
|
|
||||||
|
|
||||||
import tobiko
|
import tobiko
|
||||||
from tobiko.podman import _exception
|
from tobiko.podman import _exception
|
||||||
from tobiko.podman import _shell
|
from tobiko.podman import _shell
|
||||||
from tobiko.shell import ssh
|
from tobiko.shell import ssh
|
||||||
from tobiko.shell import sh
|
from tobiko.shell import sh
|
||||||
|
|
||||||
|
LOG = log.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def get_podman_client(ssh_client=None):
|
def get_podman_client(ssh_client=None):
|
||||||
return PodmanClientFixture(ssh_client=ssh_client)
|
return PodmanClientFixture(ssh_client=ssh_client)
|
||||||
@ -37,16 +36,31 @@ def list_podman_containers(client=None, **kwargs):
|
|||||||
return tobiko.select(containers)
|
return tobiko.select(containers)
|
||||||
|
|
||||||
|
|
||||||
|
PODMAN_CLIENT_CLASSES = \
|
||||||
|
podman1.Client, podman.PodmanClient # pylint: disable=E1101
|
||||||
|
|
||||||
|
|
||||||
def podman_client(obj=None):
|
def podman_client(obj=None):
|
||||||
if obj is None:
|
if obj is None:
|
||||||
obj = get_podman_client()
|
obj = get_podman_client()
|
||||||
|
|
||||||
if tobiko.is_fixture(obj):
|
if tobiko.is_fixture(obj):
|
||||||
obj = tobiko.setup_fixture(obj).client
|
obj = tobiko.setup_fixture(obj).client
|
||||||
if isinstance(obj, podman1.Client):
|
|
||||||
|
if isinstance(obj, PODMAN_CLIENT_CLASSES):
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
raise TypeError('Cannot obtain a Podman client from {!r}'.format(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):
|
class PodmanClientFixture(tobiko.SharedFixture):
|
||||||
|
|
||||||
client = None
|
client = None
|
||||||
@ -70,30 +84,37 @@ class PodmanClientFixture(tobiko.SharedFixture):
|
|||||||
return ssh_client
|
return ssh_client
|
||||||
|
|
||||||
def setup_client(self):
|
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 = \
|
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 groupadd -f podman && \
|
||||||
sudo usermod -a -G podman heat-admin && \
|
sudo usermod -a -G podman heat-admin && \
|
||||||
sudo chmod -R o=wxr /etc/tmpfiles.d && \
|
sudo chmod -R o=wxr /etc/tmpfiles.d && \
|
||||||
sudo echo 'd /run/podman 0770 root heat-admin' > \
|
sudo echo 'd /run/podman 0770 root heat-admin' > \
|
||||||
/etc/tmpfiles.d/podman.conf && \
|
/etc/tmpfiles.d/podman.conf && \
|
||||||
sudo cp /lib/systemd/system/io.podman.socket \
|
sudo cp /lib/systemd/system/{podman_service} \
|
||||||
/etc/systemd/system/io.podman.socket && \
|
/etc/systemd/system/{podman_service} && \
|
||||||
sudo crudini --set /etc/systemd/system/io.podman.socket Socket \
|
sudo crudini --set /etc/systemd/system/{podman_service} Socket \
|
||||||
SocketMode 0660 && \
|
SocketMode 0660 && \
|
||||||
sudo crudini --set /etc/systemd/system/io.podman.socket Socket \
|
sudo crudini --set /etc/systemd/system/{podman_service} Socket \
|
||||||
SocketGroup podman && \
|
SocketGroup podman && \
|
||||||
sudo systemctl daemon-reload && \
|
sudo systemctl daemon-reload && \
|
||||||
sudo systemd-tmpfiles --create && \
|
sudo systemd-tmpfiles --create && \
|
||||||
sudo systemctl enable --now io.podman.socket && \
|
sudo systemctl enable --now {podman_service} && \
|
||||||
sudo chmod 777 /run/podman && \
|
sudo chmod 777 /run/podman && \
|
||||||
sudo chown -R root: /run/podman && \
|
sudo chown -R root: /run/podman && \
|
||||||
sudo chmod g+rw /run/podman/io.podman && \
|
sudo chmod g+rw {podman_socket_file} && \
|
||||||
sudo chmod 777 /run/podman/io.podman && \
|
sudo chmod 777 {podman_socket_file} && \
|
||||||
sudo setenforce 0 && \
|
sudo setenforce 0 && \
|
||||||
sudo systemctl start io.podman.socket && \
|
sudo systemctl start {podman_service} && \
|
||||||
sudo touch /var/varlink_client_access_setup)"
|
sudo touch /var/podman_client_access_setup)"""
|
||||||
|
|
||||||
sh.execute(podman_client_setup_cmds, ssh_client=self.ssh_client)
|
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()
|
self.client = client = self.create_client()
|
||||||
return client
|
return client
|
||||||
|
|
||||||
def create_client(self):
|
def create_client(self): # noqa: C901
|
||||||
for _ in range(360):
|
for _ in tobiko.retry(timeout=60., interval=5.):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
podman_remote_socket = self.discover_podman_socket()
|
podman_remote_socket = self.discover_podman_socket()
|
||||||
username = self.ssh_client.connect_parameters['username']
|
username = self.ssh_client.connect_parameters['username']
|
||||||
host = self.ssh_client.connect_parameters["hostname"]
|
host = self.ssh_client.connect_parameters["hostname"]
|
||||||
socket = podman_remote_socket
|
socket = podman_remote_socket
|
||||||
podman_remote_socket_uri = \
|
podman_remote_socket_uri = f'unix:/tmp/podman.sock_{host}'
|
||||||
'unix:/tmp/podman.sock_{}'.format(host)
|
|
||||||
|
|
||||||
remote_uri = 'ssh://{username}@{host}{socket}'.format(
|
remote_uri = f'ssh://{username}@{host}{socket}'
|
||||||
username=username,
|
|
||||||
host=host,
|
|
||||||
socket=socket)
|
|
||||||
|
|
||||||
client = podman1.Client(uri=podman_remote_socket_uri,
|
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,
|
remote_uri=remote_uri,
|
||||||
identity_file='~/.ssh/id_rsa')
|
identity_file='~/.ssh/id_rsa')
|
||||||
client.system.ping()
|
if client.system.ping():
|
||||||
|
LOG.info('container_client is online')
|
||||||
return client
|
return client
|
||||||
except (ConnectionRefusedError, ConnectionResetError):
|
except (ConnectionRefusedError, ConnectionResetError):
|
||||||
# retry
|
# retry
|
||||||
|
@ -305,16 +305,32 @@ def comparable_container_keys(container, include_container_objects=False):
|
|||||||
"""returns the tuple : 'container_host','container_name',
|
"""returns the tuple : 'container_host','container_name',
|
||||||
'container_state, container object if specified'
|
'container_state, container object if specified'
|
||||||
"""
|
"""
|
||||||
if container_runtime_module == podman and include_container_objects:
|
# Differenciate between podman_ver3 with podman-py from earlier api
|
||||||
return (tripleo_topology.ip_to_hostname(
|
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._client._context.hostname), # pylint: disable=W0212
|
||||||
container.data['names'], container.data['status'],
|
container.data['names'], container.data['status'], container)
|
||||||
container)
|
|
||||||
elif container_runtime_module == podman:
|
con_host_name_stat_tuple = (tripleo_topology.ip_to_hostname(
|
||||||
return (tripleo_topology.ip_to_hostname(
|
|
||||||
container._client._context.hostname), # pylint: disable=W0212
|
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:
|
elif container_runtime_module == docker and include_container_objects:
|
||||||
return (container.attrs['Config']['Hostname'],
|
return (container.attrs['Config']['Hostname'],
|
||||||
container.attrs['Name'].strip('/'),
|
container.attrs['Name'].strip('/'),
|
||||||
@ -376,7 +392,8 @@ def action_on_container(action,
|
|||||||
# we get the specified action as function from podman lib
|
# we get the specified action as function from podman lib
|
||||||
if container_runtime_module == podman:
|
if container_runtime_module == podman:
|
||||||
container_function = getattr(
|
container_function = getattr(
|
||||||
podman1.libs.containers.Container, '{}'.format(action))
|
podman1.libs.containers.Container, # pylint: disable=E1101
|
||||||
|
'{}'.format(action))
|
||||||
else:
|
else:
|
||||||
container_function = getattr(
|
container_function = getattr(
|
||||||
dockerlib.models.containers.Container, '{}'.format(action))
|
dockerlib.models.containers.Container, '{}'.format(action))
|
||||||
|
Loading…
Reference in New Issue
Block a user