blob: 0df2bea9f11bc2d52ca86eff4929da8689dc8155 [file] [log] [blame]
David K. Bainbridgea677d4e2016-09-11 20:01:32 -07001#!/usr/bin/env python
2
3import json
4import os
5import re
6import sys
7import shlex
8import string
9import ipaddress
David K. Bainbridge5b392b82016-11-15 12:31:14 -080010# WANT_JSON
David K. Bainbridgea677d4e2016-09-11 20:01:32 -070011
12# Regular expressions to identify comments and blank lines
13comment = re.compile("^\s*#")
14blank = re.compile("^\s*$")
15
16#####
17# Parsers
18#
19# Parses are methods that take the form 'parse_<keyword>', where the keyword
20# is the first word on a line in file. The purpose of the parser is to
21# evaluate the line tna update the interface model accordingly.
22####
23
24# Compares the current and desired network configuration to see if there
25# is a change and returns:
26# 0 if no change
27# -1 if the change has no semantic value (i.e. comments differ)
28# 1 if there is a semantic change (i.e. its meaningful)
29# the highest priority of change is returned, i.e. if there is both
30# a semantic and non-semantic change a 1 is returned indicating a
31# semantic change.
David K. Bainbridgef757bc72016-11-16 21:45:52 -080032def value_equal(left, right):
33 if type(left) == type(right):
34 return left == right
35 return str(left) == str(right)
36
David K. Bainbridgea677d4e2016-09-11 20:01:32 -070037def compare(have, want):
38 result = 0
39 for key in list(set().union(have.keys(), want.keys())):
40 if key in have.keys() and key in want.keys():
David K. Bainbridgef757bc72016-11-16 21:45:52 -080041 if not value_equal(have[key], want[key]):
David K. Bainbridgea677d4e2016-09-11 20:01:32 -070042 if key in ["description"]:
43 result = -1
44 else:
45 return 1
46 else:
47 if key in ["description"]:
48 result = -1
49 else:
50 return 1
51 return result
52
53# Creates an interface definition in the model and sets the auto
54# configuration to true
55def parse_auto(data, current, words, description):
56 if words[1] in data.keys():
57 iface = data[words[1]]
58 else:
59 iface = {}
60
61 if len(description) > 0:
62 iface["description"] = description
63
David K. Bainbridge5b392b82016-11-15 12:31:14 -080064 iface["auto"] = True
David K. Bainbridgea677d4e2016-09-11 20:01:32 -070065 data[words[1]] = iface
66 return words[1]
67
68# Creates an interface definition in the model if one does not exist and
69# sets the type and configuation method
70def parse_iface(data, current, words, description):
71 if words[1] in data.keys():
72 iface = data[words[1]]
73 else:
74 iface = {}
75
76 if len(description) > 0:
77 iface["description"] = description
78
79 iface["type"] = words[2]
80 iface["config"] = words[3]
81 data[words[1]] = iface
82 return words[1]
83
David K. Bainbridge5b392b82016-11-15 12:31:14 -080084allow_lists = ["pre-up", "post-up", "pre-down", "post-down"]
85
David K. Bainbridgea677d4e2016-09-11 20:01:32 -070086# Used to evaluate attributes and add a generic name / value pair to the interface
87# model
88def parse_add_attr(data, current, words, description):
David K. Bainbridge5b392b82016-11-15 12:31:14 -080089 global allow_lists
David K. Bainbridgea677d4e2016-09-11 20:01:32 -070090 if current == "":
91 raise SyntaxError("Attempt to add attribute '%s' without an interface" % words[0])
92
93 if current in data.keys():
94 iface = data[current]
95 else:
96 iface = {}
97
98 if len(description) > 0:
99 iface["description"] = description
100
David K. Bainbridge5b392b82016-11-15 12:31:14 -0800101 if words[0] in iface and words[0] in allow_lists:
102 have = iface[words[0]]
103 if type(have) is list:
104 iface[words[0]].append(" ".join(words[1:]))
105 else:
106 iface[words[0]] = [have, " ".join(words[1:])]
107 else:
108 iface[words[0]] = " ".join(words[1:])
109
David K. Bainbridgea677d4e2016-09-11 20:01:32 -0700110 data[current] = iface
111 return current
112
113#####
114# Writers
115#
116# Writers take the form of 'write_<keyword>` where keyword is an interface
117# attribute. The role of the writer is to output the attribute to the
118# output stream, i.e. the new interface file.
119#####
120
121# Writes a generic name / value pair indented
122def write_attr(out, name, value):
David K. Bainbridge5b392b82016-11-15 12:31:14 -0800123 if isinstance(value, list):
124 for line in value:
125 out.write(" %s %s\n" % (name, line))
126 else:
127 out.write(" %s %s\n" % (name, value))
David K. Bainbridgea677d4e2016-09-11 20:01:32 -0700128
129# Writes an interface definition to the output stream
130def write_iface(out, name, iface):
131 if "description" in iface.keys():
132 val = iface["description"]
133 if len(val) > 0 and val[0] != "#":
134 val = "# " + val
135 out.write("%s\n" % (val))
David K. Bainbridge5b392b82016-11-15 12:31:14 -0800136 if "auto" in iface.keys() and iface["auto"]:
David K. Bainbridgea677d4e2016-09-11 20:01:32 -0700137 out.write("auto %s\n" % (name))
138 out.write("iface %s %s %s\n" % (name, iface["type"], iface["config"]))
139 for attr in sorted(iface.keys(), key=lambda x:x in write_sort_order.keys() and write_sort_order[x] or 100):
140 if attr in write_ignore:
141 continue
142 writer = "write_%s" % (attr)
143 if writer in all_methods:
144 globals()[writer](out, attr, iface[attr])
145 else:
146 write_attr(out, attr, iface[attr])
147 out.write("\n")
148
149# Writes the new interface file
150def write(out, data):
151# out.write("# This file describes the network interfaces available on your system\n")
152# out.write("# and how to activate them. For more information, see interfaces(5).\n\n")
153 # First to loopback
154 for name, iface in data.items():
155 if iface["config"] != "loopback":
156 continue
157 write_iface(out, name, iface)
158
159 for iface in sorted(data.keys(), key=lambda x:x in write_iface_sort_order.keys() and write_iface_sort_order[x] or x):
160 if data[iface]["config"] == "loopback":
161 continue
162 write_iface(out, iface, data[iface])
163
164# The defaults for the netfile task
165src_file = "/etc/network/interfaces"
166dest_file = None
167merge_comments = False
168state = "present"
169name = ""
170force = False
171values = {
172 "config": "manual",
173 "type": "inet"
174}
175
176# read the argument string from the arguments file
177args_file = sys.argv[1]
178args_data = file(args_file).read()
179
180# parse the task options
David K. Bainbridge5b392b82016-11-15 12:31:14 -0800181arguments = json.loads(args_data)
182for key, value in arguments.iteritems():
David K. Bainbridgea677d4e2016-09-11 20:01:32 -0700183 # if setting the time, the key 'time'
184 # will contain the value we want to set the time to
185
186 # Strip off quotes that ansible sometimes adds
David K. Bainbridge5b392b82016-11-15 12:31:14 -0800187 #value = value.strip("\"\' ")
David K. Bainbridgea677d4e2016-09-11 20:01:32 -0700188
189 if key == "src":
190 src_file = value
191 elif key == "dest":
192 dest_file = value
193 elif key == "name":
194 name = value
195 elif key == "state":
196 state = value
197 elif key == "force":
198 force = value.lower() in ['true', 't', 'yes', 'y']
199 elif key == "description":
200 values["description"] = value
201 elif key == "merge-comments":
202 merge_comments = value.lower() in ['true', 't', 'yes', 'y']
203 elif key == "address":
204 if string.find(value, "/") != -1:
205 parts = value.split('/')
David K. Bainbridge5b392b82016-11-15 12:31:14 -0800206 addr = ipaddress.ip_network(value, strict=False)
David K. Bainbridgea677d4e2016-09-11 20:01:32 -0700207 values["address"] = parts[0]
208 values["network"] = addr.network_address.exploded.encode('ascii','ignore')
209 values["netmask"] = addr.netmask.exploded.encode('ascii','ignore')
210 values["broadcast"] = addr.broadcast_address.exploded.encode('ascii','ignore')
David K. Bainbridgea677d4e2016-09-11 20:01:32 -0700211 else:
212 values["address"] = value
213 elif key[0] != '_':
214 values[key] = value
215
216# If no destination file was specified, write it back to the same file
217if not dest_file:
218 dest_file = src_file
219
220# all methods is used to check if parser or writer methods exist
221all_methods = dir()
222
223# which attributes should be ignored and not be written as single
224# attributes values against and interface
225write_ignore = ["auto", "type", "config", "description"]
226
227# specifies the order in which attributes are written against an
228# interface. Any attribute note in this list is sorted by default
229# order after the attributes specified.
230write_sort_order = {
David K. Bainbridge5b392b82016-11-15 12:31:14 -0800231 "address" : 1,
232 "network" : 2,
233 "netmask" : 3,
234 "broadcast" : 4,
235 "gateway" : 5,
236 "pre-up" : 10,
237 "post-up" : 11,
238 "pre-down" : 12,
239 "post-down" : 13
David K. Bainbridgea677d4e2016-09-11 20:01:32 -0700240}
241
242write_iface_sort_order = {
243 "fabric" : "y",
244 "mgmtbr" : "z"
245}
246
247# Read and parse the specified interface file
248file = open(src_file, "r")
249ifaces = {}
250current = "" # The current interface being parsed
251description = ""
252for line in file.readlines():
253 line = line.rstrip('\n')
254
255 if comment.match(line):
256 if len(description) > 0:
257 description = description + '\n' + line
258 else:
259 description = line
260
261 if len(description) > 0 and blank.match(line):
262 description = description + '\n'
263
264 # Drop any comment of blank line
265 if comment.match(line) or blank.match(line):
266 continue
267
268 # Parse the line
269 words = line.split()
270 parser = "parse_" + words[0].replace("-", "_")
271 if parser in all_methods:
272 current = globals()[parser](ifaces, current, words, description)
273 else:
274 current = parse_add_attr(ifaces, current, words, description)
275
276 description = ""
277
278file.close()
279
280# Assume no change unless we discover otherwise
281result = {
282 "changed" : False
283}
284change_type = 0
285
286# if the interface specified and state is present then either add
287# it to the model or replace it if it already exists.
288if state == "query":
289 if name in ifaces.keys():
290 result["interface"] = ifaces[name]
291 result["found"] = True
292 else:
293 result["found"] = False
294elif state == "present":
295 if name in ifaces.keys():
296 have = ifaces[name]
297 change_type = compare(have, values)
298 result["change_type"] = change_type
299 if change_type != 0:
300 ifaces[name] = values
301 result["desc"] = ifaces[name]["description"]
302 if merge_comments and "description" in have.keys() and len(have["description"]) > 0:
303 result["merge_comments"] = True
304 if "description" in values.keys() and len(values["description"]) > 0:
305 ifaces[name]["description"] = values["description"] + "\n" + have["description"]
306 else:
307 ifaces[name]["description"] = have["description"]
308 result["changed"] = (change_type == 1)
309 else:
310 ifaces[name] = values
311 result["changed"] = True
312
313
314# if state is absent then remove it from the model
315elif state == "absent" and name in ifaces.keys():
316 del ifaces[name]
317 result["changed"] = True
318
319# Only write the output file if something has changed or if the
320# task requests a forced write.
321if force or result["changed"] or change_type != 0:
322 file = open(dest_file, "w+")
323 write(file, ifaces)
324 file.close()
325
326# Output the task result
327print json.dumps(result)