Merge "Fix adding right deb repo based on the distro we are running on"
[yardstick.git] / yardstick / benchmark / scenarios / availability / director.py
1 ##############################################################################
2 # Copyright (c) 2016 Juan Qiu and others
3 # juan_ qiu@tongji.edu.cn
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
10 import logging
11
12 from yardstick.benchmark.scenarios.availability.monitor import basemonitor
13 from yardstick.benchmark.scenarios.availability.attacker import baseattacker
14 from yardstick.benchmark.scenarios.availability.operation import baseoperation
15 from yardstick.benchmark.scenarios.availability.result_checker \
16     import baseresultchecker
17 from yardstick.benchmark.scenarios.availability import ActionType
18 from yardstick.benchmark.scenarios.availability import actionplayers
19 from yardstick.benchmark.scenarios.availability import actionrollbackers
20
21 LOG = logging.getLogger(__name__)
22
23
24 class Director(object):
25     """
26     Director is used to direct a test scenaio
27     including the creation of action players, test result verification
28     and rollback of actions.
29     """
30
31     def __init__(self, scenario_cfg, context_cfg):
32
33         # A stack store Rollbacker that will be called after
34         # all actionplayers finish.
35         self.executionSteps = []
36         self.data = {}
37
38         self.scenario_cfg = scenario_cfg
39         self.context_cfg = context_cfg
40         nodes = self.context_cfg.get("nodes", None)
41         # setup attackers
42         if "attackers" in self.scenario_cfg["options"]:
43             LOG.debug("start init attackers...")
44             attacker_cfgs = self.scenario_cfg["options"]["attackers"]
45             self.attackerMgr = baseattacker.AttackerMgr()
46             self.data = self.attackerMgr.init_attackers(attacker_cfgs,
47                                                         nodes)
48
49         # setup monitors
50         if "monitors" in self.scenario_cfg["options"]:
51             LOG.debug("start init monitors...")
52             monitor_cfgs = self.scenario_cfg["options"]["monitors"]
53             self.monitorMgr = basemonitor.MonitorMgr(self.data)
54             self.monitorMgr.init_monitors(monitor_cfgs, nodes)
55         # setup operations
56         if "operations" in self.scenario_cfg["options"]:
57             LOG.debug("start init operations...")
58             operation_cfgs = self.scenario_cfg["options"]["operations"]
59             self.operationMgr = baseoperation.OperationMgr()
60             self.operationMgr.init_operations(operation_cfgs, nodes)
61         # setup result checker
62         if "resultCheckers" in self.scenario_cfg["options"]:
63             LOG.debug("start init resultCheckers...")
64             result_check_cfgs = self.scenario_cfg["options"]["resultCheckers"]
65             self.resultCheckerMgr = baseresultchecker.ResultCheckerMgr()
66             self.resultCheckerMgr.init_ResultChecker(result_check_cfgs, nodes)
67
68     def createActionPlayer(self, type, key, intermediate_variables=None):
69         if intermediate_variables is None:
70             intermediate_variables = {}
71         LOG.debug(
72             "the type of current action is %s, the key is %s", type, key)
73         if type == ActionType.ATTACKER:
74             return actionplayers.AttackerPlayer(self.attackerMgr[key])
75         if type == ActionType.MONITOR:
76             return actionplayers.MonitorPlayer(self.monitorMgr[key])
77         if type == ActionType.RESULTCHECKER:
78             return actionplayers.ResultCheckerPlayer(
79                 self.resultCheckerMgr[key])
80         if type == ActionType.OPERATION:
81             return actionplayers.OperationPlayer(self.operationMgr[key],
82                                                  intermediate_variables)
83         LOG.debug("something run when creatactionplayer")
84
85     def createActionRollbacker(self, type, key):
86         LOG.debug(
87             "the type of current action is %s, the key is %s", type, key)
88         if type == ActionType.ATTACKER:
89             return actionrollbackers.AttackerRollbacker(self.attackerMgr[key])
90         if type == ActionType.OPERATION:
91             return actionrollbackers.OperationRollbacker(
92                 self.operationMgr[key])
93         LOG.debug("no rollbacker created for %s", key)
94
95     def verify(self):
96         result = True
97         if hasattr(self, 'monitorMgr'):
98             result &= self.monitorMgr.verify_SLA()
99         if hasattr(self, 'resultCheckerMgr'):
100             result &= self.resultCheckerMgr.verify()
101         if result:
102             LOG.debug("monitors are passed")
103         return result
104
105     def stopMonitors(self):
106         if "monitors" in self.scenario_cfg["options"]:
107             self.monitorMgr.wait_monitors()
108
109     def knockoff(self):
110         LOG.debug("knock off ....")
111         while self.executionSteps:
112             singleStep = self.executionSteps.pop()
113             singleStep.rollback()