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
146
|
#!/usr/bin/env python
# Copyright (c) 2017 Okinawa Open Laboratory and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
# pylint: disable=missing-docstring
"""vm controll module"""
import logging
import os
import time
import yaml
from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
from functest.opnfv_tests.vnf.router.vnf_controller.command_generator import (
CommandGenerator)
from functest.opnfv_tests.vnf.router.vnf_controller.ssh_client import (
SshClient)
class VmController(object):
"""vm controll class"""
logger = logging.getLogger(__name__)
def __init__(self, util_info):
self.logger.debug("initialize vm controller")
self.command_gen = CommandGenerator()
credentials = util_info["credentials"]
self.util = Utilvnf()
self.util.set_credentials(credentials["snaps_creds"])
with open(self.util.test_env_config_yaml) as file_fd:
test_env_config_yaml = yaml.safe_load(file_fd)
file_fd.close()
self.reboot_wait = test_env_config_yaml.get("general").get(
"reboot_wait")
self.command_wait = test_env_config_yaml.get("general").get(
"command_wait")
self.ssh_connect_timeout = test_env_config_yaml.get("general").get(
"ssh_connect_timeout")
self.ssh_connect_retry_count = test_env_config_yaml.get("general").get(
"ssh_connect_retry_count")
def command_gen_from_template(self, command_file_path, cmd_input_param):
(command_file_dir, command_file_name) = os.path.split(
command_file_path)
template = self.command_gen.load_template(command_file_dir,
command_file_name)
return self.command_gen.command_create(template,
cmd_input_param)
def config_vm(self, vm_info, test_cmd_file_path,
cmd_input_param, prompt_file_path):
ssh = self.connect_ssh_and_config_vm(vm_info,
test_cmd_file_path,
cmd_input_param,
prompt_file_path)
if ssh is None:
return False
ssh.close()
return True
def connect_ssh_and_config_vm(self, vm_info, test_cmd_file_path,
cmd_input_param, prompt_file_path):
key_filename = None
if "key_path" in vm_info:
key_filename = vm_info["key_path"]
ssh = SshClient(ip_address=vm_info["floating_ip"],
user=vm_info["user"],
password=vm_info["pass"],
key_filename=key_filename)
result = ssh.connect(self.ssh_connect_timeout,
self.ssh_connect_retry_count)
if not result:
self.logger.debug("try to vm reboot.")
self.util.reboot_vm(vm_info["vnf_name"])
time.sleep(self.reboot_wait)
result = ssh.connect(self.ssh_connect_timeout,
self.ssh_connect_retry_count)
if not result:
return None
(result, _) = self.command_create_and_execute(
ssh,
test_cmd_file_path,
cmd_input_param,
prompt_file_path)
if not result:
ssh.close()
return None
return ssh
def command_create_and_execute(self, ssh, test_cmd_file_path,
cmd_input_param, prompt_file_path):
prompt_file = open(prompt_file_path,
'r')
prompt = yaml.safe_load(prompt_file)
prompt_file.close()
config_mode_prompt = prompt["config_mode"]
commands = self.command_gen_from_template(test_cmd_file_path,
cmd_input_param)
return self.command_list_execute(ssh,
commands,
config_mode_prompt)
def command_list_execute(self, ssh, command_list, prompt):
res_data_list = []
for command in command_list:
self.logger.debug("Command : " + command)
(res, res_data) = self.command_execute(ssh,
command,
prompt)
self.logger.debug("Response : " + res_data)
res_data_list.append(res_data)
if not res:
return res, res_data_list
time.sleep(self.command_wait)
return True, res_data_list
def command_execute(self, ssh, command, prompt):
res_data = ssh.send(command, prompt)
if res_data is None:
self.logger.info("retry send command : " + command)
res_data = ssh.send(command,
prompt)
if not ssh.error_check(res_data):
return False, res_data
return True, res_data
|