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.

124 lines
4.0KB

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