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
24 from snaps.openstack.utils import glance_utils
26 __author__ = 'spisarski'
28 logger = logging.getLogger('openstack_tests')
30 CIRROS_DEFAULT_IMAGE_URL =\
31 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img'
32 CIRROS_DEFAULT_KERNEL_IMAGE_URL =\
33 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel'
34 CIRROS_DEFAULT_RAMDISK_IMAGE_URL =\
35 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs'
36 CIRROS_USER = 'cirros'
38 CENTOS_DEFAULT_IMAGE_URL =\
39 'http://cloud.centos.org/centos/7/images/' \
40 'CentOS-7-x86_64-GenericCloud.qcow2'
41 CENTOS_USER = 'centos'
43 UBUNTU_DEFAULT_IMAGE_URL = \
44 'http://uec-images.ubuntu.com/releases/trusty/14.04/' \
45 'ubuntu-14.04-server-cloudimg-amd64-disk1.img'
46 UBUNTU_USER = 'ubuntu'
48 DEFAULT_IMAGE_FORMAT = 'qcow2'
51 def get_credentials(os_env_file=None, proxy_settings_str=None,
52 ssh_proxy_cmd=None, dev_os_env_file=None):
54 Returns the OpenStack credentials object. It first attempts to retrieve
55 them from a standard OpenStack source file. If that file is None, it will
56 attempt to retrieve them with a YAML file.
57 :param os_env_file: the OpenStack source file
58 :param proxy_settings_str: proxy settings string <host>:<port> (optional)
59 :param ssh_proxy_cmd: the SSH proxy command for your environment (optional)
60 :param dev_os_env_file: the YAML file to retrieve both the OS credentials
62 :return: the SNAPS credentials object
65 logger.debug('Reading RC file - ' + os_env_file)
66 config = file_utils.read_os_env_file(os_env_file)
67 proj_name = config.get('OS_PROJECT_NAME')
69 proj_name = config.get('OS_TENANT_NAME')
71 proj_domain_id = 'default'
72 user_domain_id = 'default'
74 if config.get('OS_PROJECT_DOMAIN_ID'):
75 proj_domain_id = config['OS_PROJECT_DOMAIN_ID']
76 if config.get('OS_USER_DOMAIN_ID'):
77 user_domain_id = config['OS_USER_DOMAIN_ID']
78 if config.get('OS_IDENTITY_API_VERSION'):
79 version = int(config['OS_IDENTITY_API_VERSION'])
84 if proxy_settings_str:
85 tokens = re.split(':', proxy_settings_str)
86 proxy_settings = ProxySettings(host=tokens[0], port=tokens[1],
87 ssh_proxy_cmd=ssh_proxy_cmd)
89 if config.get('OS_CACERT'):
90 https_cacert = config.get('OS_CACERT')
91 elif config.get('OS_INSECURE'):
97 if config.get('OS_INTERFACE'):
98 interface = config.get('OS_INTERFACE')
100 os_creds = OSCreds(username=config['OS_USERNAME'],
101 password=config['OS_PASSWORD'],
102 auth_url=config['OS_AUTH_URL'],
103 project_name=proj_name,
104 identity_api_version=version,
105 user_domain_id=user_domain_id,
106 project_domain_id=proj_domain_id,
108 proxy_settings=proxy_settings,
111 logger.info('Reading development os_env file - ' + dev_os_env_file)
112 config = file_utils.read_yaml(dev_os_env_file)
113 identity_api_version = config.get('identity_api_version')
114 if not identity_api_version:
115 identity_api_version = 2
117 image_api_version = config.get('image_api_version')
118 if not image_api_version:
119 image_api_version = glance_utils.VERSION_2
121 proxy_settings = None
122 proxy_str = config.get('http_proxy')
124 tokens = re.split(':', proxy_str)
125 proxy_settings = ProxySettings(
126 host=tokens[0], port=tokens[1],
127 ssh_proxy_cmd=config.get('ssh_proxy_cmd'))
129 os_creds = OSCreds(username=config['username'],
130 password=config['password'],
131 auth_url=config['os_auth_url'],
132 project_name=config['project_name'],
133 identity_api_version=identity_api_version,
134 image_api_version=image_api_version,
135 proxy_settings=proxy_settings)
137 logger.info('OS Credentials = %s', os_creds)
141 def create_image_settings(image_name, image_user, image_format, metadata,
142 disk_url=None, default_url=None,
143 kernel_settings=None, ramdisk_settings=None,
144 public=False, nic_config_pb_loc=None):
146 Returns the image settings object
147 :param image_name: the name of the image
148 :param image_user: the image's sudo user
149 :param image_format: the image's format string
150 :param metadata: custom metadata for overriding default behavior for test
152 :param disk_url: the disk image's URL
153 :param default_url: the default URL for the disk image
154 :param kernel_settings: override to the kernel settings from the
156 :param ramdisk_settings: override to the ramdisk settings from the
158 :param public: True denotes image can be used by other projects where False
159 indicates the converse (default: False)
160 :param nic_config_pb_loc: The location of the playbook used for configuring
165 logger.debug('Image metadata - ' + str(metadata))
167 if metadata and 'config' in metadata:
168 return ImageSettings(**metadata['config'])
172 disk_url = metadata.get('disk_url')
173 disk_file = metadata.get('disk_file')
175 disk_url = default_url
180 ('kernel_file' in metadata or 'kernel_url' in metadata) and \
181 kernel_settings is None:
182 kernel_image_settings = ImageSettings(
183 name=image_name + '-kernel', image_user=image_user,
184 img_format=image_format,
185 image_file=metadata.get('kernel_file'),
186 url=metadata.get('kernel_url'), public=public)
188 kernel_image_settings = kernel_settings
191 ('ramdisk_file' in metadata or 'ramdisk_url' in metadata) and \
192 ramdisk_settings is None:
193 ramdisk_image_settings = ImageSettings(
194 name=image_name + '-ramdisk', image_user=image_user,
195 img_format=image_format,
196 image_file=metadata.get('ramdisk_file'),
197 url=metadata.get('ramdisk_url'), public=public)
199 ramdisk_image_settings = ramdisk_settings
201 extra_properties = None
202 if metadata and 'extra_properties' in metadata:
203 extra_properties = metadata['extra_properties']
205 return ImageSettings(name=image_name, image_user=image_user,
206 img_format=image_format, image_file=disk_file,
207 url=disk_url, extra_properties=extra_properties,
208 kernel_image_settings=kernel_image_settings,
209 ramdisk_image_settings=ramdisk_image_settings,
211 nic_config_pb_loc=nic_config_pb_loc)
214 def cirros_image_settings(name=None, url=None, image_metadata=None,
215 kernel_settings=None, ramdisk_settings=None,
218 Returns the image settings for a Cirros QCOW2 image
219 :param name: the name of the image
220 :param url: the image's URL
221 :param image_metadata: dict() values to override URLs for disk, kernel, and
223 :param kernel_settings: override to the kernel settings from the
225 :param ramdisk_settings: override to the ramdisk settings from the
227 :param public: True denotes image can be used by other projects where False
228 indicates the converse
231 if image_metadata and 'cirros' in image_metadata:
232 metadata = image_metadata['cirros']
234 metadata = image_metadata
236 return create_image_settings(
237 image_name=name, image_user=CIRROS_USER,
238 image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
239 default_url=CIRROS_DEFAULT_IMAGE_URL,
240 kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings,
244 def file_image_test_settings(name, file_path, image_user=CIRROS_USER):
245 return ImageSettings(name=name, image_user=image_user,
246 img_format=DEFAULT_IMAGE_FORMAT, image_file=file_path)
249 def centos_image_settings(name, url=None, image_metadata=None,
250 kernel_settings=None, ramdisk_settings=None,
253 Returns the image settings for a Centos QCOW2 image
254 :param name: the name of the image
255 :param url: the image's URL
256 :param image_metadata: dict() values to override URLs for disk, kernel, and
258 :param kernel_settings: override to the kernel settings from the
260 :param ramdisk_settings: override to the ramdisk settings from the
262 :param public: True denotes image can be used by other projects where False
263 indicates the converse
266 if image_metadata and 'centos' in image_metadata:
267 metadata = image_metadata['centos']
269 metadata = image_metadata
271 pb_path = pkg_resources.resource_filename(
272 'snaps.provisioning.ansible_pb.centos-network-setup.playbooks',
273 'configure_host.yml')
274 return create_image_settings(
275 image_name=name, image_user=CENTOS_USER,
276 image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
277 default_url=CENTOS_DEFAULT_IMAGE_URL,
278 kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings,
279 public=public, nic_config_pb_loc=pb_path)
282 def ubuntu_image_settings(name, url=None, image_metadata=None,
283 kernel_settings=None, ramdisk_settings=None,
286 Returns the image settings for a Ubuntu QCOW2 image
287 :param name: the name of the image
288 :param url: the image's URL
289 :param image_metadata: dict() values to override URLs for disk, kernel, and
291 :param kernel_settings: override to the kernel settings from the
293 :param ramdisk_settings: override to the ramdisk settings from the
295 :param public: True denotes image can be used by other projects where False
296 indicates the converse
299 if image_metadata and 'ubuntu' in image_metadata:
300 metadata = image_metadata['ubuntu']
302 metadata = image_metadata
304 pb_path = pkg_resources.resource_filename(
305 'snaps.provisioning.ansible_pb.ubuntu-network-setup.playbooks',
306 'configure_host.yml')
307 return create_image_settings(
308 image_name=name, image_user=UBUNTU_USER,
309 image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
310 default_url=UBUNTU_DEFAULT_IMAGE_URL,
311 kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings,
312 public=public, nic_config_pb_loc=pb_path)
315 def get_priv_net_config(net_name, subnet_name, router_name=None,
316 cidr='10.55.0.0/24', external_net=None):
317 return OSNetworkConfig(net_name, subnet_name, cidr, router_name,
318 external_gateway=external_net)
321 def get_pub_net_config(net_name, subnet_name=None, router_name=None,
322 cidr='10.55.1.0/24', external_net=None):
323 return OSNetworkConfig(net_name, subnet_name, cidr, router_name,
324 external_gateway=external_net)
327 class OSNetworkConfig:
329 Represents the settings required for the creation of a network in OpenStack
332 def __init__(self, net_name, subnet_name=None, subnet_cidr=None,
333 router_name=None, external_gateway=None):
335 if subnet_name and subnet_cidr:
336 self.network_settings = NetworkSettings(
337 name=net_name, subnet_settings=[
338 SubnetSettings(cidr=subnet_cidr, name=subnet_name)])
340 self.network_settings = NetworkSettings(name=net_name)
344 self.router_settings = RouterSettings(
345 name=router_name, external_gateway=external_gateway,
346 internal_subnets=[subnet_name])
348 self.router_settings = RouterSettings(
349 name=router_name, external_gateway=external_gateway)