Example #1
0
 def test_basic_drop(self):
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("wireless.wl0.psk", "password")
     client.set("wireless.wl0.channel", 40)
     client.drop()
     self.assertEqual(sysadminctl_pb2.KEY_NOT_FOUND,
                      client.get("wireless.wl0.psk").status)
     self.assertEqual(sysadminctl_pb2.KEY_NOT_FOUND,
                      client.get("wireless.wl0.channel").status)
Example #2
0
def FetchAllValues(keys):
    client = SysAdminClient("127.0.0.1", 4000)
    output = {}
    for arg in keys:
        resp = client.get(arg)
        if resp.status == sysadminctl_pb2.SUCCESS:
            for value in resp.get.kvs:
                output[value.key] = UnpackFromProto(value.value)
        elif "last." not in arg:
            raise Exception("Could not find required key. Key %s, Error: %s" % (arg, resp.status))
    return condense(output)
Example #3
0
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())
Example #4
0
 def test_new_key_rollback(self):
     #make a commit with a new key and value, ensure that a rollback can remove the key
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("cheese", 42)
     self.assertEqual(1, client.commit().commit.commit_id)
     self.assertEqual(
         42, UnpackFromProto(client.get("cheese").get.kvs[0].value))
     self.assertEqual(sysadminctl_pb2.SUCCESS, client.rollback(1).status)
     self.assertEqual(sysadminctl_pb2.KEY_NOT_FOUND,
                      client.get("cheese").status)
Example #5
0
def change_type(sysadmin, key, newtype):
    existing = sysadmin.get(key)
    if existing.status == sysadminctl_pb2.KEY_NOT_FOUND:
        print("Key {} must exist to have it's type changed".format(key))
        return
    oldtype = str(existing.get.kvs[0].value.WhichOneof("value"))
    value = UnpackFromProto(existing.get.kvs[0].value)
    if "list" in oldtype and "list" not in newtype:
        if len(getattr(existing.get.kvs[0].value, oldtype).list) > 1:
            print("Can't type convert a list larger than one element")
            return
        value = value[0]
    # sysadmin's type system requires that we remove the old value
    # completely before being able to set it with a new type
    cloneClient = SysAdminClient(sysadmin.ip, sysadmin.port, 10)
    cloneClient.erase(key)
    cloneClient.commit(sysadminctl_pb2.NO_HOOKS)
    sysadmin.set(key, value, newtype)
Example #6
0
 def test_trigger(self):
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("network.dhcp.startip", "192.168.1.1")
     client.set("network.dhcp.endip", "192.168.1.100")
     client.set("network.dhcp.interfaces", ["eth0", "eth1"])
     client.set("my.service.restarter", True)
     client.set("network.dhcp.breakfast", "waffles")
     client.set("network.dhcp.lease", 1440)
     client.commit()
     self.assertEqual(sysadminctl_pb2.SUCCESS,
                      client.trigger("test.conf").status)
     self.assertEqual(sysadminctl_pb2.SUCCESS,
                      client.trigger("testService").status)
     self.assertEqual(sysadminctl_pb2.HOOK_NOT_FOUND,
                      client.trigger("nonExist").status)
Example #7
0
    def setUp(self):
        SysAdminFixture.setUp(self)
        client = SysAdminClient("127.0.0.1", 4000)
        client.set("network.dhcp.startip", "192.168.1.1")
        client.set("network.dhcp.endip", "192.168.1.100")
        client.set("network.dhcp.interfaces", ["eth0", "eth1"])
        client.set("my.service.restarter", True)
        client.set("network.breakfast", "eggs")
        client.set("network.lease", 1440)
        client.set("default.network.breakfast", "waffles")
        client.set("default.network.lease", 1440)
        client.set("loops.one", 1)
        client.set("loops.two", 2)
        client.set("loops.three", 3)

        client.commit()
