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.

206 lines
7.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. import subprocess
  8. import csv
  9. import io
  10. import os
  11. class AmbiguousValueError(ValueError):
  12. def __init__(self, msg):
  13. super().__init__(msg)
  14. def zfs_run(command):
  15. # print('Running:', command)
  16. out = subprocess.check_output(command, stderr=subprocess.STDOUT)
  17. return out
  18. def zfs_parse_output(command):
  19. out = zfs_run(command)
  20. s = io.StringIO(out.decode('utf-8'))
  21. r = csv.reader(s, delimiter='\t')
  22. return [a for a in r]
  23. def zfs_get_type(name):
  24. lst = zfs_parse_output(['zfs', 'list', '-o', 'name,type', '-H', name])
  25. return lst[0][1]
  26. def zfs_snapshot_by_tag_or_sha256(s, focker_type='image'):
  27. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,type,name',
  28. '-H', '-t', 'snapshot', '-r', poolname + '/focker/' + focker_type + 's'])
  29. lst = list(filter(lambda a: (a[0] == s or s in a[1].split(' ')) and a[2] == 'snapshot', lst))
  30. if len(lst) == 0:
  31. raise ValueError('Reference not found: ' + s)
  32. if len(lst) > 1:
  33. raise AmbiguousValueError('Ambiguous reference: ' + s)
  34. return (lst[0][3], lst[0][0])
  35. def zfs_find(reference, focker_type='image', zfs_type='filesystem'):
  36. poolname = zfs_poolname()
  37. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,type,name', '-H', '-t', zfs_type, '-r', poolname + '/focker/' + focker_type + 's'])
  38. def match(sha256, tags, type, name, exact=False):
  39. if exact:
  40. predicate = lambda a: (a == reference)
  41. else:
  42. predicate = lambda a: a.startswith(reference)
  43. if predicate(sha256) or \
  44. any(map(predicate, tags.split(' '))) or \
  45. predicate(name.split('/')[-1]):
  46. return True
  47. return False
  48. lst = list(filter(lambda a: match(*a), lst))
  49. exact_lst = list(filter(lambda a: match(*a, exact=True), lst))
  50. if len(lst) == 0:
  51. raise ValueError('Reference not found: ' + reference)
  52. if len(lst) > 1:
  53. if len(exact_lst) == 1:
  54. return (exact_lst[0][3], exact_lst[0][0])
  55. raise AmbiguousValueError('Ambiguous reference: ' + reference)
  56. return (lst[0][3], lst[0][0])
  57. def zfs_list(fields=['name'], focker_type='image', zfs_type='filesystem'):
  58. poolname = zfs_poolname()
  59. fields.append('focker:sha256')
  60. lst = zfs_parse_output(['zfs', 'list', '-o', ','.join(fields),
  61. '-H', '-t', zfs_type, '-r', poolname + '/focker/' + focker_type + 's'])
  62. lst = list(filter(lambda a: a[-1] != '-', lst))
  63. return lst
  64. def zfs_prune(focker_type='image'):
  65. poolname = zfs_poolname()
  66. again = True
  67. while again:
  68. again = False
  69. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,origin,name', '-H', '-r', poolname + '/focker/' + focker_type + 's'])
  70. used = set()
  71. for r in lst:
  72. if r[2] == '-':
  73. continue
  74. used.add(r[2].split('@')[0])
  75. for r in lst:
  76. if r[0] == '-' or r[1] != '-':
  77. continue
  78. if r[3] not in used:
  79. print('Removing:', r[3])
  80. zfs_run(['zfs', 'destroy', '-r', '-f', r[3]])
  81. again = True
  82. def zfs_clone(name, target_name):
  83. zfs_run(['zfs', 'clone', name, target_name])
  84. def zfs_exists(name):
  85. try:
  86. zfs_run(['zfs', 'list', name])
  87. except subprocess.CalledProcessError as e:
  88. return False
  89. return True
  90. def zfs_set_props(name, props):
  91. for (k, v) in props.items():
  92. zfs_run(['zfs', 'set', k + '=' + v, name])
  93. def zfs_mountpoint(name):
  94. lst = zfs_parse_output(['zfs', 'list', '-o', 'mountpoint', '-H', name])
  95. return lst[0][0]
  96. def zfs_exists_snapshot_sha256(sha256, focker_type='image'):
  97. poolname = zfs_poolname()
  98. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256', '-t', 'snap',
  99. '-r', poolname + '/focker/' + focker_type + 's'])
  100. lst = list(filter(lambda a: a[0] == sha256, lst))
  101. if len(lst) == 0:
  102. return False
  103. return True
  104. def zfs_snapshot_by_sha256(sha256, focker_type='image'):
  105. poolname = zfs_poolname()
  106. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,name',
  107. '-t', 'snap', '-H', '-r', poolname + '/focker/' + focker_type + 's'])
  108. lst = list(filter(lambda a: a[0] == sha256, lst))
  109. if len(lst) == 0:
  110. raise ValueError('Snapshot with given sha256 does not exist: ' + sha256)
  111. if len(lst) > 1:
  112. raise AmbiguousValueError('Ambiguous snapshot sha256: ' + sha256)
  113. return lst[0][1]
  114. def zfs_tag(name, tags, replace=False):
  115. if any(map(lambda a: ' ' in a, tags)):
  116. raise ValueError('Tags cannot contain spaces')
  117. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:tags', '-H', name])
  118. if not replace:
  119. tags = list(tags)
  120. tags.extend(lst[0][0].split(' '))
  121. tags = list(set(tags))
  122. tags = list(filter(lambda a: a != '-', tags))
  123. if len(tags) > 0:
  124. zfs_run(['zfs', 'set', 'focker:tags=' + ' '.join(tags), name])
  125. else:
  126. zfs_run(['zfs', 'inherit', 'focker:tags', name])
  127. def zfs_untag(tags, focker_type='image'):
  128. if any(map(lambda a: ' ' in a, tags)):
  129. raise ValueError('Tags cannot contain spaces')
  130. # print('zfs_untag(), tags:', tags)
  131. poolname = zfs_poolname()
  132. lst = zfs_parse_output(['zfs', 'list', '-o', 'name,focker:tags', '-H', '-r', poolname + '/focker/' + focker_type + 's'])
  133. lst = filter(lambda a: any([b in a[1].split(' ') for b in tags]), lst)
  134. for row in lst:
  135. cur_tags = row[1].split(' ')
  136. for t in tags:
  137. if t in cur_tags:
  138. cur_tags.remove(t)
  139. zfs_tag(row[0], cur_tags, replace=True)
  140. def zfs_name(path):
  141. lst = zfs_parse_output(['zfs', 'list', '-o', 'name', '-H', path])
  142. if len(lst) == 0:
  143. raise ValueError('Not a ZFS path')
  144. if len(lst) > 1:
  145. raise AmbiguousValueError('Ambiguous ZFS path')
  146. return lst[0][0]
  147. def zfs_poolname():
  148. poolname = zfs_parse_output(['zfs', 'list', '-H', '/'])
  149. if len(poolname) == 0:
  150. raise ValueError('Not a ZFS root')
  151. poolname = poolname[0][0].split('/')[0]
  152. return poolname
  153. def zfs_init():
  154. poolname = zfs_poolname()
  155. print('poolname:', poolname)
  156. for path in ['/focker', '/focker/images', '/focker/volumes', '/focker/jails']:
  157. if not os.path.exists(path):
  158. os.mkdir(path)
  159. if not zfs_exists(poolname + '/focker'):
  160. zfs_run(['zfs', 'create', '-o', 'canmount=off', '-o', 'mountpoint=/focker', poolname + '/focker'])
  161. if not zfs_exists(poolname + '/focker/images'):
  162. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/images'])
  163. if not zfs_exists(poolname + '/focker/volumes'):
  164. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/volumes'])
  165. if not zfs_exists(poolname + '/focker/jails'):
  166. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/jails'])