IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
8.1KB

  1. from argparse import ArgumentParser
  2. import argparse
  3. import yaml
  4. import os
  5. from functools import reduce
  6. # from weir import zfs, process
  7. from .image import command_image_build, \
  8. command_image_tag, \
  9. command_image_untag, \
  10. command_image_list, \
  11. command_image_prune, \
  12. command_image_remove
  13. from .volume import command_volume_create, \
  14. command_volume_prune, \
  15. command_volume_list, \
  16. command_volume_tag, \
  17. command_volume_untag
  18. import sys
  19. from .zfs import zfs_init
  20. from .jail import command_jail_create, \
  21. command_jail_start, \
  22. command_jail_stop, \
  23. command_jail_remove, \
  24. command_jail_exec, \
  25. command_jail_oneshot, \
  26. command_jail_list, \
  27. command_jail_tag, \
  28. command_jail_untag, \
  29. command_jail_prune
  30. from .compose import \
  31. command_compose_build, \
  32. command_compose_run
  33. class ListForwarderFunctor(object):
  34. def __init__(self, lst):
  35. self.lst = lst
  36. def __call__(self, *args, **kwargs):
  37. res = []
  38. for elem in self.lst:
  39. res.append(elem(*args, **kwargs))
  40. return ListForwarder(res)
  41. class ListForwarder(object):
  42. def __init__(self, lst):
  43. self.lst = lst
  44. def __getattr__(self, name):
  45. return ListForwarderFunctor(list(map(lambda a: getattr(a, name), self.lst)))
  46. def __setattr__(self, name, value):
  47. if name == 'lst':
  48. super().__setattr__(name, value)
  49. return
  50. # print('setattr(), name:', name, 'value:', value)
  51. for elem in self.lst:
  52. setattr(elem, name, value)
  53. def create_parser():
  54. parser_top = ArgumentParser()
  55. subparsers_top = parser_top.add_subparsers(dest='L1_command')
  56. subparsers_top.required = True
  57. # image
  58. subparsers = ListForwarder([ subparsers_top.add_parser(cmd).add_subparsers(dest='L2_command') \
  59. for cmd in ['image', 'img', 'im', 'i'] ])
  60. subparsers.required = True
  61. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['build', 'b']])
  62. parser.set_defaults(func=command_image_build)
  63. parser.add_argument('focker_dir', type=str)
  64. parser.add_argument('--tags', '-t', type=str, nargs='+', default=[])
  65. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['tag', 't']])
  66. parser.set_defaults(func=command_image_tag)
  67. parser.add_argument('reference', type=str)
  68. parser.add_argument('tags', type=str, nargs='+')
  69. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['untag', 'u']])
  70. parser.set_defaults(func=command_image_untag)
  71. parser.add_argument('tags', type=str, nargs='+', default=[])
  72. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['list', 'ls', 'l']])
  73. parser.set_defaults(func=command_image_list)
  74. parser.add_argument('--full-sha256', '-f', action='store_true')
  75. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['prune', 'p']])
  76. parser.set_defaults(func=command_image_prune)
  77. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['remove', 'r']])
  78. parser.set_defaults(func=command_image_remove)
  79. parser.add_argument('reference', type=str)
  80. # parser.add_argument('--remove-children', '-r', action='store_true')
  81. parser.add_argument('--remove-dependents', '-R', action='store_true')
  82. # jail
  83. subparsers = ListForwarder([ subparsers_top.add_parser(cmd).add_subparsers(dest='L2_command') \
  84. for cmd in ['jail', 'j'] ])
  85. subparsers.required = True
  86. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['create', 'c']])
  87. parser.set_defaults(func=command_jail_create)
  88. parser.add_argument('image', type=str)
  89. parser.add_argument('--command', '-c', type=str, default='/bin/sh')
  90. parser.add_argument('--tags', '-t', type=str, nargs='+', default=[])
  91. parser.add_argument('--env', '-e', type=str, nargs='+', default=[])
  92. parser.add_argument('--mounts', '-m', type=str, nargs='+', default=[])
  93. parser.add_argument('--hostname', '-n', type=str)
  94. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['start', 's']])
  95. parser.set_defaults(func=command_jail_start)
  96. parser.add_argument('reference', type=str)
  97. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['stop', 'S']])
  98. parser.set_defaults(func=command_jail_stop)
  99. parser.add_argument('reference', type=str)
  100. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['remove', 'r']])
  101. parser.set_defaults(func=command_jail_remove)
  102. parser.add_argument('reference', type=str)
  103. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['exec', 'e']])
  104. parser.set_defaults(func=command_jail_exec)
  105. parser.add_argument('reference', type=str)
  106. parser.add_argument('command', type=str, nargs=argparse.REMAINDER, default=['/bin/sh'])
  107. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['oneshot', 'o']])
  108. parser.set_defaults(func=command_jail_oneshot)
  109. parser.add_argument('image', type=str)
  110. parser.add_argument('--env', '-e', type=str, nargs='+', default=[])
  111. parser.add_argument('--mounts', '-m', type=str, nargs='+', default=[])
  112. parser.add_argument('command', type=str, nargs=argparse.REMAINDER, default=['/bin/sh'])
  113. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['list', 'ls', 'l']])
  114. parser.set_defaults(func=command_jail_list)
  115. parser.add_argument('--full-sha256', '-f', action='store_true')
  116. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['tag', 't']])
  117. parser.set_defaults(func=command_jail_tag)
  118. parser.add_argument('reference', type=str)
  119. parser.add_argument('tags', type=str, nargs='+')
  120. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['untag', 'u']])
  121. parser.set_defaults(func=command_jail_untag)
  122. parser.add_argument('tags', type=str, nargs='+')
  123. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['prune', 'p']])
  124. parser.set_defaults(func=command_jail_prune)
  125. parser.add_argument('--force', '-f', action='store_true')
  126. # volume
  127. subparsers = ListForwarder([ subparsers_top.add_parser(cmd).add_subparsers(dest='L2_command') \
  128. for cmd in ['volume', 'vol', 'v'] ])
  129. subparsers.required = True
  130. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['create', 'c']])
  131. parser.set_defaults(func=command_volume_create)
  132. parser.add_argument('--tags', '-t', type=str, nargs='+', default=[])
  133. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['prune', 'p']])
  134. parser.set_defaults(func=command_volume_prune)
  135. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['list', 'ls', 'l']])
  136. parser.set_defaults(func=command_volume_list)
  137. parser.add_argument('--full-sha256', '-f', action='store_true')
  138. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['tag', 't']])
  139. parser.set_defaults(func=command_volume_tag)
  140. parser.add_argument('reference', type=str)
  141. parser.add_argument('tags', type=str, nargs='+')
  142. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['untag', 'u']])
  143. parser.set_defaults(func=command_volume_untag)
  144. parser.add_argument('tags', type=str, nargs='+')
  145. # compose
  146. subparsers = ListForwarder([ subparsers_top.add_parser(cmd).add_subparsers(dest='L2_command') \
  147. for cmd in ['compose', 'comp', 'c'] ])
  148. subparsers.required = True
  149. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['build', 'b']])
  150. parser.set_defaults(func=command_compose_build)
  151. parser.add_argument('filename', type=str)
  152. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['run', 'r']])
  153. parser.set_defaults(func=command_compose_run)
  154. parser.add_argument('filename', type=str)
  155. parser.add_argument('command', type=str)
  156. return parser_top
  157. def main():
  158. zfs_init()
  159. parser = create_parser()
  160. args = parser.parse_args()
  161. if not hasattr(args, 'func'):
  162. parser.print_usage()
  163. sys.exit('You must choose a mode')
  164. args.func(args)
  165. if __name__ == '__main__':
  166. main()