aboutsummaryrefslogtreecommitdiff
path: root/lava_dispatcher/actions/deploy/environment.py
blob: 94be9d9b756bc4d1d0f72135c484c14346e16e4f (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
# Copyright (C) 2015 Linaro Limited
#
# Author: Stevan Radakovic <stevan.radakovic@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 os
import yaml

from lava_dispatcher.action import Action
from lava_common.constants import LINE_SEPARATOR


class DeployDeviceEnvironment(Action):
    """
    Create environment found in job parameters 'env_dut' and set it in common_data.
    """

    name = "deploy-device-env"
    description = "deploy device environment"
    summary = "deploy device environment"

    def __init__(self):
        super().__init__()
        self.env = ""

    def validate(self):
        super().validate()
        if 'lava_test_shell_file' not in \
           self.parameters['deployment_data'].keys():
            self.errors = "Invalid deployment data - missing lava_test_shell_file"

        if 'env_dut' in self.job.parameters and self.job.parameters['env_dut']:
            # Check that the file is valid yaml
            try:
                yaml.load(self.job.parameters['env_dut'])
            except (TypeError, yaml.scanner.ScannerError) as exc:
                self.errors = exc
                return

            self.env = self.job.parameters['env_dut']
            environment = self._create_environment()

            self.set_namespace_data(
                action=self.name,
                label='environment',
                key='shell_file',
                value=self.parameters['deployment_data']['lava_test_shell_file']
            )

            self.set_namespace_data(
                action=self.name,
                label='environment',
                key='env_dict',
                value=environment
            )

        self.set_namespace_data(
            action=self.name,
            label='environment',
            key='line_separator',
            value=self.parameters['deployment_data'].get('line_separator', LINE_SEPARATOR)
        )

    def _create_environment(self):
        """Generate the env variables for the device."""
        conf = yaml.load(self.env) if self.env != '' else {}
        if conf.get("purge", False):
            environ = {}
        else:
            environ = dict(os.environ)

        # Remove some variables (that might not exist)
        for var in conf.get("removes", {}):
            try:
                del environ[var]
            except KeyError:
                pass

        # Override
        environ.update(conf.get("overrides", {}))
        return environ