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.

131 lines
3.8KB

  1. from .zfs import *
  2. import os
  3. import yaml
  4. from .steps import create_step
  5. from .snapshot import new_snapshot
  6. from tabulate import tabulate
  7. import subprocess
  8. def process_step(step, name):
  9. cmd=['jail', '-c']
  10. cmd.append('path=' + '/focker/' + name)
  11. def process_steps(steps, name):
  12. if isinstance(steps, list):
  13. for step in steps:
  14. process_step(step, name)
  15. else:
  16. process_step(steps, name)
  17. def build(spec, args):
  18. if 'base' not in spec:
  19. raise ValueError('Missing base in specification')
  20. if 'steps' not in spec:
  21. raise ValueError('Missing steps in specification')
  22. base = spec['base']
  23. base, base_sha256 = zfs_snapshot_by_tag_or_sha256(base)
  24. root = '/'.join(base.split('/')[:-1])
  25. print('base:', base, 'root:', root)
  26. steps = spec['steps']
  27. if not isinstance(steps, list):
  28. steps = [ steps ]
  29. for st in steps:
  30. st = create_step(st)
  31. st_sha256 = st.hash(base_sha256, args=args)
  32. if zfs_exists_snapshot_sha256(st_sha256):
  33. base = zfs_snapshot_by_sha256(st_sha256)
  34. base_sha256 = st_sha256
  35. print('Reusing:', base)
  36. continue
  37. for pre in range(7, 64):
  38. name = root + '/' + st_sha256[:pre]
  39. if not zfs_exists(name):
  40. break
  41. feed = {
  42. 'focker:sha256': st_sha256
  43. }
  44. def atomic():
  45. st.execute(zfs_mountpoint(name), args=args)
  46. zfs_set_props(name, feed)
  47. snap_name = new_snapshot(base, atomic, name)
  48. # zfs_set_props(name, feed)
  49. # zfs_set_props(snap_name, feed)
  50. base = snap_name
  51. base_sha256 = st_sha256
  52. return (base, base_sha256)
  53. def command_image_build(args):
  54. # os.chdir(args.focker_dir)
  55. fname = os.path.join(args.focker_dir, 'Fockerfile')
  56. print('fname:', fname)
  57. if not os.path.exists(fname):
  58. raise ValueError('No Fockerfile could be found in the specified directory')
  59. with open(fname, 'r') as f:
  60. spec = yaml.safe_load(f)
  61. print('spec:', spec)
  62. image, image_sha256 = build(spec, args)
  63. zfs_untag(args.tag)
  64. zfs_tag(image.split('@')[0], args.tag)
  65. def command_image_tag(args):
  66. zfs_untag(args.tags)
  67. name, _ = zfs_find(args.reference, focker_type='image', zfs_type='filesystem')
  68. zfs_tag(name, args.tags)
  69. def command_image_untag(args):
  70. zfs_untag(args.tags)
  71. def command_image_list(args):
  72. lst = zfs_parse_output(['zfs', 'list', '-o', 'name,refer,focker:sha256,focker:tags,origin', '-H'])
  73. lst = list(filter(lambda a: a[2] != '-', lst))
  74. lst = list(map(lambda a: [ a[3], a[1],
  75. a[2] if args.full_sha256 else a[2][:7],
  76. a[4].split('/')[-1].split('@')[0] ], lst))
  77. print(tabulate(lst, headers=['Tags', 'Size', 'SHA256', 'Base']))
  78. def command_image_prune(args):
  79. again = True
  80. while again:
  81. again = False
  82. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,origin,name', '-H'])
  83. used = set()
  84. for r in lst:
  85. if r[2] == '-':
  86. continue
  87. used.add(r[2].split('@')[0])
  88. for r in lst:
  89. if r[0] == '-' or r[1] != '-':
  90. continue
  91. if r[3] not in used:
  92. print('Removing:', r[3])
  93. zfs_run(['zfs', 'destroy', '-r', '-f', r[3]])
  94. again = True
  95. # zfs_parse_output(['zfs'])
  96. def command_image_remove(args):
  97. snap, snap_sha256 = zfs_snapshot_by_tag_or_sha256(args.reference)
  98. ds = snap.split('@')[0]
  99. command = ['zfs', 'destroy', '-r']
  100. #if args.remove_children:
  101. # command.append('-r')
  102. if args.remove_dependents:
  103. command.append('-R')
  104. command.append(ds)
  105. subprocess.run(command)
  106. # zfs_run(['zfs', 'destroy', ds])