salt/tests/support/copyartifacts.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.7 KiB
Python
Raw Normal View History

2020-04-02 20:10:20 -05:00
"""
2017-10-24 10:07:03 -06:00
Script for copying back xml junit files from tests
2020-04-02 20:10:20 -05:00
"""
import argparse
2017-10-16 10:09:15 -06:00
import os
import subprocess
2017-10-16 10:09:15 -06:00
import paramiko
import salt.utils.yaml
2017-10-16 10:09:15 -06:00
2021-04-01 16:29:01 -07:00
class DownloadArtifacts:
2017-10-16 10:09:15 -06:00
def __init__(self, instance, artifacts):
self.instance = instance
self.artifacts = artifacts
self.transport = self.setup_transport()
self.sftpclient = paramiko.SFTPClient.from_transport(self.transport)
2017-10-16 10:09:15 -06:00
def setup_transport(self):
config = salt.utils.yaml.safe_load(
subprocess.check_output(
["bundle", "exec", "kitchen", "diagnose", self.instance]
)
2020-04-02 20:10:20 -05:00
)
2017-10-16 20:33:33 -06:00
state = config["instances"][self.instance]["state_file"]
tport = config["instances"][self.instance]["transport"]
transport = paramiko.Transport(
(state["hostname"], state.get("port", tport.get("port", 22)))
)
pkey = paramiko.rsakey.RSAKey(
filename=state.get("ssh_key", tport.get("ssh_key", "~/.ssh/id_rsa"))
)
transport.connect(
username=state.get("username", tport.get("username", "root")), pkey=pkey
)
return transport
def _set_permissions(self):
2020-04-02 20:10:20 -05:00
"""
Make sure all xml files are readable by the world so that anyone can grab them
2020-04-02 20:10:20 -05:00
"""
for remote, _ in self.artifacts:
self.transport.open_session().exec_command(f"sudo chmod -R +r {remote}")
2017-10-16 10:09:15 -06:00
def download(self):
self._set_permissions()
2017-10-16 10:09:15 -06:00
for remote, local in self.artifacts:
if remote.endswith("/"):
for fxml in self.sftpclient.listdir(remote):
2017-10-17 08:43:47 -06:00
self._do_download(
os.path.join(remote, fxml),
os.path.join(local, os.path.basename(fxml)),
)
2017-10-16 10:09:15 -06:00
else:
2017-10-17 08:43:47 -06:00
self._do_download(remote, os.path.join(local, os.path.basename(remote)))
def _do_download(self, remote, local):
print(f"Copying from {remote} to {local}")
try:
self.sftpclient.get(remote, local)
2021-04-01 16:29:01 -07:00
except OSError:
print(f"Failed to copy: {remote}")
2017-10-16 10:09:15 -06:00
2018-12-20 16:19:01 -08:00
2017-10-16 10:09:15 -06:00
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Jenkins Artifact Download Helper")
parser.add_argument(
"--instance",
required=True,
action="store",
help="Instance on Test Kitchen to pull from",
)
parser.add_argument(
"--download-artifacts",
dest="artifacts",
nargs=2,
action="append",
metavar=("REMOTE_PATH", "LOCAL_PATH"),
help="Download remote artifacts",
)
args = parser.parse_args()
downloader = DownloadArtifacts(args.instance, args.artifacts)
downloader.download()