-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from truenas/lock
NAS-128183 / 24.10 / Allow running multiple installers (on different terminals). Introduce…
- Loading branch information
Showing
5 changed files
with
58 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,13 @@ | ||
import os | ||
import pathlib | ||
import sys | ||
|
||
import psutil | ||
|
||
from ixhardware import parse_dmi | ||
|
||
from .installer import Installer | ||
|
||
|
||
if __name__ == "__main__": | ||
pidfile = pathlib.Path("/run/truenas_installer.pid") | ||
try: | ||
pid = int(pidfile.read_text().strip()) | ||
except (FileNotFoundError, UnicodeDecodeError, ValueError): | ||
pass | ||
else: | ||
try: | ||
process = psutil.Process(pid) | ||
except psutil.NoSuchProcess: | ||
pass | ||
else: | ||
if "truenas_installer" in process.cmdline(): | ||
print(f"Installer is already running (pid={pid})", file=sys.stderr) | ||
sys.exit(1) | ||
|
||
pidfile.write_text(str(os.getpid())) | ||
try: | ||
with open("/etc/version") as f: | ||
version = f.read().strip() | ||
with open("/etc/version") as f: | ||
version = f.read().strip() | ||
|
||
dmi = parse_dmi() | ||
dmi = parse_dmi() | ||
|
||
installer = Installer(version, dmi) | ||
installer.run() | ||
finally: | ||
pidfile.unlink(missing_ok=True) | ||
installer = Installer(version, dmi) | ||
installer.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
__all__ = ["InstallError"] | ||
|
||
|
||
class InstallError(Exception): | ||
def __init__(self, message): | ||
self.message = message | ||
super().__init__(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import pathlib | ||
|
||
from .exception import InstallError | ||
|
||
__all__ = ["installation_lock"] | ||
|
||
|
||
class InstallationLock: | ||
def __init__(self): | ||
self.path = pathlib.Path("/run/truenas_installer.lock") | ||
|
||
def locked(self): | ||
return self.path.exists() | ||
|
||
def __enter__(self): | ||
if self.locked(): | ||
raise InstallError("Installation is already in progress") | ||
|
||
self.path.write_text("") | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.path.unlink(missing_ok=True) | ||
|
||
|
||
installation_lock = InstallationLock() |