aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/NGINX/NGINXTools.py
blob: 9418484f153ced53e7882a8a2d1f7ebb00c58360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Copyright (c) 2021 Intel and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""This module implements initialization and cleanup of NGINX framework."""

from robot.api import logger

from resources.libraries.python.Constants import Constants
from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
from resources.libraries.python.topology import NodeType
from resources.libraries.python.NginxUtil import NginxUtil


class NGINXTools:
    """This class implements:
    - Initialization of NGINX environment,
    - Cleanup of NGINX environment.
    """

    @staticmethod
    def cleanup_nginx_framework(node, nginx_ins_path):
        """
        Cleanup the NGINX framework on the DUT node.

        :param node: Will cleanup the nginx on this nodes.
        :param nginx_ins_path: NGINX install path.
        :type node: dict
        :type nginx_ins_path: str
        :raises RuntimeError: If it fails to cleanup the nginx.
        """
        check_path_cmd = NginxUtil.get_cmd_options(path=nginx_ins_path)
        exec_cmd_no_error(node, check_path_cmd, timeout=180,
                          message=u"Check NGINX install path failed!")
        command = f"rm -rf {nginx_ins_path}"
        message = u"Cleanup the NGINX failed!"
        exec_cmd_no_error(node, command, timeout=180, message=message)

    @staticmethod
    def cleanup_nginx_framework_on_all_duts(nodes, nginx_ins_path):
        """
        Cleanup the NGINX framework on all DUT nodes.

        :param nodes: Will cleanup the nginx on this nodes.
        :param nginx_ins_path: NGINX install path.
        :type nodes: dict
        :type nginx_ins_path: str
        :raises RuntimeError: If it fails to cleanup the nginx.
        """
        for node in nodes.values():
            if node[u"type"] == NodeType.DUT:
                NGINXTools.cleanup_nginx_framework(node, nginx_ins_path)

    @staticmethod
    def install_original_nginx_framework(node, pkg_dir, nginx_version):
        """
        Prepare the NGINX framework on the DUT node.

        :param node: Node from topology file.
        :param pkg_dir: Ldp NGINX install dir.
        :param nginx_version: NGINX Version.
        :type node: dict
        :type pkg_dir: str
        :type nginx_version: str
        :raises RuntimeError: If command returns nonzero return code.
        """
        nginx_path = f"{pkg_dir}/nginx-{nginx_version}/sbin/nginx"
        cmd_options = NginxUtil.get_cmd_options(path=nginx_path)
        ret_code, _, stderr = exec_cmd(node, cmd_options, sudo=True)
        if nginx_version in stderr and ret_code == 0:
            logger.info(f"NGINX Version: {stderr}")
            return
        command = f"{Constants.REMOTE_FW_DIR}/{Constants.RESOURCES_LIB_SH}" \
                  f"/entry/install_nginx.sh nginx-{nginx_version}"
        message = u"Install the NGINX failed!"
        exec_cmd_no_error(node, command, sudo=True, timeout=600,
                          message=message)
        _, stderr = exec_cmd_no_error(node, cmd_options, sudo=True,
                                      message=message)

        logger.info(f"NGINX Version: {stderr}")

    @staticmethod
    def install_vsap_nginx_on_dut(node, pkg_dir):
        """
         Prepare the VSAP NGINX framework on all DUT

        :param node: Node from topology file.
        :param pkg_dir: Path to directory where packages are stored.
        :type node: dict
        :type pkg_dir: str
        :raises RuntimeError: If command returns nonzero return code.
        """
        command = u". /etc/lsb-release; echo \"${DISTRIB_ID}\""
        stdout, _ = exec_cmd_no_error(node, command)

        if stdout.strip() == u"Ubuntu":
            logger.console(u"NGINX install on DUT... ")
            exec_cmd_no_error(
                node, u"apt-get purge -y 'vsap*' || true", timeout=120,
                sudo=True
            )
            exec_cmd_no_error(
                node, f"dpkg -i --force-all {pkg_dir}vsap-nginx*.deb",
                timeout=120, sudo=True,
                message=u"Installation of vsap-nginx failed!"
            )

            exec_cmd_no_error(node, u"dpkg -l | grep vsap*",
                              sudo=True)

            logger.console(u"Completed!\n")
        else:
            logger.console(u"Ubuntu need!\n")

    @staticmethod
    def install_nginx_framework_on_all_duts(nodes, pkg_dir, nginx_version=None):
        """
        Prepare the NGINX framework on all DUTs.

        :param nodes: Nodes from topology file.
        :param pkg_dir: Path to directory where packages are stored.
        :param nginx_version: NGINX version.
        :type nodes: dict
        :type pkg_dir: str
        :type nginx_version: str
        """

        for node in list(nodes.values()):
            if node[u"type"] == NodeType.DUT:
                if nginx_version:
                    NGINXTools.install_original_nginx_framework(node, pkg_dir,
                                                                nginx_version)
                else:
                    NGINXTools.install_vsap_nginx_on_dut(node, pkg_dir)