203 lines
4.0 KiB
Python
203 lines
4.0 KiB
Python
"""Exclusive filesystem-based deployment locking."""
|
|
|
|
import fcntl
|
|
import os
|
|
import stat
|
|
from pathlib import Path
|
|
|
|
|
|
class LockError(RuntimeError):
|
|
"""Deployment lock unavailable or unsafe."""
|
|
|
|
|
|
class DeploymentLock:
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = Path(path)
|
|
self._fd = None
|
|
|
|
if not self.path.is_absolute():
|
|
|
|
raise LockError(
|
|
"Deployment lock path must be absolute"
|
|
)
|
|
|
|
if ".." in self.path.parts:
|
|
|
|
raise LockError(
|
|
"Unsafe deployment lock path"
|
|
)
|
|
|
|
@property
|
|
def acquired(self):
|
|
|
|
return self._fd is not None
|
|
|
|
def validate_parent(self):
|
|
|
|
parent = self.path.parent
|
|
|
|
if (
|
|
not parent.is_dir()
|
|
or parent.is_symlink()
|
|
or parent.resolve() != parent
|
|
):
|
|
|
|
raise LockError(
|
|
"Invalid deployment lock directory"
|
|
)
|
|
|
|
def acquire(self):
|
|
|
|
if self.acquired:
|
|
|
|
raise LockError(
|
|
"Deployment lock already acquired"
|
|
)
|
|
|
|
fd = None
|
|
parent_fd = None
|
|
|
|
try:
|
|
|
|
self.validate_parent()
|
|
|
|
parent_fd = os.open(
|
|
self.path.parent,
|
|
os.O_RDONLY
|
|
| os.O_DIRECTORY
|
|
| os.O_NOFOLLOW
|
|
| os.O_CLOEXEC,
|
|
)
|
|
|
|
parent = os.fstat(parent_fd)
|
|
|
|
if (
|
|
parent.st_uid != os.getuid()
|
|
or stat.S_IMODE(parent.st_mode) & 0o022
|
|
):
|
|
|
|
raise LockError(
|
|
"Unsafe deployment lock directory permissions"
|
|
)
|
|
|
|
fd = os.open(
|
|
self.path.name,
|
|
os.O_RDWR
|
|
| os.O_CREAT
|
|
| os.O_NOFOLLOW
|
|
| os.O_NONBLOCK
|
|
| os.O_CLOEXEC,
|
|
0o600,
|
|
dir_fd=parent_fd,
|
|
)
|
|
|
|
details = os.fstat(fd)
|
|
|
|
if (
|
|
not stat.S_ISREG(details.st_mode)
|
|
or details.st_uid != os.getuid()
|
|
or details.st_nlink != 1
|
|
or stat.S_IMODE(details.st_mode) != 0o600
|
|
):
|
|
|
|
raise LockError(
|
|
"Unsafe deployment lock file"
|
|
)
|
|
|
|
try:
|
|
|
|
fcntl.flock(
|
|
fd,
|
|
fcntl.LOCK_EX | fcntl.LOCK_NB,
|
|
)
|
|
|
|
except BlockingIOError:
|
|
|
|
raise LockError(
|
|
"Another deployment operation is active"
|
|
) from None
|
|
|
|
# Check that the directory entry still refers
|
|
# to the locked inode.
|
|
|
|
entry = os.stat(
|
|
self.path.name,
|
|
dir_fd=parent_fd,
|
|
follow_symlinks=False,
|
|
)
|
|
|
|
locked = os.fstat(fd)
|
|
|
|
if (
|
|
entry.st_dev != locked.st_dev
|
|
or entry.st_ino != locked.st_ino
|
|
or locked.st_nlink != 1
|
|
or not stat.S_ISREG(entry.st_mode)
|
|
):
|
|
|
|
raise LockError(
|
|
"Deployment lock identity changed"
|
|
)
|
|
|
|
self._fd = fd
|
|
fd = None
|
|
|
|
return self
|
|
|
|
except LockError:
|
|
|
|
raise
|
|
|
|
except OSError:
|
|
|
|
raise LockError(
|
|
"Unable to acquire deployment lock"
|
|
) from None
|
|
|
|
finally:
|
|
|
|
if fd is not None:
|
|
|
|
os.close(fd)
|
|
|
|
if parent_fd is not None:
|
|
|
|
os.close(parent_fd)
|
|
|
|
def release(self):
|
|
|
|
if self._fd is None:
|
|
return
|
|
|
|
fd = self._fd
|
|
|
|
self._fd = None
|
|
|
|
try:
|
|
|
|
fcntl.flock(
|
|
fd,
|
|
fcntl.LOCK_UN,
|
|
)
|
|
|
|
finally:
|
|
|
|
os.close(fd)
|
|
|
|
def __enter__(self):
|
|
|
|
return self.acquire()
|
|
|
|
def __exit__(
|
|
self,
|
|
exc_type,
|
|
exc_value,
|
|
traceback,
|
|
):
|
|
|
|
self.release()
|
|
|
|
return False
|