1
0

copyartifacts.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 paramiko
  9. import subprocess
  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(subprocess.check_output(['bundle', 'exec', 'kitchen', 'diagnose', self.instance]))
  20. # pylint: enable=minimum-python-version
  21. state = config['instances'][self.instance]['state_file']
  22. tport = config['instances'][self.instance]['transport']
  23. transport = paramiko.Transport((
  24. state['hostname'],
  25. state.get('port', tport.get('port', 22))
  26. ))
  27. pkey = paramiko.rsakey.RSAKey(
  28. filename=state.get('ssh_key', tport.get('ssh_key', '~/.ssh/id_rsa'))
  29. )
  30. transport.connect(
  31. username=state.get('username', tport.get('username', 'root')),
  32. pkey=pkey
  33. )
  34. return transport
  35. def _set_permissions(self):
  36. '''
  37. Make sure all xml files are readable by the world so that anyone can grab them
  38. '''
  39. for remote, _ in self.artifacts:
  40. self.transport.open_session().exec_command('sudo chmod -R +r {}'.format(remote))
  41. def download(self):
  42. self._set_permissions()
  43. for remote, local in self.artifacts:
  44. if remote.endswith('/'):
  45. for fxml in self.sftpclient.listdir(remote):
  46. self._do_download(os.path.join(remote, fxml), os.path.join(local, os.path.basename(fxml)))
  47. else:
  48. self._do_download(remote, os.path.join(local, os.path.basename(remote)))
  49. def _do_download(self, remote, local):
  50. print('Copying from {0} to {1}'.format(remote, local))
  51. try:
  52. self.sftpclient.get(remote, local)
  53. except IOError:
  54. print('Failed to copy: {0}'.format(remote))
  55. if __name__ == '__main__':
  56. parser = argparse.ArgumentParser(description='Jenkins Artifact Download Helper')
  57. parser.add_argument(
  58. '--instance',
  59. required=True,
  60. action='store',
  61. help='Instance on Test Kitchen to pull from',
  62. )
  63. parser.add_argument(
  64. '--download-artifacts',
  65. dest='artifacts',
  66. nargs=2,
  67. action='append',
  68. metavar=('REMOTE_PATH', 'LOCAL_PATH'),
  69. help='Download remote artifacts',
  70. )
  71. args = parser.parse_args()
  72. downloader = DownloadArtifacts(args.instance, args.artifacts)
  73. downloader.download()