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 ########################################################################
23 def __init__(self, testcase_dir, inputs={}, logger=None):
24 self.testcase_dir = testcase_dir
25 self.blueprint_dir = testcase_dir + 'cloudify-manager-blueprint/'
26 self.input_file = 'inputs.yaml'
27 self.manager_blueprint = False
30 self.manager_up = False
32 def set_credentials(self, username, password, tenant_name, auth_url):
33 self.config['keystone_username'] = username
34 self.config['keystone_password'] = password
35 self.config['keystone_url'] = auth_url
36 self.config['keystone_tenant_name'] = tenant_name
38 def set_flavor_id(self, flavor_id):
39 self.config['flavor_id'] = flavor_id
41 def set_image_id(self, image_id):
42 self.config['image_id'] = image_id
44 def set_external_network_name(self, external_network_name):
45 self.config['external_network_name'] = external_network_name
47 def set_ssh_user(self, ssh_user):
48 self.config['ssh_user'] = ssh_user
50 def set_nameservers(self, nameservers):
51 if 0 < len(nameservers):
52 self.config['dns_subnet_1'] = nameservers[0]
53 if 1 < len(nameservers):
54 self.config['dns_subnet_2'] = nameservers[1]
56 def set_logger(self, logger):
59 def download_manager_blueprint(self, manager_blueprint_url, manager_blueprint_branch):
60 if self.manager_blueprint:
63 "cloudify manager server blueprint is already downloaded !")
67 "Downloading the cloudify manager server blueprint")
68 download_result = download_blueprints(
69 manager_blueprint_url, manager_blueprint_branch, self.blueprint_dir)
71 if not download_result:
73 self.logger.error("Failed to download manager blueprint")
76 self.manager_blueprint = True
81 def deploy_manager(self):
82 if self.manager_blueprint:
84 self.logger.info("Writing the inputs file")
85 with open(self.blueprint_dir + "inputs.yaml", "w") as f:
86 f.write(yaml.dump(self.config, default_style='"'))
89 # Ensure no ssh key file already exists
90 key_files = ["/.ssh/cloudify-manager-kp.pem",
91 "/.ssh/cloudify-agent-kp.pem"]
92 home = os.path.expanduser("~")
94 for key_file in key_files:
95 if os.path.isfile(home + key_file):
96 os.remove(home + key_file)
99 self.logger.info("Launching the cloudify-manager deployment")
101 script += "source " + self.testcase_dir + "venv_cloudify/bin/activate; "
102 script += "cd " + self.testcase_dir + "; "
103 script += "cfy init -r; "
104 script += "cd cloudify-manager-blueprint; "
105 script += "cfy local create-requirements -o requirements.txt -p openstack-manager-blueprint.yaml; "
106 script += "pip install -r requirements.txt; "
107 script += "timeout 1800 cfy bootstrap --install-plugins -p openstack-manager-blueprint.yaml -i inputs.yaml; "
108 cmd = "/bin/bash -c '" + script + "'"
109 error = execute_command(cmd, self.logger)
114 self.logger.info("Cloudify-manager server is UP !")
116 self.manager_up = True
118 def undeploy_manager(self):
120 self.logger.info("Launching the cloudify-manager undeployment")
122 self.manager_up = False
124 script = "source " + self.testcase_dir + "venv_cloudify/bin/activate; "
125 script += "cd " + self.testcase_dir + "; "
126 script += "cfy teardown -f --ignore-deployments; "
127 cmd = "/bin/bash -c '" + script + "'"
128 execute_command(cmd, self.logger)
132 "Cloudify-manager server has been successfully removed!")
134 def download_upload_and_deploy_blueprint(self, blueprint, config, bp_name, dep_name):
136 self.logger.info("Downloading the {0} blueprint".format(
137 blueprint['file_name']))
138 download_result = download_blueprints(blueprint['url'], blueprint['branch'],
139 self.testcase_dir + blueprint['destination_folder'])
141 if not download_result:
144 "Failed to download blueprint {0}".format(blueprint['file_name']))
148 self.logger.info("Writing the inputs file")
149 with open(self.testcase_dir + blueprint['destination_folder'] + "/inputs.yaml", "w") as f:
150 f.write(yaml.dump(config, default_style='"'))
154 self.logger.info("Launching the {0} deployment".format(bp_name))
155 script = "source " + self.testcase_dir + "venv_cloudify/bin/activate; "
156 script += "cd " + self.testcase_dir + \
157 blueprint['destination_folder'] + "; "
158 script += "cfy blueprints upload -b " + \
159 bp_name + " -p openstack-blueprint.yaml; "
160 script += "cfy deployments create -b " + bp_name + \
161 " -d " + dep_name + " --inputs inputs.yaml; "
162 script += "cfy executions start -w install -d " + dep_name + " --timeout 1800; "
164 cmd = "/bin/bash -c '" + script + "'"
165 error = execute_command(cmd, self.logger)
169 self.logger.info("The deployment of {0} is ended".format(dep_name))
171 def undeploy_deployment(self, dep_name):
173 self.logger.info("Launching the {0} undeployment".format(dep_name))
174 script = "source " + self.testcase_dir + "venv_cloudify/bin/activate; "
175 script += "cd " + self.testcase_dir + "; "
176 script += "cfy executions start -w uninstall -d " + dep_name + " --timeout 1800 ; "
177 script += "cfy deployments delete -d " + dep_name + "; "
179 cmd = "/bin/bash -c '" + script + "'"
181 execute_command(cmd, self.logger)
184 self.logger.error("Clearwater undeployment failed")
187 def execute_command(cmd, logger):
189 Execute Linux command
192 logger.debug('Executing command : {}'.format(cmd))
193 output_file = "output.txt"
194 f = open(output_file, 'w+')
195 p = subprocess.call(cmd, shell=True, stdout=f, stderr=subprocess.STDOUT)
197 f = open(output_file, 'r')
199 if result != "" and logger:
205 logger.error("Error when executing command %s" % cmd)
206 f = open(output_file, 'r')
207 lines = f.readlines()
208 result = lines[len(lines) - 3]
209 result += lines[len(lines) - 2]
210 result += lines[len(lines) - 1]
214 def download_blueprints(blueprint_url, branch, dest_path):
215 if os.path.exists(dest_path):
216 shutil.rmtree(dest_path)
218 Repo.clone_from(blueprint_url, dest_path, branch=branch)