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개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.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. import random
  8. from .zfs import zfs_exists
  9. import os
  10. import fcntl
  11. def random_sha256_hexdigest():
  12. for _ in range(10**6):
  13. res = bytes([ random.randint(0, 255) for _ in range(32) ]).hex()
  14. if not res[:7].isnumeric():
  15. return res
  16. raise ValueError('Couldn\'t find random SHA256 hash with non-numeric 7-character prefix in 10^6 trials o_O')
  17. def find_prefix(head, tail):
  18. for pre in range(7, len(tail)):
  19. name = head + tail[:pre]
  20. if not zfs_exists(name):
  21. break
  22. return name
  23. def focker_lock():
  24. os.makedirs('/var/lock', exist_ok=True)
  25. if focker_lock.fd is None:
  26. focker_lock.fd = open('/var/lock/focker.lock', 'a+')
  27. print('Waiting for /var/lock/focker.lock ...')
  28. fcntl.flock(focker_lock.fd, fcntl.LOCK_EX)
  29. print('Lock acquired.')
  30. focker_lock.fd = None
  31. def focker_unlock():
  32. if focker_lock.fd is None:
  33. return
  34. fcntl.flock(focker_lock.fd, fcntl.LOCK_UN)
  35. print('Lock released')