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 ########################################################################
17 import subprocess32 as subprocess
23 class Orchestrator(object):
25 def __init__(self, testcase_dir, inputs={}):
26 self.testcase_dir = testcase_dir
27 self.blueprint_dir = testcase_dir + 'cloudify-manager-blueprint/'
28 self.input_file = 'inputs.yaml'
29 self.manager_blueprint = False
31 self.logger = logging.getLogger(__name__)
32 self.manager_up = False
34 def set_credentials(self, username, password, tenant_name, auth_url):
35 self.config['keystone_username'] = username
36 self.config['keystone_password'] = password
37 self.config['keystone_url'] = auth_url
38 self.config['keystone_tenant_name'] = tenant_name
40 def set_flavor_id(self, flavor_id):
41 self.config['flavor_id'] = flavor_id
43 def set_image_id(self, image_id):
44 self.config['image_id'] = image_id
46 def set_external_network_name(self, external_network_name):
47 self.config['external_network_name'] = external_network_name
49 def set_ssh_user(self, ssh_user):
50 self.config['ssh_user'] = ssh_user
52 def set_nova_url(self, nova_url):
53 self.config['nova_url'] = nova_url
55 def set_neutron_url(self, neutron_url):
56 self.config['neutron_url'] = neutron_url
58 def set_nameservers(self, nameservers):
59 if 0 < len(nameservers):
60 self.config['dns_subnet_1'] = nameservers[0]
62 def download_manager_blueprint(self, manager_blueprint_url,
63 manager_blueprint_branch):
64 if self.manager_blueprint:
66 "cloudify manager server blueprint is "
67 "already downloaded !")
70 "Downloading the cloudify manager server blueprint")
71 download_result = self._download_blueprints(
72 manager_blueprint_url,
73 manager_blueprint_branch,
76 if not download_result:
77 self.logger.error("Failed to download manager blueprint")
80 self.manager_blueprint = True
83 return self.manager_up
85 def deploy_manager(self):
86 if self.manager_blueprint:
87 self.logger.info("Writing the inputs file")
88 with open(self.blueprint_dir + "inputs.yaml", "w") as f:
89 f.write(yaml.dump(self.config, default_style='"'))
92 # Ensure no ssh key file already exists
93 key_files = ["/.ssh/cloudify-manager-kp.pem",
94 "/.ssh/cloudify-agent-kp.pem"]
95 home = os.path.expanduser("~")
97 for key_file in key_files:
98 if os.path.isfile(home + key_file):
99 os.remove(home + key_file)
101 self.logger.info("Launching the cloudify-manager deployment")
103 script += ("source " + self.testcase_dir +
104 "venv_cloudify/bin/activate; ")
105 script += "cd " + self.testcase_dir + "; "
106 script += "cfy init -r; "
107 script += "cd cloudify-manager-blueprint; "
108 script += ("cfy local create-requirements -o requirements.txt " +
109 "-p openstack-manager-blueprint.yaml; ")
110 script += "pip install -r requirements.txt; "
111 script += ("cfy bootstrap --install-plugins " +
112 "-p openstack-manager-blueprint.yaml -i inputs.yaml; ")
113 cmd = "/bin/bash -c '" + script + "'"
114 error = execute_command(cmd, self.logger)
116 self.logger.error("Failed to deploy cloudify-manager")
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)
174 self.logger.error("Failed to deploy blueprint")
176 self.logger.info("The deployment of {0} is ended".format(dep_name))
178 def undeploy_deployment(self, dep_name):
179 self.logger.info("Launching the {0} undeployment".format(dep_name))
180 script = "source " + self.testcase_dir + "venv_cloudify/bin/activate; "
181 script += "cd " + self.testcase_dir + "; "
182 script += ("cfy executions start -w uninstall -d " + dep_name +
183 " --timeout 1800 ; ")
184 script += "cfy deployments delete -d " + dep_name + "; "
186 cmd = "/bin/bash -c '" + script + "'"
188 execute_command(cmd, self.logger)
190 self.logger.error("Clearwater undeployment failed")
192 def _download_blueprints(self, blueprint_url, branch, dest_path):
193 if os.path.exists(dest_path):
194 shutil.rmtree(dest_path)
196 Repo.clone_from(blueprint_url, dest_path, branch=branch)
202 def execute_command(cmd, logger, timeout=1800):
204 Execute Linux command
207 logger.debug('Executing command : {}'.format(cmd))
208 timeout_exception = False
209 output_file = "output.txt"
210 f = open(output_file, 'w+')
212 p = subprocess.call(cmd, shell=True, stdout=f,
213 stderr=subprocess.STDOUT, timeout=timeout)
214 except subprocess.TimeoutExpired:
215 timeout_exception = True
217 logger.error("TIMEOUT when executing command %s" % cmd)
221 f = open(output_file, 'r')
223 if result != "" and logger:
228 if logger and not timeout_exception:
229 logger.error("Error when executing command %s" % cmd)
230 f = open(output_file, 'r')
231 lines = f.readlines()