0ef9d1d181618942fa605e8551b674cbbc6eed7b
[osm/devops.git] / descriptor-packages / tools / charm-generator / generator / generator.py
1 # Copyright 2019 Whitestack, LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14 #
15 # For those usages not covered by the Apache License, Version 2.0 please
16 # contact: esousa@whitestack.com or glavado@whitestack.com
17 ##
18
19 __version__ = '1.0.0'
20
21 import argparse
22 import logging
23 import sys
24
25 from datetime import datetime
26
27 from .generators.ansible_generator import AnsibleGenerator
28
29
30 def configure_logger(args):
31 global logger
32 logger = logging.getLogger()
33
34 if args.verbose:
35 logger.setLevel(logging.DEBUG)
36 else:
37 logger.setLevel(logging.INFO)
38
39 handler = logging.StreamHandler(sys.stdout)
40
41 if args.verbose:
42 handler.setLevel(logging.DEBUG)
43 else:
44 handler.setLevel(logging.INFO)
45
46 formatter = logging.Formatter('[%(levelname)s] %(message)s')
47 handler.setFormatter(formatter)
48 logger.addHandler(handler)
49
50
51 def verify_environment(args):
52 pass
53
54
55 def input_processing():
56 parser = argparse.ArgumentParser(description='Charm generator for OSM VNFs')
57
58 # Setting logger from INFO to DEBUG
59 parser.add_argument('-v', '--verbose', required=False, action='store_true',
60 help='increase output verbosity')
61
62 # Backend selection
63 backend_parser = parser.add_mutually_exclusive_group(required=True)
64
65 backend_parser.add_argument('--ansible', action='store_true',
66 help='generate an Ansible charm')
67 backend_parser.add_argument('--http', action='store_true',
68 help='generate a HTTP charm')
69 backend_parser.add_argument('--sol002', action='store_true',
70 help='generate a SOL002 charm')
71 backend_parser.add_argument('--scripts', action='store_true',
72 help='generate a Scripts charm')
73
74 # Metadata inputs
75 metadata_parser = parser.add_argument_group('metadata')
76
77 metadata_parser.add_argument('--summary', required=False, action='store',
78 help='summary to be included in the metadata.yaml')
79 metadata_parser.add_argument('--maintainer', required=False, action='store',
80 help='maintainer information to be included in the metadata.yaml')
81 metadata_parser.add_argument('--description', required=False, action='store',
82 help='description to be included in the metadata.yaml')
83
84 # License header inputs
85 license_header_group = parser.add_argument_group('license_header')
86
87 license_header_group.add_argument('--company', required=False, action='store',
88 help='company name to be included in the license headers')
89 license_header_group.add_argument('--email', required=False, action='store',
90 help='email to be included in the license headers')
91
92 return parser.parse_args()
93
94
95 def process_args(args):
96 # Metadata information for metadata.yaml
97 metadata = {}
98
99 if args.summary:
100 metadata['summary'] = args.summary
101 if args.maintainer:
102 metadata['maintainer'] = args.maintainer
103 if args.description:
104 metadata['description'] = args.description
105
106 # Information for license headers
107 license = {
108 'year': datetime.now().year
109 }
110
111 if args.company:
112 license['company'] = args.company
113 if args.email:
114 license['email'] = args.email
115
116 # Options to configure the backends
117 options = {
118 'backend': None
119 }
120
121 if args.ansible:
122 options['backend'] = 'ansible'
123 elif args.http:
124 options['backend'] = 'http'
125 elif args.sol002:
126 options['backend'] = 'sol002'
127 elif args.scripts:
128 options['backend'] = 'scripts'
129
130 return metadata, license, options
131
132
133 def main():
134 # getting the input from the user
135 args = input_processing()
136
137 # configure the logger
138 configure_logger(args)
139
140 logger.info('Starting generation process...')
141
142 # verify if the environment is correct and the args are valid
143 verify_environment(args)
144
145 # process data to input in generator
146 metadata, license, options = process_args(args)
147
148 logger.debug('Metadata: %s', metadata)
149 logger.debug('License: %s', license)
150 logger.debug('Options: %s', options)
151
152 if options['backend'] == 'ansible':
153 generator = AnsibleGenerator(metadata=metadata, license=license, options=options)
154 elif options['backend'] == 'http':
155 logger.error('HTTP backend not yet available. Available backends are: ansible')
156 sys.exit(-1)
157 elif options['backend'] == 'sol002':
158 logger.error('SOL002 backend not yet available. Available backends are: ansible')
159 sys.exit(-1)
160 elif options['backend'] == 'scripts':
161 logger.error('Scripts backend not yet available. Available backends are: ansible')
162 sys.exit(-1)
163 else:
164 logger.error('Undefined backend for generator. Available backends are: ansible')
165 sys.exit(-1)
166
167 generator.generate()
168
169 logger.info('Generation process complete.')