Update to Alpine 3.14
[functest.git] / functest / opnfv_tests / vnf / router / vnf_controller / vm_controller.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Okinawa Open Laboratory and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # pylint: disable=missing-docstring
11
12 """vm controll module"""
13
14 import logging
15 import os
16 import time
17 import yaml
18
19 from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
20 from functest.opnfv_tests.vnf.router.vnf_controller.command_generator import (
21     CommandGenerator)
22 from functest.opnfv_tests.vnf.router.vnf_controller.ssh_client import (
23     SshClient)
24
25
26 class VmController():
27     """vm controll class"""
28
29     logger = logging.getLogger(__name__)
30
31     def __init__(self, util_info):
32         self.logger.debug("initialize vm controller")
33         self.command_gen = CommandGenerator()
34         credentials = util_info["credentials"]
35
36         self.util = Utilvnf()
37         self.util.set_credentials(credentials["cloud"])
38
39         with open(self.util.test_env_config_yaml) as file_fd:
40             test_env_config_yaml = yaml.safe_load(file_fd)
41         file_fd.close()
42
43         self.command_wait = test_env_config_yaml.get("general").get(
44             "command_wait")
45         self.ssh_connect_timeout = test_env_config_yaml.get("general").get(
46             "ssh_connect_timeout")
47         self.ssh_connect_retry_count = test_env_config_yaml.get("general").get(
48             "ssh_connect_retry_count")
49
50     def command_gen_from_template(self, command_file_path, cmd_input_param):
51         (command_file_dir, command_file_name) = os.path.split(
52             command_file_path)
53         template = self.command_gen.load_template(command_file_dir,
54                                                   command_file_name)
55         return self.command_gen.command_create(template,
56                                                cmd_input_param)
57
58     def config_vm(self, vm_info, test_cmd_file_path,
59                   cmd_input_param, prompt_file_path):
60         ssh = self.connect_ssh_and_config_vm(vm_info,
61                                              test_cmd_file_path,
62                                              cmd_input_param,
63                                              prompt_file_path)
64         if ssh is None:
65             return False
66
67         ssh.close()
68
69         return True
70
71     def connect_ssh_and_config_vm(self, vm_info, test_cmd_file_path,
72                                   cmd_input_param, prompt_file_path):
73
74         key_filename = None
75         if "key_path" in vm_info:
76             key_filename = vm_info["key_path"]
77
78         ssh = SshClient(ip_address=vm_info["floating_ip"],
79                         user=vm_info["user"],
80                         password=vm_info["pass"],
81                         key_filename=key_filename)
82
83         result = ssh.connect(self.ssh_connect_timeout,
84                              self.ssh_connect_retry_count)
85         if not result:
86             self.logger.error(
87                 "Cannot establish connection to IP '%s'. Aborting!",
88                 ssh.ip_address)
89             return None
90
91         (result, _) = self.command_create_and_execute(
92             ssh,
93             test_cmd_file_path,
94             cmd_input_param,
95             prompt_file_path)
96         if not result:
97             ssh.close()
98             return None
99
100         return ssh
101
102     def command_create_and_execute(self, ssh, test_cmd_file_path,
103                                    cmd_input_param, prompt_file_path):
104         with open(prompt_file_path, 'r') as prompt_file:
105             prompt = yaml.safe_load(prompt_file)
106         config_mode_prompt = prompt["config_mode"]
107
108         commands = self.command_gen_from_template(test_cmd_file_path,
109                                                   cmd_input_param)
110         return self.command_list_execute(ssh,
111                                          commands,
112                                          config_mode_prompt)
113
114     def command_list_execute(self, ssh, command_list, prompt):
115         res_data_list = []
116         for command in command_list:
117             self.logger.debug("Command : %s", command)
118             (res, res_data) = self.command_execute(ssh,
119                                                    command,
120                                                    prompt)
121             self.logger.debug("Response : %s", res_data)
122             res_data_list.append(res_data)
123             if not res:
124                 return res, res_data_list
125
126             time.sleep(self.command_wait)
127
128         return True, res_data_list
129
130     def command_execute(self, ssh, command, prompt):
131         res_data = ssh.send(command, prompt)
132         if res_data is None:
133             self.logger.info("retry send command : %s", command)
134             res_data = ssh.send(command,
135                                 prompt)
136             if not ssh.error_check(res_data):
137                 return False, res_data
138
139         return True, res_data