def main(): parser = argparse.ArgumentParser(description="Templater for config files") parser.add_argument('--template-args', type=str, nargs="*", help="Special Arguments to be sent to templates") parser.add_argument('--committed', type=str, nargs="*", help="Key:Value mappings to be slotted into template") parser.add_argument('template', type=str, help="The file path of the jinja template") parser.add_argument('dest', type=str, help="The file path of the rendered template") args = parser.parse_args() lazy = LazySysAdmin(SysAdminClient("127.0.0.1", 4000)) lazy.prefetch() renderer = ConfigTemplateRenderer(args.template) fullArgs = {'values': lazy} fullArgs['args'] = getattr(args, 'template_args', None) fullArgs['committed'] = getattr(args, 'committed', None) rendered = renderer.renderTemplate(fullArgs) dirname = os.path.dirname(args.dest) try: if not os.path.exists(dirname): os.makedirs(dirname) except OSError as e: print "Error making dirs for template %s" % args.dest with open(args.dest, "w") as f: f.write(rendered) f.flush() os.fsync(f.fileno())
def test_prefetch(self): s = LazySysAdmin(ClientWrapper()) s["network"].prefetch() call_count = s.sysadmin.call_counts["get"] self.assertEqual("192.168.1.1", s["network"]["dhcp"]["startip"]) self.assertEqual(call_count, s.sysadmin.call_counts["get"]) self.assertEqual("192.168.1.1", s["network"]["dhcp"]["startip"]) self.assertEqual(call_count, s.sysadmin.call_counts["get"]) self.assertEqual("192.168.1.100", s["network"]["dhcp"]["endip"]) self.assertEqual(True, s["my"]["service"]["restarter"]) self.assertLess(call_count, s.sysadmin.call_counts["get"]) s.prefetch() call_count = s.sysadmin.call_counts["get"] self.assertEqual("waffles", s["default"]["network"]["breakfast"]) self.assertEqual(call_count, s.sysadmin.call_counts["get"])
def test_iteritems(self): s = LazySysAdmin(ClientWrapper()) loop = s["loops"] i = 0 for key, value in loop.iteritems(): i += 1 if key == "one": self.assertEqual(value, 1) elif key == "two": self.assertEqual(value, 2) else: self.assertEqual(value, 3) self.assertEqual(i, 3) self.assertEqual(2, s.sysadmin.call_counts["get"]) for key, value in s.iteritems(): self.assertEqual(s[key], value)
def test_get_default(self): s = LazySysAdmin(ClientWrapper()) self.assertEqual("192.168.1.1", s["network"]["dhcp"].get("startip", "")) self.assertEqual("192.168.1.100", s["network"]["dhcp"].get("endip", "")) self.assertEqual(["eth0", "eth1"], s["network"]["dhcp"].get("interfaces", "")) self.assertFalse(s["network"]["dhcp"].get("notreal", False)) self.assertTrue(s["network"].get("dhcp", True))
def test_iteration(self): s = LazySysAdmin(ClientWrapper()) # This is an interesting thing to test because the act of iteration # should save all the namespaces values in the lazy dictionary, and not # require further fetches from sysadmin to access the sub values for _ in s["network"]: pass self.assertEqual(2, s.sysadmin.call_counts["get"]) s["network"] self.assertEqual(2, s.sysadmin.call_counts["get"]) self.assertEqual("192.168.1.1", s["network"]["dhcp"]["startip"]) self.assertEqual(2, s.sysadmin.call_counts["get"]) self.assertEqual("192.168.1.1", s["network"]["dhcp"]["startip"]) self.assertEqual(2, s.sysadmin.call_counts["get"]) self.assertEqual("192.168.1.100", s["network"]["dhcp"]["endip"]) self.assertEqual(2, s.sysadmin.call_counts["get"]) self.assertEqual(["eth0", "eth1"], s["network"]["dhcp"]["interfaces"]) self.assertEqual(2, s.sysadmin.call_counts["get"]) for _ in s["network"]["dhcp"]: pass self.assertEqual(3, s.sysadmin.call_counts["get"])
def test_looping(self): s = LazySysAdmin(ClientWrapper()) counter = 0 for key in s: counter += 1 self.assertEqual(11, counter) all_values = [ "192.168.1.1", "192.168.1.100", ["eth0", "eth1"], ] for key in s["network"]["dhcp"]: value = s["network"]["dhcp"][key] all_values.remove(value) self.assertEqual(0, len(all_values)) for key in s["network"]: if key == "dhcp": self.assertTrue(s["network"].is_namespace(key)) for dk in s["network"][key]: self.assertTrue(s["network"][key].is_value(dk)) else: self.assertTrue(s["network"].is_value(key))
def test_identifiers(self): s = LazySysAdmin(ClientWrapper()) self.assertTrue(s["network"].is_namespace("dhcp")) self.assertFalse(s["network"].is_value("dhcp")) self.assertTrue(s["network"]["dhcp"].is_value("startip")) self.assertFalse(s["network"]["dhcp"].is_namespace("startip"))
def test_list_iteration(self): s = LazySysAdmin(ClientWrapper()) for iface in s["network"]["dhcp"]["interfaces"]: # just confirming I'm getting a nice string value out of this # iteration self.assertTrue("eth" in iface)