aboutsummaryrefslogtreecommitdiff
path: root/lava_scheduler_app/api/devices.py
blob: bb6c6fb5d00115b6a7fe287a7e5e2688ca612fee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# Copyright (C) 2017 Linaro Limited
#
# Author: Remi Duraffort <remi.duraffort@linaro.org>
#
# This file is part of Lava Server.
#
# Lava Server is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License version 3
# as published by the Free Software Foundation
#
# Lava Server is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Lava Server.  If not, see <http://www.gnu.org/licenses/>.

import yaml
import xmlrpc.client

from django.contrib.auth.models import User, Group
from django.core.exceptions import ValidationError
from django.db import IntegrityError, transaction

from linaro_django_xmlrpc.models import ExposedV2API
from lava_scheduler_app.api import check_superuser
from lava_scheduler_app.models import (
    Device,
    DeviceType,
    Tag,
    Worker
)


class SchedulerDevicesAPI(ExposedV2API):

    @check_superuser
    def add(self, hostname, type_name, worker_hostname,
            user_name=None, group_name=None, public=True,
            health=None, description=None):
        """
        Name
        ----
        `scheduler.devices.add` (`hostname`, `type_name`, `worker_hostname`,
                                 `user_name=None`, `group_name=None`,
                                 `public=True`, `health=None`,
                                 `description=None`)

        Description
        -----------
        [superuser only]
        Add a new device to the database, to support V2.

        Each device will also need a device dictionary.

        Arguments
        ---------
        `hostname`: string
          Hostname of the device
        `type_name`: string
          Type of the new device
        `worker_hostname`: string
          Worker hostname
        `user_name`: string
          Device owner, None by default
        `group_name`: string
          Device group owner, None by default
        `public`: boolean
          Is the device public?
        `health`: string
          Device health, among ["GOOD", "UNKNOWN", "LOOPING", "BAD", "MAINTENANCE", "RETIRED"]
        `description`: string
          Device description

        Return value
        ------------
        None
        """
        user = group = None
        try:
            device_type = DeviceType.objects.get(name=type_name)
            worker = Worker.objects.get(hostname=worker_hostname)
            if user_name is not None:
                user = User.objects.get(username=user_name)
            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)
        except Worker.DoesNotExist:
            raise xmlrpc.client.Fault(
                404, "Worker '%s' was not found." % worker_hostname)
        except User.DoesNotExist:
            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)

        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")

        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)

        except (IntegrityError, ValidationError) as exc:
            raise xmlrpc.client.Fault(
                400, "Bad request: %s" % exc.message)

    def get_dictionary(self, hostname, render=False, context=None):
        """
        Name
        ----
        `scheduler.devices.get_dictionary` (`hostname`, `render=False`, `context=None`)

        Support for the context argument is new in api_version 2
        see system.api_version().

        Description
        -----------
        Return the device configuration

        Arguments
        ---------
        `hostname`: string
          Hostname of the device
        `render`: bool
          Render the device configuration. By default, return the dictionary
        `context`: string
          Some device templates need a context specific when processing the
          device-type template. This can be specified as a YAML string.
          New in api_version 2 - see system.api_version()

        Return value
        ------------
        The device dictionary
        """
        try:
            device = Device.objects.get(hostname=hostname)
        except Device.DoesNotExist:
            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))

        job_ctx = None
        if context is not None:
            try:
                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)))

        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'))

    @check_superuser
    def set_dictionary(self, hostname, dictionary):
        """
        Name
        ----
        `scheduler.devices.set_dictionary` (`hostname`, `dictionary`)

        Description
        -----------
        [superuser only]
        Set the device dictionary

        Arguments
        ---------
        `hostname`: string
          Hostname of the device
        `dictionary`: string
          The device dictionary as a jinja2 template

        Return value
        ------------
        True if the dictionary was saved to file, False otherwise.
        """
        try:
            device = Device.objects.get(hostname=hostname)
        except Device.DoesNotExist:
            raise xmlrpc.client.Fault(
                404, "Device '%s' was not found." % hostname)

        return device.save_configuration(dictionary)

    def list(self, show_all=False, offline_info=False):
        """
        Name
        ----
        `scheduler.devices.list` (`show_all=False`, `offline_info=False`)

        Description
        -----------
        List available devices with their state and type information.

        Arguments
        ---------
        `show_all`: boolean
          Show all devices, including retired
        `offline_info`: boolean
          Add date from which each of the returned devices is offline (if the
          device is offline) and the user who put the device offline (if the
          device is offline) to the returned dictionary.

        Return value
        ------------
        This function returns an XML-RPC array in which each item is a
        dictionary with device information
        """
        devices = Device.objects.all()
        if not show_all:
            devices = Device.objects.exclude(health=Device.HEALTH_RETIRED)
        devices = devices.order_by("hostname")

        ret = []
        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}
                ret.append(device_dict)

        return ret

    def show(self, hostname):
        """
        Name
        ----
        `scheduler.devices.show` (`hostname`)

        Description
        -----------
        Show some details about the given device.

        Arguments
        ---------
        `hostname`: string
          Hostname of the device

        Return value
        ------------
        This function returns an XML-RPC dictionary with device details
        """
        try:
            device = Device.objects.get(hostname=hostname)
        except Device.DoesNotExist:
            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)
            )

        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")]}
        if device.worker_host is not None:
            device_dict["worker"] = device.worker_host.hostname

        return device_dict

    @check_superuser
    def update(self, hostname, worker_hostname=None, user_name=None,
               group_name=None, public=True, health=None, description=None):
        """
        Name
        ----
        `scheduler.devices.update` (`hostname`, `worker_hostname=None`,
                                    `user_name=None`, `group_name=None`,
                                    `public=True`, `health=None`,
                                    `description=None`)

        Description
        -----------
        [superuser only]
        Update device parameters. Only the non-None values will be updated.
        Owner and group are always updated at the same time.

        Arguments
        ---------
        `hostname`: string
          Hostname of the device
        `worker_hostname`: string
          Worker hostname
        `user_name`: string
          Device owner
        `group_name`: string
          Device group owner
        `public`: boolean
          Is the device public?
        `health`: string
          Device health, among ["GOOD", "UNKNOWN", "LOOPING", "BAD", "MAINTENANCE", "RETIRED"]
        `description`: string
          Device description

        Return value
        ------------
        None
        """
        try:
            with transaction.atomic():
                device = Device.objects.get(hostname=hostname)

                if worker_hostname is not None:
                    try:
                        device.worker_host = Worker.objects.get(hostname=worker_hostname)
                    except Worker.DoesNotExist:
                        raise xmlrpc.client.Fault(
                            404, "Unable to find worker '%s'" % worker_hostname)

                user = group = None
                try:
                    if user_name is not None:
                        user = User.objects.get(username=user_name)
                    if group_name is not None:
                        group = Group.objects.get(name=group_name)
                except User.DoesNotExist:
                    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)

                if user is not None or group is not None:
                    device.user = user
                    device.group = group

                if public is not None:
                    device.is_public = public

                try:
                    if health is not None:
                        device.health = Device.HEALTH_REVERSE[health]
                except KeyError:
                    raise xmlrpc.client.Fault(
                        400, "Health '%s' is invalid" % health)

                if description is not None:
                    device.description = description

                device.save()
        except Device.DoesNotExist:
            raise xmlrpc.client.Fault(
                404, "Device '%s' was not found." % hostname
            )
        except (IntegrityError, ValidationError) as exc:
            raise xmlrpc.client.Fault(
                400, "Bad request: %s" % exc.message)


