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.

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