Merge "Add AggregateInstanceExtraSpecFilter to Scheduler"
[apex.git] / lib / python / apex_python_utils.py
1 ##############################################################################
2 # Copyright (c) 2016 Feng Pan (fpan@redhat.com), Dan Radez (dradez@redhat.com)
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import apex
11 import argparse
12 import sys
13 import logging
14 import os
15 import yaml
16
17 from copy import copy
18
19 from jinja2 import Environment
20 from jinja2 import FileSystemLoader
21
22 from apex import NetworkSettings
23 from apex import NetworkEnvironment
24 from apex import DeploySettings
25 from apex import Inventory
26 from apex import ip_utils
27 from apex.common.constants import ADMIN_NETWORK
28
29
30 def parse_net_settings(args):
31     """
32     Parse OPNFV Apex network_settings.yaml config file
33     and dump bash syntax to set environment variables
34
35     Args:
36     - file: string
37       file to network_settings.yaml file
38     - network_isolation: bool
39       enable or disable network_isolation
40     """
41     settings = NetworkSettings(args.net_settings_file,
42                                args.network_isolation)
43     net_env = NetworkEnvironment(settings, args.net_env_file,
44                                  args.compute_pre_config,
45                                  args.controller_pre_config)
46     dump_yaml(dict(net_env), '/tmp/network-environment.yaml')
47     settings.dump_bash()
48
49
50 def dump_yaml(data, file):
51     """
52     Dumps data to a file as yaml
53     :param data: yaml to be written to file
54     :param file: filename to write to
55     :return:
56     """
57     with open(file, "w") as fh:
58         yaml.dump(data, fh, default_flow_style=False)
59
60
61 def parse_deploy_settings(args):
62     settings = DeploySettings(args.file)
63     settings.dump_bash()
64
65
66 def run_clean(args):
67     apex.clean_nodes(args.file)
68
69
70 def parse_inventory(args):
71     inventory = Inventory(args.file, ha=args.ha, virtual=args.virtual)
72     inventory.dump_instackenv_json()
73
74
75 def find_ip(args):
76     """
77     Get and print the IP from a specific interface
78
79     Args:
80     - interface: string
81       network interface name
82     - address_family: int
83       4 or 6, respective to ipv4 or ipv6
84     """
85     interface = ip_utils.get_interface(args.interface,
86                                        args.address_family)
87     if interface:
88         print(interface.ip)
89
90
91 def build_nic_template(args):
92     """
93     Build and print a Triple-O nic template from jinja template
94
95     Args:
96     - template: string
97       path to jinja template to load
98     - enabled_networks: comma delimited list
99       list of networks defined in net_env.py
100     - ext_net_type: string
101       interface or br-ex, defines the external network configuration
102     - address_family: string
103       4 or 6, respective to ipv4 or ipv6
104     - ovs_dpdk_bridge: string
105       bridge name to use as ovs_dpdk
106     """
107     template_dir, template = args.template.rsplit('/', 1)
108
109     netsets = NetworkSettings(args.net_settings_file,
110                               args.network_isolation)
111     env = Environment(loader=FileSystemLoader(template_dir))
112     template = env.get_template(template)
113
114     # gather vlan values into a dict
115     net_list = copy(netsets.enabled_network_list)
116     net_list.remove(ADMIN_NETWORK)
117     vlans_vals = map(lambda x: netsets[x]['vlan'], net_list)
118     vlans = dict(zip(net_list, vlans_vals))
119     nics = netsets.nics
120
121     print(template.render(enabled_networks=netsets.enabled_network_list,
122                           role=args.role,
123                           vlans=vlans,
124                           external_net_type=args.ext_net_type,
125                           external_net_af=args.address_family,
126                           ovs_dpdk_bridge=args.ovs_dpdk_bridge,
127                           nics=nics))
128
129
130 def get_parser():
131     parser = argparse.ArgumentParser()
132     parser.add_argument('--debug', action='store_true', default=False,
133                         help="Turn on debug messages")
134     parser.add_argument('-l', '--log-file', default='/var/log/apex/apex.log',
135                         dest='log_file', help="Log file to log to")
136     subparsers = parser.add_subparsers()
137     # parse-net-settings
138     net_settings = subparsers.add_parser('parse-net-settings',
139                                          help='Parse network settings file')
140     net_settings.add_argument('-s', '--net-settings-file',
141                               default='network-settings.yaml',
142                               dest='net_settings_file',
143                               help='path to network settings file')
144     net_settings.add_argument('--flat', action='store_false',
145                               default=True, dest='network_isolation',
146                               help='disable network isolation')
147     net_settings.add_argument('-e', '--net-env-file',
148                               default="network-environment.yaml",
149                               dest='net_env_file',
150                               help='path to network environment file')
151     net_settings.add_argument('--compute-pre-config',
152                               default=False,
153                               action='store_true',
154                               dest='compute_pre_config',
155                               help='Boolean to enable Compute Pre Config')
156     net_settings.add_argument('--controller-pre-config',
157                               action='store_true',
158                               default=False,
159                               dest='controller_pre_config',
160                               help='Boolean to enable Controller Pre Config')
161
162     net_settings.set_defaults(func=parse_net_settings)
163     # find-ip
164     get_int_ip = subparsers.add_parser('find-ip',
165                                        help='Find interface ip')
166     get_int_ip.add_argument('-i', '--interface', required=True,
167                             help='Interface name')
168     get_int_ip.add_argument('-af', '--address-family', default=4, type=int,
169                             choices=[4, 6], dest='address_family',
170                             help='IP Address family')
171     get_int_ip.set_defaults(func=find_ip)
172     # nic-template
173     nic_template = subparsers.add_parser('nic-template',
174                                          help='Build NIC templates')
175     nic_template.add_argument('-r', '--role', required=True,
176                               choices=['controller', 'compute'],
177                               help='Role template generated for')
178     nic_template.add_argument('-t', '--template', required=True,
179                               dest='template',
180                               help='Template file to process')
181     nic_template.add_argument('-s', '--net-settings-file',
182                               default='network-settings.yaml',
183                               dest='net_settings_file',
184                               help='path to network settings file')
185     nic_template.add_argument('--flat', action='store_false',
186                               default=True, dest='network_isolation',
187                               help='disable network isolation')
188     nic_template.add_argument('-e', '--ext-net-type', default='interface',
189                               dest='ext_net_type',
190                               choices=['interface', 'br-ex'],
191                               help='External network type')
192     nic_template.add_argument('-af', '--address-family', type=int, default=4,
193                               dest='address_family', help='IP address family')
194     nic_template.add_argument('-d', '--ovs-dpdk-bridge',
195                               default=None, dest='ovs_dpdk_bridge',
196                               help='OVS DPDK Bridge Name')
197     nic_template.set_defaults(func=build_nic_template)
198     # parse-deploy-settings
199     deploy_settings = subparsers.add_parser('parse-deploy-settings',
200                                             help='Parse deploy settings file')
201     deploy_settings.add_argument('-f', '--file',
202                                  default='deploy_settings.yaml',
203                                  help='path to deploy settings file')
204     deploy_settings.set_defaults(func=parse_deploy_settings)
205     # parse-inventory
206     inventory = subparsers.add_parser('parse-inventory',
207                                       help='Parse inventory file')
208     inventory.add_argument('-f', '--file',
209                            default='deploy_settings.yaml',
210                            help='path to deploy settings file')
211     inventory.add_argument('--ha',
212                            default=False,
213                            action='store_true',
214                            help='Indicate if deployment is HA or not')
215     inventory.add_argument('--virtual',
216                            default=False,
217                            action='store_true',
218                            help='Indicate if deployment inventory is virtual')
219     inventory.set_defaults(func=parse_inventory)
220
221     clean = subparsers.add_parser('clean',
222                                   help='Parse deploy settings file')
223     clean.add_argument('-f', '--file',
224                        help='path to inventory file')
225     clean.set_defaults(func=run_clean)
226
227     return parser
228
229
230 def main():
231     parser = get_parser()
232     args = parser.parse_args(sys.argv[1:])
233     if args.debug:
234         logging.basicConfig(level=logging.DEBUG)
235     else:
236         apex_log_filename = args.log_file
237         os.makedirs(os.path.dirname(apex_log_filename), exist_ok=True)
238         logging.basicConfig(filename=apex_log_filename,
239                             format='%(asctime)s %(levelname)s: %(message)s',
240                             datefmt='%m/%d/%Y %I:%M:%S %p',
241                             level=logging.DEBUG)
242     if hasattr(args, 'func'):
243         args.func(args)
244     else:
245         parser.print_help()
246         exit(1)
247
248 if __name__ == "__main__":
249     main()