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