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