Merge "Modify how to disable logging in unit test."
[functest.git] / functest / opnfv_tests / vnf / ims / orchestrator_cloudify.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 logging
15 import os
16 import shutil
17 import subprocess32 as subprocess
18 import yaml
19
20 from git import Repo
21
22
23 class Orchestrator(object):
24
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
30         self.config = inputs
31         self.logger = logging.getLogger(__name__)
32         self.manager_up = False
33
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
39
40     def set_flavor_id(self, flavor_id):
41         self.config['flavor_id'] = flavor_id
42
43     def set_image_id(self, image_id):
44         self.config['image_id'] = image_id
45
46     def set_external_network_name(self, external_network_name):
47         self.config['external_network_name'] = external_network_name
48
49     def set_ssh_user(self, ssh_user):
50         self.config['ssh_user'] = ssh_user
51
52     def set_nova_url(self, nova_url):
53         self.config['nova_url'] = nova_url
54
55     def set_neutron_url(self, neutron_url):
56         self.config['neutron_url'] = neutron_url
57
58     def set_nameservers(self, nameservers):
59         if 0 < len(nameservers):
60             self.config['dns_subnet_1'] = nameservers[0]
61
62     def download_manager_blueprint(self, manager_blueprint_url,
63                                    manager_blueprint_branch):
64         if self.manager_blueprint:
65             self.logger.info(
66                 "cloudify manager server blueprint is "
67                 "already downloaded !")
68         else:
69             self.logger.info(
70                 "Downloading the cloudify manager server blueprint")
71             download_result = self._download_blueprints(
72                 manager_blueprint_url,
73                 manager_blueprint_branch,
74                 self.blueprint_dir)
75
76             if not download_result:
77                 self.logger.error("Failed to download manager blueprint")
78                 exit(-1)
79             else:
80                 self.manager_blueprint = True
81
82     def manager_up(self):
83         return self.manager_up
84
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='"'))
90             f.close()
91
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("~")
96
97             for key_file in key_files:
98                 if os.path.isfile(home + key_file):
99                     os.remove(home + key_file)
100
101             self.logger.info("Launching the cloudify-manager deployment")
102             script = "set -e; "
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)
115             if error:
116                 self.logger.error("Failed to deploy cloudify-manager")
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 = os.path.join(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             self.logger.error("Failed to deploy blueprint")
175             return error
176         self.logger.info("The deployment of {0} is ended".format(dep_name))
177
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 + "; "
185
186         cmd = "/bin/bash -c '" + script + "'"
187         try:
188             execute_command(cmd, self.logger)
189         except:
190             self.logger.error("Clearwater undeployment failed")
191
192     def _download_blueprints(self, blueprint_url, branch, dest_path):
193         if os.path.exists(dest_path):
194             shutil.rmtree(dest_path)
195         try:
196             Repo.clone_from(blueprint_url, dest_path, branch=branch)
197             return True
198         except:
199             return False
200
201
202 def execute_command(cmd, logger, timeout=1800):
203     """
204     Execute Linux command
205     """
206     if logger:
207         logger.debug('Executing command : {}'.format(cmd))
208     timeout_exception = False
209     output_file = "output.txt"
210     f = open(output_file, 'w+')
211     try:
212         p = subprocess.call(cmd, shell=True, stdout=f,
213                             stderr=subprocess.STDOUT, timeout=timeout)
214     except subprocess.TimeoutExpired:
215         timeout_exception = True
216         if logger:
217             logger.error("TIMEOUT when executing command %s" % cmd)
218         pass
219
220     f.close()
221     f = open(output_file, 'r')
222     result = f.read()
223     if result != "" and logger:
224         logger.debug(result)
225     if p == 0:
226         return False
227     else:
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()
232         return lines[-5:]