Commit f2d9b658 authored by Neil Williams's avatar Neil Williams
Browse files

Apply black to lava_scheduler_app/api and tests.


Signed-off-by: default avatarNeil Williams <neil.williams@linaro.org>
parent 7980d84c
This diff is collapsed.
......@@ -26,7 +26,6 @@ from lava_scheduler_app.models import Alias
class SchedulerAliasesAPI(ExposedV2API):
@check_perm("lava_scheduler_app.add_alias")
def add(self, name):
"""
......@@ -51,8 +50,7 @@ class SchedulerAliasesAPI(ExposedV2API):
try:
Alias.objects.create(name=name)
except IntegrityError as exc:
raise xmlrpc.client.Fault(
400, "Bad request: %s" % exc.message)
raise xmlrpc.client.Fault(400, "Bad request: %s" % exc.message)
@check_perm("lava_scheduler_app.delete_alias")
def delete(self, name):
......@@ -78,8 +76,7 @@ class SchedulerAliasesAPI(ExposedV2API):
try:
Alias.objects.get(name=name).delete()
except Alias.DoesNotExist:
raise xmlrpc.client.Fault(
404, "Alias '%s' was not found." % name)
raise xmlrpc.client.Fault(404, "Alias '%s' was not found." % name)
def list(self):
"""
......@@ -126,8 +123,7 @@ class SchedulerAliasesAPI(ExposedV2API):
try:
alias = Alias.objects.get(name=name)
except Alias.DoesNotExist:
raise xmlrpc.client.Fault(
404, "Alias '%s' was not found." % name)
raise xmlrpc.client.Fault(404, "Alias '%s' was not found." % name)
device_types = []
for dt in alias.device_types.all():
......@@ -135,5 +131,4 @@ class SchedulerAliasesAPI(ExposedV2API):
continue
device_types.append(dt.name)
return {"name": alias.name,
"device_types": device_types}
return {"name": alias.name, "device_types": device_types}
......@@ -31,11 +31,12 @@ from lava_scheduler_app.models import Alias, DeviceType
class SchedulerDeviceTypesAPI(ExposedV2API):
def _available_device_types(self):
""" List avaiable device types by looking at the configuration files """
available_types = []
for fname in glob.iglob("/etc/lava-server/dispatcher-config/device-types/*.jinja2"):
for fname in glob.iglob(
"/etc/lava-server/dispatcher-config/device-types/*.jinja2"
):
device_type = os.path.basename(fname[:-7])
if not device_type.startswith("base"):
available_types.append(device_type)
......@@ -43,8 +44,15 @@ class SchedulerDeviceTypesAPI(ExposedV2API):
return available_types
@check_perm("lava_scheduler_app.add_devicetype")
def add(self, name, description, display, owners_only,
health_frequency, health_denominator):
def add(
self,
name,
description,
display,
owners_only,
health_frequency,
health_denominator,
):
"""
Name
----
......@@ -81,17 +89,21 @@ class SchedulerDeviceTypesAPI(ExposedV2API):
elif health_denominator == "jobs":
health_denominator = DeviceType.HEALTH_PER_JOB
else:
raise xmlrpc.client.Fault(
400, "Bad request: invalid health_denominator.")
raise xmlrpc.client.Fault(400, "Bad request: invalid health_denominator.")
try:
DeviceType.objects.create(name=name, description=description,
display=display, owners_only=owners_only,
health_frequency=health_frequency,
health_denominator=health_denominator)
DeviceType.objects.create(
name=name,
description=description,
display=display,
owners_only=owners_only,
health_frequency=health_frequency,
health_denominator=health_denominator,
)
except IntegrityError:
raise xmlrpc.client.Fault(
400, "Bad request: device-type name is already used.")
400, "Bad request: device-type name is already used."
)
def get_health_check(self, name):
"""
......@@ -128,15 +140,19 @@ class SchedulerDeviceTypesAPI(ExposedV2API):
raise xmlrpc.client.Fault(400, "Invalid device-type '%s'" % name)
try:
filename = os.path.join("/etc/lava-server/dispatcher-config/health-checks", name)
filename += ".yaml" if not filename.endswith('.yaml') else ''
filename = os.path.join(
"/etc/lava-server/dispatcher-config/health-checks", name
)
filename += ".yaml" if not filename.endswith(".yaml") else ""
with open(filename, "r") as f_in:
return xmlrpc.client.Binary(f_in.read().encode("utf-8"))
except OSError as exc:
if exc.errno == errno.ENOENT:
raise xmlrpc.client.Fault(404, "Device-type '%s' was not found." % name)
else:
raise xmlrpc.client.Fault(400, "Unable to read health-check: %s" % exc.strerror)
raise xmlrpc.client.Fault(
400, "Unable to read health-check: %s" % exc.strerror
)
def get_template(self, name):
"""
......@@ -173,15 +189,19 @@ class SchedulerDeviceTypesAPI(ExposedV2API):
raise xmlrpc.client.Fault(400, "Invalid device-type '%s'" % name)
try:
filename = os.path.join("/etc/lava-server/dispatcher-config/device-types", name)
filename += ".jinja2" if not filename.endswith('.jinja2') else ''
filename = os.path.join(
"/etc/lava-server/dispatcher-config/device-types", name
)
filename += ".jinja2" if not filename.endswith(".jinja2") else ""
with open(filename, "r") as f_in:
return xmlrpc.client.Binary(f_in.read().encode("utf-8"))
except OSError as exc:
if exc.errno == errno.ENOENT:
raise xmlrpc.client.Fault(404, "Device-type '%s' was not found." % name)
else:
raise xmlrpc.client.Fault(400, "Unable to read device-type configuration: %s" % exc.strerror)
raise xmlrpc.client.Fault(
400, "Unable to read device-type configuration: %s" % exc.strerror
)
@check_perm("lava_scheduler_app.change_devicetype")
def set_health_check(self, name, config):
......@@ -217,13 +237,16 @@ class SchedulerDeviceTypesAPI(ExposedV2API):
raise xmlrpc.client.Fault(400, "Invalid device-type '%s'" % name)
try:
filename = os.path.join("/etc/lava-server/dispatcher-config/health-checks", name)
filename += ".yaml" if not filename.endswith('.yaml') else ''
filename = os.path.join(
"/etc/lava-server/dispatcher-config/health-checks", name
)
filename += ".yaml" if not filename.endswith(".yaml") else ""
with open(filename, "w") as f_out:
f_out.write(config)
except OSError as exc:
raise xmlrpc.client.Fault(
400, "Unable to write health-check: %s" % exc.strerror)
400, "Unable to write health-check: %s" % exc.strerror
)
@check_perm("lava_scheduler_app.change_devicetype")
def set_template(self, name, config):
......@@ -259,13 +282,16 @@ class SchedulerDeviceTypesAPI(ExposedV2API):
raise xmlrpc.client.Fault(400, "Invalid device-type '%s'" % name)
try:
filename = os.path.join("/etc/lava-server/dispatcher-config/device-types", name)
filename += ".jinja2" if not filename.endswith('.jinja2') else ''
filename = os.path.join(
"/etc/lava-server/dispatcher-config/device-types", name
)
filename += ".jinja2" if not filename.endswith(".jinja2") else ""
with open(filename, "w") as f_out:
f_out.write(config)
except OSError as exc:
raise xmlrpc.client.Fault(
400, "Unable to write device-type configuration: %s" % exc.strerror)
400, "Unable to write device-type configuration: %s" % exc.strerror
)
def list(self, show_all=False):
"""
......@@ -288,22 +314,33 @@ class SchedulerDeviceTypesAPI(ExposedV2API):
This function returns an XML-RPC array of device-types
"""
available_types = self._available_device_types()
device_types = [dt for dt in DeviceType.objects.all().order_by('name')
if not dt.owners_only or dt.some_devices_visible_to(self.user)]
device_types = [
dt
for dt in DeviceType.objects.all().order_by("name")
if not dt.owners_only or dt.some_devices_visible_to(self.user)
]
ret = []
for dt in device_types:
ret.append({"name": dt.name,
"devices": dt.device_set.count(),
"installed": True,
"template": dt.name in available_types})
ret.append(
{
"name": dt.name,
"devices": dt.device_set.count(),
"installed": True,
"template": dt.name in available_types,
}
)
if show_all:
device_type_names = [dt.name for dt in device_types]
for dt_name in available_types:
if dt_name not in device_type_names:
ret.append({"name": dt_name,
"devices": 0,
"installed": False,
"template": True})
ret.append(
{
"name": dt_name,
"devices": 0,
"installed": False,
"template": True,
}
)
return ret
def show(self, name):
......@@ -329,28 +366,38 @@ class SchedulerDeviceTypesAPI(ExposedV2API):
try:
dt = DeviceType.objects.get(name=name)
except DeviceType.DoesNotExist:
raise xmlrpc.client.Fault(
404, "Device-type '%s' was not found." % name)
raise xmlrpc.client.Fault(404, "Device-type '%s' was not found." % name)
if dt.owners_only and not dt.some_devices_visible_to(self.user):
raise xmlrpc.client.Fault(
404, "Device-type '%s' was not found." % name)
raise xmlrpc.client.Fault(404, "Device-type '%s' was not found." % name)
aliases = [str(alias.name) for alias in dt.aliases.all()]
devices = [str(d.hostname) for d in dt.device_set.all() if d.is_visible_to(self.user)]
dt_dict = {"name": dt.name,
"description": dt.description,
"display": dt.display,
"owners_only": dt.owners_only,
"health_disabled": dt.disable_health_check,
"aliases": aliases,
"devices": devices}
devices = [
str(d.hostname) for d in dt.device_set.all() if d.is_visible_to(self.user)
]
dt_dict = {
"name": dt.name,
"description": dt.description,
"display": dt.display,
"owners_only": dt.owners_only,
"health_disabled": dt.disable_health_check,
"aliases": aliases,
"devices": devices,
}
return dt_dict
@check_perm("lava_scheduler_app.change_devicetype")
def update(self, name, description, display, owners_only, health_frequency,
health_denominator, health_disabled):
def update(
self,
name,
description,
display,
owners_only,
health_frequency,
health_denominator,
health_disabled,
):
"""
Name
----
......@@ -389,8 +436,7 @@ class SchedulerDeviceTypesAPI(ExposedV2API):
try:
dt = DeviceType.objects.get(name=name)
except DeviceType.DoesNotExist:
raise xmlrpc.client.Fault(
404, "Device-type '%s' was not found." % name)
raise xmlrpc.client.Fault(404, "Device-type '%s' was not found." % name)
if description is not None:
dt.description = description
......@@ -411,7 +457,8 @@ class SchedulerDeviceTypesAPI(ExposedV2API):
health_denominator = DeviceType.HEALTH_PER_JOB
else:
raise xmlrpc.client.Fault(
400, "Bad request: invalid health_denominator.")
400, "Bad request: invalid health_denominator."
)
dt.health_denominator = health_denominator
......@@ -422,7 +469,6 @@ class SchedulerDeviceTypesAPI(ExposedV2API):
class SchedulerDeviceTypesAliasesAPI(ExposedV2API):
@check_perm("lava_scheduler_app.add_alias")
@check_perm("lava_scheduler_app.change_devicetype")
def add(self, name, alias):
......@@ -450,8 +496,7 @@ class SchedulerDeviceTypesAliasesAPI(ExposedV2API):
try:
dt = DeviceType.objects.get(name=name)
except DeviceType.DoesNotExist:
raise xmlrpc.client.Fault(
404, "DeviceType '%s' was not found." % name)
raise xmlrpc.client.Fault(404, "DeviceType '%s' was not found." % name)
alias_obj, _ = Alias.objects.get_or_create(name=alias)
dt.aliases.add(alias_obj)
......@@ -479,12 +524,10 @@ class SchedulerDeviceTypesAliasesAPI(ExposedV2API):
try:
dt = DeviceType.objects.get(name=name)
except DeviceType.DoesNotExist:
raise xmlrpc.client.Fault(
404, "DeviceType '%s' was not found." % name)
raise xmlrpc.client.Fault(404, "DeviceType '%s' was not found." % name)
if dt.owners_only and not dt.some_devices_visible_to(self.user):
raise xmlrpc.client.Fault(
404, "Device-type '%s' was not found." % name)
raise xmlrpc.client.Fault(404, "Device-type '%s' was not found." % name)
return [a.name for a in dt.aliases.all().order_by("name")]
......@@ -513,14 +556,12 @@ class SchedulerDeviceTypesAliasesAPI(ExposedV2API):
try:
dt = DeviceType.objects.get(name=name)
except DeviceType.DoesNotExist:
raise xmlrpc.client.Fault(
404, "DeviceType '%s' was not found." % name)
raise xmlrpc.client.Fault(404, "DeviceType '%s' was not found." % name)
try:
alias_obj = Alias.objects.get(name=alias)
except Alias.DoesNotExist:
raise xmlrpc.client.Fault(
404, "Alias '%s' was not found." % name)
raise xmlrpc.client.Fault(404, "Alias '%s' was not found." % name)
dt.aliases.remove(alias_obj)
dt.save()
......@@ -26,20 +26,22 @@ from django.db import IntegrityError, transaction
from linaro_django_xmlrpc.models import ExposedV2API
from lava_scheduler_app.api import check_perm
from lava_scheduler_app.models import (
Device,
DeviceType,
Tag,
Worker
)
from lava_scheduler_app.models import Device, DeviceType, Tag, Worker
class SchedulerDevicesAPI(ExposedV2API):
@check_perm("lava_scheduler_app.add_device")
def add(self, hostname, type_name, worker_hostname,
user_name=None, group_name=None, public=True,
health=None, description=None):
def add(
self,
hostname,
type_name,
worker_hostname,
user_name=None,
group_name=None,
public=True,
health=None,
description=None,
):
"""
Name
----
......@@ -87,35 +89,38 @@ class SchedulerDevicesAPI(ExposedV2API):
if group_name is not None:
group = Group.objects.get(name=group_name)
except DeviceType.DoesNotExist:
raise xmlrpc.client.Fault(
404, "DeviceType '%s' was not found." % type_name)
raise xmlrpc.client.Fault(404, "DeviceType '%s' was not found." % type_name)
except Worker.DoesNotExist:
raise xmlrpc.client.Fault(
404, "Worker '%s' was not found." % worker_hostname)
404, "Worker '%s' was not found." % worker_hostname
)
except User.DoesNotExist:
raise xmlrpc.client.Fault(
404, "User '%s' was not found." % user_name)
raise xmlrpc.client.Fault(404, "User '%s' was not found." % user_name)
except Group.DoesNotExist:
raise xmlrpc.client.Fault(
404, "Group '%s' was not found." % group_name)
raise xmlrpc.client.Fault(404, "Group '%s' was not found." % group_name)
health_val = Device.HEALTH_UNKNOWN
try:
if health is not None:
health_val = Device.HEALTH_REVERSE[health]
except KeyError:
raise xmlrpc.client.Fault(
400, "Invalid health")
raise xmlrpc.client.Fault(400, "Invalid health")
try:
Device.objects.create(hostname=hostname, device_type=device_type,
user=user, group=group, is_public=public,
state=Device.STATE_IDLE, health=health_val,
worker_host=worker, description=description)
Device.objects.create(
hostname=hostname,
device_type=device_type,
user=user,
group=group,
is_public=public,
state=Device.STATE_IDLE,
health=health_val,
worker_host=worker,
description=description,
)
except (IntegrityError, ValidationError) as exc:
raise xmlrpc.client.Fault(
400, "Bad request: %s" % exc.message)
raise xmlrpc.client.Fault(400, "Bad request: %s" % exc.message)
def get_dictionary(self, hostname, render=False, context=None):
"""
......@@ -148,13 +153,12 @@ class SchedulerDevicesAPI(ExposedV2API):
try:
device = Device.objects.get(hostname=hostname)
except Device.DoesNotExist:
raise xmlrpc.client.Fault(
404, "Device '%s' was not found." % hostname)
raise xmlrpc.client.Fault(404, "Device '%s' was not found." % hostname)
if not device.is_visible_to(self.user):
raise xmlrpc.client.Fault(
403, "Device '%s' not available to user '%s'." %
(hostname, self.user))
403, "Device '%s' not available to user '%s'." % (hostname, self.user)
)
job_ctx = None
if context is not None:
......@@ -162,14 +166,17 @@ class SchedulerDevicesAPI(ExposedV2API):
job_ctx = yaml.safe_load(context)
except yaml.YAMLError as exc:
raise xmlrpc.client.Fault(
400, "Job Context '%s' is not valid: %s" % (context, str(exc)))
400, "Job Context '%s' is not valid: %s" % (context, str(exc))
)
config = device.load_configuration(job_ctx=job_ctx,
output_format="raw" if not render else "yaml")
config = device.load_configuration(
job_ctx=job_ctx, output_format="raw" if not render else "yaml"
)
if config is None:
raise xmlrpc.client.Fault(
404, "Device '%s' does not have a configuration" % hostname)
return xmlrpc.client.Binary(config.encode('utf-8'))
404, "Device '%s' does not have a configuration" % hostname
)
return xmlrpc.client.Binary(config.encode("utf-8"))
@check_perm("lava_scheduler_app.change_device")
def set_dictionary(self, hostname, dictionary):
......@@ -197,8 +204,7 @@ class SchedulerDevicesAPI(ExposedV2API):
try:
device = Device.objects.get(hostname=hostname)
except Device.DoesNotExist:
raise xmlrpc.client.Fault(
404, "Device '%s' was not found." % hostname)
raise xmlrpc.client.Fault(404, "Device '%s' was not found." % hostname)
return device.save_configuration(dictionary)
......@@ -235,12 +241,14 @@ class SchedulerDevicesAPI(ExposedV2API):
for device in devices:
if device.is_visible_to(self.user):
current_job = device.current_job()
device_dict = {"hostname": device.hostname,
"type": device.device_type.name,
"health": device.get_health_display(),
"state": device.get_state_display(),
"current_job": current_job.pk if current_job else None,
"pipeline": True}
device_dict = {
"hostname": device.hostname,
"type": device.device_type.name,
"health": device.get_health_display(),
"state": device.get_state_display(),
"current_job": current_job.pk if current_job else None,
"pipeline": True,
}
ret.append(device_dict)
return ret
......@@ -267,39 +275,46 @@ class SchedulerDevicesAPI(ExposedV2API):
try:
device = Device.objects.get(hostname=hostname)
except Device.DoesNotExist:
raise xmlrpc.client.Fault(
404, "Device '%s' was not found." % hostname
)
raise xmlrpc.client.Fault(404, "Device '%s' was not found." % hostname)
if not device.is_visible_to(self.user):
raise xmlrpc.client.Fault(
403, "Device '%s' not available to user '%s'." %
(hostname, self.user)
403, "Device '%s' not available to user '%s'." % (hostname, self.user)
)
current_job = device.current_job()
device_dict = {"hostname": device.hostname,
"device_type": device.device_type.name,
"health": device.get_health_display(),
"state": device.get_state_display(),
"health_job": bool(device.get_health_check()),
"description": device.description,
"public": device.is_public,
"pipeline": True,
"has_device_dict": bool(device.load_configuration(output_format="raw")),
"worker": None,
"user": device.user.username if device.user else None,
"group": device.group.name if device.group else None,
"current_job": current_job.pk if current_job else None,
"tags": [t.name for t in device.tags.all().order_by("name")]}
device_dict = {
"hostname": device.hostname,
"device_type": device.device_type.name,
"health": device.get_health_display(),
"state": device.get_state_display(),
"health_job": bool(device.get_health_check()),
"description": device.description,
"public": device.is_public,
"pipeline": True,
"has_device_dict": bool(device.load_configuration(output_format="raw")),
"worker": None,
"user": device.user.username if device.user else None,
"group": device.group.name if device.group else None,
"current_job": current_job.pk if current_job else None,
"tags": [t.name for t in device.tags.all().order_by("name")],
}
if device.worker_host is not None:
device_dict["worker"] = device.worker_host.hostname
return device_dict
@check_perm("lava_scheduler_app.change_device")
def update(self, hostname, worker_hostname=None, user_name=None,
group_name=None, public=True, health=None, description=None):
def update(
self,
hostname,
worker_hostname=None,
user_name=None,
group_name=None,
public=True,
health=None,
description=None,
):
"""
Name
----
......@@ -341,10 +356,13 @@ class SchedulerDevicesAPI(ExposedV2API):
if worker_hostname is not None:
try:
device.worker_host = Worker.objects.get(hostname=worker_hostname)
device.worker_host = Worker.objects.get(
hostname=worker_hostname
)
except Worker.DoesNotExist:
raise xmlrpc.client.Fault(
404, "Unable to find worker '%s'" % worker_hostname)
404, "Unable to find worker '%s'" % worker_hostname
)