perf: add TCP Nginx+LDPRELOAD suites
[csit.git] / resources / libraries / python / NGINX / NGINXTools.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
15 """This module implements initialization and cleanup of NGINX framework."""
16
17 from robot.api import logger
18
19 from resources.libraries.python.Constants import Constants
20 from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
21 from resources.libraries.python.topology import NodeType
22 from resources.libraries.python.NginxUtil import NginxUtil
23
24
25 class NGINXTools:
26     """This class implements:
27     - Initialization of NGINX environment,
28     - Cleanup of NGINX environment.
29     """
30
31     @staticmethod
32     def cleanup_nginx_framework(node, nginx_ins_path):
33         """
34         Cleanup the NGINX framework on the DUT node.
35
36         :param node: Will cleanup the nginx on this nodes.
37         :param nginx_ins_path: NGINX install path.
38         :type node: dict
39         :type nginx_ins_path: str
40         :raises RuntimeError: If it fails to cleanup the nginx.
41         """
42         check_path_cmd = NginxUtil.get_cmd_options(path=nginx_ins_path)
43         exec_cmd_no_error(node, check_path_cmd, timeout=180,
44                           message=u"Check NGINX install path failed!")
45         command = f"rm -rf {nginx_ins_path}"
46         message = u"Cleanup the NGINX failed!"
47         exec_cmd_no_error(node, command, timeout=180, message=message)
48
49     @staticmethod
50     def cleanup_nginx_framework_on_all_duts(nodes, nginx_ins_path):
51         """
52         Cleanup the NGINX framework on all DUT nodes.
53
54         :param nodes: Will cleanup the nginx on this nodes.
55         :param nginx_ins_path: NGINX install path.
56         :type nodes: dict
57         :type nginx_ins_path: str
58         :raises RuntimeError: If it fails to cleanup the nginx.
59         """
60         for node in nodes.values():
61             if node[u"type"] == NodeType.DUT:
62                 NGINXTools.cleanup_nginx_framework(node, nginx_ins_path)
63
64     @staticmethod
65     def install_original_nginx_framework(node, pkg_dir, nginx_version):
66         """
67         Prepare the NGINX framework on the DUT node.
68
69         :param node: Node from topology file.
70         :param pkg_dir: Ldp NGINX install dir.
71         :param nginx_version: NGINX Version.
72         :type node: dict
73         :type pkg_dir: str
74         :type nginx_version: str
75         :raises RuntimeError: If command returns nonzero return code.
76         """
77         nginx_path = f"{pkg_dir}/nginx-{nginx_version}/sbin/nginx"
78         cmd_options = NginxUtil.get_cmd_options(path=nginx_path)
79         ret_code, _, stderr = exec_cmd(node, cmd_options, sudo=True)
80         if nginx_version in stderr and ret_code == 0:
81             logger.info(f"NGINX Version: {stderr}")
82             return
83         command = f"{Constants.REMOTE_FW_DIR}/{Constants.RESOURCES_LIB_SH}" \
84                   f"/entry/install_nginx.sh nginx-{nginx_version}"
85         message = u"Install the NGINX failed!"
86         exec_cmd_no_error(node, command, sudo=True, timeout=600,
87                           message=message)
88         _, stderr = exec_cmd_no_error(node, cmd_options, sudo=True,
89                                       message=message)
90
91         logger.info(f"NGINX Version: {stderr}")
92
93     @staticmethod
94     def install_vsap_nginx_on_dut(node, pkg_dir):
95         """
96          Prepare the VSAP NGINX framework on all DUT
97
98         :param node: Node from topology file.
99         :param pkg_dir: Path to directory where packages are stored.
100         :type node: dict
101         :type pkg_dir: str
102         :raises RuntimeError: If command returns nonzero return code.
103         """
104         command = u". /etc/lsb-release; echo \"${DISTRIB_ID}\""
105         stdout, _ = exec_cmd_no_error(node, command)
106
107         if stdout.strip() == u"Ubuntu":
108             logger.console(u"NGINX install on DUT... ")
109             exec_cmd_no_error(
110                 node, u"apt-get purge -y 'vsap*' || true", timeout=120,
111                 sudo=True
112             )
113             exec_cmd_no_error(
114                 node, f"dpkg -i --force-all {pkg_dir}vsap-nginx*.deb",
115                 timeout=120, sudo=True,
116                 message=u"Installation of vsap-nginx failed!"
117             )
118
119             exec_cmd_no_error(node, u"dpkg -l | grep vsap*",
120                               sudo=True)
121
122             logger.console(u"Completed!\n")
123         else:
124             logger.console(u"Ubuntu need!\n")
125
126     @staticmethod
127     def install_nginx_framework_on_all_duts(nodes, pkg_dir, nginx_version=None):
128         """
129         Prepare the NGINX framework on all DUTs.
130
131         :param nodes: Nodes from topology file.
132         :param pkg_dir: Path to directory where packages are stored.
133         :param nginx_version: NGINX version.
134         :type nodes: dict
135         :type pkg_dir: str
136         :type nginx_version: str
137         """
138
139         for node in list(nodes.values()):
140             if node[u"type"] == NodeType.DUT:
141                 if nginx_version:
142                     NGINXTools.install_original_nginx_framework(node, pkg_dir,
143                                                                 nginx_version)
144                 else:
145                     NGINXTools.install_vsap_nginx_on_dut(node, pkg_dir)