1
0
Fork 0
mirror of https://github.com/containers/ansible-podman-collections.git synced 2026-02-04 07:11:49 +00:00

Run black -l 120 on all Python files to unify the style (#939)

Signed-off-by: Sagi Shnaidman <sshnaidm@redhat.com>
This commit is contained in:
Sergey 2025-06-15 18:25:48 +03:00 committed by GitHub
parent 50c5a2549d
commit 4c682e170c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 3828 additions and 3129 deletions

View file

@ -7,11 +7,12 @@
#
# Written by: Tomas Tomecek (https://github.com/TomasTomecek)
from __future__ import (absolute_import, division, print_function)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
DOCUMENTATION = """
short_description: Interact with an existing buildah container
description:
- Run commands or put/fetch files to an existing container using buildah tool.
@ -39,7 +40,7 @@ DOCUMENTATION = '''
- name: ansible_user
# keyword:
# - name: remote_user
'''
"""
import os
import shlex
@ -61,7 +62,7 @@ class Connection(ConnectionBase):
"""
# String used to identify this Connection class from other classes
transport = 'containers.podman.buildah'
transport = "containers.podman.buildah"
has_pipelining = True
def __init__(self, play_context, new_stdin, *args, **kwargs):
@ -77,7 +78,9 @@ class Connection(ConnectionBase):
display.vvvv("Using buildah connection from collection")
def _set_user(self):
self._buildah(b"config", [b"--user=" + to_bytes(self.user, errors='surrogate_or_strict')])
self._buildah(
b"config", [b"--user=" + to_bytes(self.user, errors="surrogate_or_strict")]
)
def _buildah(self, cmd, cmd_args=None, in_data=None, outfile_stdout=None):
"""
@ -89,17 +92,17 @@ class Connection(ConnectionBase):
:param outfile_stdout: file for writing STDOUT to
:return: return code, stdout, stderr
"""
buildah_exec = 'buildah'
buildah_exec = "buildah"
local_cmd = [buildah_exec]
if isinstance(cmd, str):
local_cmd.append(cmd)
else:
local_cmd.extend(cmd)
if self.user and self.user != 'root':
if cmd == 'run':
if self.user and self.user != "root":
if cmd == "run":
local_cmd.extend(("--user", self.user))
elif cmd == 'copy':
elif cmd == "copy":
local_cmd.extend(("--chown", self.user))
local_cmd.append(self._container_id)
@ -109,23 +112,27 @@ class Connection(ConnectionBase):
else:
local_cmd.extend(cmd_args)
local_cmd = [to_bytes(i, errors='surrogate_or_strict')
for i in local_cmd]
local_cmd = [to_bytes(i, errors="surrogate_or_strict") for i in local_cmd]
display.vvv("RUN %s" % (local_cmd,), host=self._container_id)
if outfile_stdout:
stdout_fd = open(outfile_stdout, "wb")
else:
stdout_fd = subprocess.PIPE
p = subprocess.Popen(local_cmd, shell=False, stdin=subprocess.PIPE,
stdout=stdout_fd, stderr=subprocess.PIPE)
p = subprocess.Popen(
local_cmd,
shell=False,
stdin=subprocess.PIPE,
stdout=stdout_fd,
stderr=subprocess.PIPE,
)
stdout, stderr = p.communicate(input=in_data)
display.vvvv("STDOUT %s" % to_text(stdout))
display.vvvv("STDERR %s" % to_text(stderr))
display.vvvv("RC CODE %s" % p.returncode)
stdout = to_bytes(stdout, errors='surrogate_or_strict')
stderr = to_bytes(stderr, errors='surrogate_or_strict')
stdout = to_bytes(stdout, errors="surrogate_or_strict")
stderr = to_bytes(stderr, errors="surrogate_or_strict")
return p.returncode, stdout, stderr
def _connect(self):
@ -136,19 +143,26 @@ class Connection(ConnectionBase):
super(Connection, self)._connect()
rc, self._mount_point, stderr = self._buildah("mount")
if rc != 0:
display.v("Failed to mount container %s: %s" % (self._container_id, stderr.strip()))
display.v(
"Failed to mount container %s: %s"
% (self._container_id, stderr.strip())
)
else:
self._mount_point = self._mount_point.strip() + to_bytes(os.path.sep, errors='surrogate_or_strict')
display.vvvv("MOUNTPOINT %s RC %s STDERR %r" % (self._mount_point, rc, stderr))
self._mount_point = self._mount_point.strip() + to_bytes(
os.path.sep, errors="surrogate_or_strict"
)
display.vvvv(
"MOUNTPOINT %s RC %s STDERR %r" % (self._mount_point, rc, stderr)
)
self._connected = True
@ensure_connect
def exec_command(self, cmd, in_data=None, sudoable=False):
""" run specified command in a running OCI container using buildah """
"""run specified command in a running OCI container using buildah"""
super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)
# shlex.split has a bug with text strings on Python-2.6 and can only handle text strings on Python-3
cmd_args_list = shlex.split(to_native(cmd, errors='surrogate_or_strict'))
cmd_args_list = shlex.split(to_native(cmd, errors="surrogate_or_strict"))
rc, stdout, stderr = self._buildah("run", cmd_args_list, in_data)
@ -156,47 +170,51 @@ class Connection(ConnectionBase):
return rc, stdout, stderr
def put_file(self, in_path, out_path):
""" Place a local file located in 'in_path' inside container at 'out_path' """
"""Place a local file located in 'in_path' inside container at 'out_path'"""
super(Connection, self).put_file(in_path, out_path)
display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._container_id)
if not self._mount_point or self.user:
rc, stdout, stderr = self._buildah(
"copy", [in_path, out_path])
rc, stdout, stderr = self._buildah("copy", [in_path, out_path])
if rc != 0:
raise AnsibleError(
"Failed to copy file from %s to %s in container %s\n%s" % (
in_path, out_path, self._container_id, stderr)
"Failed to copy file from %s to %s in container %s\n%s"
% (in_path, out_path, self._container_id, stderr)
)
else:
real_out_path = self._mount_point + to_bytes(out_path, errors='surrogate_or_strict')
real_out_path = self._mount_point + to_bytes(
out_path, errors="surrogate_or_strict"
)
shutil.copyfile(
to_bytes(in_path, errors='surrogate_or_strict'),
to_bytes(real_out_path, errors='surrogate_or_strict')
to_bytes(in_path, errors="surrogate_or_strict"),
to_bytes(real_out_path, errors="surrogate_or_strict"),
)
def fetch_file(self, in_path, out_path):
""" obtain file specified via 'in_path' from the container and place it at 'out_path' """
"""obtain file specified via 'in_path' from the container and place it at 'out_path'"""
super(Connection, self).fetch_file(in_path, out_path)
display.vvv("FETCH %s TO %s" %
(in_path, out_path), host=self._container_id)
display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self._container_id)
if not self._mount_point:
rc, stdout, stderr = self._buildah(
"run",
["cat", to_bytes(in_path, errors='surrogate_or_strict')],
outfile_stdout=out_path)
["cat", to_bytes(in_path, errors="surrogate_or_strict")],
outfile_stdout=out_path,
)
if rc != 0:
raise AnsibleError("Failed to fetch file from %s to %s from container %s\n%s" % (
in_path, out_path, self._container_id, stderr))
raise AnsibleError(
"Failed to fetch file from %s to %s from container %s\n%s"
% (in_path, out_path, self._container_id, stderr)
)
else:
real_in_path = self._mount_point + \
to_bytes(in_path, errors='surrogate_or_strict')
real_in_path = self._mount_point + to_bytes(
in_path, errors="surrogate_or_strict"
)
shutil.copyfile(
to_bytes(real_in_path, errors='surrogate_or_strict'),
to_bytes(out_path, errors='surrogate_or_strict')
to_bytes(real_in_path, errors="surrogate_or_strict"),
to_bytes(out_path, errors="surrogate_or_strict"),
)
def close(self):
""" unmount container's filesystem """
"""unmount container's filesystem"""
super(Connection, self).close()
rc, stdout, stderr = self._buildah("umount")
display.vvvv("RC %s STDOUT %r STDERR %r" % (rc, stdout, stderr))