summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Wedgwood <cw@f00f.org>2017-10-31 01:19:50 +0000
committerChris Wedgwood <cw@f00f.org>2017-10-31 01:19:50 +0000
commita2befb278b5990c268564a676446568ab4548455 (patch)
tree8679b50233f4a11ff255c76fa5de1671b0ad35b6
parent92a678bb5ca1bf1f13eb24aa82bca664e1204b4f (diff)
VM values validator
-rw-r--r--Makefile7
-rw-r--r--examples/bogus-vm.yaml4
-rw-r--r--validate.py109
3 files changed, 120 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index aced210..7d4fde5 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,13 @@
1# This Makefile is used during development and can usually be ignored 1# This Makefile is used during development and can usually be ignored
2# by most people. 2# by most people.
3 3
4validation:
5 @echo ===========================================================================
6 python validate.py example-vmlist-1.yaml
7 @echo ===========================================================================
8 python validate.py example-vmlist-bad.yaml
9 @echo ===========================================================================
10
4all: test 11all: test
5 12
6default: test 13default: test
diff --git a/examples/bogus-vm.yaml b/examples/bogus-vm.yaml
index 03a4d62..af45c64 100644
--- a/examples/bogus-vm.yaml
+++ b/examples/bogus-vm.yaml
@@ -8,3 +8,7 @@ vmlist:
8 cloudconfig: 8 cloudconfig:
9 metadata: "{ " 9 metadata: "{ "
10 userdata: false 10 userdata: false
11# bogus mess
12 - noname: "idonthaveaname"
13 enabled: "can't be bothered"
14 extra: "peanuts"
diff --git a/validate.py b/validate.py
new file mode 100644
index 0000000..61b6c50
--- /dev/null
+++ b/validate.py
@@ -0,0 +1,109 @@
1#!/usr/bin/python
2#
3# Ideally we would use jsonschema.validate but getting useful error
4# output has been challenging. This can be revisited as needed.
5
6import yaml
7import sys
8
9def validate_leaves(prefix, vm, l):
10 valid_leaves = [ ]
11
12 # first check to make sure we have what we think we should
13 for nd in l:
14 n = nd[0]
15 t = nd[1]
16 o = False
17 if len(nd)>2:
18 o = nd[2]
19 try:
20 if n not in vm.keys():
21 if o:
22 print "[W] Missing leaf:", prefix+n
23 else:
24 print "[E] Missing leaf:", prefix+n
25 continue
26 if type(vm[n]) != type(t):
27 print "[E] Wrong type for %s (got '%s' expected '%s')" % (prefix+n, type(vm[n]).__name__, type(t).__name__)
28 continue
29 except:
30 print "[W] Unable to validate leaf:", prefix+n
31 continue
32
33 valid_leaves.append(n)
34
35 if type(vm) == type(dict()):
36 # now look for things we don't know how to deal with
37 for n in vm.keys():
38 if n not in [ x[0] for x in l]:
39 print "[W] Unexpected leaf:", prefix+n
40
41 return valid_leaves
42
43def validate_file(filename):
44 yamlgen = yaml.load_all(open(filename))
45 top = [ x for x in yamlgen ][0]
46
47 vmlist = top["vmlist"]
48 index = 0
49 for vm in vmlist:
50 index += 1
51 name = ""
52 if "name" in vm:
53 name = vm["name"]
54
55 print "Checking:", name, ("(index %d)" % index)
56
57 vl = validate_leaves("", vm, [ ("name",str()), ("enabled",bool()), ("vmconfig",dict()), ("netconfig",dict()), ("cloudconfig",dict()) ] )
58 if "vmconfig" in vl:
59 # validate vmconfig
60 vmconfig = vm["vmconfig"]
61 vl2 = validate_leaves("vmconfig.", vmconfig, [ ("cpu",dict()), ("rootfs",dict()) ])
62
63 if "cpu" in vl2:
64 # validate vmconfig.cpu
65 cpu = vmconfig["cpu"]
66 vl3 = validate_leaves("vmconfig.cpu.", cpu, [ ("vcpu",int()), ("ram_mb",int()) ])
67 if "vcpus" in vl3:
68 vcpu = int(cpu["vcpus"])
69 if vcpu < 1 or vcpu > 8:
70 print "[W] vmconfig.cpu.vcpu has odd looking value:", vcpu
71 if "ram_mb" in vl3:
72 ram_mb = int(cpu["ram_mb"])
73 if ram_mb < 512 or ram_mb > 32768:
74 print "[W] vmconfig.cpu.ram_mb has odd looking value:", ram_mb
75
76 if "rootfs" in vl2:
77 # validate vmconfig.rootfs
78 rootfs = vmconfig["rootfs"]
79 vl3 = validate_leaves("vmconfig.rootfs.", rootfs, [ ("sourceurl",str()), ("type_notimpl",str()), ("localtarget",str()), ("pvc_size",str()), ("pvc_class",str(), True) ])
80
81 if "sourceurl" in vl3:
82 if not rootfs["sourceurl"].startswith("http"):
83 print "[W] vmconfig.rootfs.sourceurl has odd looking value:", rootfs["sourceurl"]
84
85 if "netconfig" in vl:
86 # validate netconfig
87 netconfig = vm["netconfig"]
88 vm2 = validate_leaves("netconfig.", netconfig, [ ("ports",dict()), ("readinessTcpProbe",int(), True) ])
89 # do more?
90
91 if "cloudconfig" in vl:
92 # validate cloudconfig
93 cloudconfig = vm["cloudconfig"]
94 vl2 = validate_leaves("cloudconfig.", cloudconfig, [ ("instance_id",str()), ("metadata",str()), ("userdata",str()) ])
95
96 # check things look sane
97 for yamlobj in [ "metadata", "userdata" ]:
98 if yamlobj in vl2:
99 try:
100 yaml.load(cloudconfig[yamlobj])
101 except:
102 print "[E] Bad yaml for vmconfig.cloudconfig.%s" % yamlobj
103
104 print "Done"
105 print
106
107if __name__ == "__main__":
108 for fn in sys.argv[1:]:
109 validate_file(fn)