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.
15 """This module implements initialization and cleanup of NGINX framework."""
17 from robot.api import logger
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
26 """This class implements:
27 - Initialization of NGINX environment,
28 - Cleanup of NGINX environment.
32 def cleanup_nginx_framework(node, nginx_ins_path):
34 Cleanup the NGINX framework on the DUT node.
36 :param node: Will cleanup the nginx on this nodes.
37 :param nginx_ins_path: NGINX install path.
39 :type nginx_ins_path: str
40 :raises RuntimeError: If it fails to cleanup the nginx.
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)
50 def cleanup_nginx_framework_on_all_duts(nodes, nginx_ins_path):
52 Cleanup the NGINX framework on all DUT nodes.
54 :param nodes: Will cleanup the nginx on this nodes.
55 :param nginx_ins_path: NGINX install path.
57 :type nginx_ins_path: str
58 :raises RuntimeError: If it fails to cleanup the nginx.
60 for node in nodes.values():
61 if node[u"type"] == NodeType.DUT:
62 NGINXTools.cleanup_nginx_framework(node, nginx_ins_path)
65 def install_original_nginx_framework(node, pkg_dir, nginx_version):
67 Prepare the NGINX framework on the DUT node.
69 :param node: Node from topology file.
70 :param pkg_dir: Ldp NGINX install dir.
71 :param nginx_version: NGINX Version.
74 :type nginx_version: str
75 :raises RuntimeError: If command returns nonzero return code.
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}")
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,
88 _, stderr = exec_cmd_no_error(node, cmd_options, sudo=True,
91 logger.info(f"NGINX Version: {stderr}")
94 def install_vsap_nginx_on_dut(node, pkg_dir):
96 Prepare the VSAP NGINX framework on all DUT
98 :param node: Node from topology file.
99 :param pkg_dir: Path to directory where packages are stored.
102 :raises RuntimeError: If command returns nonzero return code.
104 command = u". /etc/lsb-release; echo \"${DISTRIB_ID}\""
105 stdout, _ = exec_cmd_no_error(node, command)
107 if stdout.strip() == u"Ubuntu":
108 logger.console(u"NGINX install on DUT... ")
110 node, u"apt-get purge -y 'vsap*' || true", timeout=120,
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!"
119 exec_cmd_no_error(node, u"dpkg -l | grep vsap*",
122 logger.console(u"Completed!\n")
124 logger.console(u"Ubuntu need!\n")
127 def install_nginx_framework_on_all_duts(nodes, pkg_dir, nginx_version=None):
129 Prepare the NGINX framework on all DUTs.
131 :param nodes: Nodes from topology file.
132 :param pkg_dir: Path to directory where packages are stored.
133 :param nginx_version: NGINX version.
136 :type nginx_version: str
139 for node in list(nodes.values()):
140 if node[u"type"] == NodeType.DUT:
142 NGINXTools.install_original_nginx_framework(node, pkg_dir,
145 NGINXTools.install_vsap_nginx_on_dut(node, pkg_dir)