Add pinning support
[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 import argparse
11 import sys
12 import apex
13 import logging
14 import os
15 from jinja2 import Environment, FileSystemLoader
16
17
18 def parse_net_settings(settings_args):
19     settings = apex.NetworkSettings(settings_args.path,
20                                     settings_args.network_isolation)
21     settings.dump_bash()
22
23 def parse_deploy_settings(settings_args):
24     settings = apex.DeploySettings(settings_args.path)
25     settings.dump_bash()
26
27
28 def find_ip(int_args):
29     interface = apex.ip_utils.get_interface(int_args.interface,
30                                       int_args.address_family)
31     if interface:
32         print(interface.ip)
33
34
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))
41
42
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()
47
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)
55
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,
61                         choices=[4, 6],
62                         help='IP Address family')
63 get_int_ip.set_defaults(func=find_ip)
64
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)
78
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)
84
85 args = parser.parse_args(sys.argv[1:])
86 if args.DEBUG:
87     logging.basicConfig(level=logging.DEBUG)
88 else:
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',
94                         level=logging.DEBUG)
95
96 if hasattr(args, 'func'):
97     args.func(args)
98 else:
99     parser.print_help()
100     exit(1)