aboutsummaryrefslogtreecommitdiff
path: root/lava_dispatcher/device.py
blob: 5ae4fcb686d2f47680ccdfdc65c9f8bf08523046 (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
# Copyright (C) 2014 Linaro Limited
#
# Author: Neil Williams <neil.williams@linaro.org>
#         Remi Duraffort <remi.duraffort@linaro.org>
#
# This file is part of LAVA Dispatcher.
#
# LAVA Dispatcher is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LAVA Dispatcher 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 General Public License
# along
# with this program; if not, see <http://www.gnu.org/licenses>.

import yaml

from lava_common.exceptions import ConfigurationError


class PipelineDevice(dict):
    """
    Dictionary Device class which accepts data rather than a filename.
    This allows the scheduler to use the same class without needing to write
    out YAML files from database content.
    """

    def __init__(self, config):
        super().__init__()
        self.update(config)

    def check_config(self, job):
        """
        Validates the combination of the job and the device
        *before* the Deployment actions are initialised.
        """
        raise NotImplementedError("check_config")

    @property
    def hard_reset_command(self):
        return self.get('commands', {}).get('hard_reset', '')

    @property
    def soft_reset_command(self):
        return self.get('commands', {}).get('soft_reset', '')

    @property
    def pre_os_command(self):
        return self.get('commands', {}).get('pre_os_command')

    @property
    def pre_power_command(self):
        return self.get('commands', {}).get('pre_power_command')

    @property
    def power_command(self):
        return self.get('commands', {}).get('power_on', '')

    @property
    def connect_command(self):
        if 'connect' in self['commands']:
            return self['commands']['connect']
        elif 'connections' in self['commands']:
            for hardware, value in self['commands']['connections'].items():
                if 'connect' not in value:
                    return ''
                if 'tags' in value and 'primary' in value['tags']:
                    return value['connect']
        return ''

    def get_constant(self, const, prefix=None, missing_ok=False):
        if 'constants' not in self:
            raise ConfigurationError("constants section not present in the device config.")
        if prefix:
            if not self['constants'].get(prefix, {}).get(const, {}):
                if missing_ok:
                    return None
                raise ConfigurationError("Constant %s,%s does not exist in the device config 'constants' section." % (prefix, const))
            else:
                return self['constants'][prefix][const]
        if const not in self['constants']:
            if missing_ok:
                return None
            raise ConfigurationError("Constant %s does not exist in the device config 'constants' section." % const)
        return self['constants'][const]


class NewDevice(PipelineDevice):
    """
    YAML based PipelineDevice class with clearer support for the pipeline overrides
    and deployment types. Simple change of init whilst allowing the scheduler and
    the dispatcher to share the same functionality.
    """

    def __init__(self, target):
        super().__init__({})
        # Parse the yaml configuration
        try:
            if isinstance(target, str):
                with open(target) as f_in:
                    data = f_in.read()
                data = yaml.load(data)
            elif isinstance(target, dict):
                data = target
            else:
                data = target.read()
                data = yaml.load(data)
            if data is None:
                raise ConfigurationError("Missing device configuration")
            self.update(data)
        except yaml.parser.ParserError:
            raise ConfigurationError("%s could not be parsed" % target)

        self.setdefault('power_state', 'off')  # assume power is off at start of job

    def check_config(self, job):
        """
        Validates the combination of the job and the device
        *before* the Deployment actions are initialised.
        """
        raise NotImplementedError("check_config")