Fang Deng | af30e7c | 2014-11-15 13:57:03 -0800 | [diff] [blame] | 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 | |
| 7 | import abc |
| 8 | import logging |
| 9 | |
| 10 | import common |
| 11 | |
| 12 | |
| 13 | class 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 | |
| 31 | class TestActionable(BaseActionable): |
| 32 | """A test to be executed as an action""" |
| 33 | |
Fang Deng | 9d54874 | 2015-02-03 11:35:02 -0800 | [diff] [blame] | 34 | def __init__(self, test, extra_kwargs={}): |
Fang Deng | af30e7c | 2014-11-15 13:57:03 -0800 | [diff] [blame] | 35 | """Init method. |
| 36 | |
| 37 | @param test: String, the test to run, e.g. dummy_PassServer |
Fang Deng | 9d54874 | 2015-02-03 11:35:02 -0800 | [diff] [blame] | 38 | @param extra_kargs: A dictionary, extra keyval-based args |
| 39 | that will be passed when execute the test. |
Fang Deng | af30e7c | 2014-11-15 13:57:03 -0800 | [diff] [blame] | 40 | """ |
| 41 | self.test = test |
Fang Deng | 9d54874 | 2015-02-03 11:35:02 -0800 | [diff] [blame] | 42 | self.extra_kwargs = extra_kwargs |
Fang Deng | af30e7c | 2014-11-15 13:57:03 -0800 | [diff] [blame] | 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 | """ |
Fang Deng | 9d54874 | 2015-02-03 11:35:02 -0800 | [diff] [blame] | 55 | kwargs.update(self.extra_kwargs) |
Fang Deng | af30e7c | 2014-11-15 13:57:03 -0800 | [diff] [blame] | 56 | return job.run_test(self.test, host=host, *args, **kwargs) |
| 57 | |
| 58 | |
| 59 | class 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 |