Merge "Allow deployment on Centos 6.5 environment"
[genesis.git] / fuel / prototypes / libvirt / deploy / tools / transplant_interfaces.py
1 #!/usr/bin/python
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 # stefan.k.berg@ericsson.com
5 # jonas.bjurel@ericsson.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 import yaml
13 import re
14 import sys
15 import os
16
17 if len(sys.argv) != 3:
18     sys.stderr.write("Usage: "+sys.argv[0]+" <infile> <deafile>\n")
19     sys.exit(1)
20
21 infile = sys.argv[1]
22 if not os.path.exists(infile):
23     sys.stderr.write("ERROR: The file "+infile+" could not be opened\n")
24     sys.exit(1)
25
26 deafile = sys.argv[2]
27 if not os.path.exists(deafile):
28     sys.stderr.write("ERROR: The file "+deafile+" could not be opened\n")
29     sys.exit(1)
30 deafile = sys.argv[2]
31
32 namespace = "interfaces"
33
34 f1 = open(infile, 'r')
35 doc1 = yaml.load(f1)
36 f1.close()
37
38 f2 = open(deafile, 'r')
39 doc2 = yaml.load(f2)
40 f2.close()
41
42 # Create lookup table network name -> id for current setup
43 nwlookup = {}
44 for interface in doc1:
45   iface = {}
46   networks = []
47   for network in interface["assigned_networks"]:
48     nwlookup[network["name"]] = network["id"]
49   
50
51 out = {}
52 out["interfaces"] = {}
53
54 for interface in doc1:
55   assigned = []
56   nw = {}
57   interface["assigned_networks"] = []
58   for nwname in doc2["interfaces"][interface["name"]]:
59     iface = {}
60     iface["id"] = nwlookup[nwname]
61     iface["name"] = nwname
62     interface["assigned_networks"].append(iface)
63
64 f3 = open(infile, 'w')
65 f3.write(yaml.dump(doc1, default_flow_style=False))
66 f3.close()