tripleo-heat-templates/tools/convert_v1_net_data.py
Harald Jensås a77d5ebde2 Make tools scripts executable
Set +x permission on files:
 * tools/convert_heat_nic_config_to_ansible_j2.py
 * tools/convert_v1_net_data.py

Other python scripts have the executable permission set.
Align the two scripts with the rest.

Change-Id: I0a0bd4a353bdc35698444b72f093cce176bbe5a0
2021-12-06 17:10:58 +01:00

76 lines
2.5 KiB
Python
Executable File

#!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import os
import yaml
def parse_opts():
"""Parse all of the conversion options."""
parser = argparse.ArgumentParser(
description="Convert a network V1 template to a V2 template."
)
parser.add_argument(
"v1", metavar="<network_data.yaml>", help="Existing V1 Template."
)
return parser.parse_args()
def main():
"""Convert a network v1 template to the network v2 format.
The V1 template will be converted to V2 format. The V1 template will be
saved as a backup file before writing the V2 net-data format.
"""
args = parse_opts()
net_data_file = os.path.abspath(os.path.expanduser(args.v1))
with open(net_data_file) as f:
template_data = yaml.safe_load(f)
new_template_data = list()
for item in template_data:
new_item = dict()
item.pop("enabled", False) # Drop unused var
name = new_item["name"] = item.pop("name")
name_lower = new_item["name_lower"] = item.pop(
"name_lower", name.lower()
)
new_item["vip"] = item.pop("vip", False)
new_item["mtu"] = item.pop("mtu", 1500)
new_item["ipv6"] = item.pop("ipv6", False)
new_item["subnets"] = item.pop("subnets", dict())
new_item["subnets"]["{}_subnet".format(name_lower)] = item
new_template_data.append(new_item)
os.rename(net_data_file, "{}.bak".format(net_data_file))
try:
# content is converted to yaml before opening the file.
# This is done to ensure that we're not breaking any existing files
# during the conversion process.
dump_yaml = yaml.safe_dump(
new_template_data, default_style=False, sort_keys=False
)
except Exception as e:
print("Conversion could not be completed. Error:{}".format(str(e)))
else:
with open(net_data_file, "w") as f:
f.write(dump_yaml)
if __name__ == "__main__":
main()