1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 from snaps import file_utils
20 from snaps.openstack.create_image import ImageSettings
21 from snaps.openstack.create_network import NetworkSettings, SubnetSettings
22 from snaps.openstack.create_router import RouterSettings
23 from snaps.openstack.os_credentials import OSCreds, ProxySettings
25 __author__ = 'spisarski'
27 logger = logging.getLogger('openstack_tests')
29 CIRROS_DEFAULT_IMAGE_URL =\
30 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img'
31 CIRROS_DEFAULT_KERNEL_IMAGE_URL =\
32 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel'
33 CIRROS_DEFAULT_RAMDISK_IMAGE_URL =\
34 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs'
35 CIRROS_USER = 'cirros'
37 CENTOS_DEFAULT_IMAGE_URL =\
38 'http://cloud.centos.org/centos/7/images/' \
39 'CentOS-7-x86_64-GenericCloud.qcow2'
40 CENTOS_USER = 'centos'
42 UBUNTU_DEFAULT_IMAGE_URL = \
43 'http://uec-images.ubuntu.com/releases/trusty/14.04/' \
44 'ubuntu-14.04-server-cloudimg-amd64-disk1.img'
45 UBUNTU_USER = 'ubuntu'
47 DEFAULT_IMAGE_FORMAT = 'qcow2'
50 def get_credentials(os_env_file=None, proxy_settings_str=None,
51 ssh_proxy_cmd=None, dev_os_env_file=None):
53 Returns the OpenStack credentials object. It first attempts to retrieve
54 them from a standard OpenStack source file. If that file is None, it will
55 attempt to retrieve them with a YAML file.
56 :param os_env_file: the OpenStack source file
57 :param proxy_settings_str: proxy settings string <host>:<port> (optional)
58 :param ssh_proxy_cmd: the SSH proxy command for your environment (optional)
59 :param dev_os_env_file: the YAML file to retrieve both the OS credentials
61 :return: the SNAPS credentials object
64 logger.debug('Reading RC file - ' + os_env_file)
65 config = file_utils.read_os_env_file(os_env_file)
66 proj_name = config.get('OS_PROJECT_NAME')
68 proj_name = config.get('OS_TENANT_NAME')
71 if proxy_settings_str:
72 tokens = re.split(':', proxy_settings_str)
73 proxy_settings = ProxySettings(host=tokens[0], port=tokens[1],
74 ssh_proxy_cmd=ssh_proxy_cmd)
77 if config.get('OS_CACERT'):
78 https_cacert = config.get('OS_CACERT')
79 elif config.get('OS_INSECURE'):
83 if config.get('OS_INTERFACE'):
84 interface = config.get('OS_INTERFACE')
87 'username': config['OS_USERNAME'],
88 'password': config['OS_PASSWORD'],
89 'auth_url': config['OS_AUTH_URL'],
90 'project_name': proj_name,
91 'identity_api_version': config.get('OS_IDENTITY_API_VERSION'),
92 'image_api_version': config.get('OS_IMAGE_API_VERSION'),
93 'network_api_version': config.get('OS_NETWORK_API_VERSION'),
94 'compute_api_version': config.get('OS_COMPUTE_API_VERSION'),
95 'heat_api_version': config.get('OS_HEAT_API_VERSION'),
96 'user_domain_id': config.get(
97 'OS_USER_DOMAIN_ID', config.get('OS_USER_DOMAIN_NAME')),
98 'project_domain_id': config.get(
99 'OS_PROJECT_DOMAIN_ID', config.get('OS_PROJECT_DOMAIN_NAME')),
100 'interface': interface,
101 'proxy_settings': proxy_settings,
102 'cacert': https_cacert,
103 'region_name': config.get('OS_REGION_NAME')}
105 logger.info('Reading development os_env file - ' + dev_os_env_file)
106 config = file_utils.read_yaml(dev_os_env_file)
108 proxy_settings = None
109 proxy_str = config.get('http_proxy')
111 tokens = re.split(':', proxy_str)
112 proxy_settings = ProxySettings(
113 host=tokens[0], port=tokens[1],
114 ssh_proxy_cmd=config.get('ssh_proxy_cmd'))
117 'username': config['username'],
118 'password': config['password'],
119 'auth_url': config['os_auth_url'],
120 'project_name': config['project_name'],
121 'identity_api_version': config.get('identity_api_version'),
122 'image_api_version': config.get('image_api_version'),
123 'network_api_version': config.get('network_api_version'),
124 'compute_api_version': config.get('compute_api_version'),
125 'heat_api_version': config.get('heat_api_version'),
126 'user_domain_id': config.get('user_domain_id'),
127 'project_domain_id': config.get('project_domain_id'),
128 'interface': config.get('interface'),
129 'proxy_settings': proxy_settings,
130 'cacert': config.get('cacert'),
131 'region_name': config.get('region_name')}
133 os_creds = OSCreds(**creds_dict)
134 logger.info('OS Credentials = %s', os_creds)
138 def create_image_settings(image_name, image_user, image_format, metadata,
139 disk_url=None, default_url=None,
140 kernel_settings=None, ramdisk_settings=None,
141 public=False, nic_config_pb_loc=None):
143 Returns the image settings object
144 :param image_name: the name of the image
145 :param image_user: the image's sudo user
146 :param image_format: the image's format string
147 :param metadata: custom metadata for overriding default behavior for test
149 :param disk_url: the disk image's URL
150 :param default_url: the default URL for the disk image
151 :param kernel_settings: override to the kernel settings from the
153 :param ramdisk_settings: override to the ramdisk settings from the
155 :param public: True denotes image can be used by other projects where False
156 indicates the converse (default: False)
157 :param nic_config_pb_loc: The location of the playbook used for configuring
162 logger.debug('Image metadata - ' + str(metadata))
164 if metadata and 'config' in metadata:
165 return ImageSettings(**metadata['config'])
169 disk_url = metadata.get('disk_url')
170 disk_file = metadata.get('disk_file')
172 disk_url = default_url
177 ('kernel_file' in metadata or 'kernel_url' in metadata) and \
178 kernel_settings is None:
179 kernel_image_settings = ImageSettings(
180 name=image_name + '-kernel', image_user=image_user,
181 img_format=image_format,
182 image_file=metadata.get('kernel_file'),
183 url=metadata.get('kernel_url'), public=public)
185 kernel_image_settings = kernel_settings
188 ('ramdisk_file' in metadata or 'ramdisk_url' in metadata) and \
189 ramdisk_settings is None:
190 ramdisk_image_settings = ImageSettings(
191 name=image_name + '-ramdisk', image_user=image_user,
192 img_format=image_format,
193 image_file=metadata.get('ramdisk_file'),
194 url=metadata.get('ramdisk_url'), public=public)
196 ramdisk_image_settings = ramdisk_settings
198 extra_properties = None
199 if metadata and 'extra_properties' in metadata:
200 extra_properties = metadata['extra_properties']
202 return ImageSettings(name=image_name, image_user=image_user,
203 img_format=image_format, image_file=disk_file,
204 url=disk_url, extra_properties=extra_properties,
205 kernel_image_settings=kernel_image_settings,
206 ramdisk_image_settings=ramdisk_image_settings,
208 nic_config_pb_loc=nic_config_pb_loc)
211 def cirros_image_settings(name=None, url=None, image_metadata=None,
212 kernel_settings=None, ramdisk_settings=None,
215 Returns the image settings for a Cirros QCOW2 image
216 :param name: the name of the image
217 :param url: the image's URL
218 :param image_metadata: dict() values to override URLs for disk, kernel, and
220 :param kernel_settings: override to the kernel settings from the
222 :param ramdisk_settings: override to the ramdisk settings from the
224 :param public: True denotes image can be used by other projects where False
225 indicates the converse
228 if image_metadata and 'cirros' in image_metadata:
229 metadata = image_metadata['cirros']
231 metadata = image_metadata
233 return create_image_settings(
234 image_name=name, image_user=CIRROS_USER,
235 image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
236 default_url=CIRROS_DEFAULT_IMAGE_URL,
237 kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings,
241 def file_image_test_settings(name, file_path, image_user=CIRROS_USER):
242 return ImageSettings(name=name, image_user=image_user,
243 img_format=DEFAULT_IMAGE_FORMAT, image_file=file_path)
246 def centos_image_settings(name, url=None, image_metadata=None,
247 kernel_settings=None, ramdisk_settings=None,
250 Returns the image settings for a Centos QCOW2 image
251 :param name: the name of the image
252 :param url: the image's URL
253 :param image_metadata: dict() values to override URLs for disk, kernel, and
255 :param kernel_settings: override to the kernel settings from the
257 :param ramdisk_settings: override to the ramdisk settings from the
259 :param public: True denotes image can be used by other projects where False
260 indicates the converse
263 if image_metadata and 'centos' in image_metadata:
264 metadata = image_metadata['centos']
266 metadata = image_metadata
268 pb_path = pkg_resources.resource_filename(
269 'snaps.provisioning.ansible_pb.centos-network-setup.playbooks',
270 'configure_host.yml')
271 return create_image_settings(
272 image_name=name, image_user=CENTOS_USER,
273 image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
274 default_url=CENTOS_DEFAULT_IMAGE_URL,
275 kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings,
276 public=public, nic_config_pb_loc=pb_path)
279 def ubuntu_image_settings(name, url=None, image_metadata=None,
280 kernel_settings=None, ramdisk_settings=None,
283 Returns the image settings for a Ubuntu QCOW2 image
284 :param name: the name of the image
285 :param url: the image's URL
286 :param image_metadata: dict() values to override URLs for disk, kernel, and
288 :param kernel_settings: override to the kernel settings from the
290 :param ramdisk_settings: override to the ramdisk settings from the
292 :param public: True denotes image can be used by other projects where False
293 indicates the converse
296 if image_metadata and 'ubuntu' in image_metadata:
297 metadata = image_metadata['ubuntu']
299 metadata = image_metadata
301 pb_path = pkg_resources.resource_filename(
302 'snaps.provisioning.ansible_pb.ubuntu-network-setup.playbooks',
303 'configure_host.yml')
304 return create_image_settings(
305 image_name=name, image_user=UBUNTU_USER,
306 image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
307 default_url=UBUNTU_DEFAULT_IMAGE_URL,
308 kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings,
309 public=public, nic_config_pb_loc=pb_path)
312 def get_priv_net_config(net_name, subnet_name, router_name=None,
313 cidr='10.55.0.0/24', external_net=None):
314 return OSNetworkConfig(net_name, subnet_name, cidr, router_name,
315 external_gateway=external_net)
318 def get_pub_net_config(net_name, subnet_name=None, router_name=None,
319 cidr='10.55.1.0/24', external_net=None):
320 return OSNetworkConfig(net_name, subnet_name, cidr, router_name,
321 external_gateway=external_net)
324 class OSNetworkConfig:
326 Represents the settings required for the creation of a network in OpenStack
329 def __init__(self, net_name, subnet_name=None, subnet_cidr=None,
330 router_name=None, external_gateway=None):
332 if subnet_name and subnet_cidr:
333 self.network_settings = NetworkSettings(
334 name=net_name, subnet_settings=[
335 SubnetSettings(cidr=subnet_cidr, name=subnet_name)])
337 self.network_settings = NetworkSettings(name=net_name)
341 self.router_settings = RouterSettings(
342 name=router_name, external_gateway=external_gateway,
343 internal_subnets=[subnet_name])
345 self.router_settings = RouterSettings(
346 name=router_name, external_gateway=external_gateway)