Example #8
0
    def test_new_key_migration(self):
        client = SysAdminClient("127.0.0.1", 4000)
        client.set("network.dhcp.startip", "192.168.1.1")
        client.set("network.dhcp.endip", "192.168.1.100")
        client.set("network.dhcp.interfaces", ["eth0", "eth1"])
        client.set("my.service.restarter", True)
        client.set("network.breakfast", "eggs")
        client.set("network.lease", 1440)
        client.set("default.network.breakfast", "waffles")
        client.set("default.network.lease", 1440)
        client.commit()

        client.reset()
        resp = client.get("network.dhcp.startip")
        self.assertEqual(sysadminctl_pb2.KEY_NOT_FOUND, resp.status)
        self.assertEqual(
            "waffles",
            UnpackFromProto(client.get("network.breakfast").get.kvs[0].value))
Example #9
0
 def test_dump_hooks(self):
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("network.dhcp.startip", "192.168.1.1")
     client.set("network.dhcp.endip", "192.168.1.100")
     client.set("network.dhcp.interfaces", ["eth0", "eth1"])
     client.set("my.service.restarter", True)
     client.set("network.dhcp.breakfast", "waffles")
     client.set("network.dhcp.lease", 1440)
     client.commit()
     resp = client.dumphooks()
     self.assertEqual(sysadminctl_pb2.SUCCESS, resp.status)
     self.assertEqual("test.conf", resp.dump.templatehooks[0])
     self.assertEqual("testService", resp.dump.servicehooks[0])
Example #10
0
 def test_overridden_xid(self):
     # This client gets the regular sysadmin assigned id
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("network.dhcp.startip", "192.168.1.1")
     client.set("network.dhcp.endip", "192.168.1.100")
     # This client has a overridden id 0
     otherclient = SysAdminClient("127.0.0.1", 4000, 0)
     # This client can't commit the original clients changes
     otherclient.commit()
     self.assertEqual(sysadminctl_pb2.KEY_NOT_FOUND,
                      client.get("network.dhcp.startip").status)
     self.assertEqual(sysadminctl_pb2.KEY_NOT_FOUND,
                      client.get("network.dhcp.endip").status)
     client.commit()
     self.assertEqual(
         "192.168.1.1",
         UnpackFromProto(
             client.get("network.dhcp.startip").get.kvs[0].value))
     self.assertEqual(
         "192.168.1.100",
         UnpackFromProto(client.get("network.dhcp.endip").get.kvs[0].value))
Example #11
0
 def test_basic_rollback(self):
     # This client gets the regular sysadmin assigned id
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("network.dhcp.startip", "192.168.1.1")
     client.set("network.dhcp.endip", "192.168.1.100")
     self.assertEqual(1, client.commit().commit.commit_id)
     client.set("network.dhcp.startip", "10.0.0.1")
     client.set("network.dhcp.endip", "10.0.0.100")
     self.assertEqual(2, client.commit().commit.commit_id)
     # Can't rollback the first commit
     self.assertEqual(sysadminctl_pb2.FAILED_ROLLBACK,
                      client.rollback(1).status)
     self.assertEqual(sysadminctl_pb2.SUCCESS, client.rollback(2).status)
     self.assertEqual(
         "192.168.1.1",
         UnpackFromProto(
             client.get("network.dhcp.startip").get.kvs[0].value))
     self.assertEqual(sysadminctl_pb2.FAILED_ROLLBACK,
                      client.rollback(100).status)
