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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """NGINX Utilities Library."""
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
23 """Utilities for NGINX."""
26 def get_cmd_options(**kwargs):
27 """Create parameters options.
29 :param kwargs: Dict of cmd parameters.
31 :returns: cmd parameters.
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"-")
40 # Verify Configuration
42 # Send signal to a master process: stop, quit, reopen.
43 options.add_with_value_from_dict(
44 u"s", u"signal", kwargs
46 # Set prefix path (default: /usr/local/nginx/).
47 options.add_with_value_from_dict(
48 u"p", u"prefix", kwargs
50 # Set configuration file (default: conf/nginx.conf).
51 options.add_with_value_from_dict(
52 u"c", u"filename", kwargs
54 # Set global directives out of configuration file
55 options.add_with_value_from_dict(
56 u"g", u"directives", kwargs
58 cmd_options.extend(options)
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.
70 cmd_options = NginxUtil.get_cmd_options(path=path, signal=u"stop")
72 exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True,
73 message=u"Nginx stop failed!")
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.
87 cmd_options = NginxUtil.get_cmd_options(path=path,
90 exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True,
91 message=u"Nginx start failed!")
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.
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!")
107 def taskset_nginx_pid_to_idle_cores(node, cpu_idle_list):
108 """Set idle cpus to NGINX pid on node.
110 :param node: Topology node.
111 :param cpu_idle_list: Idle Cpus.
113 :type cpu_idle_list: list
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}"
122 node, cmd, sudo=True, timeout=180,
123 message=u"taskset cores to nginx pid failed!"