Fix: Nginx check simplified
[csit.git] / resources / libraries / python / NGINX / NGINXTools.py
1 # Copyright (c) 2022 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         cmd = f"test -f {pkg_dir}/nginx-{nginx_version}/sbin/nginx"
78         ret_code, _, _ = exec_cmd(node, cmd, sudo=True)
79         if ret_code == 0:
80             return
81         command = f"{Constants.REMOTE_FW_DIR}/{Constants.RESOURCES_LIB_SH}" \
82                   f"/entry/install_nginx.sh nginx-{nginx_version}"
83         message = u"Install the NGINX failed!"
84         exec_cmd_no_error(node, command, sudo=True, timeout=600,
85                           message=message)
86
87     @staticmethod
88     def install_vsap_nginx_on_dut(node, pkg_dir):
89         """
90          Prepare the VSAP NGINX framework on all DUT
91
92         :param node: Node from topology file.
93         :param pkg_dir: Path to directory where packages are stored.
94         :type node: dict
95         :type pkg_dir: str
96         :raises RuntimeError: If command returns nonzero return code.
97         """
98         command = u". /etc/lsb-release; echo \"${DISTRIB_ID}\""
99         stdout, _ = exec_cmd_no_error(node, command)
100
101         if stdout.strip() == u"Ubuntu":
102             logger.console(u"NGINX install on DUT... ")
103             exec_cmd_no_error(
104                 node, u"apt-get purge -y 'vsap*' || true", timeout=120,
105                 sudo=True
106             )
107             exec_cmd_no_error(
108                 node, f"dpkg -i --force-all {pkg_dir}vsap-nginx*.deb",
109                 timeout=120, sudo=True,
110                 message=u"Installation of vsap-nginx failed!"
111             )
112
113             exec_cmd_no_error(node, u"dpkg -l | grep vsap*",
114                               sudo=True)
115
116             logger.console(u"Completed!\n")
117         else:
118             logger.console(u"Ubuntu need!\n")
119
120     @staticmethod
121     def install_nginx_framework_on_all_duts(nodes, pkg_dir, nginx_version=None):
122         """
123         Prepare the NGINX framework on all DUTs.
124
125         :param nodes: Nodes from topology file.
126         :param pkg_dir: Path to directory where packages are stored.
127         :param nginx_version: NGINX version.
128         :type nodes: dict
129         :type pkg_dir: str
130         :type nginx_version: str
131         """
132
133         for node in list(nodes.values()):
134             if node[u"type"] == NodeType.DUT:
135                 if nginx_version:
136                     NGINXTools.install_original_nginx_framework(node, pkg_dir,
137                                                                 nginx_version)
138                 else:
139                     NGINXTools.install_vsap_nginx_on_dut(node, pkg_dir)