Fix path for vping.sh and exit if SCP fails
[functest.git] / utils / generate_defaults.py
1 #!/usr/bin/env python
2 #
3 # Description:
4 #  Generates a list of the current Openstack objects in the deployment:
5 #       - Nova instances
6 #       - Glance images
7 #       - Cinder volumes
8 #       - Floating IPs
9 #       - Neutron networks, subnets and ports
10 #       - Routers
11 #       - Users and tenants
12 #
13 # Author:
14 #    jose.lausuch@ericsson.com
15 #
16 #
17 # All rights reserved. This program and the accompanying materials
18 # are made available under the terms of the Apache License, Version 2.0
19 # which accompanies this distribution, and is available at
20 # http://www.apache.org/licenses/LICENSE-2.0
21 #
22
23 import os
24 import yaml
25
26 from novaclient import client as novaclient
27 from neutronclient.v2_0 import client as neutronclient
28 from keystoneclient.v2_0 import client as keystoneclient
29 from cinderclient import client as cinderclient
30
31 import functest.utils.openstack_utils as os_utils
32 import functest.utils.functest_logger as ft_logger
33
34 """ logging configuration """
35 logger = ft_logger.Logger("generate_defaults").getLogger()
36
37 REPO_PATH = os.environ['repos_dir'] + '/functest/'
38 if not os.path.exists(REPO_PATH):
39     logger.error("Functest repository directory not found '%s'" % REPO_PATH)
40     exit(-1)
41
42
43 DEFAULTS_FILE = '/home/opnfv/functest/conf/os_defaults.yaml'
44
45
46 def separator():
47     logger.info("-------------------------------------------")
48
49
50 def get_instances(nova_client):
51     logger.debug("Getting instances...")
52     dic_instances = {}
53     instances = os_utils.get_instances(nova_client)
54     if not (instances is None or len(instances) == 0):
55         for instance in instances:
56             dic_instances.update({getattr(instance, 'id'): getattr(instance,
57                                                                    'name')})
58     return {'instances': dic_instances}
59
60
61 def get_images(nova_client):
62     logger.debug("Getting images...")
63     dic_images = {}
64     images = os_utils.get_images(nova_client)
65     if not (images is None or len(images) == 0):
66         for image in images:
67             dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
68     return {'images': dic_images}
69
70
71 def get_volumes(cinder_client):
72     logger.debug("Getting volumes...")
73     dic_volumes = {}
74     volumes = os_utils.get_volumes(cinder_client)
75     if volumes is not None:
76         for volume in volumes:
77             dic_volumes.update({volume.id: volume.display_name})
78     return {'volumes': dic_volumes}
79
80
81 def get_networks(neutron_client):
82     logger.debug("Getting networks")
83     dic_networks = {}
84     networks = os_utils.get_network_list(neutron_client)
85     if networks is not None:
86         for network in networks:
87             dic_networks.update({network['id']: network['name']})
88     return {'networks': dic_networks}
89
90
91 def get_routers(neutron_client):
92     logger.debug("Getting routers")
93     dic_routers = {}
94     routers = os_utils.get_router_list(neutron_client)
95     if routers is not None:
96         for router in routers:
97             dic_routers.update({router['id']: router['name']})
98     return {'routers': dic_routers}
99
100
101 def get_security_groups(neutron_client):
102     logger.debug("Getting Security groups...")
103     dic_secgroups = {}
104     secgroups = os_utils.get_security_groups(neutron_client)
105     if not (secgroups is None or len(secgroups) == 0):
106         for secgroup in secgroups:
107             dic_secgroups.update({secgroup['id']: secgroup['name']})
108     return {'secgroups': dic_secgroups}
109
110
111 def get_floatinips(nova_client):
112     logger.debug("Getting Floating IPs...")
113     dic_floatingips = {}
114     floatingips = os_utils.get_floating_ips(nova_client)
115     if not (floatingips is None or len(floatingips) == 0):
116         for floatingip in floatingips:
117             dic_floatingips.update({floatingip.id: floatingip.ip})
118     return {'floatingips': dic_floatingips}
119
120
121 def get_users(keystone_client):
122     logger.debug("Getting users...")
123     dic_users = {}
124     users = os_utils.get_users(keystone_client)
125     if not (users is None or len(users) == 0):
126         for user in users:
127             dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
128     return {'users': dic_users}
129
130
131 def get_tenants(keystone_client):
132     logger.debug("Getting users...")
133     dic_tenants = {}
134     tenants = os_utils.get_tenants(keystone_client)
135     if not (tenants is None or len(tenants) == 0):
136         for tenant in tenants:
137             dic_tenants.update({getattr(tenant, 'id'):
138                                 getattr(tenant, 'name')})
139     return {'tenants': dic_tenants}
140
141
142 def main():
143     creds_nova = os_utils.get_credentials("nova")
144     nova_client = novaclient.Client('2', **creds_nova)
145
146     creds_neutron = os_utils.get_credentials("neutron")
147     neutron_client = neutronclient.Client(**creds_neutron)
148
149     creds_keystone = os_utils.get_credentials("keystone")
150     keystone_client = keystoneclient.Client(**creds_keystone)
151
152     creds_cinder = os_utils.get_credentials("cinder")
153     cinder_client = cinderclient.Client('1', creds_cinder['username'],
154                                         creds_cinder['api_key'],
155                                         creds_cinder['project_id'],
156                                         creds_cinder['auth_url'],
157                                         service_type="volume")
158
159     if not os_utils.check_credentials():
160         logger.error("Please source the openrc credentials and run the" +
161                      "script again.")
162         exit(-1)
163
164     defaults = {}
165     defaults.update(get_instances(nova_client))
166     defaults.update(get_images(nova_client))
167     defaults.update(get_volumes(cinder_client))
168     defaults.update(get_networks(neutron_client))
169     defaults.update(get_routers(neutron_client))
170     defaults.update(get_security_groups(neutron_client))
171     defaults.update(get_floatinips(nova_client))
172     defaults.update(get_users(keystone_client))
173     defaults.update(get_tenants(keystone_client))
174
175     with open(DEFAULTS_FILE, 'w+') as yaml_file:
176         yaml_file.write(yaml.safe_dump(defaults, default_flow_style=False))
177         yaml_file.seek(0)
178         logger.info("Openstack Defaults found in the deployment:\n%s"
179                     % yaml_file.read())
180         logger.debug("NOTE: These objects will NOT be deleted after " +
181                      "running the tests.")
182
183
184 if __name__ == '__main__':
185     main()