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.

41 lines
991B

  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 random
  8. from .zfs import zfs_exists
  9. import os
  10. import fcntl
  11. def random_sha256_hexdigest():
  12. return bytes([ random.randint(0, 255) for _ in range(32) ]).hex()
  13. def find_prefix(head, tail):
  14. for pre in range(7, len(tail)):
  15. name = head + tail[:pre]
  16. if not zfs_exists(name):
  17. break
  18. return name
  19. def focker_lock():
  20. os.makedirs('/var/lock', exist_ok=True)
  21. if focker_lock.fd is None:
  22. focker_lock.fd = open('/var/lock/focker.lock', 'a+')
  23. print('Waiting for /var/lock/focker.lock ...')
  24. fcntl.flock(focker_lock.fd, fcntl.LOCK_EX)
  25. print('Lock acquired.')
  26. focker_lock.fd = None
  27. def focker_unlock():
  28. if focker_lock.fd is None:
  29. return
  30. fcntl.flock(focker_lock.fd, fcntl.LOCK_UN)
  31. print('Lock released')