summaryrefslogtreecommitdiffstats
path: root/lib/python/apex-python-utils.py
blob: 7e947ea6e8dc89c4aa96e70c0f02c19b890b1ed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
##############################################################################
# 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
from jinja2 import Environment, FileSystemLoader


def parse_net_settings(settings_args):
    settings = apex.NetworkSettings(settings_args.path,
                                    settings_args.network_isolation)
    settings.dump_bash()

def parse_deploy_settings(settings_args):
    settings = apex.DeploySettings(settings_args.path)
    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)


def build_nic_template(nic_args):
    env = Environment(loader=FileSystemLoader(nic_args.template_directory))
    template = env.get_template(nic_args.template_filename)
    print(template.render(enabled_networks=nic_args.enabled_networks,
                          external_net_type=nic_args.ext_net_type,
                          external_net_af=nic_args.address_family))


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)

nic_template = subparsers.add_parser('nic_template', help='Build NIC templates')
nic_template.add_argument('-d', '--template_directory', required=True,
                          help='Template file directory')
nic_template.add_argument('-f', '--template_filename', required=True,
                          help='Template file to process')
nic_template.add_argument('-n', '--enabled_networks', required=True,
                          help='enabled network list')
nic_template.add_argument('-e', '--ext_net_type', default='interface',
                          choices=['interface', 'br-ex'],
                          help='External network type')
nic_template.add_argument('-af', '--address_family', type=int, default=4,
                          help='IP address family')
nic_template.set_defaults(func=build_nic_template)

deploy_settings = subparsers.add_parser('parse-deploy-settings',
                                        help='Parse deploy settings file')
deploy_settings.add_argument('-f', '--path', default='deploy_settings.yaml',
                             help='path to deploy settings file')
deploy_settings.set_defaults(func=parse_deploy_settings)

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)