update_dependencies 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python
  2. import argparse
  3. import errno
  4. import os
  5. import subprocess
  6. class ValidationException(Exception):
  7. def __init__(self, error_msg, error_no=errno.EFAULT):
  8. self.errmsg = error_msg
  9. self.errno = error_no
  10. def get_error_name(self):
  11. return errno.errorcode.get(self.errno) or 'EUNKNOWN'
  12. def __str__(self):
  13. return f'[{self.get_error_name()}] {self.errmsg}'
  14. class NotFoundException(ValidationException):
  15. def __init__(self, error):
  16. super().__init__(error, errno.ENOENT)
  17. class TrainNotFoundException(NotFoundException):
  18. def __init__(self):
  19. super(TrainNotFoundException, self).__init__('Failed to find train')
  20. class CatalogItemNotFoundException(NotFoundException):
  21. def __init__(self, path):
  22. super(CatalogItemNotFoundException, self).__init__(f'Failed to find {path!r} catalog item')
  23. def update_train_charts(train_path):
  24. # We will gather all charts in the train and then for each chart all it's versions will be updated
  25. if not os.path.exists(train_path):
  26. raise TrainNotFoundException()
  27. for item in os.listdir(train_path):
  28. process_catalog_item(os.path.join(train_path, item))
  29. def process_catalog_item(item_path):
  30. if not os.path.exists(item_path):
  31. raise CatalogItemNotFoundException(item_path)
  32. for item_version in os.listdir(item_path):
  33. update_item_version(os.path.join(item_path, item_version))
  34. def update_item_version(version_path):
  35. cp = subprocess.Popen(
  36. ['helm', 'dependency', 'update', version_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE
  37. )
  38. stdout, stderr = cp.communicate()
  39. if cp.returncode:
  40. # TODO: Let's keep a log of success and failure scenarios
  41. pass
  42. def main():
  43. parser = argparse.ArgumentParser()
  44. subparsers = parser.add_subparsers(help='sub-command help', dest='action')
  45. parser_setup = subparsers.add_parser('update', help='Update dependencies for specified train')
  46. parser_setup.add_argument('--train', help='Specify train path to update dependencies', required=True)
  47. args = parser.parse_args()
  48. if args.action == 'update':
  49. update_train_charts(args.train)
  50. else:
  51. parser.print_help()
  52. if __name__ == '__main__':
  53. main()