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!
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

173 lines
5.2KB

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