3 #######################################################################
5 # Copyright (c) 2015 Orange
6 # valentin.boucher@orange.com
8 # All rights reserved. This program and the accompanying materials
9 # are made available under the terms of the Apache License, Version 2.0
10 # which accompanies this distribution, and is available at
11 # http://www.apache.org/licenses/LICENSE-2.0
12 ########################################################################
16 import subprocess32 as subprocess
21 import functest.utils.functest_logger as ft_logger
26 def __init__(self, testcase_dir, inputs={}):
27 self.testcase_dir = testcase_dir
28 self.blueprint_dir = testcase_dir + 'cloudify-manager-blueprint/'
29 self.input_file = 'inputs.yaml'
30 self.manager_blueprint = False
32 self.logger = ft_logger.Logger("Orchestrator").getLogger()
33 self.manager_up = False
35 def set_credentials(self, username, password, tenant_name, auth_url):
36 self.config['keystone_username'] = username
37 self.config['keystone_password'] = password
38 self.config['keystone_url'] = auth_url
39 self.config['keystone_tenant_name'] = tenant_name
41 def set_flavor_id(self, flavor_id):
42 self.config['flavor_id'] = flavor_id
44 def set_image_id(self, image_id):
45 self.config['image_id'] = image_id
47 def set_external_network_name(self, external_network_name):
48 self.config['external_network_name'] = external_network_name
50 def set_ssh_user(self, ssh_user):
51 self.config['ssh_user'] = ssh_user
53 def set_nova_url(self, nova_url):
54 self.config['nova_url'] = nova_url
56 def set_neutron_url(self, neutron_url):
57 self.config['neutron_url'] = neutron_url
59 def set_nameservers(self, nameservers):
60 if 0 < len(nameservers):
61 self.config['dns_subnet_1'] = nameservers[0]
63 def download_manager_blueprint(self, manager_blueprint_url,
64 manager_blueprint_branch):
65 if self.manager_blueprint:
67 "cloudify manager server blueprint is "
68 "already downloaded !")
71 "Downloading the cloudify manager server blueprint")
72 download_result = self._download_blueprints(
73 manager_blueprint_url,
74 manager_blueprint_branch,
77 if not download_result:
78 self.logger.error("Failed to download manager blueprint")
81 self.manager_blueprint = True
84 return self.manager_up
86 def deploy_manager(self):
87 if self.manager_blueprint:
88 self.logger.info("Writing the inputs file")
89 with open(self.blueprint_dir + "inputs.yaml", "w") as f:
90 f.write(yaml.dump(self.config, default_style='"'))
93 # Ensure no ssh key file already exists
94 key_files = ["/.ssh/cloudify-manager-kp.pem",
95 "/.ssh/cloudify-agent-kp.pem"]
96 home = os.path.expanduser("~")
98 for key_file in key_files:
99 if os.path.isfile(home + key_file):
100 os.remove(home + key_file)
102 self.logger.info("Launching the cloudify-manager deployment")
104 script += ("source " + self.testcase_dir +
105 "venv_cloudify/bin/activate; ")
106 script += "cd " + self.testcase_dir + "; "
107 script += "cfy init -r; "
108 script += "cd cloudify-manager-blueprint; "
109 script += ("cfy local create-requirements -o requirements.txt " +
110 "-p openstack-manager-blueprint.yaml; ")
111 script += "pip install -r requirements.txt; "
112 script += ("cfy bootstrap --install-plugins " +
113 "-p openstack-manager-blueprint.yaml -i inputs.yaml; ")
114 cmd = "/bin/bash -c '" + script + "'"
115 error = execute_command(cmd, self.logger)
119 self.logger.info("Cloudify-manager server is UP !")
121 self.manager_up = True
123 def undeploy_manager(self):
124 self.logger.info("Launching the cloudify-manager undeployment")
126 self.manager_up = False
128 script = "source " + self.testcase_dir + "venv_cloudify/bin/activate; "
129 script += "cd " + self.testcase_dir + "; "
130 script += "cfy teardown -f --ignore-deployments; "
131 cmd = "/bin/bash -c '" + script + "'"
132 execute_command(cmd, self.logger)
135 "Cloudify-manager server has been successfully removed!")
137 def download_upload_and_deploy_blueprint(self, blueprint, config,
139 self.logger.info("Downloading the {0} blueprint".format(
140 blueprint['file_name']))
141 destination_folder = os.path.join(self.testcase_dir,
142 blueprint['destination_folder'])
143 download_result = self._download_blueprints(blueprint['url'],
147 if not download_result:
149 "Failed to download blueprint {0}".
150 format(blueprint['file_name']))
153 self.logger.info("Writing the inputs file")
155 with open(self.testcase_dir + blueprint['destination_folder'] +
156 "/inputs.yaml", "w") as f:
157 f.write(yaml.dump(config, default_style='"'))
160 self.logger.info("Launching the {0} deployment".format(bp_name))
161 script = "source " + self.testcase_dir + "venv_cloudify/bin/activate; "
162 script += ("cd " + self.testcase_dir +
163 blueprint['destination_folder'] + "; ")
164 script += ("cfy blueprints upload -b " +
165 bp_name + " -p openstack-blueprint.yaml; ")
166 script += ("cfy deployments create -b " + bp_name +
167 " -d " + dep_name + " --inputs inputs.yaml; ")
168 script += ("cfy executions start -w install -d " +
169 dep_name + " --timeout 1800; ")
171 cmd = "/bin/bash -c '" + script + "'"
172 error = execute_command(cmd, self.logger, 2000)
175 self.logger.info("The deployment of {0} is ended".format(dep_name))
177 def undeploy_deployment(self, dep_name):
178 self.logger.info("Launching the {0} undeployment".format(dep_name))
179 script = "source " + self.testcase_dir + "venv_cloudify/bin/activate; "
180 script += "cd " + self.testcase_dir + "; "
181 script += ("cfy executions start -w uninstall -d " + dep_name +
182 " --timeout 1800 ; ")
183 script += "cfy deployments delete -d " + dep_name + "; "
185 cmd = "/bin/bash -c '" + script + "'"
187 execute_command(cmd, self.logger)
189 self.logger.error("Clearwater undeployment failed")
191 def _download_blueprints(self, blueprint_url, branch, dest_path):
192 if os.path.exists(dest_path):
193 shutil.rmtree(dest_path)
195 Repo.clone_from(blueprint_url, dest_path, branch=branch)
201 def execute_command(cmd, logger, timeout=1800):
203 Execute Linux command
206 logger.debug('Executing command : {}'.format(cmd))
207 timeout_exception = False
208 output_file = "output.txt"
209 f = open(output_file, 'w+')
211 p = subprocess.call(cmd, shell=True, stdout=f,
212 stderr=subprocess.STDOUT, timeout=timeout)
213 except subprocess.TimeoutExpired:
214 timeout_exception = True
216 logger.error("TIMEOUT when executing command %s" % cmd)
220 f = open(output_file, 'r')
222 if result != "" and logger:
227 if logger and not timeout_exception:
228 logger.error("Error when executing command %s" % cmd)
229 f = open(output_file, 'r')
230 lines = f.readlines()
231 result = lines[len(lines) - 3]
232 result += lines[len(lines) - 2]
233 result += lines[len(lines) - 1]