3 # Copyright (c) 2017 Okinawa Open Laboratory and others.
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
10 # pylint: disable=missing-docstring
12 """vm controll module"""
19 from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
20 from functest.opnfv_tests.vnf.router.vnf_controller.command_generator import (
22 from functest.opnfv_tests.vnf.router.vnf_controller.ssh_client import (
26 class VmController(object):
27 """vm controll class"""
29 logger = logging.getLogger(__name__)
31 def __init__(self, util_info):
32 self.logger.debug("initialize vm controller")
33 self.command_gen = CommandGenerator()
34 credentials = util_info["credentials"]
37 self.util.set_credentials(credentials["snaps_creds"])
39 with open(self.util.test_env_config_yaml) as file_fd:
40 test_env_config_yaml = yaml.safe_load(file_fd)
43 self.reboot_wait = test_env_config_yaml.get("general").get(
45 self.command_wait = test_env_config_yaml.get("general").get(
47 self.ssh_connect_timeout = test_env_config_yaml.get("general").get(
48 "ssh_connect_timeout")
49 self.ssh_connect_retry_count = test_env_config_yaml.get("general").get(
50 "ssh_connect_retry_count")
52 def command_gen_from_template(self, command_file_path, cmd_input_param):
53 (command_file_dir, command_file_name) = os.path.split(
55 template = self.command_gen.load_template(command_file_dir,
57 return self.command_gen.command_create(template,
60 def config_vm(self, vm_info, test_cmd_file_path,
61 cmd_input_param, prompt_file_path):
62 ssh = self.connect_ssh_and_config_vm(vm_info,
73 def connect_ssh_and_config_vm(self, vm_info, test_cmd_file_path,
74 cmd_input_param, prompt_file_path):
77 if "key_path" in vm_info:
78 key_filename = vm_info["key_path"]
80 ssh = SshClient(ip_address=vm_info["floating_ip"],
82 password=vm_info["pass"],
83 key_filename=key_filename)
85 result = ssh.connect(self.ssh_connect_timeout,
86 self.ssh_connect_retry_count)
88 self.logger.debug("try to vm reboot.")
89 self.util.reboot_vm(vm_info["vnf_name"])
90 time.sleep(self.reboot_wait)
91 result = ssh.connect(self.ssh_connect_timeout,
92 self.ssh_connect_retry_count)
96 (result, _) = self.command_create_and_execute(
107 def command_create_and_execute(self, ssh, test_cmd_file_path,
108 cmd_input_param, prompt_file_path):
109 prompt_file = open(prompt_file_path,
111 prompt = yaml.safe_load(prompt_file)
113 config_mode_prompt = prompt["config_mode"]
115 commands = self.command_gen_from_template(test_cmd_file_path,
117 return self.command_list_execute(ssh,
121 def command_list_execute(self, ssh, command_list, prompt):
123 for command in command_list:
124 self.logger.debug("Command : " + command)
125 (res, res_data) = self.command_execute(ssh,
128 self.logger.debug("Response : " + res_data)
129 res_data_list.append(res_data)
131 return res, res_data_list
133 time.sleep(self.command_wait)
135 return True, res_data_list
137 def command_execute(self, ssh, command, prompt):
138 res_data = ssh.send(command, prompt)
140 self.logger.info("retry send command : " + command)
141 res_data = ssh.send(command,
143 if not ssh.error_check(res_data):
144 return False, res_data
146 return True, res_data