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.

227 line
9.1KB

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