• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""This file defines tasks to be executed in provision actions."""
6
7import abc
8import logging
9
10import common
11
12
13class BaseActionable(object):
14    """Base class of an actionable item."""
15
16    @abc.abstractmethod
17    def execute(self, job, host, *args, **kwargs):
18        """Execute the action item.
19
20        @param job: A job object from a control file.
21        @param host: A host to run this action against.
22        @param args: arguments to passed to the test.
23        @param kwargs: keyword arguments to passed to the test.
24
25        @returns True if succeeds, False otherwise,
26                 subclass should override this method.
27        """
28        raise NotImplementedError('Subclass should override execute.')
29
30
31class TestActionable(BaseActionable):
32    """A test to be executed as an action"""
33
34    def __init__(self, test, extra_kwargs={}):
35        """Init method.
36
37        @param test: String, the test to run, e.g. dummy_PassServer
38        @param extra_kargs: A dictionary, extra keyval-based args
39                            that will be passed when execute the test.
40        """
41        self.test = test
42        self.extra_kwargs = extra_kwargs
43
44
45    def execute(self, job, host, *args, **kwargs):
46        """Execute the action item.
47
48        @param job: A job object from a control file.
49        @param host: A host to run this action against.
50        @param args: arguments to passed to the test.
51        @param kwargs: keyword arguments to passed to the test.
52
53        @returns True if succeeds, False otherwise.
54        """
55        kwargs.update(self.extra_kwargs)
56        return job.run_test(self.test, host=host, *args, **kwargs)
57
58
59class RebootActionable(BaseActionable):
60    """Reboot action."""
61
62    def execute(self, job, host, *args, **kwargs):
63        """Execute the action item.
64
65        @param job: A job object from a control file.
66        @param host: A host to run this action against.
67        @param args: arguments to passed to the test.
68        @param kwargs: keyword arguments to passed to the test.
69
70        @returns True if succeeds.
71        """
72        logging.error('Executing RebootActionable ... ')
73        host.reboot()
74        logging.error('RebootActionable execution succeeds. ')
75        return True
76