Merge "Rebasing opnfv-tht Colorado with ODL patches"
[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
17 def parse_net_settings(settings_args):
18     settings = apex.NetworkSettings(settings_args.path,
19                                     settings_args.network_isolation)
20     settings.dump_bash()
21
22
23 def find_ip(int_args):
24     interface = apex.ip_utils.get_interface(int_args.interface,
25                                       int_args.address_family)
26     if interface:
27         print(interface.ip)
28
29
30 parser = argparse.ArgumentParser()
31 parser.add_argument('--DEBUG', action='store_true', default=False,
32                     help="Turn on debug messages")
33 subparsers = parser.add_subparsers()
34
35 net_settings = subparsers.add_parser('parse_net_settings',
36                                      help='Parse network settings file')
37 net_settings.add_argument('-n', '--path', default='network_settings.yaml',
38                           help='path to network settings file')
39 net_settings.add_argument('-i', '--network_isolation', type=bool, default=True,
40                           help='network isolation')
41 net_settings.set_defaults(func=parse_net_settings)
42
43 get_int_ip = subparsers.add_parser('find_ip',
44                                    help='Find interface ip')
45 get_int_ip.add_argument('-i', '--interface', required=True,
46                         help='Interface name')
47 get_int_ip.add_argument('-af', '--address_family', default=4, type=int,
48                         choices=[4, 6],
49                         help='IP Address family')
50 get_int_ip.set_defaults(func=find_ip)
51
52 args = parser.parse_args(sys.argv[1:])
53 if args.DEBUG:
54     logging.basicConfig(level=logging.DEBUG)
55 else:
56     apex_log_filename = '/var/log/apex/apex.log'
57     os.makedirs(os.path.dirname(apex_log_filename), exist_ok=True)
58     logging.basicConfig(filename=apex_log_filename,
59                         format='%(asctime)s %(levelname)s: %(message)s',
60                         datefmt='%m/%d/%Y %I:%M:%S %p',
61                         level=logging.DEBUG)
62
63 if hasattr(args, 'func'):
64     args.func(args)
65 else:
66     parser.print_help()
67     exit(1)