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 ##############################################################################
15 from jinja2 import Environment, FileSystemLoader
18 def parse_net_settings(settings_args):
19 settings = apex.NetworkSettings(settings_args.path,
20 settings_args.network_isolation)
23 def parse_deploy_settings(settings_args):
24 settings = apex.DeploySettings(settings_args.path)
28 def find_ip(int_args):
29 interface = apex.ip_utils.get_interface(int_args.interface,
30 int_args.address_family)
35 def build_nic_template(nic_args):
36 env = Environment(loader=FileSystemLoader(nic_args.template_directory))
37 template = env.get_template(nic_args.template_filename)
38 print(template.render(enabled_networks=nic_args.enabled_networks,
39 external_net_type=nic_args.ext_net_type,
40 external_net_af=nic_args.address_family))
43 parser = argparse.ArgumentParser()
44 parser.add_argument('--DEBUG', action='store_true', default=False,
45 help="Turn on debug messages")
46 subparsers = parser.add_subparsers()
48 net_settings = subparsers.add_parser('parse_net_settings',
49 help='Parse network settings file')
50 net_settings.add_argument('-n', '--path', default='network_settings.yaml',
51 help='path to network settings file')
52 net_settings.add_argument('-i', '--network_isolation', type=bool, default=True,
53 help='network isolation')
54 net_settings.set_defaults(func=parse_net_settings)
56 get_int_ip = subparsers.add_parser('find_ip',
57 help='Find interface ip')
58 get_int_ip.add_argument('-i', '--interface', required=True,
59 help='Interface name')
60 get_int_ip.add_argument('-af', '--address_family', default=4, type=int,
62 help='IP Address family')
63 get_int_ip.set_defaults(func=find_ip)
65 nic_template = subparsers.add_parser('nic_template', help='Build NIC templates')
66 nic_template.add_argument('-d', '--template_directory', required=True,
67 help='Template file directory')
68 nic_template.add_argument('-f', '--template_filename', required=True,
69 help='Template file to process')
70 nic_template.add_argument('-n', '--enabled_networks', required=True,
71 help='enabled network list')
72 nic_template.add_argument('-e', '--ext_net_type', default='interface',
73 choices=['interface', 'br-ex'],
74 help='External network type')
75 nic_template.add_argument('-af', '--address_family', type=int, default=4,
76 help='IP address family')
77 nic_template.set_defaults(func=build_nic_template)
79 deploy_settings = subparsers.add_parser('parse-deploy-settings',
80 help='Parse deploy settings file')
81 deploy_settings.add_argument('-f', '--path', default='deploy_settings.yaml',
82 help='path to deploy settings file')
83 deploy_settings.set_defaults(func=parse_deploy_settings)
85 args = parser.parse_args(sys.argv[1:])
87 logging.basicConfig(level=logging.DEBUG)
89 apex_log_filename = '/var/log/apex/apex.log'
90 os.makedirs(os.path.dirname(apex_log_filename), exist_ok=True)
91 logging.basicConfig(filename=apex_log_filename,
92 format='%(asctime)s %(levelname)s: %(message)s',
93 datefmt='%m/%d/%Y %I:%M:%S %p',
96 if hasattr(args, 'func'):