class SchedulerDevicesTagsAPI(ExposedV2API):

    @check_superuser
    def add(self, hostname, name):
        """
        Name
        ----
        `scheduler.devices.tags.add` (`hostname`, `name`)

        Description
        -----------
        [superuser only]
        Add a device tag to the specific device

        Arguments
        ---------
        `hostname`: string
          Device hostname
        `name`: string
          Tag name to add

        Return value
        ------------
        None
        """
        try:
            device = Device.objects.get(hostname=hostname)
        except Device.DoesNotExist:
            raise xmlrpc.client.Fault(
                404, "Device '%s' was not found." % hostname)

        tag, _ = Tag.objects.get_or_create(name=name)
        device.tags.add(tag)
        device.save()

    def list(self, hostname):
        """
        Name
        ----
        `scheduler.devices.tags.list` (`hostname`)

        Description
        -----------
        List device tags

        Arguments
        ---------
        `hostname`: string
          Device hostname

        Return value
        ------------
        This function returns an XML-RPC array of tag names
        """
        try:
            device = Device.objects.get(hostname=hostname)
        except Device.DoesNotExist:
            raise xmlrpc.client.Fault(
                404, "Device '%s' was not found." % hostname)

        return [t.name for t in device.tags.all()]

    @check_superuser
    def delete(self, hostname, name):
        """
        Name
        ----
        `scheduler.devices.tags.delete` (`hostname`, `name`)

        Description
        -----------
        [superuser only]
        Remove a device tag from the device

        Arguments
        ---------
        `hostname`: string
          Device hostname
        `name`: string
          Tag name to remove
        Return value
        ------------
        None
        """
        try:
            device = Device.objects.get(hostname=hostname)
        except Device.DoesNotExist:
            raise xmlrpc.client.Fault(
                404, "Device '%s' was not found." % hostname)

        try:
            tag = Tag.objects.get(name=name)
        except Tag.DoesNotExist:
            raise xmlrpc.client.Fault(
                404, "Tag '%s' was not found." % name)

        device.tags.remove(tag)
        device.save()