aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/availability/operation/operation_general.py
blob: 6ec8601afa150504b6d54e0bffcc5bb0e5d97ce9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
##############################################################################
# Copyright (c) 2016 Juan Qiu and others
# juan_ qiu@tongji.edu.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from __future__ import absolute_import
import logging

from yardstick.benchmark.scenarios.availability.operation.baseoperation \
    import \
    BaseOperation

import yardstick.ssh as ssh
from yardstick.benchmark.scenarios.availability.util \
    import buildshellparams, execute_shell_command

LOG = logging.getLogger(__name__)


class GeneralOperaion(BaseOperation):

    __operation__type__ = "general-operation"

    def setup(self):
        LOG.debug("config:%s context:%s", self._config, self._context)
        host = self._context.get(self._config.get('host', None), None)

        self.connection = None
        if host:
            self.connection = ssh.SSH.from_node(
                host, defaults={"user": "root"})
            self.connection.wait(timeout=600)
            LOG.debug("ssh host success!")

        self.key = self._config['key']
        self.operation_key = self._config['operation_key']

        if "action_parameter" in self._config:
            actionParameter = self._config['action_parameter']
            str = buildshellparams(
                actionParameter, True if self.connection else False)
            l = list(item for item in actionParameter.values())
            self.action_param = str.format(*l)

        if "rollback_parameter" in self._config:
            rollbackParameter = self._config['rollback_parameter']
            str = buildshellparams(
                rollbackParameter, True if self.connection else False)
            l = list(item for item in rollbackParameter.values())
            self.rollback_param = str.format(*l)

        self.operation_cfgs = BaseOperation.operation_cfgs.get(
            self.operation_key)
        self.action_script = self.get_script_fullpath(
            self.operation_cfgs['action_script'])
        self.rollback_script = self.get_script_fullpath(
            self.operation_cfgs['rollback_script'])

    def run(self):
        if "action_parameter" in self._config:
            if self.connection:
                with open(self.action_script, "r") as stdin_file:
                    exit_status, stdout, stderr = self.connection.execute(
                        self.action_param,
                        stdin=stdin_file)
            else:
                exit_status, stdout = \
                    execute_shell_command(
                        "/bin/bash {0} {1}".format(
                            self.action_script, self.action_param))
        else:
            if self.connection:
                with open(self.action_script, "r") as stdin_file:
                    exit_status, stdout, stderr = self.connection.execute(
                        "/bin/sh -s ",
                        stdin=stdin_file)
            else:
                exit_status, stdout = execute_shell_command(
                    "/bin/bash {0}".format(self.action_script))

        if exit_status == 0:
            LOG.debug("success,the operation's output is: %s", stdout)
        else:
            LOG.error(
                "the operation's error, stdout:%s, stderr:%s",
                stdout, stderr)

    def rollback(self):
        if "rollback_parameter" in self._config:
            if self.connection:
                with open(self.rollback_script, "r") as stdin_file:
                    exit_status, stdout, stderr = self.connection.execute(
                        self.rollback_param,
                        stdin=stdin_file)
            else:
                exit_status, stdout = \
                    execute_shell_command(
                        "/bin/bash {0} {1}".format(
                            self.rollback_script, self.rollback_param))
        else:
            if self.connection:
                with open(self.rollback_script, "r") as stdin_file:
                    exit_status, stdout, stderr = self.connection.execute(
                        "/bin/sh -s ",
                        stdin=stdin_file)
            else:
                exit_status, stdout = execute_shell_command(
                    "/bin/bash {0}".format(self.rollback_script))