1 ##############################################################################
2 # Copyright (c) 2016 Feng Pan (fpan@redhat.com), Dan Radez (dradez@redhat.com)
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 ##############################################################################
19 from jinja2 import Environment
20 from jinja2 import FileSystemLoader
22 from apex import NetworkSettings
23 from apex import NetworkEnvironment
24 from apex import DeploySettings
25 from apex import Inventory
26 from apex import ip_utils
27 from apex.common.constants import ADMIN_NETWORK
30 def parse_net_settings(args):
32 Parse OPNFV Apex network_settings.yaml config file
33 and dump bash syntax to set environment variables
37 file to network_settings.yaml file
38 - network_isolation: bool
39 enable or disable network_isolation
41 settings = NetworkSettings(args.net_settings_file,
42 args.network_isolation)
43 net_env = NetworkEnvironment(settings, args.net_env_file,
44 args.compute_pre_config,
45 args.controller_pre_config)
46 dump_yaml(dict(net_env), '/tmp/network-environment.yaml')
50 def dump_yaml(data, file):
52 Dumps data to a file as yaml
53 :param data: yaml to be written to file
54 :param file: filename to write to
57 with open(file, "w") as fh:
58 yaml.dump(data, fh, default_flow_style=False)
61 def parse_deploy_settings(args):
62 settings = DeploySettings(args.file)
67 apex.clean_nodes(args.file)
70 def parse_inventory(args):
71 inventory = Inventory(args.file, ha=args.ha, virtual=args.virtual)
72 inventory.dump_instackenv_json()
77 Get and print the IP from a specific interface
81 network interface name
83 4 or 6, respective to ipv4 or ipv6
85 interface = ip_utils.get_interface(args.interface,
91 def build_nic_template(args):
93 Build and print a Triple-O nic template from jinja template
97 path to jinja template to load
98 - enabled_networks: comma delimited list
99 list of networks defined in net_env.py
100 - ext_net_type: string
101 interface or br-ex, defines the external network configuration
102 - address_family: string
103 4 or 6, respective to ipv4 or ipv6
104 - ovs_dpdk_bridge: string
105 bridge name to use as ovs_dpdk
107 template_dir, template = args.template.rsplit('/', 1)
109 netsets = NetworkSettings(args.net_settings_file,
110 args.network_isolation)
111 env = Environment(loader=FileSystemLoader(template_dir))
112 template = env.get_template(template)
114 # gather vlan values into a dict
115 net_list = copy(netsets.enabled_network_list)
116 net_list.remove(ADMIN_NETWORK)
117 vlans_vals = map(lambda x: netsets[x]['vlan'], net_list)
118 vlans = dict(zip(net_list, vlans_vals))
121 print(template.render(enabled_networks=netsets.enabled_network_list,
124 external_net_type=args.ext_net_type,
125 external_net_af=args.address_family,
126 ovs_dpdk_bridge=args.ovs_dpdk_bridge,
131 parser = argparse.ArgumentParser()
132 parser.add_argument('--debug', action='store_true', default=False,
133 help="Turn on debug messages")
134 parser.add_argument('-l', '--log-file', default='/var/log/apex/apex.log',
135 dest='log_file', help="Log file to log to")
136 subparsers = parser.add_subparsers()
138 net_settings = subparsers.add_parser('parse-net-settings',
139 help='Parse network settings file')
140 net_settings.add_argument('-s', '--net-settings-file',
141 default='network-settings.yaml',
142 dest='net_settings_file',
143 help='path to network settings file')
144 net_settings.add_argument('--flat', action='store_false',
145 default=True, dest='network_isolation',
146 help='disable network isolation')
147 net_settings.add_argument('-e', '--net-env-file',
148 default="network-environment.yaml",
150 help='path to network environment file')
151 net_settings.add_argument('--compute-pre-config',
154 dest='compute_pre_config',
155 help='Boolean to enable Compute Pre Config')
156 net_settings.add_argument('--controller-pre-config',
159 dest='controller_pre_config',
160 help='Boolean to enable Controller Pre Config')
162 net_settings.set_defaults(func=parse_net_settings)
164 get_int_ip = subparsers.add_parser('find-ip',
165 help='Find interface ip')
166 get_int_ip.add_argument('-i', '--interface', required=True,
167 help='Interface name')
168 get_int_ip.add_argument('-af', '--address-family', default=4, type=int,
169 choices=[4, 6], dest='address_family',
170 help='IP Address family')
171 get_int_ip.set_defaults(func=find_ip)
173 nic_template = subparsers.add_parser('nic-template',
174 help='Build NIC templates')
175 nic_template.add_argument('-r', '--role', required=True,
176 choices=['controller', 'compute'],
177 help='Role template generated for')
178 nic_template.add_argument('-t', '--template', required=True,
180 help='Template file to process')
181 nic_template.add_argument('-s', '--net-settings-file',
182 default='network-settings.yaml',
183 dest='net_settings_file',
184 help='path to network settings file')
185 nic_template.add_argument('--flat', action='store_false',
186 default=True, dest='network_isolation',
187 help='disable network isolation')
188 nic_template.add_argument('-e', '--ext-net-type', default='interface',
190 choices=['interface', 'br-ex'],
191 help='External network type')
192 nic_template.add_argument('-af', '--address-family', type=int, default=4,
193 dest='address_family', help='IP address family')
194 nic_template.add_argument('-d', '--ovs-dpdk-bridge',
195 default=None, dest='ovs_dpdk_bridge',
196 help='OVS DPDK Bridge Name')
197 nic_template.set_defaults(func=build_nic_template)
198 # parse-deploy-settings
199 deploy_settings = subparsers.add_parser('parse-deploy-settings',
200 help='Parse deploy settings file')
201 deploy_settings.add_argument('-f', '--file',
202 default='deploy_settings.yaml',
203 help='path to deploy settings file')
204 deploy_settings.set_defaults(func=parse_deploy_settings)
206 inventory = subparsers.add_parser('parse-inventory',
207 help='Parse inventory file')
208 inventory.add_argument('-f', '--file',
209 default='deploy_settings.yaml',
210 help='path to deploy settings file')
211 inventory.add_argument('--ha',
214 help='Indicate if deployment is HA or not')
215 inventory.add_argument('--virtual',
218 help='Indicate if deployment inventory is virtual')
219 inventory.set_defaults(func=parse_inventory)
221 clean = subparsers.add_parser('clean',
222 help='Parse deploy settings file')
223 clean.add_argument('-f', '--file',
224 help='path to inventory file')
225 clean.set_defaults(func=run_clean)
231 parser = get_parser()
232 args = parser.parse_args(sys.argv[1:])
234 logging.basicConfig(level=logging.DEBUG)
236 apex_log_filename = args.log_file
237 os.makedirs(os.path.dirname(apex_log_filename), exist_ok=True)
238 logging.basicConfig(filename=apex_log_filename,
239 format='%(asctime)s %(levelname)s: %(message)s',
240 datefmt='%m/%d/%Y %I:%M:%S %p',
242 if hasattr(args, 'func'):
248 if __name__ == "__main__":