blob: ac5c79900ba624c1d97c5e2e44595a336d13c0e0 [file] [log] [blame]
Fang Dengaf30e7c2014-11-15 13:57:03 -08001# 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
Fang Deng9d548742015-02-03 11:35:02 -080034 def __init__(self, test, extra_kwargs={}):
Fang Dengaf30e7c2014-11-15 13:57:03 -080035 """Init method.
36
37 @param test: String, the test to run, e.g. dummy_PassServer
Fang Deng9d548742015-02-03 11:35:02 -080038 @param extra_kargs: A dictionary, extra keyval-based args
39 that will be passed when execute the test.
Fang Dengaf30e7c2014-11-15 13:57:03 -080040 """
41 self.test = test
Fang Deng9d548742015-02-03 11:35:02 -080042 self.extra_kwargs = extra_kwargs
Fang Dengaf30e7c2014-11-15 13:57:03 -080043
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 Deng9d548742015-02-03 11:35:02 -080055 kwargs.update(self.extra_kwargs)
Fang Dengaf30e7c2014-11-15 13:57:03 -080056 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