04bc5d624cf1d04fb7c9d90dff2ddcc2723dcb4c
[snaps.git] / examples / launch.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
4 #                    and others.  All rights reserved.
5 #
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:
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
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.
17 #
18 # This script is responsible for deploying virtual environments
19 import argparse
20 import logging
21
22 from jinja2 import Environment, FileSystemLoader
23 import os
24 import yaml
25
26 from snaps import file_utils
27 from snaps.openstack.utils import launch_utils
28
29 __author__ = 'spisarski'
30
31 logger = logging.getLogger('snaps_launcher')
32
33 ARG_NOT_SET = "argument not set"
34
35
36 def main(arguments):
37     """
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:
41
42     [defaults]
43     host_key_checking = False
44
45     CWD must be this directory where this script is located.
46
47     :return: To the OS
48     """
49     log_level = logging.INFO
50     if arguments.log_level != 'INFO':
51         log_level = logging.DEBUG
52     logging.basicConfig(level=log_level)
53
54     logger.info('Starting to Deploy')
55
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))
60
61     env_dict = dict()
62     if arguments.env_file:
63         env_dict = file_utils.read_yaml(arguments.env_file)
64     output = template.render(**env_dict)
65
66     config = yaml.load(output)
67
68     if config:
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)
74     else:
75         logger.error(
76             'Unable to read configuration file - ' + arguments.tmplt_file)
77         exit(1)
78
79     exit(0)
80
81
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__)))
86
87     parser = argparse.ArgumentParser()
88     parser.add_argument(
89         '-d', '--deploy', dest='deploy', nargs='?', default=ARG_NOT_SET,
90         help='When used, environment will be deployed and provisioned')
91     parser.add_argument(
92         '-c', '--clean', dest='clean', nargs='?', default=ARG_NOT_SET,
93         help='When used, the environment will be removed')
94     parser.add_argument(
95         '-i', '--clean-image', dest='clean_image', nargs='?',
96         default=ARG_NOT_SET,
97         help='When cleaning, if this is set, the image will be cleaned too')
98     parser.add_argument(
99         '-t', '--tmplt', dest='tmplt_file', required=True,
100         help='The SNAPS deployment template YAML file - REQUIRED')
101     parser.add_argument(
102         '-e', '--env-file', dest='env_file',
103         help='Yaml file containing substitution values to the env file')
104     parser.add_argument(
105         '-l', '--log-level', dest='log_level', default='INFO',
106         help='Logging Level (INFO|DEBUG)')
107     args = parser.parse_args()
108
109     if args.deploy is ARG_NOT_SET and args.clean is ARG_NOT_SET:
110         print(
111             'Must enter either -d for deploy or -c for cleaning up and '
112             'environment')
113         exit(1)
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')
116         exit(1)
117     main(args)