Skip to content

Commit

Permalink
user subprocess.run, add option to start blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
palto42 committed Oct 14, 2020
1 parent 38c07f2 commit c813d94
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions rguard
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,11 @@ import sys

prog = "rguard"
vers = {
"version": "2.0.0",
"date": "2020/10/08",
"version": "2.0.2",
"date": "2020/14/08",
}


def call(cmd, showStdout=True, shell=False):
"""Execute *cmd* and return True on success."""
if showStdout:
return subprocess.call(cmd, shell=shell) == 0
else:
with open(os.devnull, "w") as n:
return subprocess.call(cmd, shell=shell, stdout=n) == 0


class RebootGuard:
"""Object for enabling/disabling blocks that prevent shutdown, reboot, etc.
Expand Down Expand Up @@ -93,7 +84,7 @@ class RebootGuard:
reloadDaemon |= self.del_systemd_unit_start_block(t)
if reloadDaemon:
logging.debug("Reloading systemd via: systemctl daemon-reload")
if subprocess.call(["systemctl", "daemon-reload"]) != 0:
if subprocess.run(["systemctl", "daemon-reload"]).returncode != 0:
logging.error("Unexpected error reloading systemd")

def systemd_unit_is_blocked(self, unit):
Expand Down Expand Up @@ -149,19 +140,19 @@ class RebootGuard:

def mkdir_p(self, path):
"""Make directory *path*."""
if call(["mkdir", "-p", path]):
if subprocess.run(["mkdir", "-p", path]).returncode == 0:
return True
else:
logging.error(f"Unexpected error making directory '{path}'")
return False
return False

def rm_rf(self, path):
"""Recursively remove *path*."""
if call(["rm", "-rf", path]):
if subprocess.run(["rm", "-rf", path]).returncode == 0:
return True
else:
logging.error(f"Unexpected error deleting path '{path}'")
return False
return False


class ConditionChecker:
Expand Down Expand Up @@ -247,7 +238,12 @@ class ConditionChecker:
def test_active_units(self):
if self.units:
for u in self.units:
if call(["systemctl", "is-active", u], showStdout=False):
if (
subprocess.run(
["systemctl", "is-active", u], capture_output=True
).returncode
== 0
):
logging.info(f"✘ Fail: Unit '{u}' shouldn't be active")
return True
logging.info("✔ Pass: No active units match")
Expand All @@ -258,7 +254,12 @@ class ConditionChecker:
def test_running_cmds(self):
if self.cmds:
for c in self.cmds:
if call(["pgrep", "--exact", c], showStdout=False):
if (
subprocess.run(
["pgrep", "--exact", c], capture_output=True
).returncode
== 0
):
logging.info(f"✘ Fail: Process named '{c}' shouldn't exist")
return True
logging.info("✔ Pass: No running processes match commands")
Expand All @@ -269,7 +270,12 @@ class ConditionChecker:
def test_running_cmd_args(self):
if self.cmdArgs:
for a in self.cmdArgs:
if call(["pgrep", "--full", "--exact", a], showStdout=False):
if (
subprocess.run(
["pgrep", "--full", "--exact", a], capture_output=True
).returncode
== 0
):
logging.info(
f"✘ Fail: Process name+args matching '{a}' " f"shouldn't exist"
)
Expand All @@ -295,7 +301,9 @@ class ConditionChecker:
shell = False
logging.debug(f"Running cmd: {c}")
c = c.split()
if not call(c, showStdout=False, shell=shell) == desiredOutcome:
if (
subprocess.run(c, capture_output=True, shell=shell).returncode == 0
) != desiredOutcome:
logging.info(
f"✘ Fail: Exec-command {{{c}}} needs to return "
f"{z[desiredOutcome]}"
Expand Down Expand Up @@ -517,6 +525,12 @@ def parse_args():
action="store_true",
help="Exit (and remove reboot-guard) the first time all condition checks pass",
)
g2.add_argument(
"-b",
"--start-blocked",
action="store_true",
help="Install reboot-guard at start, regardless of initial conditions check",
)
g2.add_argument(
"-v",
"--loglevel",
Expand Down Expand Up @@ -577,11 +591,13 @@ def main():
# Initialize signal handler
_ = GracefulDeath(r)
# Run initial check
if c.check_conditions():
if c.check_conditions() and not opts.start_blocked:
if opts.exit_on_pass:
logging.warning("Exiting due to passed condition checks")
sys.exit()
else:
if opts.start_blocked:
logging.debug("Forced to start with blocked targets.")
r.guard(enforce=True)
# Run forever-loop
while True:
Expand Down

0 comments on commit c813d94

Please sign in to comment.