copyartifacts.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. """
  3. Script for copying back xml junit files from tests
  4. """
  5. from __future__ import absolute_import, print_function
  6. import argparse # pylint: disable=minimum-python-version
  7. import os
  8. import subprocess
  9. import paramiko
  10. import salt.utils.yaml
  11. class DownloadArtifacts(object):
  12. def __init__(self, instance, artifacts):
  13. self.instance = instance
  14. self.artifacts = artifacts
  15. self.transport = self.setup_transport()
  16. self.sftpclient = paramiko.SFTPClient.from_transport(self.transport)
  17. def setup_transport(self):
  18. # pylint: disable=minimum-python-version
  19. config = salt.utils.yaml.safe_load(
  20. subprocess.check_output(
  21. ["bundle", "exec", "kitchen", "diagnose", self.instance]
  22. )
  23. )
  24. # pylint: enable=minimum-python-version
  25. state = config["instances"][self.instance]["state_file"]
  26. tport = config["instances"][self.instance]["transport"]
  27. transport = paramiko.Transport(
  28. (state["hostname"], state.get("port", tport.get("port", 22)))
  29. )
  30. pkey = paramiko.rsakey.RSAKey(
  31. filename=state.get("ssh_key", tport.get("ssh_key", "~/.ssh/id_rsa"))
  32. )
  33. transport.connect(
  34. username=state.get("username", tport.get("username", "root")), pkey=pkey
  35. )
  36. return transport
  37. def _set_permissions(self):
  38. """
  39. Make sure all xml files are readable by the world so that anyone can grab them
  40. """
  41. for remote, _ in self.artifacts:
  42. self.transport.open_session().exec_command(
  43. "sudo chmod -R +r {}".format(remote)
  44. )
  45. def download(self):
  46. self._set_permissions()
  47. for remote, local in self.artifacts:
  48. if remote.endswith("/"):
  49. for fxml in self.sftpclient.listdir(remote):
  50. self._do_download(
  51. os.path.join(remote, fxml),
  52. os.path.join(local, os.path.basename(fxml)),
  53. )
  54. else:
  55. self._do_download(remote, os.path.join(local, os.path.basename(remote)))
  56. def _do_download(self, remote, local):
  57. print("Copying from {0} to {1}".format(remote, local))
  58. try:
  59. self.sftpclient.get(remote, local)
  60. except IOError:
  61. print("Failed to copy: {0}".format(remote))
  62. if __name__ == "__main__":
  63. parser = argparse.ArgumentParser(description="Jenkins Artifact Download Helper")
  64. parser.add_argument(
  65. "--instance",
  66. required=True,
  67. action="store",
  68. help="Instance on Test Kitchen to pull from",
  69. )
  70. parser.add_argument(
  71. "--download-artifacts",
  72. dest="artifacts",
  73. nargs=2,
  74. action="append",
  75. metavar=("REMOTE_PATH", "LOCAL_PATH"),
  76. help="Download remote artifacts",
  77. )
  78. args = parser.parse_args()
  79. downloader = DownloadArtifacts(args.instance, args.artifacts)
  80. downloader.download()