Example #12
0
 def test_new_key_migration(self):
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("network.dhcp.startip", "192.168.1.1")
     client.set("network.dhcp.endip", "192.168.1.100")
     client.set("network.dhcp.interfaces", ["eth0", "eth1"])
     client.set("my.service.restarter", True)
     client.set("network.breakfast", "waffles")
     client.set("network.lease", 1440)
     client.commit()
     m = SysAdminMigrator(client)
     m.new_keys({"new.key": "value"})
     client.commit()
     self.assertEqual(
         "value", UnpackFromProto(client.get("new.key").get.kvs[0].value))
     m.change_values({"new.key": "4"})
     client.commit()
     self.assertEqual(
         "4", UnpackFromProto(client.get("new.key").get.kvs[0].value))
     m.change_types({"new.key": "int32"})
     client.commit()
     self.assertEqual(
         4, UnpackFromProto(client.get("new.key").get.kvs[0].value))
     m.rename_keys([{"new.key": "renamed.key"}])
     client.commit()
     self.assertEqual(
         4, UnpackFromProto(client.get("renamed.key").get.kvs[0].value))
     m.remove_keys(["new.key"])
     client.commit()
     self.assertEqual(
         4, UnpackFromProto(client.get("renamed.key").get.kvs[0].value))
Example #13
0
    def test_basic_templating(self):
        client = SysAdminClient("127.0.0.1", 4000)
        client.set("network.dhcp.startip", "192.168.1.1")
        client.set("network.dhcp.endip", "192.168.1.100")
        client.set("network.dhcp.lease", 1440)
        client.set("network.dhcp.interfaces", ["eth0", "eth1"])
        client.set("network.dhcp.breakfast", "waffles")
        # fulfilled for the service which isn't tested here
        client.set("my.service.restarter", True)
        client.commit()
        self.assertFileExistsTimeout("test.conf")
        self.assertRenderedTemplateMatches(
            """dhcp-range=192.168.1.1,192.168.1.100,1440
dhcp-authoritative
interfaces=eth0,eth1,
breakfast=waffles
""", "test.conf")
Example #14
0
 def test_fetching(self):
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("network.dhcp.startip", "192.168.1.1")
     client.set("network.dhcp.endip", "192.168.1.100")
     client.set("network.dhcp.interfaces", ["eth0", "eth1"])
     client.set("my.service.restarter", True)
     client.set("network.dhcp.breakfast", "waffles")
     client.set("network.dhcp.lease", 1440)
     client.commit()
     output = FetchAllValues([
         "network.dhcp.startip", "network.dhcp.lease",
         "network.dhcp.interfaces"
     ])
     dhcp = {
         "startip": "192.168.1.1",
         "lease": 1440,
         "interfaces": [u"eth0", u"eth1"]
     }
     self.assertEqual(output, {"network": {"dhcp": dhcp}})
Example #15
0
 def test_type_checking(self):
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("wireless.wl0.psk", "password")
     resp = client.set("wireless.wl0.psk", 5)
     self.assertEqual(sysadminctl_pb2.SUCCESS, resp.status)
     client.commit()
     resp = client.set("wireless.wl0.psk", "password")
     self.assertEqual(sysadminctl_pb2.TYPE_MISMATCH, resp.status)
     client.commit()
     self.assertEqual(
         5,
         UnpackFromProto(client.get("wireless.wl0.psk").get.kvs[0].value))
Example #16
0
 def test_fire_hooks_timeout(self):
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("network.dhcp.startip", "192.168.1.1")
     client.set("network.dhcp.endip", "192.168.1.100")
     client.set("network.dhcp.interfaces", ["eth0", "eth1"])
     client.set("my.service.restarter", True)
     client.set("network.dhcp.breakfast", "waffles")
     client.set("network.dhcp.lease", 1440)
     client.commit()
     client.socket.settimeout(0.001)
     self.assertRaises(socket.timeout, client.firehooks)
Example #17
0
    def test_fire_hooks(self):
        client = SysAdminClient("127.0.0.1", 4000)
        client.set("network.dhcp.startip", "192.168.1.1")
        client.set("network.dhcp.endip", "192.168.1.100")
        client.set("network.dhcp.interfaces", ["eth0", "eth1"])
        client.set("my.service.restarter", True)
        client.set("network.dhcp.breakfast", "waffles")
        client.set("network.dhcp.lease", 1440)
        client.commit()
        self.assertFileExistsTimeout("test.conf")
        self.assertFileExistsTimeout("argdumper")
        # remove the files dumped by the commit hook firing
        os.remove("test.conf")
        os.remove("argdumper")
        client.firehooks()
        self.assertFileExistsTimeout("test.conf")
        self.assertFileExistsTimeout("argdumper")
        self.assertRenderedTemplateMatches(
            """dhcp-range=192.168.1.1,192.168.1.100,1440
dhcp-authoritative
interfaces=eth0,eth1,
breakfast=waffles\n""", "test.conf")
        self.assertArgsDumpedProperly(
            "network.dhcp.startip network.dhcp.endip network.dhcp.interfaces my.service.restarter network.dhcp.breakfast network.dhcp.lease"
        )  # noqa
