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.
18 from snaps import file_utils
19 from snaps.openstack.utils import glance_utils
20 from snaps.openstack.create_network import NetworkSettings, SubnetSettings
21 from snaps.openstack.create_router import RouterSettings
22 from snaps.openstack.os_credentials import OSCreds, ProxySettings
23 from snaps.openstack.create_image import ImageSettings
26 __author__ = 'spisarski'
29 logger = logging.getLogger('openstack_tests')
31 CIRROS_DEFAULT_IMAGE_URL = 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img'
32 CIRROS_DEFAULT_KERNEL_IMAGE_URL = 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel'
33 CIRROS_DEFAULT_RAMDISK_IMAGE_URL = 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs'
34 CIRROS_USER = 'cirros'
36 CENTOS_DEFAULT_IMAGE_URL = 'http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2'
37 CENTOS_USER = 'centos'
39 UBUNTU_DEFAULT_IMAGE_URL =\
40 'http://uec-images.ubuntu.com/releases/trusty/14.04/ubuntu-14.04-server-cloudimg-amd64-disk1.img'
41 UBUNTU_USER = 'ubuntu'
43 DEFAULT_IMAGE_FORMAT = 'qcow2'
46 def get_credentials(os_env_file=None, proxy_settings_str=None, ssh_proxy_cmd=None, dev_os_env_file=None):
48 Returns the OpenStack credentials object. It first attempts to retrieve them from a standard OpenStack source file.
49 If that file is None, it will attempt to retrieve them with a YAML file.
50 it will retrieve them from a
51 :param os_env_file: the OpenStack source file
52 :param proxy_settings_str: proxy settings string <host>:<port> (optional)
53 :param ssh_proxy_cmd: the SSH proxy command for your environment (optional)
54 :param dev_os_env_file: the YAML file to retrieve both the OS credentials and proxy settings
55 :return: the SNAPS credentials object
58 logger.debug('Reading RC file - ' + os_env_file)
59 config = file_utils.read_os_env_file(os_env_file)
60 proj_name = config.get('OS_PROJECT_NAME')
62 proj_name = config.get('OS_TENANT_NAME')
64 proj_domain_id = 'default'
65 user_domain_id = 'default'
67 if config.get('OS_PROJECT_DOMAIN_ID'):
68 proj_domain_id = config['OS_PROJECT_DOMAIN_ID']
69 if config.get('OS_USER_DOMAIN_ID'):
70 user_domain_id = config['OS_USER_DOMAIN_ID']
71 if config.get('OS_IDENTITY_API_VERSION'):
72 version = int(config['OS_IDENTITY_API_VERSION'])
77 if proxy_settings_str:
78 tokens = re.split(':', proxy_settings_str)
79 proxy_settings = ProxySettings(tokens[0], tokens[1], ssh_proxy_cmd)
81 os_creds = OSCreds(username=config['OS_USERNAME'],
82 password=config['OS_PASSWORD'],
83 auth_url=config['OS_AUTH_URL'],
84 project_name=proj_name,
85 identity_api_version=version,
86 user_domain_id=user_domain_id,
87 project_domain_id=proj_domain_id,
88 proxy_settings=proxy_settings)
90 logger.info('Reading development os_env file - ' + dev_os_env_file)
91 config = file_utils.read_yaml(dev_os_env_file)
92 identity_api_version = config.get('identity_api_version')
93 if not identity_api_version:
94 identity_api_version = 2
96 image_api_version = config.get('image_api_version')
97 if not image_api_version:
98 image_api_version = glance_utils.VERSION_2
100 proxy_settings = None
101 proxy_str = config.get('http_proxy')
103 tokens = re.split(':', proxy_str)
104 proxy_settings = ProxySettings(tokens[0], tokens[1], config.get('ssh_proxy_cmd'))
106 os_creds = OSCreds(username=config['username'], password=config['password'],
107 auth_url=config['os_auth_url'], project_name=config['project_name'],
108 identity_api_version=identity_api_version, image_api_version=image_api_version,
109 proxy_settings=proxy_settings)
111 logger.info('OS Credentials = ' + str(os_creds))
115 def create_image_settings(image_name, image_user, image_format, metadata, disk_url=None, default_url=None,
116 kernel_settings=None, ramdisk_settings=None, public=False, nic_config_pb_loc=None):
118 Returns the image settings object
119 :param image_name: the name of the image
120 :param image_user: the image's sudo user
121 :param image_format: the image's format string
122 :param metadata: custom metadata for overriding default behavior for test image settings
123 :param disk_url: the disk image's URL
124 :param default_url: the default URL for the disk image
125 :param kernel_settings: override to the kernel settings from the image_metadata
126 :param ramdisk_settings: override to the ramdisk settings from the image_metadata
127 :param public: True denotes image can be used by other projects where False indicates the converse (default: False)
128 :param nic_config_pb_loc: The location of the playbook used for configuring multiple NICs
132 logger.debug('Image metadata - ' + str(metadata))
134 if metadata and 'config' in metadata:
135 return ImageSettings(config=metadata['config'])
139 disk_url = metadata.get('disk_url')
140 disk_file = metadata.get('disk_file')
142 disk_url = default_url
146 if metadata and ('kernel_file' in metadata or 'kernel_url' in metadata) and kernel_settings is None:
147 kernel_image_settings = ImageSettings(
148 name=image_name + '-kernel', image_user=image_user, img_format=image_format,
149 image_file=metadata.get('kernel_file'), url=metadata.get('kernel_url'), public=public)
151 kernel_image_settings = kernel_settings
153 if metadata and ('ramdisk_file' in metadata or 'ramdisk_url' in metadata) and ramdisk_settings is None:
154 ramdisk_image_settings = ImageSettings(
155 name=image_name + '-ramdisk', image_user=image_user, img_format=image_format,
156 image_file=metadata.get('ramdisk_file'), url=metadata.get('ramdisk_url'), public=public)
158 ramdisk_image_settings = ramdisk_settings
160 extra_properties = None
161 if metadata and 'extra_properties' in metadata:
162 extra_properties = metadata['extra_properties']
164 return ImageSettings(name=image_name, image_user=image_user, img_format=image_format, image_file=disk_file,
165 url=disk_url, extra_properties=extra_properties, kernel_image_settings=kernel_image_settings,
166 ramdisk_image_settings=ramdisk_image_settings, public=public,
167 nic_config_pb_loc=nic_config_pb_loc)
170 def cirros_image_settings(name=None, url=None, image_metadata=None, kernel_settings=None, ramdisk_settings=None,
173 Returns the image settings for a Cirros QCOW2 image
174 :param name: the name of the image
175 :param url: the image's URL
176 :param image_metadata: dict() values to override URLs for disk, kernel, and ramdisk
177 :param kernel_settings: override to the kernel settings from the image_metadata
178 :param ramdisk_settings: override to the ramdisk settings from the image_metadata
179 :param public: True denotes image can be used by other projects where False indicates the converse
182 if image_metadata and 'cirros' in image_metadata:
183 metadata = image_metadata['cirros']
185 metadata = image_metadata
187 return create_image_settings(
188 image_name=name, image_user=CIRROS_USER, image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
189 default_url=CIRROS_DEFAULT_IMAGE_URL,
190 kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings, public=public)
193 def file_image_test_settings(name, file_path, image_user=CIRROS_USER):
194 return ImageSettings(name=name, image_user=image_user, img_format=DEFAULT_IMAGE_FORMAT, image_file=file_path)
197 def centos_image_settings(name, url=None, image_metadata=None, kernel_settings=None, ramdisk_settings=None,
200 Returns the image settings for a Centos QCOW2 image
201 :param name: the name of the image
202 :param url: the image's URL
203 :param image_metadata: dict() values to override URLs for disk, kernel, and ramdisk
204 :param kernel_settings: override to the kernel settings from the image_metadata
205 :param ramdisk_settings: override to the ramdisk settings from the image_metadata
206 :param public: True denotes image can be used by other projects where False indicates the converse
209 if image_metadata and 'centos' in image_metadata:
210 metadata = image_metadata['centos']
212 metadata = image_metadata
214 pb_path = pkg_resources.resource_filename('snaps.provisioning.ansible.centos-network-setup.playbooks',
215 'configure_host.yml')
216 return create_image_settings(
217 image_name=name, image_user=CENTOS_USER, image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
218 default_url=CENTOS_DEFAULT_IMAGE_URL,
219 kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings, public=public, nic_config_pb_loc=pb_path)
222 def ubuntu_image_settings(name, url=None, image_metadata=None, kernel_settings=None, ramdisk_settings=None,
225 Returns the image settings for a Ubuntu QCOW2 image
226 :param name: the name of the image
227 :param url: the image's URL
228 :param image_metadata: dict() values to override URLs for disk, kernel, and ramdisk
229 :param kernel_settings: override to the kernel settings from the image_metadata
230 :param ramdisk_settings: override to the ramdisk settings from the image_metadata
231 :param public: True denotes image can be used by other projects where False indicates the converse
234 if image_metadata and 'ubuntu' in image_metadata:
235 metadata = image_metadata['ubuntu']
237 metadata = image_metadata
239 pb_path = pkg_resources.resource_filename('snaps.provisioning.ansible.ubuntu-network-setup.playbooks',
240 'configure_host.yml')
241 return create_image_settings(
242 image_name=name, image_user=UBUNTU_USER, image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
243 default_url=UBUNTU_DEFAULT_IMAGE_URL,
244 kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings, public=public, nic_config_pb_loc=pb_path)
247 def get_priv_net_config(net_name, subnet_name, router_name=None, cidr='10.55.0.0/24', external_net=None):
248 return OSNetworkConfig(net_name, subnet_name, cidr, router_name, external_gateway=external_net)
251 def get_pub_net_config(net_name, subnet_name=None, router_name=None, cidr='10.55.1.0/24', external_net=None):
252 return OSNetworkConfig(net_name, subnet_name, cidr, router_name, external_gateway=external_net)
255 class OSNetworkConfig:
257 Represents the settings required for the creation of a network in OpenStack
260 def __init__(self, net_name, subnet_name=None, subnet_cidr=None, router_name=None, external_gateway=None):
262 if subnet_name and subnet_cidr:
263 self.network_settings = NetworkSettings(
264 name=net_name, subnet_settings=[SubnetSettings(cidr=subnet_cidr, name=subnet_name)])
266 self.network_settings = NetworkSettings(name=net_name)
270 self.router_settings = RouterSettings(name=router_name, external_gateway=external_gateway,
271 internal_subnets=[subnet_name])
273 self.router_settings = RouterSettings(name=router_name, external_gateway=external_gateway)