Merge "Update rally version"
[functest.git] / testcases / vnf / vIMS / orchestrator.py
1 #!/usr/bin/python
2 # coding: utf8
3 #######################################################################
4 #
5 #   Copyright (c) 2015 Orange
6 #   valentin.boucher@orange.com
7 #
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 ########################################################################
13
14 import os
15 import shutil
16 import subprocess32 as subprocess
17 import yaml
18
19 from git import Repo
20
21 import functest.utils.functest_logger as ft_logger
22
23
24 class orchestrator:
25
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
31         self.config = inputs
32         self.logger = ft_logger.Logger("Orchestrator").getLogger()
33         self.manager_up = False
34
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
40
41     def set_flavor_id(self, flavor_id):
42         self.config['flavor_id'] = flavor_id
43
44     def set_image_id(self, image_id):
45         self.config['image_id'] = image_id
46
47     def set_external_network_name(self, external_network_name):
48         self.config['external_network_name'] = external_network_name
49
50     def set_ssh_user(self, ssh_user):
51         self.config['ssh_user'] = ssh_user
52
53     def set_nova_url(self, nova_url):
54         self.config['nova_url'] = nova_url
55
56     def set_neutron_url(self, neutron_url):
57         self.config['neutron_url'] = neutron_url
58
59     def set_nameservers(self, nameservers):
60         if 0 < len(nameservers):
61             self.config['dns_subnet_1'] = nameservers[0]
62
63     def download_manager_blueprint(self, manager_blueprint_url,
64                                    manager_blueprint_branch):
65         if self.manager_blueprint:
66             self.logger.info(
67                 "cloudify manager server blueprint is "
68                 "already downloaded !")
69         else:
70             self.logger.info(
71                 "Downloading the cloudify manager server blueprint")
72             download_result = self._download_blueprints(
73                 manager_blueprint_url,
74                 manager_blueprint_branch,
75                 self.blueprint_dir)
76
77             if not download_result:
78                 self.logger.error("Failed to download manager blueprint")
79                 exit(-1)
80             else:
81                 self.manager_blueprint = True
82
83     def manager_up(self):
84         return self.manager_up
85
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='"'))
91             f.close()
92
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("~")
97
98             for key_file in key_files:
99                 if os.path.isfile(home + key_file):
100                     os.remove(home + key_file)
101
102             self.logger.info("Launching the cloudify-manager deployment")
103             script = "set -e; "
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)
116             if error:
117                 return error
118
119             self.logger.info("Cloudify-manager server is UP !")
120
121             self.manager_up = True
122
123     def undeploy_manager(self):
124         self.logger.info("Launching the cloudify-manager undeployment")
125
126         self.manager_up = False
127
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)
133
134         self.logger.info(
135             "Cloudify-manager server has been successfully removed!")
136
137     def download_upload_and_deploy_blueprint(self, blueprint, config,
138                                              bp_name, dep_name):
139         self.logger.info("Downloading the {0} blueprint".format(
140             blueprint['file_name']))
141         destination_folder = self.testcase_dir + \
142             blueprint['destination_folder']
143         download_result = self._download_blueprints(blueprint['url'],
144                                                     blueprint['branch'],
145                                                     destination_folder)
146
147         if not download_result:
148             self.logger.error(
149                 "Failed to download blueprint {0}".
150                 format(blueprint['file_name']))
151             exit(-1)
152
153         self.logger.info("Writing the inputs file")
154
155         with open(self.testcase_dir + blueprint['destination_folder'] +
156                   "/inputs.yaml", "w") as f:
157             f.write(yaml.dump(config, default_style='"'))
158         f.close()
159
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; ")
170
171         cmd = "/bin/bash -c '" + script + "'"
172         error = execute_command(cmd, self.logger, 2000)
173         if error:
174             return error
175         self.logger.info("The deployment of {0} is ended".format(dep_name))
176
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 + "; "
184
185         cmd = "/bin/bash -c '" + script + "'"
186         try:
187             execute_command(cmd, self.logger)
188         except:
189             self.logger.error("Clearwater undeployment failed")
190
191     def _download_blueprints(self, blueprint_url, branch, dest_path):
192         if os.path.exists(dest_path):
193             shutil.rmtree(dest_path)
194         try:
195             Repo.clone_from(blueprint_url, dest_path, branch=branch)
196             return True
197         except:
198             return False
199
200
201 def execute_command(cmd, logger, timeout=1800):
202     """
203     Execute Linux command
204     """
205     if logger:
206         logger.debug('Executing command : {}'.format(cmd))
207     timeout_exception = False
208     output_file = "output.txt"
209     f = open(output_file, 'w+')
210     try:
211         p = subprocess.call(cmd, shell=True, stdout=f,
212                             stderr=subprocess.STDOUT, timeout=timeout)
213     except subprocess.TimeoutExpired:
214         timeout_exception = True
215         if logger:
216             logger.error("TIMEOUT when executing command %s" % cmd)
217         pass
218
219     f.close()
220     f = open(output_file, 'r')
221     result = f.read()
222     if result != "" and logger:
223         logger.debug(result)
224     if p == 0:
225         return False
226     else:
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]
234         return result