1 ##############################################################################
2 # Copyright (c) 2016 Feng Pan (fpan@redhat.com)
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 ##############################################################################
16 from jinja2 import Environment, FileSystemLoader
19 def parse_net_settings(settings_args):
20 settings = apex.NetworkSettings(settings_args.path,
21 settings_args.network_isolation)
25 def find_ip(int_args):
26 interface = apex.ip_utils.get_interface(int_args.interface,
27 int_args.address_family)
32 def build_nic_template(nic_args):
33 env = Environment(loader=FileSystemLoader(nic_args.template_directory))
34 template = env.get_template(nic_args.template_filename)
35 print(template.render(enabled_networks=nic_args.enabled_networks,
36 external_net_type=nic_args.ext_net_type,
37 external_net_af=nic_args.address_family))
40 parser = argparse.ArgumentParser()
41 parser.add_argument('--DEBUG', action='store_true', default=False,
42 help="Turn on debug messages")
43 subparsers = parser.add_subparsers()
45 net_settings = subparsers.add_parser('parse_net_settings',
46 help='Parse network settings file')
47 net_settings.add_argument('-n', '--path', default='network_settings.yaml',
48 help='path to network settings file')
49 net_settings.add_argument('-i', '--network_isolation', type=bool, default=True,
50 help='network isolation')
51 net_settings.set_defaults(func=parse_net_settings)
53 get_int_ip = subparsers.add_parser('find_ip',
54 help='Find interface ip')
55 get_int_ip.add_argument('-i', '--interface', required=True,
56 help='Interface name')
57 get_int_ip.add_argument('-af', '--address_family', default=4, type=int,
59 help='IP Address family')
60 get_int_ip.set_defaults(func=find_ip)
62 nic_template = subparsers.add_parser('nic_template', help='Build NIC templates')
63 nic_template.add_argument('-d', '--template_directory', required=True,
64 help='Template file directory')
65 nic_template.add_argument('-f', '--template_filename', required=True,
66 help='Template file to process')
67 nic_template.add_argument('-n', '--enabled_networks', required=True,
68 help='enabled network list')
69 nic_template.add_argument('-e', '--ext_net_type', default='interface',
70 choices=['interface', 'br-ex'],
71 help='External network type')
72 nic_template.add_argument('-af', '--address_family', type=int, default=4,
73 help='IP address family')
74 nic_template.set_defaults(func=build_nic_template)
76 args = parser.parse_args(sys.argv[1:])
78 logging.basicConfig(level=logging.DEBUG)
80 apex_log_filename = '/var/log/apex/apex.log'
81 os.makedirs(os.path.dirname(apex_log_filename), exist_ok=True)
82 logging.basicConfig(filename=apex_log_filename,
83 format='%(asctime)s %(levelname)s: %(message)s',
84 datefmt='%m/%d/%Y %I:%M:%S %p',
87 if hasattr(args, 'func'):