Example #18
0
 def test_basic_service(self):
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("network.dhcp.startip", "192.168.1.1")
     client.set("network.dhcp.endip", "192.168.1.100")
     client.set("network.dhcp.interfaces", ["eth0", "eth1"])
     client.set("my.service.restarter", True)
     # the following are fulfilled for the template that isn't tested here
     client.set("network.dhcp.breakfast", "waffles")
     client.set("network.dhcp.lease", 1440)
     client.commit(sysadminctl_pb2.NO_HOOKS)
     self.assertFileDoesNotExistTimeout("argdumper")
     client.set("network.dhcp.endip", "192.168.1.99")
     client.commit(sysadminctl_pb2.TEMPLATE_ONLY)
     self.assertFileDoesNotExistTimeout("argdumper")
     client.set("network.dhcp.endip", "192.168.1.98")
     client.commit(sysadminctl_pb2.DEFAULT)
     self.assertFileExistsTimeout("argdumper")
     self.assertArgsDumpedProperly("network.dhcp.endip")  # noqa
Example #19
0
    def test_basic_sets(self):
        # one in-flight
        client = SysAdminClient("127.0.0.1", 4000)
        resp = client.set("wireless.wl0.ssid", "cheese")
        self.assertEqual("wireless.wl0.ssid", resp.get.kvs[0].key)
        self.assertEqual("cheese", UnpackFromProto(resp.get.kvs[0].value))
        resp = client.get("wireless.wl0.ssid")
        self.assertEqual(sysadminctl_pb2.KEY_NOT_FOUND, resp.status)
        client.commit()
        resp = client.get("wireless.wl0.ssid")
        self.assertEqual("cheese", UnpackFromProto(resp.get.kvs[0].value))

        # multiple in-flight, one erasing the other
        client.set("wireless.wl0.psk", "password")
        client.set("wireless.wl0.channel", 40)
        client.set("wireless.wl0.channel", 60)
        client.set("wireless.wl0.enabled", True)
        client.set("wireless.wl0.frequencies", [1, 2, 3])
        client.commit()
        self.assertEqual(
            60,
            UnpackFromProto(
                client.get("wireless.wl0.channel").get.kvs[0].value))
        self.assertEqual(
            "password",
            UnpackFromProto(client.get("wireless.wl0.psk").get.kvs[0].value))
        self.assertEqual(
            True,
            UnpackFromProto(
                client.get("wireless.wl0.enabled").get.kvs[0].value))
        self.assertEqual(
            [1, 2, 3],
            UnpackFromProto(
                client.get("wireless.wl0.frequencies").get.kvs[0].value))
