Skip to content

Commit f2d41c7

Browse files
fix: factorize common code between start/stop functions and use util.doexec instead of subprocess.run
Signed-off-by: Mathieu Labourier <mathieu.labourier@vates.tech>
1 parent 698659d commit f2d41c7

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

drivers/cleanup.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,6 +4033,18 @@ def start_gc(session, sr_uuid):
40334033
subprocess.run([__file__, '-b', '-u', sr_uuid, '-g'],
40344034
stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
40354035

4036+
def _gc_service_cmd(sr_uuid, action, extra_args=None):
4037+
"""
4038+
Build and run the systemctl command for the GC service using util.doexec.
4039+
"""
4040+
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
4041+
cmd=[ "/usr/bin/systemctl", "--quiet" ]
4042+
if extra_args:
4043+
cmd.extend(extra_args)
4044+
cmd += [action, f"SMGC@{sr_uuid_esc}"]
4045+
return util.doexec(cmd)
4046+
4047+
40364048
def start_gc_service(sr_uuid, wait=False):
40374049
"""
40384050
This starts the templated systemd service which runs GC on the given SR UUID.
@@ -4043,26 +4055,18 @@ def start_gc_service(sr_uuid, wait=False):
40434055
run has finished. This is used to force a run of the GC instead of just kicking it
40444056
in the background.
40454057
"""
4046-
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
40474058
util.SMlog(f"Kicking SMGC@{sr_uuid}...")
4048-
cmd=[ "/usr/bin/systemctl", "--quiet" ]
4049-
if not wait:
4050-
cmd.append("--no-block")
4051-
cmd += ["start", f"SMGC@{sr_uuid_esc}"]
4052-
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
4059+
_gc_service_cmd(sr_uuid, "start", extra_args=None if wait else ["--no-block"])
40534060

40544061

40554062
def stop_gc_service(sr_uuid):
40564063
"""
40574064
Stops the templated systemd service which runs GC on the given SR UUID.
40584065
"""
4059-
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
40604066
util.SMlog(f"Stopping SMGC@{sr_uuid}...")
4061-
cmd = ["/usr/bin/systemctl", "--quiet", "stop", f"SMGC@{sr_uuid_esc}"]
4062-
try:
4063-
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, check=True)
4064-
except subprocess.CalledProcessError as e:
4065-
util.SMlog(f"Failed to stop gc service `SMGC@{sr_uuid_esc}`: `{e.stderr.decode().strip()}`")
4067+
(rc, _stdout, stderr) = _gc_service_cmd(sr_uuid, "stop", check=True)
4068+
if rc != 0:
4069+
util.SMlog(f"Failed to stop gc service `SMGC@{sr_uuid}`: `{stderr}`")
40664070

40674071
def gc_force(session, srUuid, force=False, dryRun=False, lockSR=False):
40684072
"""Garbage collect all deleted VDIs in SR "srUuid". The caller must ensure

0 commit comments

Comments
 (0)