aboutsummaryrefslogtreecommitdiff
path: root/server/cros/provision_actionables.py
blob: 01a6b04dbb27695bba569092fc5bc1b6527cc30c (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
# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""This file defines tasks to be executed in provision actions."""

import abc
import logging

import common


class BaseActionable(object):
    """Base class of an actionable item."""

    @abc.abstractmethod
    def execute(self, job, host, *args, **kwargs):
        """Execute the action item.

        @param job: A job object from a control file.
        @param host: A host to run this action against.
        @param args: arguments to passed to the test.
        @param kwargs: keyword arguments to passed to the test.

        @returns True if succeeds, False otherwise,
                 subclass should override this method.
        """
        raise NotImplementedError('Subclass should override execute.')


class TestActionable(BaseActionable):
    """A test to be executed as an action"""

    def __init__(self, test, extra_kwargs={}):
        """Init method.

        @param test: String, the test to run, e.g. stub_ServerToClientPass
        @param extra_kargs: A dictionary, extra keyval-based args
                            that will be passed when execute the test.
        """
        self.test = test
        self.extra_kwargs = extra_kwargs


    def execute(self, job, host, *args, **kwargs):
        """Execute the action item.

        @param job: A job object from a control file.
        @param host: A host to run this action against.
        @param args: arguments to passed to the test.
        @param kwargs: keyword arguments to passed to the test.

        @returns True if succeeds, False otherwise.
        """
        kwargs.update(self.extra_kwargs)
        return job.run_test(self.test, host=host, *args, **kwargs)


class RebootActionable(BaseActionable):
    """Reboot action."""

    def execute(self, job, host, *args, **kwargs):
        """Execute the action item.

        @param job: A job object from a control file.
        @param host: A host to run this action against.
        @param args: arguments to passed to the test.
        @param kwargs: keyword arguments to passed to the test.

        @returns True if succeeds.
        """
        logging.error('Executing RebootActionable ... ')
        host.reboot()
        logging.error('RebootActionable execution succeeds. ')
        return True