summaryrefslogtreecommitdiffstats
path: root/lib/python/apex-python-utils.py
diff options
context:
space:
mode:
authorFeng Pan <fpan@redhat.com>2016-04-22 18:49:07 -0400
committerFeng Pan <fpan@redhat.com>2016-05-04 23:48:00 -0400
commit568656d2bd0341aacc74a936e28315b06f1881ca (patch)
tree5e9b45102935b03f2676be7fe5396828bc05729b /lib/python/apex-python-utils.py
parentd93d2992ff5de7f60c47fd3c31e429e5c06f6bce (diff)
Add python parsing library for network settings file.
Changes: - Implements network_settings.yaml file parsing in python. - Adds support for both IPv4 and IPv6 in network_settings.yaml - Adds support for api_network in network_settings.yaml - Removes bash library functions for network related functions. - Adds dependency to python34-yaml for apex-common package. Note that support for ipv6 and api_network is not complete yet. Proper configuriration of network environment and nic template files will be added later. Change-Id: I087f725dabedfef109c9de1f58ce2611da647e87 Signed-off-by: Feng Pan <fpan@redhat.com>
Diffstat (limited to 'lib/python/apex-python-utils.py')
-rwxr-xr-xlib/python/apex-python-utils.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/python/apex-python-utils.py b/lib/python/apex-python-utils.py
new file mode 100755
index 00000000..802e8571
--- /dev/null
+++ b/lib/python/apex-python-utils.py
@@ -0,0 +1,67 @@
+##############################################################################
+# Copyright (c) 2016 Feng Pan (fpan@redhat.com)
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+
+import argparse
+import sys
+import apex
+import logging
+import os
+
+def parse_net_settings(settings_args):
+ settings = apex.NetworkSettings(settings_args.path,
+ settings_args.network_isolation)
+ settings.dump_bash()
+
+
+def find_ip(int_args):
+ interface = apex.ip_utils.get_interface(int_args.interface,
+ int_args.address_family)
+ if interface:
+ print(interface.ip)
+
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--DEBUG', action='store_true', default=False,
+ help="Turn on debug messages")
+subparsers = parser.add_subparsers()
+
+net_settings = subparsers.add_parser('parse_net_settings',
+ help='Parse network settings file')
+net_settings.add_argument('-n', '--path', default='network_settings.yaml',
+ help='path to network settings file')
+net_settings.add_argument('-i', '--network_isolation', type=bool, default=True,
+ help='network isolation')
+net_settings.set_defaults(func=parse_net_settings)
+
+get_int_ip = subparsers.add_parser('find_ip',
+ help='Find interface ip')
+get_int_ip.add_argument('-i', '--interface', required=True,
+ help='Interface name')
+get_int_ip.add_argument('-af', '--address_family', default=4, type=int,
+ choices=[4, 6],
+ help='IP Address family')
+get_int_ip.set_defaults(func=find_ip)
+
+args = parser.parse_args(sys.argv[1:])
+if args.DEBUG:
+ logging.basicConfig(level=logging.DEBUG)
+else:
+ apex_log_filename = '/var/log/apex/apex.log'
+ os.makedirs(os.path.dirname(apex_log_filename), exist_ok=True)
+ logging.basicConfig(filename=apex_log_filename,
+ format='%(asctime)s %(levelname)s: %(message)s',
+ datefmt='%m/%d/%Y %I:%M:%S %p',
+ level=logging.DEBUG)
+
+if hasattr(args, 'func'):
+ args.func(args)
+else:
+ parser.print_help()
+ exit(1)