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.

image.py 2.7KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. def process_step(step, name):
  8. cmd=['jail', '-c']
  9. cmd.append('path=' + '/focker/' + name)
  10. def process_steps(steps, name):
  11. if isinstance(steps, list):
  12. for step in steps:
  13. process_step(step, name)
  14. else:
  15. process_step(steps, name)
  16. def build(spec, args):
  17. if 'base' not in spec:
  18. raise ValueError('Missing base in specification')
  19. if 'steps' not in spec:
  20. raise ValueError('Missing steps in specification')
  21. base = spec['base']
  22. base, base_sha256 = zfs_snapshot_by_tag_or_sha256(base)
  23. root = '/'.join(base.split('/')[:-1])
  24. print('base:', base, 'root:', root)
  25. steps = spec['steps']
  26. if not isinstance(steps, list):
  27. steps = [ steps ]
  28. for st in steps:
  29. st = create_step(st)
  30. st_sha256 = st.hash(base_sha256, args=args)
  31. if zfs_exists_snapshot_sha256(st_sha256):
  32. base = zfs_snapshot_by_sha256(st_sha256)
  33. base_sha256 = st_sha256
  34. print('Reusing:', base)
  35. continue
  36. for pre in range(7, 64):
  37. name = root + '/' + st_sha256[:pre]
  38. if not zfs_exists(name):
  39. break
  40. feed = {
  41. 'focker:sha256': st_sha256
  42. }
  43. def atomic():
  44. st.execute(zfs_mountpoint(name), args=args)
  45. zfs_set_props(name, feed)
  46. snap_name = new_snapshot(base, atomic, name)
  47. # zfs_set_props(name, feed)
  48. # zfs_set_props(snap_name, feed)
  49. base = snap_name
  50. base_sha256 = st_sha256
  51. return (base, base_sha256)
  52. def command_image_build(args):
  53. # os.chdir(args.focker_dir)
  54. fname = os.path.join(args.focker_dir, 'Fockerfile')
  55. print('fname:', fname)
  56. if not os.path.exists(fname):
  57. raise ValueError('No Fockerfile could be found in the specified directory')
  58. with open(fname, 'r') as f:
  59. spec = yaml.safe_load(f)
  60. print('spec:', spec)
  61. image, image_sha256 = build(spec, args)
  62. zfs_untag(args.tag)
  63. zfs_tag(image.split('@')[0], args.tag)
  64. def command_image_untag(args):
  65. zfs_untag(args.tags)
  66. def command_image_list(args):
  67. lst = zfs_parse_output(['zfs', 'list', '-o', 'name,refer,focker:sha256,focker:tags,origin', '-H'])
  68. lst = list(filter(lambda a: a[2] != '-', lst))
  69. lst = list(map(lambda a: [ a[3], a[1],
  70. a[2] if args.full_sha256 else a[2][:7],
  71. a[4].split('/')[-1].split('@')[0] ], lst))
  72. print(tabulate(lst, headers=['Tags', 'Size', 'SHA256', 'Base']))