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)
117 self.logger.error("Failed to deploy cloudify-manager")
120 self.logger.info("Cloudify-manager server is UP !")
122 self.manager_up = True
124 def undeploy_manager(self):
125 self.logger.info("Launching the cloudify-manager undeployment")
127 self.manager_up = False
129 script = "source " + self.testcase_dir + "venv_cloudify/bin/activate; "
130 script += "cd " + self.testcase_dir + "; "
131 script += "cfy teardown -f --ignore-deployments; "
132 cmd = "/bin/bash -c '" + script + "'"
133 execute_command(cmd, self.logger)
136 "Cloudify-manager server has been successfully removed!")
138 def download_upload_and_deploy_blueprint(self, blueprint, config,
140 self.logger.info("Downloading the {0} blueprint".format(
141 blueprint['file_name']))
142 destination_folder = os.path.join(self.testcase_dir,
143 blueprint['destination_folder'])
144 download_result = self._download_blueprints(blueprint['url'],
148 if not download_result:
150 "Failed to download blueprint {0}".
151 format(blueprint['file_name']))
154 self.logger.info("Writing the inputs file")
156 with open(self.testcase_dir + blueprint['destination_folder'] +
157 "/inputs.yaml", "w") as f:
158 f.write(yaml.dump(config, default_style='"'))
161 self.logger.info("Launching the {0} deployment".format(bp_name))
162 script = "source " + self.testcase_dir + "venv_cloudify/bin/activate; "
163 script += ("cd " + self.testcase_dir +
164 blueprint['destination_folder'] + "; ")
165 script += ("cfy blueprints upload -b " +
166 bp_name + " -p openstack-blueprint.yaml; ")
167 script += ("cfy deployments create -b " + bp_name +
168 " -d " + dep_name + " --inputs inputs.yaml; ")
169 script += ("cfy executions start -w install -d " +
170 dep_name + " --timeout 1800; ")
172 cmd = "/bin/bash -c '" + script + "'"
173 error = execute_command(cmd, self.logger, 2000)
175 self.logger.error("Failed to deploy blueprint")
177 self.logger.info("The deployment of {0} is ended".format(dep_name))
179 def undeploy_deployment(self, dep_name):
180 self.logger.info("Launching the {0} undeployment".format(dep_name))
181 script = "source " + self.testcase_dir + "venv_cloudify/bin/activate; "
182 script += "cd " + self.testcase_dir + "; "
183 script += ("cfy executions start -w uninstall -d " + dep_name +
184 " --timeout 1800 ; ")
185 script += "cfy deployments delete -d " + dep_name + "; "
187 cmd = "/bin/bash -c '" + script + "'"
189 execute_command(cmd, self.logger)
191 self.logger.error("Clearwater undeployment failed")
193 def _download_blueprints(self, blueprint_url, branch, dest_path):
194 if os.path.exists(dest_path):
195 shutil.rmtree(dest_path)
197 Repo.clone_from(blueprint_url, dest_path, branch=branch)
203 def execute_command(cmd, logger, timeout=1800):
205 Execute Linux command
208 logger.debug('Executing command : {}'.format(cmd))
209 timeout_exception = False
210 output_file = "output.txt"
211 f = open(output_file, 'w+')
213 p = subprocess.call(cmd, shell=True, stdout=f,
214 stderr=subprocess.STDOUT, timeout=timeout)
215 except subprocess.TimeoutExpired:
216 timeout_exception = True
218 logger.error("TIMEOUT when executing command %s" % cmd)
222 f = open(output_file, 'r')
224 if result != "" and logger:
229 if logger and not timeout_exception:
230 logger.error("Error when executing command %s" % cmd)
231 f = open(output_file, 'r')
232 lines = f.readlines()