Reformat with black

Signed-off-by: Stanislav Zaprudskiy <s.zaprudskiy@sap.com>
This commit is contained in:
Stanislav Zaprudskiy
2023-01-13 11:38:01 +01:00
parent b4803ca894
commit d1c608a281

View File

@@ -12,7 +12,7 @@ from django.utils.timezone import now
from awx.main.models import Instance, UnifiedJob from awx.main.models import Instance, UnifiedJob
class AWXInstance(): class AWXInstance:
def __init__(self, **filter): def __init__(self, **filter):
self.filter = filter self.filter = filter
self.get_instance() self.get_instance()
@@ -21,8 +21,7 @@ class AWXInstance():
filter = self.filter if self.filter is not None else dict(hostname=socket.gethostname()) filter = self.filter if self.filter is not None else dict(hostname=socket.gethostname())
qs = Instance.objects.filter(**filter) qs = Instance.objects.filter(**filter)
if not qs.exists(): if not qs.exists():
raise ValueError(f"No AWX instance found with {filter} "\ raise ValueError(f"No AWX instance found with {filter} parameters")
"parameters")
self.instance = qs.first() self.instance = qs.first()
def disable(self): def disable(self):
@@ -39,8 +38,7 @@ class AWXInstance():
def jobs(self): def jobs(self):
return UnifiedJob.objects.filter( return UnifiedJob.objects.filter(
Q(controller_node=self.instance.hostname) | Q(execution_node=self.instance.hostname), Q(controller_node=self.instance.hostname) | Q(execution_node=self.instance.hostname), status__in=("running", "waiting")
status__in=("running", "waiting")
) )
def jobs_pretty(self): def jobs_pretty(self):
@@ -52,17 +50,15 @@ class AWXInstance():
elapsed = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / (10**6 * 1.0) elapsed = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / (10**6 * 1.0)
elapsed = float(elapsed) elapsed = float(elapsed)
details = dict( details = dict(
name = j.name, name=j.name,
url = j.get_ui_url(), url=j.get_ui_url(),
elapsed = elapsed, elapsed=elapsed,
) )
jobs.append(details) jobs.append(details)
jobs = sorted(jobs, reverse=True, key=lambda j: j["elapsed"]) jobs = sorted(jobs, reverse=True, key=lambda j: j["elapsed"])
return ", ".join( return ", ".join([f"[\"{j['name']}\"]({j['url']})" for j in jobs])
[f"[\"{j['name']}\"]({j['url']})" for j in jobs]
)
def instance_pretty(self): def instance_pretty(self):
instance = ( instance = (
@@ -73,8 +69,7 @@ class AWXInstance():
class Command(BaseCommand): class Command(BaseCommand):
help = "Disable instance, optionally waiting for all its managed jobs " \ help = "Disable instance, optionally waiting for all its managed jobs to finish."
"to finish."
@staticmethod @staticmethod
def int_positive(arg): def int_positive(arg):
@@ -86,29 +81,32 @@ class Command(BaseCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
filter_group = parser.add_mutually_exclusive_group() filter_group = parser.add_mutually_exclusive_group()
filter_group.add_argument("--hostname", type=str, filter_group.add_argument(
"--hostname",
type=str,
default=socket.gethostname(), default=socket.gethostname(),
help=f"{Instance.hostname.field.help_text} Defaults to the " \ help=f"{Instance.hostname.field.help_text} Defaults to the hostname of the machine where the Python interpreter is currently executing".strip(),
"hostname of the machine where the Python interpreter is " \
"currently executing".strip()
) )
filter_group.add_argument("--id", type=self.int_positive, filter_group.add_argument("--id", type=self.int_positive, help=Instance.id.field.help_text)
help=Instance.id.field.help_text
parser.add_argument(
"--wait",
action="store_true",
help="Wait for jobs managed by the instance to finish. With default retry arguments waits for about 3h",
) )
parser.add_argument("--wait", action="store_true", parser.add_argument(
help="Wait for jobs managed by the instance to finish. With " \ "--retry",
"default retry arguments waits for about 3h", type=self.int_positive,
default=360,
help="Number of retries when waiting for jobs to finish. Default: 360",
) )
parser.add_argument("--retry", type=self.int_positive, default=360, parser.add_argument(
help="Number of retries when waiting for jobs to finish. " \ "--retry_sleep",
"Default: 360", type=self.int_positive,
) default=30,
help="Number of seconds to sleep before consequtive retries when waiting. Default: 30",
parser.add_argument("--retry_sleep", type=self.int_positive, default=30,
help="Number of seconds to sleep before consequtive retries " \
"when waiting. Default: 30",
) )
def handle(self, *args, **options): def handle(self, *args, **options):
@@ -119,13 +117,9 @@ class Command(BaseCommand):
raise CommandError(e) raise CommandError(e)
if instance.disable(): if instance.disable():
self.stdout.write(self.style.SUCCESS( self.stdout.write(self.style.SUCCESS(f"Instance {instance.instance_pretty()} has been disabled"))
f"Instance {instance.instance_pretty()} has been disabled"
))
else: else:
self.stdout.write( self.stdout.write(f"Instance {instance.instance_pretty()} has already been disabled")
f"Instance {instance.instance_pretty()} has already been disabled"
)
if not options["wait"]: if not options["wait"]:
return return
@@ -134,20 +128,13 @@ class Command(BaseCommand):
while instance.jobs().count() > 0: while instance.jobs().count() > 0:
if rc < options["retry"]: if rc < options["retry"]:
self.stdout.write( self.stdout.write(
f"{rc}/{options['retry']}: " \ f"{rc}/{options['retry']}: Waiting {options['retry_sleep']}s before the next attempt to see if the following instance' managed jobs have finished: {instance.jobs_pretty()}"
f"Waiting {options['retry_sleep']}s before the next " \
"attempt to see if the following instance' managed jobs " \
f"have finished: {instance.jobs_pretty()}"
) )
rc += 1 rc += 1
time.sleep(options["retry_sleep"]) time.sleep(options["retry_sleep"])
else: else:
raise CommandError( raise CommandError(
f"{rc}/{options['retry']}: " \ f"{rc}/{options['retry']}: No more retry attempts left, but the instance still has associated managed jobs: {instance.jobs_pretty()}"
"No more retry attempts left, but the instance still " \
f"has associated managed jobs: {instance.jobs_pretty()}"
) )
else: else:
self.stdout.write(self.style.SUCCESS( self.stdout.write(self.style.SUCCESS("Done waiting for instance' managed jobs to finish!"))
"Done waiting for instance' managed jobs to finish!"
))