1d5b4a7cf5ad3c0be309480f335913be6a7e375e
[apex.git] / lib / python / apex-python-utils.py
1 ##############################################################################
2 # Copyright (c) 2016 Feng Pan (fpan@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
11 import argparse
12 import sys
13 import apex
14 import logging
15 import os
16 from jinja2 import Environment, FileSystemLoader
17
18
19 def parse_net_settings(settings_args):
20     settings = apex.NetworkSettings(settings_args.path,
21                                     settings_args.network_isolation)
22     settings.dump_bash()
23
24
25 def find_ip(int_args):
26     interface = apex.ip_utils.get_interface(int_args.interface,
27                                       int_args.address_family)
28     if interface:
29         print(interface.ip)
30
31
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))
38
39
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()
44
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)
52
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,
58                         choices=[4, 6],
59                         help='IP Address family')
60 get_int_ip.set_defaults(func=find_ip)
61
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)
75
76 args = parser.parse_args(sys.argv[1:])
77 if args.DEBUG:
78     logging.basicConfig(level=logging.DEBUG)
79 else:
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',
85                         level=logging.DEBUG)
86
87 if hasattr(args, 'func'):
88     args.func(args)
89 else:
90     parser.print_help()
91     exit(1)