|
11 | 11 | import logging
|
12 | 12 | import multiprocessing
|
13 | 13 | import os
|
| 14 | +import pathlib |
14 | 15 | import queue
|
15 | 16 | import re
|
16 | 17 | import shlex
|
| 18 | +import shutil |
17 | 19 | import subprocess
|
18 | 20 | import sys
|
19 | 21 | import tarfile
|
@@ -467,7 +469,14 @@ def key_deploy(self, host, ret):
|
467 | 469 | if target.get("passwd", False) or self.opts["ssh_passwd"]:
|
468 | 470 | self._key_deploy_run(host, target, False)
|
469 | 471 | return ret
|
470 |
| - if ret[host].get("stderr", "").count("Permission denied"): |
| 472 | + stderr = ret[host].get("stderr", "") |
| 473 | + # -failed to upload file- is detecting scp errors |
| 474 | + # Errors to ignore when Permission denied is in the stderr. For example |
| 475 | + # scp can get a permission denied on the target host, but they where |
| 476 | + # able to accurate authenticate against the box |
| 477 | + ignore_err = ["failed to upload file"] |
| 478 | + check_err = [x for x in ignore_err if stderr.count(x)] |
| 479 | + if "Permission denied" in stderr and not check_err: |
471 | 480 | target = self.targets[host]
|
472 | 481 | # permission denied, attempt to auto deploy ssh key
|
473 | 482 | print(
|
@@ -1007,11 +1016,32 @@ def run_ssh_pre_flight(self):
|
1007 | 1016 | """
|
1008 | 1017 | Run our pre_flight script before running any ssh commands
|
1009 | 1018 | """
|
1010 |
| - script = os.path.join(tempfile.gettempdir(), self.ssh_pre_file) |
1011 |
| - |
1012 |
| - self.shell.send(self.ssh_pre_flight, script) |
| 1019 | + with tempfile.NamedTemporaryFile() as temp: |
| 1020 | + # ensure we use copyfile to not copy the file attributes |
| 1021 | + # we want to ensure we use the perms set by the secure |
| 1022 | + # NamedTemporaryFile |
| 1023 | + try: |
| 1024 | + shutil.copyfile(self.ssh_pre_flight, temp.name) |
| 1025 | + except OSError as err: |
| 1026 | + return ( |
| 1027 | + "", |
| 1028 | + f"Could not copy pre flight script {self.ssh_pre_flight} to temporary path", |
| 1029 | + 1, |
| 1030 | + ) |
| 1031 | + target_script = f".{pathlib.Path(temp.name).name}" |
| 1032 | + log.trace(f"Copying the pre flight script {self.ssh_pre_file} to target") |
| 1033 | + stdout, stderr, retcode = self.shell.send(temp.name, target_script) |
| 1034 | + if retcode != 0: |
| 1035 | + # We could not copy the script to the target |
| 1036 | + log.error( |
| 1037 | + f"Could not copy the pre flight script {self.ssh_pre_file} to target" |
| 1038 | + ) |
| 1039 | + return stdout, stderr, retcode |
1013 | 1040 |
|
1014 |
| - return self.execute_script(script, script_args=self.ssh_pre_flight_args) |
| 1041 | + log.trace(f"Executing the pre flight script {self.ssh_pre_file} on target") |
| 1042 | + return self.execute_script( |
| 1043 | + target_script, script_args=self.ssh_pre_flight_args |
| 1044 | + ) |
1015 | 1045 |
|
1016 | 1046 | def check_thin_dir(self):
|
1017 | 1047 | """
|
@@ -1388,18 +1418,20 @@ def shim_cmd(self, cmd_str, extension="py"):
|
1388 | 1418 | return self.shell.exec_cmd(cmd_str)
|
1389 | 1419 |
|
1390 | 1420 | # Write the shim to a temporary file in the default temp directory
|
1391 |
| - with tempfile.NamedTemporaryFile( |
1392 |
| - mode="w+b", prefix="shim_", delete=False |
1393 |
| - ) as shim_tmp_file: |
| 1421 | + with tempfile.NamedTemporaryFile(mode="w+b", delete=False) as shim_tmp_file: |
1394 | 1422 | shim_tmp_file.write(salt.utils.stringutils.to_bytes(cmd_str))
|
1395 | 1423 |
|
1396 | 1424 | # Copy shim to target system, under $HOME/.<randomized name>
|
1397 |
| - target_shim_file = ".{}.{}".format( |
1398 |
| - binascii.hexlify(os.urandom(6)).decode("ascii"), extension |
1399 |
| - ) |
| 1425 | + target_shim_file = f".{pathlib.Path(shim_tmp_file.name).name}" |
| 1426 | + |
1400 | 1427 | if self.winrm:
|
1401 | 1428 | target_shim_file = saltwinshell.get_target_shim_file(self, target_shim_file)
|
1402 |
| - self.shell.send(shim_tmp_file.name, target_shim_file, makedirs=True) |
| 1429 | + stdout, stderr, retcode = self.shell.send( |
| 1430 | + shim_tmp_file.name, target_shim_file, makedirs=True |
| 1431 | + ) |
| 1432 | + if retcode != 0: |
| 1433 | + log.error(f"Could not copy the shim script to target") |
| 1434 | + return stdout, stderr, retcode |
1403 | 1435 |
|
1404 | 1436 | # Remove our shim file
|
1405 | 1437 | try:
|
|
0 commit comments