Line length: Fix recent merges
[csit.git] / resources / libraries / python / NginxUtil.py
1 # Copyright (c) 2021 Intel and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """NGINX Utilities Library."""
15
16 from resources.libraries.python.OptionString import OptionString
17 from resources.libraries.python.ssh import exec_cmd_no_error
18 from resources.libraries.python.topology import NodeType
19 from resources.libraries.python.DUTSetup import DUTSetup
20
21
22 class NginxUtil:
23     """Utilities for NGINX."""
24
25     @staticmethod
26     def get_cmd_options(**kwargs):
27         """Create parameters options.
28
29         :param kwargs: Dict of cmd parameters.
30         :type kwargs: dict
31         :returns: cmd parameters.
32         :rtype: OptionString
33         """
34         cmd_options = OptionString()
35         nginx_path = kwargs.get(u"path", u"/usr/local/nginx")
36         cmd_options.add(nginx_path)
37         options = OptionString(prefix=u"-")
38         # Show Nginx Version
39         options.add(u"v")
40         # Verify Configuration
41         options.add(u"t")
42         # Send signal to a master process: stop, quit, reopen.
43         options.add_with_value_from_dict(
44             u"s", u"signal", kwargs
45         )
46         # Set prefix path (default: /usr/local/nginx/).
47         options.add_with_value_from_dict(
48             u"p", u"prefix", kwargs
49         )
50         # Set configuration file (default: conf/nginx.conf).
51         options.add_with_value_from_dict(
52             u"c", u"filename", kwargs
53         )
54         # Set global directives out of configuration file
55         options.add_with_value_from_dict(
56             u"g", u"directives", kwargs
57         )
58         cmd_options.extend(options)
59         return cmd_options
60
61     @staticmethod
62     def nginx_cmd_stop(node, path):
63         """Stop NGINX cmd app on node.
64         :param node: Topology node.
65         :param path: Nginx install path.
66         :type node: dict
67         :type path: str
68         :returns: nothing
69         """
70         cmd_options = NginxUtil.get_cmd_options(path=path, signal=u"stop")
71
72         exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True,
73                           message=u"Nginx stop failed!")
74
75     @staticmethod
76     def nginx_cmd_start(node, path, filename):
77         """Start NGINX cmd app on node.
78         :param node: Topology node.
79         :param path: Nginx install path.
80         :param filename: Nginx conf name.
81         :type node: dict
82         :type path: str
83         :type filename: str
84
85         :returns: nothing
86         """
87         cmd_options = NginxUtil.get_cmd_options(path=path,
88                                                 filename=filename)
89
90         exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True,
91                           message=u"Nginx start failed!")
92
93     @staticmethod
94     def nginx_config_verify(node, path):
95         """Start NGINX cmd app on node.
96         :param node: Topology node.
97         :param path: Nginx install path.
98         :type node: dict
99         :type path: str
100         :returns: nothing
101         """
102         cmd_options = NginxUtil.get_cmd_options(path=path)
103         exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True,
104                           message=u"Nginx Config failed!")
105
106     @staticmethod
107     def taskset_nginx_pid_to_idle_cores(node, cpu_idle_list):
108         """Set idle cpus to NGINX pid on node.
109
110         :param node: Topology node.
111         :param cpu_idle_list: Idle Cpus.
112         :type node: dict
113         :type cpu_idle_list: list
114         :returns: nothing
115         """
116         if node[u"type"] != NodeType.DUT:
117             raise RuntimeError(u'Node type is not a DUT!')
118         pids = DUTSetup.get_pid(node, u"nginx")
119         for index, pid in enumerate(pids):
120             cmd = f"taskset -pc {cpu_idle_list[index]} {pid}"
121             exec_cmd_no_error(
122                 node, cmd, sudo=True, timeout=180,
123                 message=u"taskset cores to nginx pid failed!"
124             )