Example #20
0
 def test_change_types(self):
     client = SysAdminClient("127.0.0.1", 4000)
     m = SysAdminMigrator(client)
     m.new_keys({"gonna.be.list": "value"})
     client.commit()
     m.change_types({"gonna.be.list": "stringlist"})
     client.commit()
     self.assertEqual(["value"],
                      UnpackFromProto(
                          client.get("gonna.be.list").get.kvs[0].value))
     m.change_types({"gonna.be.list": "str"})
     client.commit()
     self.assertEqual(
         "value",
         UnpackFromProto(client.get("gonna.be.list").get.kvs[0].value))
     m.new_keys({"gonna.be.intlist": "4"})
     client.commit()
     m.change_types({"gonna.be.intlist": "int32list"})
     client.commit()
     self.assertEqual([4],
                      UnpackFromProto(
                          client.get("gonna.be.intlist").get.kvs[0].value))
     m.change_types({"gonna.be.intlist": "str"})
     client.commit()
     self.assertEqual(
         "4",
         UnpackFromProto(client.get("gonna.be.intlist").get.kvs[0].value))
     m.new_keys({"gonna.be.boollist": "true"})
     client.commit()
     m.change_types({"gonna.be.boollist": "boollist"})
     client.commit()
     self.assertEqual([True],
                      UnpackFromProto(
                          client.get("gonna.be.boollist").get.kvs[0].value))
     m.change_types({"gonna.be.boollist": "str"})
     client.commit()
     self.assertEqual(
         "True",
         UnpackFromProto(client.get("gonna.be.boollist").get.kvs[0].value))
     m.new_keys({"long.list": ["stringlist", "4", "5", "6"]})
     client.commit()
     m.change_types({"long.list": "int32list"})
     client.commit()
     self.assertEqual([4, 5, 6],
                      UnpackFromProto(
                          client.get("long.list").get.kvs[0].value))
     m.change_types({"long.list": "int32list"})
     client.commit()
     self.assertEqual([4, 5, 6],
                      UnpackFromProto(
                          client.get("long.list").get.kvs[0].value))
     m.change_types({"long.list": "stringlist"})
     client.commit()
     self.assertEqual(["4", "5", "6"],
                      UnpackFromProto(
                          client.get("long.list").get.kvs[0].value))
Example #21
0
 def test_get_all_keys(self):
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("network.dhcp.startip", "192.168.1.1")
     client.set("network.dhcp.endip", "192.168.1.100")
     client.set("network.dhcp.interfaces", ["eth0", "eth1"])
     client.set("my.service.restarter", True)
     client.set("network.breakfast", "waffles")
     client.set("network.lease", 1440)
     client.commit()
     resp = set(map(lambda x: x.key, client.get("network.dhcp.*").get.kvs))
     self.assertEqual(
         set([
             "network.dhcp.startip", "network.dhcp.endip",
             "network.dhcp.interfaces"
         ]), resp)
     resp = map(lambda x: UnpackFromProto(x.value),
                client.get("network.dhcp.*").get.kvs)
     self.assertEqual(3, len(resp))
     self.assertTrue("192.168.1.1" in resp)
     self.assertTrue("192.168.1.100" in resp)
     self.assertTrue(["eth0", "eth1"] in resp)
     resp = set(map(lambda x: x.key, client.get("network.*").get.kvs))
     self.assertEqual(
         set([
             "network.dhcp.startip", "network.dhcp.endip",
             "network.dhcp.interfaces", "network.breakfast", "network.lease"
         ]), resp)
     resp = map(lambda x: UnpackFromProto(x.value),
                client.get("network.*").get.kvs)
     self.assertEqual(5, len(resp))
     self.assertTrue("192.168.1.1" in resp)
     self.assertTrue("192.168.1.100" in resp)
     self.assertTrue(["eth0", "eth1"] in resp)
     self.assertTrue("waffles" in resp)
     self.assertTrue(1440 in resp)
Example #22
0
 def test_erase(self):
     client = SysAdminClient("127.0.0.1", 4000)
     client.set("wireless.wl0.psk", "password")
     client.set("wireless.wl0.channel", 40)
     client.set("wireless.wl0.enabled", True)
     client.set("wireless.wl0.frequencies", [1, 2, 3])
     client.commit()
     self.assertEqual(
         "password",
         UnpackFromProto(client.get("wireless.wl0.psk").get.kvs[0].value))
     client.erase("wireless.wl0.psk")
     self.assertEqual(
         "password",
         UnpackFromProto(client.get("wireless.wl0.psk").get.kvs[0].value))
     client.erase("not.a.key")
     client.commit()
     self.assertEqual(sysadminctl_pb2.KEY_NOT_FOUND,
                      client.get("wireless.wl0.psk").status)