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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
##############################################################################
# Copyright (c) 2016 Feng Pan (fpan@redhat.com), Dan Radez (dradez@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 apex
import argparse
import sys
import logging
import os
import yaml
from jinja2 import Environment
from jinja2 import FileSystemLoader
from apex import NetworkSettings
from apex import NetworkEnvironment
from apex import DeploySettings
from apex import Inventory
from apex import ip_utils
from apex.common.constants import ADMIN_NETWORK
def parse_net_settings(args):
"""
Parse OPNFV Apex network_settings.yaml config file
and dump bash syntax to set environment variables
Args:
- file: string
file to network_settings.yaml file
"""
settings = NetworkSettings(args.net_settings_file)
net_env = NetworkEnvironment(settings, args.net_env_file,
args.compute_pre_config,
args.controller_pre_config)
target = args.target_dir.split('/')
target.append('network-environment.yaml')
dump_yaml(dict(net_env), '/'.join(target))
settings.dump_bash()
def dump_yaml(data, file):
"""
Dumps data to a file as yaml
:param data: yaml to be written to file
:param file: filename to write to
:return:
"""
with open(file, "w") as fh:
yaml.dump(data, fh, default_flow_style=False)
def parse_deploy_settings(args):
settings = DeploySettings(args.file)
settings.dump_bash()
def run_clean(args):
apex.clean_nodes(args.file)
def parse_inventory(args):
inventory = Inventory(args.file, ha=args.ha, virtual=args.virtual)
inventory.dump_instackenv_json()
def find_ip(args):
"""
Get and print the IP from a specific interface
Args:
- interface: string
network interface name
- address_family: int
4 or 6, respective to ipv4 or ipv6
"""
interface = ip_utils.get_interface(args.interface,
args.address_family)
if interface:
print(interface.ip)
def build_nic_template(args):
"""
Build and print a Triple-O nic template from jinja template
Args:
- template: string
path to jinja template to load
- enabled_networks: comma delimited list
list of networks defined in net_env.py
- ext_net_type: string
interface or br-ex, defines the external network configuration
- address_family: string
4 or 6, respective to ipv4 or ipv6
- ovs_dpdk_bridge: string
bridge name to use as ovs_dpdk
"""
template_dir, template = args.template.rsplit('/', 1)
netsets = NetworkSettings(args.net_settings_file)
env = Environment(loader=FileSystemLoader(template_dir), autoescape=True)
template = env.get_template(template)
print(template.render(nets=netsets['networks'],
role=args.role,
external_net_af=netsets.get_ip_addr_family(),
external_net_type=args.ext_net_type,
ovs_dpdk_bridge=args.ovs_dpdk_bridge))
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--debug', action='store_true', default=False,
help="Turn on debug messages")
parser.add_argument('-l', '--log-file', default='/var/log/apex/apex.log',
dest='log_file', help="Log file to log to")
subparsers = parser.add_subparsers()
# parse-net-settings
net_settings = subparsers.add_parser('parse-net-settings',
help='Parse network settings file')
net_settings.add_argument('-s', '--net-settings-file',
default='network-settings.yaml',
dest='net_settings_file',
help='path to network settings file')
net_settings.add_argument('-e', '--net-env-file',
default="network-environment.yaml",
dest='net_env_file',
help='path to network environment file')
net_settings.add_argument('-td', '--target-dir',
default="/tmp",
dest='target_dir',
help='directory to write the'
'network-environment.yaml file')
net_settings.add_argument('--compute-pre-config',
default=False,
action='store_true',
dest='compute_pre_config',
help='Boolean to enable Compute Pre Config')
net_settings.add_argument('--controller-pre-config',
action='store_true',
default=False,
dest='controller_pre_config',
help='Boolean to enable Controller Pre Config')
net_settings.set_defaults(func=parse_net_settings)
# find-ip
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], dest='address_family',
help='IP Address family')
get_int_ip.set_defaults(func=find_ip)
# nic-template
nic_template = subparsers.add_parser('nic-template',
help='Build NIC templates')
nic_template.add_argument('-r', '--role', required=True,
choices=['controller', 'compute'],
help='Role template generated for')
nic_template.add_argument('-t', '--template', required=True,
dest='template',
help='Template file to process')
nic_template.add_argument('-s', '--net-settings-file',
default='network-settings.yaml',
dest='net_settings_file',
help='path to network settings file')
nic_template.add_argument('-e', '--ext-net-type', default='interface',
dest='ext_net_type',
choices=['interface', 'br-ex'],
help='External network type')
nic_template.add_argument('-d', '--ovs-dpdk-bridge',
default=None, dest='ovs_dpdk_bridge',
help='OVS DPDK Bridge Name')
nic_template.set_defaults(func=build_nic_template)
# parse-deploy-settings
deploy_settings = subparsers.add_parser('parse-deploy-settings',
help='Parse deploy settings file')
deploy_settings.add_argument('-f', '--file',
default='deploy_settings.yaml',
help='path to deploy settings file')
deploy_settings.set_defaults(func=parse_deploy_settings)
# parse-inventory
inventory = subparsers.add_parser('parse-inventory',
help='Parse inventory file')
inventory.add_argument('-f', '--file',
default='deploy_settings.yaml',
help='path to deploy settings file')
inventory.add_argument('--ha',
default=False,
action='store_true',
help='Indicate if deployment is HA or not')
inventory.add_argument('--virtual',
default=False,
action='store_true',
help='Indicate if deployment inventory is virtual')
inventory.set_defaults(func=parse_inventory)
clean = subparsers.add_parser('clean',
help='Parse deploy settings file')
clean.add_argument('-f', '--file',
help='path to inventory file')
clean.set_defaults(func=run_clean)
return parser
def main():
parser = get_parser()
args = parser.parse_args(sys.argv[1:])
if args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
apex_log_filename = args.log_file
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)
if __name__ == "__main__":
main()
|