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!
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

97 satır
3.0KB

  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 os
  8. import yaml
  9. from .zfs import AmbiguousValueError, \
  10. zfs_find, \
  11. zfs_tag, \
  12. zfs_untag, \
  13. zfs_mountpoint
  14. from .jail import jail_fs_create, \
  15. jail_create, \
  16. jail_remove
  17. from .misc import random_sha256_hexdigest, \
  18. find_prefix
  19. import subprocess
  20. import jailconf
  21. import os
  22. def build_volumes(spec):
  23. for tag in spec.keys():
  24. try:
  25. name, _ = zfs_find(tag, focker_type='volume')
  26. continue
  27. except AmbiguousValueError:
  28. raise
  29. except ValueError:
  30. pass
  31. sha256 = random_sha256_hexdigest()
  32. name = find_prefix(poolname + '/focker/volumes/', sha256)
  33. subprocess.check_output(['zfs', 'create', name])
  34. zfs_untag([ tag ], focker_type='volume')
  35. zfs_tag(name, [ tag ])
  36. def build_images(spec, path):
  37. # print('build_images(): NotImplementedError')
  38. for (tag, focker_dir) in spec.items():
  39. res = subprocess.run(['focker', 'image', 'build',
  40. os.path.join(path, focker_dir), '-t', tag])
  41. if res.returncode != 0:
  42. raise RuntimeError('Image build failed: ' + str(res.returncode))
  43. def build_jails(spec):
  44. #if os.path.exists('/etc/jail.conf'):
  45. # conf = jailconf.load('/etc/jail.conf')
  46. #else:
  47. # conf = jailconf.JailConf()
  48. for (jailname, jailspec) in spec.items():
  49. try:
  50. name, _ = zfs_find(jailname, focker_type='jail')
  51. jail_remove(zfs_mountpoint(name))
  52. except AmbiguousValueError:
  53. raise
  54. except ValueError:
  55. pass
  56. name = jail_fs_create(jailspec['image'])
  57. zfs_untag([ jailname ], focker_type='jail')
  58. zfs_tag(name, [ jailname ])
  59. path = zfs_mountpoint(name)
  60. jail_create(path,
  61. jailspec.get('exec.start', '/bin/sh /etc/rc'),
  62. jailspec.get('env', {}),
  63. [ [from_, on] \
  64. for (from_, on) in jailspec.get('mounts', {}).items() ],
  65. hostname=jailname,
  66. overrides={
  67. 'exec.stop': jailspec.get('exec.stop', '/bin/sh /etc/rc.shutdown'),
  68. 'ip4.addr': jailspec.get('ip4.addr', '127.0.1.0'),
  69. 'interface': jailspec.get('interface', 'lo1')
  70. })
  71. def command_compose_build(args):
  72. if not os.path.exists(args.filename):
  73. raise ValueError('File not found: ' + args.filename)
  74. path, _ = os.path.split(args.filename)
  75. with open(args.filename, 'r') as f:
  76. spec = yaml.safe_load(f)
  77. if 'volumes' in spec:
  78. build_volumes(spec['volumes'])
  79. if 'images' in spec:
  80. build_images(spec['images'], path)
  81. if 'jails' in spec:
  82. build_jails(spec['jails'])
  83. def command_compose_run(args):
  84. raise NotImplementedError