3 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
4 # and others. All rights reserved.
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at:
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # This script is responsible for deploying virtual environments
24 from jinja2 import Environment, FileSystemLoader
28 from snaps import file_utils
29 from snaps.openstack.create_flavor import FlavorSettings, OpenStackFlavor
30 from snaps.openstack.create_image import ImageSettings, OpenStackImage
31 from snaps.openstack.create_instance import VmInstanceSettings
32 from snaps.openstack.create_keypairs import KeypairSettings, OpenStackKeypair
33 from snaps.openstack.create_network import (
34 PortSettings, NetworkSettings, OpenStackNetwork)
35 from snaps.openstack.create_project import OpenStackProject, ProjectSettings
36 from snaps.openstack.create_qos import QoSSettings, OpenStackQoS
37 from snaps.openstack.create_router import RouterSettings, OpenStackRouter
38 from snaps.openstack.create_security_group import (
39 OpenStackSecurityGroup, SecurityGroupSettings)
40 from snaps.openstack.create_user import OpenStackUser, UserSettings
41 from snaps.openstack.create_volume import OpenStackVolume, VolumeSettings
42 from snaps.openstack.create_volume_type import (
43 OpenStackVolumeType, VolumeTypeSettings)
44 from snaps.openstack.os_credentials import OSCreds, ProxySettings
45 from snaps.openstack.utils import deploy_utils
46 from snaps.provisioning import ansible_utils
48 __author__ = 'spisarski'
50 logger = logging.getLogger('snaps_launcher')
52 ARG_NOT_SET = "argument not set"
53 DEFAULT_CREDS_KEY = 'admin'
56 def __get_creds_dict(os_conn_config):
58 Returns a dict of OSCreds where the key is the creds name.
59 For backwards compatibility, credentials not contained in a list (only
60 one) will be returned with the key of None
61 :param os_conn_config: the credential configuration
62 :return: a dict of OSCreds objects
64 if 'connection' in os_conn_config:
65 return {DEFAULT_CREDS_KEY: __get_os_credentials(os_conn_config)}
66 elif 'connections' in os_conn_config:
68 for os_conn_dict in os_conn_config['connections']:
69 config = os_conn_dict.get('connection')
71 raise Exception('Invalid connection format')
73 name = config.get('name')
75 raise Exception('Connection config requires a name field')
77 out[name] = __get_os_credentials(os_conn_dict)
81 def __get_creds(os_creds_dict, os_user_dict, inst_config):
83 Returns the appropriate credentials
84 :param os_creds_dict: a dictionary of OSCreds objects where the name is the
86 :param os_user_dict: a dictionary of OpenStackUser objects where the name
89 :return: an OSCreds instance or None
91 os_creds = os_creds_dict.get(DEFAULT_CREDS_KEY)
92 if 'os_user' in inst_config:
93 os_user_conf = inst_config['os_user']
94 if 'name' in os_user_conf:
95 user_creator = os_user_dict.get(os_user_conf['name'])
97 return user_creator.get_os_creds(
98 project_name=os_user_conf.get('project_name'))
99 elif 'os_creds_name' in inst_config:
100 if 'os_creds_name' in inst_config:
101 os_creds = os_creds_dict[inst_config['os_creds_name']]
105 def __get_os_credentials(os_conn_config):
107 Returns an object containing all of the information required to access
109 :param os_conn_config: The configuration holding the credentials
110 :return: an OSCreds instance
112 config = os_conn_config.get('connection')
114 raise Exception('Invalid connection configuration')
116 proxy_settings = None
117 http_proxy = config.get('http_proxy')
119 tokens = re.split(':', http_proxy)
120 ssh_proxy_cmd = config.get('ssh_proxy_cmd')
121 proxy_settings = ProxySettings(host=tokens[0], port=tokens[1],
122 ssh_proxy_cmd=ssh_proxy_cmd)
124 if 'proxy_settings' in config:
125 host = config['proxy_settings'].get('host')
126 port = config['proxy_settings'].get('port')
127 if host and host != 'None' and port and port != 'None':
128 proxy_settings = ProxySettings(**config['proxy_settings'])
131 config['proxy_settings'] = proxy_settings
133 if config.get('proxy_settings'):
134 del config['proxy_settings']
136 return OSCreds(**config)
139 def __parse_ports_config(config):
141 Parses the "ports" configuration
142 :param config: The dictionary to parse
143 :return: a list of PortConfig objects
146 for port_config in config:
147 out.append(PortSettings(**port_config.get('port')))
151 def __create_instances(os_creds_dict, creator_class, config_class, config,
152 config_key, cleanup=False, os_users_dict=None):
154 Returns a dictionary of SNAPS creator objects where the key is the name
155 :param os_creds_dict: Dictionary of OSCreds objects where the key is the
157 :param config: The list of configurations for the same type
158 :param config_key: The list of configurations for the same type
159 :param cleanup: Denotes whether or not this is being called for cleanup
166 for config_dict in config:
167 inst_config = config_dict.get(config_key)
169 creator = creator_class(
170 __get_creds(os_creds_dict, os_users_dict, inst_config),
171 config_class(**inst_config))
177 out[inst_config['name']] = creator
178 logger.info('Created configured %s', config_key)
179 except Exception as e:
180 logger.error('Unexpected error instantiating creator [%s] '
181 'with exception %s', creator_class, e)
186 def __create_vm_instances(os_creds_dict, os_users_dict, instances_config,
187 image_dict, keypairs_dict, cleanup=False):
189 Returns a dictionary of OpenStackVmInstance objects where the key is the
191 :param os_creds_dict: Dictionary of OSCreds objects where the key is the
193 :param os_users_dict: Dictionary of OpenStackUser objects where the key is
195 :param instances_config: The list of VM instance configurations
196 :param image_dict: A dictionary of images that will probably be used to
197 instantiate the VM instance
198 :param keypairs_dict: A dictionary of keypairs that will probably be used
199 to instantiate the VM instance
200 :param cleanup: Denotes whether or not this is being called for cleanup
207 for instance_config in instances_config:
208 conf = instance_config.get('instance')
211 image_creator = image_dict.get(conf.get('imageName'))
213 instance_settings = VmInstanceSettings(
214 **instance_config['instance'])
215 kp_name = conf.get('keypair_name')
217 'name']] = deploy_utils.create_vm_instance(
219 os_creds_dict, os_users_dict, conf),
221 image_creator.image_settings,
222 keypair_creator=keypairs_dict[kp_name],
225 raise Exception('Image creator instance not found.'
226 ' Cannot instantiate')
228 raise Exception('Image dictionary is None. Cannot '
231 raise Exception('Instance configuration is None. Cannot '
233 logger.info('Created configured instances')
234 except Exception as e:
235 logger.error('Unexpected error creating VM instances - %s', e)
239 def __apply_ansible_playbooks(ansible_configs, os_creds_dict, vm_dict,
240 image_dict, flavor_dict, env_file):
242 Applies ansible playbooks to running VMs with floating IPs
243 :param ansible_configs: a list of Ansible configurations
244 :param os_creds_dict: Dictionary of OSCreds objects where the key is the
246 :param vm_dict: the dictionary of newly instantiated VMs where the name is
248 :param image_dict: the dictionary of newly instantiated images where the
250 :param flavor_dict: the dictionary of newly instantiated flavors where the
252 :param env_file: the path of the environment for setting the CWD so
253 playbook location is relative to the deployment file
254 :return: t/f - true if successful
256 logger.info("Applying Ansible Playbooks")
258 # Ensure all hosts are accepting SSH session requests
259 for vm_inst in list(vm_dict.values()):
260 if not vm_inst.vm_ssh_active(block=True):
262 "Timeout waiting for instance to respond to SSH requests")
265 # Set CWD so the deployment file's playbook location can leverage
267 orig_cwd = os.getcwd()
268 env_dir = os.path.dirname(env_file)
272 for ansible_config in ansible_configs:
273 if 'pre_sleep_time' in ansible_config:
275 sleep_time = int(ansible_config['pre_sleep_time'])
276 logger.info('Waiting %s seconds to apply playbooks',
278 time.sleep(sleep_time)
282 os_creds = os_creds_dict.get(None, 'admin')
283 __apply_ansible_playbook(ansible_config, os_creds, vm_dict,
284 image_dict, flavor_dict)
286 # Return to original directory
292 def __apply_ansible_playbook(ansible_config, os_creds, vm_dict, image_dict,
295 Applies an Ansible configuration setting
296 :param ansible_config: the configuration settings
297 :param os_creds: the OpenStack credentials object
298 :param vm_dict: the dictionary of newly instantiated VMs where the name is
300 :param image_dict: the dictionary of newly instantiated images where the
302 :param flavor_dict: the dictionary of newly instantiated flavors where the
306 (remote_user, floating_ips, private_key_filepath,
307 proxy_settings) = __get_connection_info(
308 ansible_config, vm_dict)
310 retval = ansible_utils.apply_playbook(
311 ansible_config['playbook_location'], floating_ips, remote_user,
312 private_key_filepath,
313 variables=__get_variables(ansible_config.get('variables'),
314 os_creds, vm_dict, image_dict,
316 proxy_setting=proxy_settings)
318 # Not a fatal type of event
320 'Unable to apply playbook found at location - %s',
321 ansible_config.get('playbook_location'))
324 def __get_connection_info(ansible_config, vm_dict):
326 Returns a tuple of data required for connecting to the running VMs
327 (remote_user, [floating_ips], private_key_filepath, proxy_settings)
328 :param ansible_config: the configuration settings
329 :param vm_dict: the dictionary of VMs where the VM name is the key
330 :return: tuple where the first element is the user and the second is a list
331 of floating IPs and the third is the
332 private key file location and the fourth is an instance of the
333 snaps.ProxySettings class
334 (note: in order to work, each of the hosts need to have the same sudo_user
335 and private key file location values)
337 if ansible_config.get('hosts'):
338 hosts = ansible_config['hosts']
340 floating_ips = list()
343 proxy_settings = None
345 vm = vm_dict.get(host)
347 fip = vm.get_floating_ip()
349 remote_user = vm.get_image_user()
352 floating_ips.append(fip.ip)
355 'Could not find floating IP for VM - ' +
358 pk_file = vm.keypair_settings.private_filepath
359 proxy_settings = vm.get_os_creds().proxy_settings
361 logger.error('Could not locate VM with name - ' + host)
363 return remote_user, floating_ips, pk_file, proxy_settings
367 def __get_variables(var_config, os_creds, vm_dict, image_dict, flavor_dict):
369 Returns a dictionary of substitution variables to be used for Ansible
371 :param var_config: the variable configuration settings
372 :param os_creds: the OpenStack credentials object
373 :param vm_dict: the dictionary of newly instantiated VMs where the name is
375 :param image_dict: the dictionary of newly instantiated images where the
377 :param flavor_dict: the dictionary of newly instantiated flavors where the
379 :return: dictionary or None
381 if var_config and vm_dict and len(vm_dict) > 0:
383 for key, value in var_config.items():
384 value = __get_variable_value(value, os_creds, vm_dict, image_dict,
387 variables[key] = value
389 "Set Jinga2 variable with key [%s] the value [%s]",
392 logger.warning('Key [%s] or Value [%s] must not be None',
393 str(key), str(value))
398 def __get_variable_value(var_config_values, os_creds, vm_dict, image_dict,
401 Returns the associated variable value for use by Ansible for substitution
403 :param var_config_values: the configuration dictionary
404 :param os_creds: the OpenStack credentials object
405 :param vm_dict: the dictionary of newly instantiated VMs where the name is
407 :param image_dict: the dictionary of newly instantiated images where the
409 :param flavor_dict: the dictionary of newly instantiated flavors where the
413 if var_config_values['type'] == 'string':
414 return __get_string_variable_value(var_config_values)
415 if var_config_values['type'] == 'vm-attr':
416 return __get_vm_attr_variable_value(var_config_values, vm_dict)
417 if var_config_values['type'] == 'os_creds':
418 return __get_os_creds_variable_value(var_config_values, os_creds)
419 if var_config_values['type'] == 'port':
420 return __get_vm_port_variable_value(var_config_values, vm_dict)
421 if var_config_values['type'] == 'floating_ip':
422 return __get_vm_fip_variable_value(var_config_values, vm_dict)
423 if var_config_values['type'] == 'image':
424 return __get_image_variable_value(var_config_values, image_dict)
425 if var_config_values['type'] == 'flavor':
426 return __get_flavor_variable_value(var_config_values, flavor_dict)
430 def __get_string_variable_value(var_config_values):
432 Returns the associated string value
433 :param var_config_values: the configuration dictionary
434 :return: the value contained in the dictionary with the key 'value'
436 return var_config_values['value']
439 def __get_vm_attr_variable_value(var_config_values, vm_dict):
441 Returns the associated value contained on a VM instance
442 :param var_config_values: the configuration dictionary
443 :param vm_dict: the dictionary containing all VMs where the key is the VM's
447 vm = vm_dict.get(var_config_values['vm_name'])
449 if var_config_values['value'] == 'floating_ip':
450 return vm.get_floating_ip().ip
451 if var_config_values['value'] == 'image_user':
452 return vm.get_image_user()
455 def __get_os_creds_variable_value(var_config_values, os_creds):
457 Returns the associated OS credentials value
458 :param var_config_values: the configuration dictionary
459 :param os_creds: the credentials
462 logger.info("Retrieving OS Credentials")
464 if var_config_values['value'] == 'username':
465 logger.info("Returning OS username")
466 return os_creds.username
467 elif var_config_values['value'] == 'password':
468 logger.info("Returning OS password")
469 return os_creds.password
470 elif var_config_values['value'] == 'auth_url':
471 logger.info("Returning OS auth_url")
472 return os_creds.auth_url
473 elif var_config_values['value'] == 'project_name':
474 logger.info("Returning OS project_name")
475 return os_creds.project_name
477 logger.info("Returning none")
481 def __get_vm_port_variable_value(var_config_values, vm_dict):
483 Returns the associated OS credentials value
484 :param var_config_values: the configuration dictionary
485 :param vm_dict: the dictionary containing all VMs where the key is the VM's
489 port_name = var_config_values.get('port_name')
490 vm_name = var_config_values.get('vm_name')
492 if port_name and vm_name:
493 vm = vm_dict.get(vm_name)
495 port_value_id = var_config_values.get('port_value')
497 if port_value_id == 'mac_address':
498 return vm.get_port_mac(port_name)
499 if port_value_id == 'ip_address':
500 return vm.get_port_ip(port_name)
503 def __get_vm_fip_variable_value(var_config_values, vm_dict):
505 Returns the floating IP value if found
506 :param var_config_values: the configuration dictionary
507 :param vm_dict: the dictionary containing all VMs where the key is the VM's
509 :return: the floating IP string value or None
511 fip_name = var_config_values.get('fip_name')
512 vm_name = var_config_values.get('vm_name')
515 vm = vm_dict.get(vm_name)
517 fip = vm.get_floating_ip(fip_name)
522 def __get_image_variable_value(var_config_values, image_dict):
524 Returns the associated image value
525 :param var_config_values: the configuration dictionary
526 :param image_dict: the dictionary containing all images where the key is
530 logger.info("Retrieving image values")
533 if var_config_values.get('image_name'):
534 image_creator = image_dict.get(var_config_values['image_name'])
536 if var_config_values.get('value') and \
537 var_config_values['value'] == 'id':
538 return image_creator.get_image().id
539 if var_config_values.get('value') and \
540 var_config_values['value'] == 'user':
541 return image_creator.image_settings.image_user
543 logger.info("Returning none")
547 def __get_flavor_variable_value(var_config_values, flavor_dict):
549 Returns the associated flavor value
550 :param var_config_values: the configuration dictionary
551 :param flavor_dict: the dictionary containing all flavor creators where the
553 :return: the value or None
555 logger.info("Retrieving flavor values")
558 if var_config_values.get('flavor_name'):
559 flavor_creator = flavor_dict.get(var_config_values['flavor_name'])
561 if var_config_values.get('value') and \
562 var_config_values['value'] == 'id':
563 return flavor_creator.get_flavor().id
568 Will need to set environment variable ANSIBLE_HOST_KEY_CHECKING=False or
569 Create a file located in /etc/ansible/ansible/cfg or ~/.ansible.cfg
570 containing the following content:
573 host_key_checking = False
575 CWD must be this directory where this script is located.
579 log_level = logging.INFO
580 if arguments.log_level != 'INFO':
581 log_level = logging.DEBUG
582 logging.basicConfig(level=log_level)
584 logger.info('Starting to Deploy')
586 # Apply env_file/substitution file to template
587 env = Environment(loader=FileSystemLoader(
588 searchpath=os.path.dirname(arguments.tmplt_file)))
589 template = env.get_template(os.path.basename(arguments.tmplt_file))
592 if arguments.env_file:
593 env_dict = file_utils.read_yaml(arguments.env_file)
594 output = template.render(**env_dict)
596 config = yaml.load(output)
599 os_config = config.get('openstack')
604 flavors_dict = dict()
605 os_creds_dict = dict()
606 clean = arguments.clean is not ARG_NOT_SET
609 os_creds_dict = __get_creds_dict(os_config)
613 projects_dict = __create_instances(
614 os_creds_dict, OpenStackProject, ProjectSettings,
615 os_config.get('projects'), 'project', clean)
616 creators.append(projects_dict)
619 users_dict = __create_instances(
620 os_creds_dict, OpenStackUser, UserSettings,
621 os_config.get('users'), 'user', clean)
622 creators.append(users_dict)
624 # Associate new users to projects
626 for project_creator in projects_dict.values():
627 users = project_creator.project_settings.users
628 for user_name in users:
629 user_creator = users_dict.get(user_name)
631 project_creator.assoc_user(
632 user_creator.get_user())
635 flavors_dict = __create_instances(
636 os_creds_dict, OpenStackFlavor, FlavorSettings,
637 os_config.get('flavors'), 'flavor', clean, users_dict)
638 creators.append(flavors_dict)
641 qos_dict = __create_instances(
642 os_creds_dict, OpenStackQoS, QoSSettings,
643 os_config.get('qos_specs'), 'qos_spec', clean, users_dict)
644 creators.append(qos_dict)
646 # Create volume types
647 vol_type_dict = __create_instances(
648 os_creds_dict, OpenStackVolumeType, VolumeTypeSettings,
649 os_config.get('volume_types'), 'volume_type', clean,
651 creators.append(vol_type_dict)
653 # Create volume types
654 vol_dict = __create_instances(
655 os_creds_dict, OpenStackVolume, VolumeSettings,
656 os_config.get('volumes'), 'volume', clean, users_dict)
657 creators.append(vol_dict)
660 images_dict = __create_instances(
661 os_creds_dict, OpenStackImage, ImageSettings,
662 os_config.get('images'), 'image', clean, users_dict)
663 creators.append(images_dict)
666 creators.append(__create_instances(
667 os_creds_dict, OpenStackNetwork, NetworkSettings,
668 os_config.get('networks'), 'network', clean, users_dict))
671 creators.append(__create_instances(
672 os_creds_dict, OpenStackRouter, RouterSettings,
673 os_config.get('routers'), 'router', clean, users_dict))
676 keypairs_dict = __create_instances(
677 os_creds_dict, OpenStackKeypair, KeypairSettings,
678 os_config.get('keypairs'), 'keypair', clean, users_dict)
679 creators.append(keypairs_dict)
681 # Create security groups
682 creators.append(__create_instances(
683 os_creds_dict, OpenStackSecurityGroup,
684 SecurityGroupSettings,
685 os_config.get('security_groups'), 'security_group', clean,
689 vm_dict = __create_vm_instances(
690 os_creds_dict, users_dict, os_config.get('instances'),
691 images_dict, keypairs_dict,
692 arguments.clean is not ARG_NOT_SET)
693 creators.append(vm_dict)
695 'Completed creating/retrieving all configured instances')
696 except Exception as e:
698 'Unexpected error deploying environment. Rolling back due'
702 # Must enter either block
703 if arguments.clean is not ARG_NOT_SET:
704 # Cleanup Environment
705 __cleanup(creators, arguments.clean_image is not ARG_NOT_SET)
706 elif arguments.deploy is not ARG_NOT_SET:
707 logger.info('Configuring NICs where required')
708 for vm in vm_dict.values():
710 logger.info('Completed NIC configuration')
713 ansible_config = config.get('ansible')
714 if ansible_config and vm_dict:
715 if not __apply_ansible_playbooks(ansible_config,
716 os_creds_dict, vm_dict,
717 images_dict, flavors_dict,
718 arguments.tmplt_file):
719 logger.error("Problem applying ansible playbooks")
722 'Unable to read configuration file - ' + arguments.tmplt_file)
728 def __cleanup(creators, clean_image=False):
729 for creator_dict in reversed(creators):
730 for key, creator in creator_dict.items():
731 if ((isinstance(creator, OpenStackImage) and clean_image)
732 or not isinstance(creator, OpenStackImage)):
735 except Exception as e:
736 logger.warning('Error cleaning component - %s', e)
739 if __name__ == '__main__':
740 # To ensure any files referenced via a relative path will begin from the
741 # directory in which this file resides
742 os.chdir(os.path.dirname(os.path.realpath(__file__)))
744 parser = argparse.ArgumentParser()
746 '-d', '--deploy', dest='deploy', nargs='?', default=ARG_NOT_SET,
747 help='When used, environment will be deployed and provisioned')
749 '-c', '--clean', dest='clean', nargs='?', default=ARG_NOT_SET,
750 help='When used, the environment will be removed')
752 '-i', '--clean-image', dest='clean_image', nargs='?',
754 help='When cleaning, if this is set, the image will be cleaned too')
756 '-t', '--tmplt', dest='tmplt_file', required=True,
757 help='The SNAPS deployment template YAML file - REQUIRED')
759 '-e', '--env-file', dest='env_file',
760 help='Yaml file containing substitution values to the env file')
762 '-l', '--log-level', dest='log_level', default='INFO',
763 help='Logging Level (INFO|DEBUG)')
764 args = parser.parse_args()
766 if args.deploy is ARG_NOT_SET and args.clean is ARG_NOT_SET:
768 'Must enter either -d for deploy or -c for cleaning up and '
771 if args.deploy is not ARG_NOT_SET and args.clean is not ARG_NOT_SET:
772 print('Cannot enter both options -d/--deploy and -c/--clean')