Merge "Bugfix: update Yardstick custom VM image name in user guide"
[yardstick.git] / yardstick / benchmark / scenarios / availability / attacker / attacker_general.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 import logging
10
11 from baseattacker import BaseAttacker
12 import yardstick.ssh as ssh
13 from yardstick.benchmark.scenarios.availability import util
14
15 LOG = logging.getLogger(__name__)
16
17
18 class GeneralAttacker(BaseAttacker):
19
20     __attacker_type__ = 'general-attacker'
21
22     def setup(self):
23         LOG.debug("config:%s context:%s", self._config, self._context)
24         host = self._context.get(self._config['host'], None)
25         ip = host.get("ip", None)
26         user = host.get("user", "root")
27         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
28         key_filename = host.get("key_filename", "~/.ssh/id_rsa")
29
30         self.connection = ssh.SSH(user, ip, key_filename=key_filename,
31                                   port=ssh_port)
32         self.connection.wait(timeout=600)
33         LOG.debug("ssh host success!")
34
35         self.key = self._config['key']
36         self.attack_key = self._config['attack_key']
37
38         if "action_parameter" in self._config:
39             actionParameter = self._config['action_parameter']
40             str = util.buildshellparams(actionParameter)
41             LOG.debug("inject parameter is: {0}".format(actionParameter))
42             LOG.debug("inject parameter values are: {0}"
43                       .format(actionParameter.values()))
44             l = list(item for item in actionParameter.values())
45             self.action_param = str.format(*l)
46
47         if "rollback_parameter" in self._config:
48             rollbackParameter = self._config['rollback_parameter']
49             str = util.buildshellparams(rollbackParameter)
50             LOG.debug("recover parameter is: {0}".format(rollbackParameter))
51             LOG.debug("recover parameter values are: {0}".
52                       format(rollbackParameter.values()))
53             l = list(item for item in rollbackParameter.values())
54             self.rollback_param = str.format(*l)
55
56         self.fault_cfg = BaseAttacker.attacker_cfgs.get(self.attack_key)
57         self.inject_script = self.get_script_fullpath(
58             self.fault_cfg['inject_script'])
59         self.recovery_script = self.get_script_fullpath(
60             self.fault_cfg['recovery_script'])
61
62     def inject_fault(self):
63         LOG.debug("{0} starting inject!".format(self.key))
64         LOG.debug("the inject_script path:{0}".format(self.inject_script))
65
66         if "action_parameter" in self._config:
67             LOG.debug("the shell command is: {0}".format(self.action_param))
68             with open(self.inject_script, "r") as stdin_file:
69                 exit_status, stdout, stderr = self.connection.execute(
70                     self.action_param,
71                     stdin=stdin_file)
72         else:
73             with open(self.inject_script, "r") as stdin_file:
74                 exit_status, stdout, stderr = self.connection.execute(
75                     "/bin/bash -s ",
76                     stdin=stdin_file)
77
78         LOG.debug("the inject_fault's exit status is: {0}".format(exit_status))
79         if exit_status == 0:
80             LOG.debug("success,the inject_fault's output is: {0}"
81                       .format(stdout))
82         else:
83             LOG.error(
84                 "the inject_fault's error, stdout:%s, stderr:%s",
85                 stdout, stderr)
86
87     def recover(self):
88         if "rollback_parameter" in self._config:
89             LOG.debug("the shell command is: {0}".format(self.rollback_param))
90             with open(self.recovery_script, "r") as stdin_file:
91                 exit_status, stdout, stderr = self.connection.execute(
92                     self.rollback_param,
93                     stdin=stdin_file)
94         else:
95             with open(self.recovery_script, "r") as stdin_file:
96                 exit_status, stdout, stderr = self.connection.execute(
97                     "/bin/bash -s ",
98                     stdin=stdin_file)