| 1 |
# Copyright (c) 2014, Chris Church <[email protected]> |
| 2 |
# Copyright (c) 2017 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
DOCUMENTATION = """ |
| 7 |
name: sh |
| 8 |
short_description: "POSIX shell (/bin/sh)" |
| 9 |
version_added: historical |
| 10 |
description: |
| 11 |
- This shell plugin is the one you want to use on most Unix systems, it is the most compatible and widely installed shell. |
| 12 |
extends_documentation_fragment: |
| 13 |
- shell_common |
| 14 |
""" |
| 15 |
|
| 16 |
from ansible.plugins.shell import ShellBase |
| 17 |
from ansible.utils.display import Display |
| 18 |
|
| 19 |
|
| 20 |
display = Display() |
| 21 |
|
| 22 |
|
| 23 |
class ShellModule(ShellBase): |
| 24 |
|
| 25 |
# Common shell filenames that this plugin handles. |
| 26 |
# Note: sh is the default shell plugin so this plugin may also be selected |
| 27 |
# This code needs to be SH-compliant. BASH-isms will not work if /bin/sh points to a non-BASH shell. |
| 28 |
|
| 29 |
# if the filename is not listed in any Shell plugin. |
| 30 |
COMPATIBLE_SHELLS = frozenset(('sh', 'zsh', 'bash', 'dash', 'ksh')) |
| 31 |
# Family of shells this has. Must match the filename without extension |
| 32 |
SHELL_FAMILY = 'sh' |
| 33 |
|
| 34 |
# commonly used |
| 35 |
ECHO = 'echo' |
| 36 |
CD = 'cd' |
| 37 |
COMMAND_SEP = ';' |
| 38 |
|
| 39 |
# How to end lines in a python script one-liner |
| 40 |
_SHELL_EMBEDDED_PY_EOL = '\n' |
| 41 |
_SHELL_REDIRECT_ALLNULL = '> /dev/null 2>&1' |
| 42 |
_SHELL_AND = '&&' |
| 43 |
_SHELL_OR = '||' |
| 44 |
_SHELL_SUB_LEFT = '"`' |
| 45 |
_SHELL_SUB_RIGHT = '`"' |
| 46 |
_SHELL_GROUP_LEFT = '(' |
| 47 |
_SHELL_GROUP_RIGHT = ')' |
| 48 |
|
| 49 |
def checksum(self, path, python_interp): |
| 50 |
display.deprecated( |
| 51 |
msg="The `ShellModule.checksum` method is deprecated.", |
| 52 |
version="2.23", |
| 53 |
help_text="Use `ActionBase._execute_remote_stat()` instead.", |
| 54 |
) |
| 55 |
# In the following test, each condition is a check and logical |
| 56 |
# comparison (|| or &&) that sets the rc value. Every check is run so |
| 57 |
# the last check in the series to fail will be the rc that is returned. |
| 58 |
# |
| 59 |
# If a check fails we error before invoking the hash functions because |
| 60 |
# hash functions may successfully take the hash of a directory on BSDs |
| 61 |
# (UFS filesystem?) which is not what the rest of the ansible code expects |
| 62 |
# |
| 63 |
# If all of the available hashing methods fail we fail with an rc of 0. |
| 64 |
# This logic is added to the end of the cmd at the bottom of this function. |
| 65 |
|
| 66 |
# Return codes: |
| 67 |
# checksum: success! |
| 68 |
# 0: Unknown error |
| 69 |
# 1: Remote file does not exist |
| 70 |
# 2: No read permissions on the file |
| 71 |
# 3: File is a directory |
| 72 |
# 4: No python interpreter |
| 73 |
|
| 74 |
# Quoting gets complex here. We're writing a python string that's |
| 75 |
# used by a variety of shells on the remote host to invoke a python |
| 76 |
# "one-liner". |
| 77 |
shell_escaped_path = self.quote(path) |
| 78 |
test = "rc=flag; [ -r %(p)s ] %(shell_or)s rc=2; [ -f %(p)s ] %(shell_or)s rc=1; [ -d %(p)s ] %(shell_and)s rc=3; %(i)s -V 2>/dev/null %(shell_or)s rc=4; [ x\"$rc\" != \"xflag\" ] %(shell_and)s echo \"${rc} \"%(p)s %(shell_and)s exit 0" % dict(p=shell_escaped_path, i=python_interp, shell_and=self._SHELL_AND, shell_or=self._SHELL_OR) # NOQA |
| 79 |
cmd = "({0} -c 'import hashlib; BLOCKSIZE = 65536; hasher = hashlib.sha1();{2}afile = open(\"'{1}'\", \"rb\"){2}buf = afile.read(BLOCKSIZE){2}while len(buf) > 0:{2}\thasher.update(buf){2}\tbuf = afile.read(BLOCKSIZE){2}afile.close(){2}print(hasher.hexdigest())' 2>/dev/null)".format(python_interp, shell_escaped_path, self._SHELL_EMBEDDED_PY_EOL) # NOQA |
| 80 |
|
| 81 |
cmd = "%s; %s %s (echo \'0 \'%s)" % (test, cmd, self._SHELL_OR, shell_escaped_path) |
| 82 |
return cmd |