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!
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.4KB

  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 ListForwarder(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([subparsers.add_parser(cmd) for cmd in ['build', 'b']])
  59. parser.set_defaults(func=command_image_build)
  60. parser.add_argument('focker_dir', type=str)
  61. parser.add_argument('--tags', '-t', type=str, nargs='+', default=[])
  62. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['tag', 't']])
  63. parser.set_defaults(func=command_image_tag)
  64. parser.add_argument('reference', type=str)
  65. parser.add_argument('tags', type=str, nargs='+')
  66. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['untag', 'u']])
  67. parser.set_defaults(func=command_image_untag)
  68. parser.add_argument('tags', type=str, nargs='+', default=[])
  69. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['list', 'ls', 'l']])
  70. parser.set_defaults(func=command_image_list)
  71. parser.add_argument('--full-sha256', '-f', action='store_true')
  72. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['prune', 'p']])
  73. parser.set_defaults(func=command_image_prune)
  74. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['remove', 'r']])
  75. parser.set_defaults(func=command_image_remove)
  76. parser.add_argument('reference', type=str)
  77. # parser.add_argument('--remove-children', '-r', action='store_true')
  78. parser.add_argument('--remove-dependents', '-R', action='store_true')
  79. # jail
  80. subparsers = ListForwarder([ subparsers_top.add_parser(cmd).add_subparsers(dest='L2_command') \
  81. for cmd in ['jail', 'j'] ])
  82. subparsers.required = True
  83. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['create', 'c']])
  84. parser.set_defaults(func=command_jail_create)
  85. parser.add_argument('image', type=str)
  86. parser.add_argument('--command', '-c', type=str, default='/bin/sh')
  87. parser.add_argument('--tags', '-t', type=str, nargs='+', default=[])
  88. parser.add_argument('--env', '-e', type=str, nargs='+', default=[])
  89. parser.add_argument('--mounts', '-m', type=str, nargs='+', default=[])
  90. parser.add_argument('--hostname', '-n', type=str)
  91. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['start', 's']])
  92. parser.set_defaults(func=command_jail_start)
  93. parser.add_argument('reference', type=str)
  94. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['stop', 'S']])
  95. parser.set_defaults(func=command_jail_stop)
  96. parser.add_argument('reference', type=str)
  97. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['remove', 'r']])
  98. parser.set_defaults(func=command_jail_remove)
  99. parser.add_argument('reference', type=str)
  100. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['exec', 'e']])
  101. parser.set_defaults(func=command_jail_exec)
  102. parser.add_argument('reference', type=str)
  103. parser.add_argument('command', type=str, nargs=argparse.REMAINDER, default=['/bin/sh'])
  104. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['oneshot', 'o']])
  105. parser.set_defaults(func=command_jail_oneshot)
  106. parser.add_argument('image', type=str)
  107. parser.add_argument('--env', '-e', type=str, nargs='+', default=[])
  108. parser.add_argument('--mounts', '-m', type=str, nargs='+', default=[])
  109. parser.add_argument('command', type=str, nargs=argparse.REMAINDER, default=['/bin/sh'])
  110. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['list', 'ls', 'l']])
  111. parser.set_defaults(func=command_jail_list)
  112. parser.add_argument('--full-sha256', '-f', action='store_true')
  113. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['tag', 't']])
  114. parser.set_defaults(func=command_jail_tag)
  115. parser.add_argument('reference', type=str)
  116. parser.add_argument('tags', type=str, nargs='+')
  117. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['untag', 'u']])
  118. parser.set_defaults(func=command_jail_untag)
  119. parser.add_argument('tags', type=str, nargs='+')
  120. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['prune', 'p']])
  121. parser.set_defaults(func=command_jail_prune)
  122. parser.add_argument('--force', '-f', action='store_true')
  123. # volume
  124. subparsers = ListForwarder([ subparsers_top.add_parser(cmd).add_subparsers(dest='L2_command') \
  125. for cmd in ['volume', 'v'] ])
  126. subparsers.required = True
  127. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['create', 'c']])
  128. parser.set_defaults(func=command_volume_create)
  129. parser.add_argument('--tags', '-t', type=str, nargs='+', default=[])
  130. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['prune', 'p']])
  131. parser.set_defaults(func=command_volume_prune)
  132. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['list', 'ls', 'l']])
  133. parser.set_defaults(func=command_volume_list)
  134. parser.add_argument('--full-sha256', '-f', action='store_true')
  135. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['tag', 't']])
  136. parser.set_defaults(func=command_volume_tag)
  137. parser.add_argument('reference', type=str)
  138. parser.add_argument('tags', type=str, nargs='+')
  139. parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['untag', 'u']])
  140. parser.set_defaults(func=command_volume_untag)
  141. parser.add_argument('tags', type=str, nargs='+')
  142. return parser_top
  143. def main():
  144. zfs_init()
  145. parser = create_parser()
  146. args = parser.parse_args()
  147. if not hasattr(args, 'func'):
  148. parser.print_usage()
  149. sys.exit('You must choose a mode')
  150. args.func(args)
  151. if __name__ == '__main__':
  152. main()