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
22 from jinja2 import Environment, FileSystemLoader
26 from snaps import file_utils
27 from snaps.openstack.utils import launch_utils
29 __author__ = 'spisarski'
31 logger = logging.getLogger('snaps_launcher')
33 ARG_NOT_SET = "argument not set"
38 Will need to set environment variable ANSIBLE_HOST_KEY_CHECKING=False or
39 Create a file located in /etc/ansible/ansible/cfg or ~/.ansible.cfg
40 containing the following content:
43 host_key_checking = False
45 CWD must be this directory where this script is located.
49 log_level = logging.INFO
50 if arguments.log_level != 'INFO':
51 log_level = logging.DEBUG
52 logging.basicConfig(level=log_level)
54 logger.info('Starting to Deploy')
56 # Apply env_file/substitution file to template
57 env = Environment(loader=FileSystemLoader(
58 searchpath=os.path.dirname(arguments.tmplt_file)))
59 template = env.get_template(os.path.basename(arguments.tmplt_file))
62 if arguments.env_file:
63 env_dict = file_utils.read_yaml(arguments.env_file)
64 output = template.render(**env_dict)
66 config = yaml.load(output)
69 clean = arguments.clean is not ARG_NOT_SET
70 clean_image = arguments.clean_image is not ARG_NOT_SET
71 deploy = arguments.deploy is not ARG_NOT_SET
72 launch_utils.launch_config(
73 config, arguments.tmplt_file, deploy, clean, clean_image)
76 'Unable to read configuration file - ' + arguments.tmplt_file)
82 if __name__ == '__main__':
83 # To ensure any files referenced via a relative path will begin from the
84 # directory in which this file resides
85 os.chdir(os.path.dirname(os.path.realpath(__file__)))
87 parser = argparse.ArgumentParser()
89 '-d', '--deploy', dest='deploy', nargs='?', default=ARG_NOT_SET,
90 help='When used, environment will be deployed and provisioned')
92 '-c', '--clean', dest='clean', nargs='?', default=ARG_NOT_SET,
93 help='When used, the environment will be removed')
95 '-i', '--clean-image', dest='clean_image', nargs='?',
97 help='When cleaning, if this is set, the image will be cleaned too')
99 '-t', '--tmplt', dest='tmplt_file', required=True,
100 help='The SNAPS deployment template YAML file - REQUIRED')
102 '-e', '--env-file', dest='env_file',
103 help='Yaml file containing substitution values to the env file')
105 '-l', '--log-level', dest='log_level', default='INFO',
106 help='Logging Level (INFO|DEBUG)')
107 args = parser.parse_args()
109 if args.deploy is ARG_NOT_SET and args.clean is ARG_NOT_SET:
111 'Must enter either -d for deploy or -c for cleaning up and '
114 if args.deploy is not ARG_NOT_SET and args.clean is not ARG_NOT_SET:
115 print('Cannot enter both options -d/--deploy and -c/--clean')