aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/NginxUtil.py
blob: a19ac372919c9963a59dda2e7ff2ed19e1992924 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright (c) 2021 Intel and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""NGINX Utilities Library."""

from resources.libraries.python.OptionString import OptionString
from resources.libraries.python.ssh import exec_cmd_no_error
from resources.libraries.python.topology import NodeType
from resources.libraries.python.DUTSetup import DUTSetup


class NginxUtil:
    """Utilities for NGINX."""

    @staticmethod
    def get_cmd_options(**kwargs):
        """Create parameters options.

        :param kwargs: Dict of cmd parameters.
        :type kwargs: dict
        :returns: cmd parameters.
        :rtype: OptionString
        """
        cmd_options = OptionString()
        nginx_path = kwargs.get(u"path", u"/usr/local/nginx")
        cmd_options.add(nginx_path)
        options = OptionString(prefix=u"-")
        # Show Nginx Version
        options.add(u"v")
        # Verify Configuration
        options.add(u"t")
        # Send signal to a master process: stop, quit, reopen.
        options.add_with_value_from_dict(
            u"s", u"signal", kwargs
        )
        # Set prefix path (default: /usr/local/nginx/).
        options.add_with_value_from_dict(
            u"p", u"prefix", kwargs
        )
        # Set configuration file (default: conf/nginx.conf).
        options.add_with_value_from_dict(
            u"c", u"filename", kwargs
        )
        # Set global directives out of configuration file
        options.add_with_value_from_dict(
            u"g", u"directives", kwargs
        )
        cmd_options.extend(options)
        return cmd_options

    @staticmethod
    def nginx_cmd_stop(node, path):
        """Stop NGINX cmd app on node.
        :param node: Topology node.
        :param path: Nginx install path.
        :type node: dict
        :type path: str
        :returns: nothing
        """
        cmd_options = NginxUtil.get_cmd_options(path=path, signal=u"stop")

        exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True,
                          message=u"Nginx stop failed!")

    @staticmethod
    def nginx_cmd_start(node, path, filename):
        """Start NGINX cmd app on node.
        :param node: Topology node.
        :param path: Nginx install path.
        :param filename: Nginx conf name.
        :type node: dict
        :type path: str
        :type filename: str

        :returns: nothing
        """
        cmd_options = NginxUtil.get_cmd_options(path=path,
                                                filename=filename)

        exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True,
                          message=u"Nginx start failed!")

    @staticmethod
    def nginx_config_verify(node, path):
        """Start NGINX cmd app on node.
        :param node: Topology node.
        :param path: Nginx install path.
        :type node: dict
        :type path: str
        :returns: nothing
        """
        cmd_options = NginxUtil.get_cmd_options(path=path)
        exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True,
                          message=u"Nginx Config failed!")

    @staticmethod
    def taskset_nginx_pid_to_idle_cores(node, cpu_idle_list):
        """Set idle cpus to NGINX pid on node.

        :param node: Topology node.
        :param cpu_idle_list: Idle Cpus.
        :type node: dict
        :type cpu_idle_list: list
        :returns: nothing
        """
        if node[u"type"] != NodeType.DUT:
            raise RuntimeError(u'Node type is not a DUT!')
        pids = DUTSetup.get_pid(node, u"nginx")
        for index, pid in enumerate(pids):
            cmd = f"taskset -pc {cpu_idle_list[index]} {pid}"
            exec_cmd_no_error(
                node, cmd, sudo=True, timeout=180,
                message=u"taskset cores to nginx pid failed!"
            )