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 ##############################################################################
11 from baseattacker import BaseAttacker
12 import yardstick.ssh as ssh
13 from yardstick.benchmark.scenarios.availability import util
15 LOG = logging.getLogger(__name__)
18 class GeneralAttacker(BaseAttacker):
20 __attacker_type__ = 'general-attacker'
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 key_filename = host.get("key_filename", "~/.ssh/id_rsa")
29 self.connection = ssh.SSH(user, ip, key_filename=key_filename)
30 self.connection.wait(timeout=600)
31 LOG.debug("ssh host success!")
33 self.key = self._config['key']
34 self.attack_key = self._config['attack_key']
36 if "action_parameter" in self._config:
37 actionParameter = self._config['action_parameter']
38 str = util.buildshellparams(actionParameter)
39 LOG.debug("inject parameter is: {0}".format(actionParameter))
40 LOG.debug("inject parameter values are: {0}"
41 .format(actionParameter.values()))
42 l = list(item for item in actionParameter.values())
43 self.action_param = str.format(*l)
45 if "rollback_parameter" in self._config:
46 rollbackParameter = self._config['rollback_parameter']
47 str = util.buildshellparams(rollbackParameter)
48 LOG.debug("recover parameter is: {0}".format(rollbackParameter))
49 LOG.debug("recover parameter values are: {0}".
50 format(rollbackParameter.values()))
51 l = list(item for item in rollbackParameter.values())
52 self.rollback_param = str.format(*l)
54 self.fault_cfg = BaseAttacker.attacker_cfgs.get(self.attack_key)
55 self.inject_script = self.get_script_fullpath(
56 self.fault_cfg['inject_script'])
57 self.recovery_script = self.get_script_fullpath(
58 self.fault_cfg['recovery_script'])
60 def inject_fault(self):
61 LOG.debug("{0} starting inject!".format(self.key))
62 LOG.debug("the inject_script path:{0}".format(self.inject_script))
64 if "action_parameter" in self._config:
65 LOG.debug("the shell command is: {0}".format(self.action_param))
66 exit_status, stdout, stderr = self.connection.execute(
68 stdin=open(self.inject_script, "r"))
70 exit_status, stdout, stderr = self.connection.execute(
72 stdin=open(self.inject_script, "r"))
74 LOG.debug("the inject_fault's exit status is: {0}".format(exit_status))
76 LOG.debug("success,the inject_fault's output is: {0}"
80 "the inject_fault's error, stdout:%s, stderr:%s" %
84 if "rollback_parameter" in self._config:
85 LOG.debug("the shell command is: {0}".format(self.rollback_param))
86 exit_status, stdout, stderr = self.connection.execute(
88 stdin=open(self.recovery_script, "r"))
90 exit_status, stdout, stderr = self.connection.execute(
92 stdin=open(self.recovery_script, "r"))