mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 05:42:50 +00:00
[PR #11095/2b4333a0 backport][stable-12] Use raise from in plugins (#11129)
Use raise from in plugins (#11095)
* Use raise from.
* Add changelog fragment.
(cherry picked from commit 2b4333a033)
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
@@ -129,7 +129,7 @@ class Connection(ConnectionBase):
|
||||
try:
|
||||
self.chroot_cmd = get_bin_path(self.get_option("chroot_exe"))
|
||||
except ValueError as e:
|
||||
raise AnsibleError(str(e))
|
||||
raise AnsibleError(str(e)) from e
|
||||
|
||||
super()._connect()
|
||||
if not self._connected:
|
||||
@@ -191,17 +191,17 @@ class Connection(ConnectionBase):
|
||||
count = ""
|
||||
try:
|
||||
p = self._buffered_exec_command(f"dd of={out_path} bs={BUFSIZE}{count}", stdin=in_file)
|
||||
except OSError:
|
||||
raise AnsibleError("chroot connection requires dd command in the chroot")
|
||||
except OSError as e:
|
||||
raise AnsibleError("chroot connection requires dd command in the chroot") from e
|
||||
try:
|
||||
stdout, stderr = p.communicate()
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}")
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}") from e
|
||||
if p.returncode != 0:
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}:\n{stdout}\n{stderr}")
|
||||
except IOError:
|
||||
raise AnsibleError(f"file or module does not exist at: {in_path}")
|
||||
except IOError as e:
|
||||
raise AnsibleError(f"file or module does not exist at: {in_path}") from e
|
||||
|
||||
def fetch_file(self, in_path, out_path):
|
||||
"""fetch a file from chroot to local"""
|
||||
@@ -211,8 +211,8 @@ class Connection(ConnectionBase):
|
||||
in_path = shlex_quote(self._prefix_login_path(in_path))
|
||||
try:
|
||||
p = self._buffered_exec_command(f"dd if={in_path} bs={BUFSIZE}")
|
||||
except OSError:
|
||||
raise AnsibleError("chroot connection requires dd command in the chroot")
|
||||
except OSError as e:
|
||||
raise AnsibleError("chroot connection requires dd command in the chroot") from e
|
||||
|
||||
with open(to_bytes(out_path, errors="surrogate_or_strict"), "wb+") as out_file:
|
||||
try:
|
||||
@@ -220,9 +220,9 @@ class Connection(ConnectionBase):
|
||||
while chunk:
|
||||
out_file.write(chunk)
|
||||
chunk = p.stdout.read(BUFSIZE)
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}")
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}") from e
|
||||
stdout, stderr = p.communicate()
|
||||
if p.returncode != 0:
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}:\n{stdout}\n{stderr}")
|
||||
|
||||
@@ -79,8 +79,8 @@ class Connection(ConnectionBase):
|
||||
def _search_executable(executable):
|
||||
try:
|
||||
return get_bin_path(executable)
|
||||
except ValueError:
|
||||
raise AnsibleError(f"{executable} command not found in PATH")
|
||||
except ValueError as e:
|
||||
raise AnsibleError(f"{executable} command not found in PATH") from e
|
||||
|
||||
def list_jails(self):
|
||||
p = subprocess.Popen(
|
||||
@@ -161,19 +161,19 @@ class Connection(ConnectionBase):
|
||||
count = ""
|
||||
try:
|
||||
p = self._buffered_exec_command(f"dd of={out_path} bs={BUFSIZE}{count}", stdin=in_file)
|
||||
except OSError:
|
||||
raise AnsibleError("jail connection requires dd command in the jail")
|
||||
except OSError as e:
|
||||
raise AnsibleError("jail connection requires dd command in the jail") from e
|
||||
try:
|
||||
stdout, stderr = p.communicate()
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}")
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}") from e
|
||||
if p.returncode != 0:
|
||||
raise AnsibleError(
|
||||
f"failed to transfer file {in_path} to {out_path}:\n{to_native(stdout)}\n{to_native(stderr)}"
|
||||
)
|
||||
except IOError:
|
||||
raise AnsibleError(f"file or module does not exist at: {in_path}")
|
||||
except IOError as e:
|
||||
raise AnsibleError(f"file or module does not exist at: {in_path}") from e
|
||||
|
||||
def fetch_file(self, in_path, out_path):
|
||||
"""fetch a file from jail to local"""
|
||||
@@ -183,8 +183,8 @@ class Connection(ConnectionBase):
|
||||
in_path = shlex_quote(self._prefix_login_path(in_path))
|
||||
try:
|
||||
p = self._buffered_exec_command(f"dd if={in_path} bs={BUFSIZE}")
|
||||
except OSError:
|
||||
raise AnsibleError("jail connection requires dd command in the jail")
|
||||
except OSError as e:
|
||||
raise AnsibleError("jail connection requires dd command in the jail") from e
|
||||
|
||||
with open(to_bytes(out_path, errors="surrogate_or_strict"), "wb+") as out_file:
|
||||
try:
|
||||
@@ -192,9 +192,9 @@ class Connection(ConnectionBase):
|
||||
while chunk:
|
||||
out_file.write(chunk)
|
||||
chunk = p.stdout.read(BUFSIZE)
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}")
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}") from e
|
||||
stdout, stderr = p.communicate()
|
||||
if p.returncode != 0:
|
||||
raise AnsibleError(
|
||||
|
||||
@@ -173,9 +173,9 @@ class Connection(ConnectionBase):
|
||||
raise errors.AnsibleFileNotFound(msg)
|
||||
try:
|
||||
src_file = open(in_path, "rb")
|
||||
except IOError:
|
||||
except IOError as e:
|
||||
traceback.print_exc()
|
||||
raise errors.AnsibleError(f"failed to open input file to {in_path}")
|
||||
raise errors.AnsibleError(f"failed to open input file to {in_path}") from e
|
||||
try:
|
||||
|
||||
def write_file(args):
|
||||
@@ -184,10 +184,10 @@ class Connection(ConnectionBase):
|
||||
|
||||
try:
|
||||
self.container.attach_wait(write_file, None)
|
||||
except IOError:
|
||||
except IOError as e:
|
||||
traceback.print_exc()
|
||||
msg = f"failed to transfer file to {out_path}"
|
||||
raise errors.AnsibleError(msg)
|
||||
raise errors.AnsibleError(msg) from e
|
||||
finally:
|
||||
src_file.close()
|
||||
|
||||
@@ -200,10 +200,10 @@ class Connection(ConnectionBase):
|
||||
|
||||
try:
|
||||
dst_file = open(out_path, "wb")
|
||||
except IOError:
|
||||
except IOError as e:
|
||||
traceback.print_exc()
|
||||
msg = f"failed to open output file {out_path}"
|
||||
raise errors.AnsibleError(msg)
|
||||
raise errors.AnsibleError(msg) from e
|
||||
try:
|
||||
|
||||
def write_file(args):
|
||||
@@ -217,10 +217,10 @@ class Connection(ConnectionBase):
|
||||
|
||||
try:
|
||||
self.container.attach_wait(write_file, None)
|
||||
except IOError:
|
||||
except IOError as e:
|
||||
traceback.print_exc()
|
||||
msg = f"failed to transfer file from {in_path} to {out_path}"
|
||||
raise errors.AnsibleError(msg)
|
||||
raise errors.AnsibleError(msg) from e
|
||||
finally:
|
||||
dst_file.close()
|
||||
|
||||
|
||||
@@ -93,8 +93,8 @@ class Connection(ConnectionBase):
|
||||
|
||||
try:
|
||||
self._lxc_cmd = get_bin_path("lxc")
|
||||
except ValueError:
|
||||
raise AnsibleError("lxc command not found in PATH")
|
||||
except ValueError as e:
|
||||
raise AnsibleError("lxc command not found in PATH") from e
|
||||
|
||||
def _host(self):
|
||||
"""translate remote_addr to lxd (short) hostname"""
|
||||
|
||||
@@ -478,11 +478,11 @@ class Connection(ConnectionBase):
|
||||
except IOError:
|
||||
pass # file was not found, but not required to function
|
||||
except paramiko.hostkeys.InvalidHostKey as e:
|
||||
raise AnsibleConnectionFailure(f"Invalid host key: {to_text(e.line)}")
|
||||
raise AnsibleConnectionFailure(f"Invalid host key: {to_text(e.line)}") from e
|
||||
try:
|
||||
ssh.load_system_host_keys()
|
||||
except paramiko.hostkeys.InvalidHostKey as e:
|
||||
raise AnsibleConnectionFailure(f"Invalid host key: {to_text(e.line)}")
|
||||
raise AnsibleConnectionFailure(f"Invalid host key: {to_text(e.line)}") from e
|
||||
|
||||
ssh_connect_kwargs = self._parse_proxy_command(port)
|
||||
ssh.set_missing_host_key_policy(MyAddPolicy(self))
|
||||
@@ -518,22 +518,24 @@ class Connection(ConnectionBase):
|
||||
**ssh_connect_kwargs,
|
||||
)
|
||||
except paramiko.ssh_exception.BadHostKeyException as e:
|
||||
raise AnsibleConnectionFailure(f"host key mismatch for {to_text(e.hostname)}")
|
||||
raise AnsibleConnectionFailure(f"host key mismatch for {to_text(e.hostname)}") from e
|
||||
except paramiko.ssh_exception.AuthenticationException as e:
|
||||
msg = f"Failed to authenticate: {e}"
|
||||
raise AnsibleAuthenticationFailure(msg)
|
||||
raise AnsibleAuthenticationFailure(msg) from e
|
||||
except Exception as e:
|
||||
msg = to_text(e)
|
||||
if "PID check failed" in msg:
|
||||
raise AnsibleError("paramiko version issue, please upgrade paramiko on the machine running ansible")
|
||||
raise AnsibleError(
|
||||
"paramiko version issue, please upgrade paramiko on the machine running ansible"
|
||||
) from e
|
||||
elif "Private key file is encrypted" in msg:
|
||||
msg = (
|
||||
f"ssh {self.get_option('remote_user')}@{self.get_options('remote_addr')}:{port} : "
|
||||
f"{msg}\nTo connect as a different user, use -u <username>."
|
||||
)
|
||||
raise AnsibleConnectionFailure(msg)
|
||||
raise AnsibleConnectionFailure(msg) from e
|
||||
else:
|
||||
raise AnsibleConnectionFailure(msg)
|
||||
raise AnsibleConnectionFailure(msg) from e
|
||||
self.ssh = ssh
|
||||
self._connected = True
|
||||
return self
|
||||
@@ -609,7 +611,7 @@ class Connection(ConnectionBase):
|
||||
msg = "Failed to open session"
|
||||
if text_e:
|
||||
msg += f": {text_e}"
|
||||
raise AnsibleConnectionFailure(to_native(msg))
|
||||
raise AnsibleConnectionFailure(to_native(msg)) from e
|
||||
|
||||
display.vvv(f"EXEC {cmd}", host=self.get_option("remote_addr"))
|
||||
|
||||
@@ -665,8 +667,8 @@ class Connection(ConnectionBase):
|
||||
elif in_data == b"":
|
||||
chan.shutdown_write()
|
||||
|
||||
except socket.timeout:
|
||||
raise AnsibleError(f"ssh timed out waiting for privilege escalation.\n{to_text(become_output)}")
|
||||
except socket.timeout as e:
|
||||
raise AnsibleError(f"ssh timed out waiting for privilege escalation.\n{to_text(become_output)}") from e
|
||||
|
||||
stdout = b"".join(chan.makefile("rb", bufsize))
|
||||
stderr = b"".join(chan.makefile_stderr("rb", bufsize))
|
||||
@@ -699,7 +701,7 @@ class Connection(ConnectionBase):
|
||||
)
|
||||
raise AnsibleError(f"{to_text(stdout)}\n{to_text(stderr)}")
|
||||
except Exception as e:
|
||||
raise AnsibleError(f"error occurred while putting file from {in_path} to {out_path}!\n{to_text(e)}")
|
||||
raise AnsibleError(f"error occurred while putting file from {in_path} to {out_path}!\n{to_text(e)}") from e
|
||||
|
||||
def fetch_file(self, in_path: str, out_path: str) -> None:
|
||||
"""save a remote file to the specified path"""
|
||||
@@ -718,7 +720,7 @@ class Connection(ConnectionBase):
|
||||
with open(out_path, "wb") as f:
|
||||
f.write(stdout)
|
||||
except Exception as e:
|
||||
raise AnsibleError(f"error occurred while fetching file from {in_path} to {out_path}!\n{to_text(e)}")
|
||||
raise AnsibleError(f"error occurred while fetching file from {in_path} to {out_path}!\n{to_text(e)}") from e
|
||||
|
||||
def reset(self) -> None:
|
||||
"""reset the connection"""
|
||||
@@ -772,16 +774,16 @@ class Connection(ConnectionBase):
|
||||
self._save_ssh_host_keys(tmp_keyfile_name)
|
||||
|
||||
os.rename(tmp_keyfile_name, self.keyfile)
|
||||
except LockTimeout:
|
||||
except LockTimeout as e:
|
||||
raise AnsibleError(
|
||||
f"writing lock file for {self.keyfile} ran in to the timeout of {self.get_option('lock_file_timeout')}s"
|
||||
)
|
||||
) from e
|
||||
except paramiko.hostkeys.InvalidHostKey as e:
|
||||
raise AnsibleConnectionFailure(f"Invalid host key: {e.line}")
|
||||
raise AnsibleConnectionFailure(f"Invalid host key: {e.line}") from e
|
||||
except Exception as e:
|
||||
# unable to save keys, including scenario when key was invalid
|
||||
# and caught earlier
|
||||
raise AnsibleError(f"error occurred while writing SSH host keys!\n{to_text(e)}")
|
||||
raise AnsibleError(f"error occurred while writing SSH host keys!\n{to_text(e)}") from e
|
||||
finally:
|
||||
if tmp_keyfile_name is not None:
|
||||
pathlib.Path(tmp_keyfile_name).unlink(missing_ok=True)
|
||||
|
||||
@@ -66,8 +66,8 @@ class Connection(ConnectionBase):
|
||||
def _search_executable(executable):
|
||||
try:
|
||||
return get_bin_path(executable)
|
||||
except ValueError:
|
||||
raise AnsibleError(f"{executable} command not found in PATH")
|
||||
except ValueError as e:
|
||||
raise AnsibleError(f"{executable} command not found in PATH") from e
|
||||
|
||||
def list_zones(self):
|
||||
process = subprocess.Popen(
|
||||
@@ -160,17 +160,17 @@ class Connection(ConnectionBase):
|
||||
count = ""
|
||||
try:
|
||||
p = self._buffered_exec_command(f"dd of={out_path} bs={BUFSIZE}{count}", stdin=in_file)
|
||||
except OSError:
|
||||
raise AnsibleError("jail connection requires dd command in the jail")
|
||||
except OSError as e:
|
||||
raise AnsibleError("jail connection requires dd command in the jail") from e
|
||||
try:
|
||||
stdout, stderr = p.communicate()
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}")
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}") from e
|
||||
if p.returncode != 0:
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}:\n{stdout}\n{stderr}")
|
||||
except IOError:
|
||||
raise AnsibleError(f"file or module does not exist at: {in_path}")
|
||||
except IOError as e:
|
||||
raise AnsibleError(f"file or module does not exist at: {in_path}") from e
|
||||
|
||||
def fetch_file(self, in_path, out_path):
|
||||
"""fetch a file from zone to local"""
|
||||
@@ -180,8 +180,8 @@ class Connection(ConnectionBase):
|
||||
in_path = shlex_quote(self._prefix_login_path(in_path))
|
||||
try:
|
||||
p = self._buffered_exec_command(f"dd if={in_path} bs={BUFSIZE}")
|
||||
except OSError:
|
||||
raise AnsibleError("zone connection requires dd command in the zone")
|
||||
except OSError as e:
|
||||
raise AnsibleError("zone connection requires dd command in the zone") from e
|
||||
|
||||
with open(out_path, "wb+") as out_file:
|
||||
try:
|
||||
@@ -189,9 +189,9 @@ class Connection(ConnectionBase):
|
||||
while chunk:
|
||||
out_file.write(chunk)
|
||||
chunk = p.stdout.read(BUFSIZE)
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}")
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}") from e
|
||||
stdout, stderr = p.communicate()
|
||||
if p.returncode != 0:
|
||||
raise AnsibleError(f"failed to transfer file {in_path} to {out_path}:\n{stdout}\n{stderr}")
|
||||
|
||||
Reference in New Issue
Block a user