A minimalist VM runner for Kubernetes using Helm.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validate.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. import yaml
  6. import sys
  7. def validate_leaves(prefix, vm, l):
  8. valid_leaves = [ ]
  9. # first check to make sure we have what we think we should
  10. for nd in l:
  11. n = nd[0]
  12. t = nd[1]
  13. o = False
  14. if len(nd)>2:
  15. o = nd[2]
  16. try:
  17. if n not in vm.keys():
  18. if o:
  19. print "[W] Missing leaf:", prefix+n
  20. else:
  21. print "[E] Missing leaf:", prefix+n
  22. continue
  23. if type(vm[n]) != type(t):
  24. print "[E] Wrong type for %s (got '%s' expected '%s')" % (prefix+n, type(vm[n]).__name__, type(t).__name__)
  25. continue
  26. except:
  27. print "[W] Unable to validate leaf:", prefix+n
  28. continue
  29. valid_leaves.append(n)
  30. if type(vm) == type(dict()):
  31. # now look for things we don't know how to deal with
  32. for n in vm.keys():
  33. if n not in [ x[0] for x in l]:
  34. print "[W] Unexpected leaf:", prefix+n
  35. return valid_leaves
  36. def validate_file(filename):
  37. try:
  38. yamlgen = list(yaml.safe_load_all(open(filename)))
  39. except yaml.parser.ParserError:
  40. print "[E] Invalid yaml"
  41. return
  42. if not yamlgen or not yamlgen[0]:
  43. print "[E] File contains no valid yaml"
  44. return
  45. top = list(yamlgen)[0]
  46. vmlist = top["vmlist"]
  47. if not vmlist or not isinstance(vmlist, dict):
  48. print "[E] No vmlist dict declared"
  49. return
  50. for name in vmlist:
  51. vm = vmlist[name]
  52. print "VM:", name
  53. vl = validate_leaves("", vm, [ ("enabled",bool()), ("vmconfig",dict()), ("netconfig",dict()), ("cloudconfig",dict()) ] )
  54. if "vmconfig" in vl:
  55. # validate vmconfig
  56. vmconfig = vm["vmconfig"]
  57. vl2 = validate_leaves("vmconfig.", vmconfig, [ ("cpu",dict()), ("rootfs",dict()) ])
  58. if "cpu" in vl2:
  59. # validate vmconfig.cpu
  60. cpu = vmconfig["cpu"]
  61. vl3 = validate_leaves("vmconfig.cpu.", cpu, [ ("vcpu",int()), ("ram_mb",int()) ])
  62. if "vcpus" in vl3:
  63. vcpu = int(cpu["vcpus"])
  64. if vcpu < 1 or vcpu > 8:
  65. print "[W] vmconfig.cpu.vcpu has odd looking value:", vcpu
  66. if "ram_mb" in vl3:
  67. ram_mb = int(cpu["ram_mb"])
  68. if ram_mb < 512 or ram_mb > 32768:
  69. print "[W] vmconfig.cpu.ram_mb has odd looking value:", ram_mb
  70. if "rootfs" in vl2:
  71. # validate vmconfig.rootfs
  72. rootfs = vmconfig["rootfs"]
  73. vl3 = validate_leaves("vmconfig.rootfs.", rootfs, [ ("sourceurl",str()), ("localtarget",str()), ("pvc_size",str()), ("pvc_class",str(), True) ])
  74. if "sourceurl" in vl3:
  75. if not rootfs["sourceurl"].startswith("http"):
  76. print "[W] vmconfig.rootfs.sourceurl has odd looking value:", rootfs["sourceurl"]
  77. if "netconfig" in vl:
  78. # validate netconfig
  79. netconfig = vm["netconfig"]
  80. vm2 = validate_leaves("netconfig.", netconfig, [ ("ports",dict()), ("readinessTcpProbe",int(), True) ])
  81. # do more?
  82. if "cloudconfig" in vl:
  83. # validate cloudconfig
  84. cloudconfig = vm["cloudconfig"]
  85. vl2 = validate_leaves("cloudconfig.", cloudconfig, [ ("metadata",str()), ("userdata",str()) ])
  86. # check things look sane
  87. for yamlobj in [ "metadata", "userdata" ]:
  88. if yamlobj in vl2:
  89. try:
  90. yaml.load(cloudconfig[yamlobj])
  91. except:
  92. print "[E] Bad yaml for vmconfig.cloudconfig.%s" % yamlobj
  93. if __name__ == "__main__":
  94. for fn in sys.argv[1:]:
  95. print "Filename:", fn
  96. validate_file(fn)
  97. print