Subprocess commands; retry on failure

This commit is contained in:
Théophile Bastian 2020-03-12 09:53:37 +01:00
parent be299901e9
commit cae5e2244c
2 changed files with 17 additions and 5 deletions

View file

@ -6,7 +6,6 @@ from .jinja_template import JinjaTemplate
import libvirt import libvirt
import tempfile import tempfile
import subprocess
from pathlib import Path from pathlib import Path
import getpass import getpass
import weakref import weakref
@ -75,7 +74,8 @@ class OverlayDirectory:
self.mount_point, self.mount_point,
] ]
subprocess.run(command) util.run_cmd_retry(command)
self.mounted = True self.mounted = True
self.temp_dir_cleaner = weakref.finalize( self.temp_dir_cleaner = weakref.finalize(
@ -91,7 +91,7 @@ class OverlayDirectory:
"umount", "umount",
self.mount_point, self.mount_point,
] ]
subprocess.run(command) util.run_cmd_retry(command)
self.mounted = False self.mounted = False
def _set_perms_for_cleanup(self): def _set_perms_for_cleanup(self):
@ -109,7 +109,8 @@ class OverlayDirectory:
getpass.getuser(), getpass.getuser(),
self.temp_dir.resolve(), self.temp_dir.resolve(),
] ]
subprocess.run(command_chown) util.run_cmd_retry(command_chown)
command_chmod = [ command_chmod = [
"sudo", "sudo",
"chmod", "chmod",
@ -117,7 +118,7 @@ class OverlayDirectory:
"700", "700",
self.temp_dir.resolve(), self.temp_dir.resolve(),
] ]
subprocess.run(command_chmod) util.run_cmd_retry(command_chmod)
def cleanup_mount(self): def cleanup_mount(self):
self._umount() self._umount()

View file

@ -3,6 +3,9 @@
from . import settings from . import settings
import uuid import uuid
import subprocess
import sys
class NumberedClass: class NumberedClass:
""" A class that counts its current instance number """ """ A class that counts its current instance number """
@ -125,3 +128,11 @@ class Addrv6:
return "{base_range}:{link_id:04x}::{dev_id:04x}".format( return "{base_range}:{link_id:04x}::{dev_id:04x}".format(
base_range=settings.IPV6_RANGE, link_id=self.link_id, dev_id=self.dev_id, base_range=settings.IPV6_RANGE, link_id=self.link_id, dev_id=self.dev_id,
) )
def run_cmd_retry(command, *args, **kwargs):
rc = subprocess.run(command, *args, **kwargs)
while rc.returncode != 0:
print("Command failed. Try again:", file=sys.stderr)
rc = subprocess.run(command, *args, **kwargs)
return rc