From 56503fdcdb3f6b1bdfde7e6e41204dad11dcc0e6 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Thu, 1 Sep 2022 23:15:22 -0700 Subject: [PATCH] add host object, to be used for mappings... --- ui/fixtures/cmd.hosts.json | 21 +++++++++++++ ui/medashare/cli.py | 62 +++++++++++++++++++++++++++++++++++--- 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 ui/fixtures/cmd.hosts.json diff --git a/ui/fixtures/cmd.hosts.json b/ui/fixtures/cmd.hosts.json new file mode 100644 index 0000000..a950d88 --- /dev/null +++ b/ui/fixtures/cmd.hosts.json @@ -0,0 +1,21 @@ +[ +{ + "title": "gen ident", + "cmd": [ "genident", "name=A Test User" ] +}, +{ + "special": "set hostid", + "comment": "and that a modified hostid", + "hostid": "ceaa4862-dd00-41ba-9787-7480ec1b2679" +}, +{ + "title": "that hosts lists itself", + "cmd": [ "hosts" ], + "stdout_re": ".*\tceaa4862-dd00-41ba-9787-7480ec1b2679\n" +}, +{ + "special": "verify store object cnt", + "comment": "and that the host object was created", + "count": 1 +} +] diff --git a/ui/medashare/cli.py b/ui/medashare/cli.py index 7ce9392..936ffe2 100644 --- a/ui/medashare/cli.py +++ b/ui/medashare/cli.py @@ -30,6 +30,7 @@ import pathlib import pasn1 import re import shutil +import socket import string import sys import tempfile @@ -327,6 +328,11 @@ class Persona(object): self._created_by_ref = self._identity.uuid + def Host(self, *args, **kwargs): + kwargs['created_by_ref'] = self.uuid + + return self.sign(Host(*args, **kwargs)) + def Container(self, *args, **kwargs): kwargs['created_by_ref'] = self.uuid @@ -471,6 +477,13 @@ class ObjectStore(object): self._created_by_ref = created_by_ref self._uuids = {} self._hashes = {} + self._hostuuids = {} + + def get_host(self, hostuuid): + return self._hostuuids[hostuuid] + + def get_hosts(self): + return self._hostuuids.values() @staticmethod def makehash(hashstr, strict=True): @@ -553,10 +566,18 @@ class ObjectStore(object): self._uuids[_makeuuid(obj.id)] = obj elif obj.type == 'container': self._uuids[obj.make_id(obj.uri)] = obj + elif obj.type == 'host': + self._uuids[obj.hostuuid] = obj + self._hostuuids[obj.hostuuid] = obj - for j in obj.hashes: - h = self.makehash(j) - self._hashes.setdefault(h, []).append(obj) + try: + hashes = obj.hashes + except AttributeError: + pass + else: + for j in hashes: + h = self.makehash(j) + self._hashes.setdefault(h, []).append(obj) @classmethod def load(cls, fname): @@ -679,6 +700,9 @@ def _hashfile(fname): return '%s:%s' % (_defaulthash, hash.hexdigest()) +class Host(MDBase): + _type = 'host' + class FileObject(MDBase): _type = 'file' @@ -892,6 +916,32 @@ def cmd_modify(options): write_objstore(options, objstr) +def printhost(host): + print('%s\t%s' % (host.name, host.hostuuid)) + +def cmd_hosts(options): + persona, objstr = get_objstore(options) + + selfuuid = hostuuid() + + try: + host = objstr.get_host(selfuuid) + except KeyError: + host = persona.Host(name=socket.gethostname(), hostuuid=selfuuid) + objstr.loadobj(host) + + printhost(host) + + hosts = objstr.get_hosts() + + for i in hosts: + if i is host: + continue + + printhost(i) + + write_objstore(options, objstr) + def cmd_dump(options): persona, objstr = get_objstore(options) @@ -1096,6 +1146,9 @@ def main(): help='files to modify') parser_container.set_defaults(func=cmd_container) + parser_hosts = subparsers.add_parser('hosts', help='dump all the hosts, self is always first') + parser_hosts.set_defaults(func=cmd_hosts) + parser_dump = subparsers.add_parser('dump', help='dump all the objects') parser_dump.set_defaults(func=cmd_dump) @@ -1597,7 +1650,8 @@ class _TestCases(unittest.TestCase): self.assertEqual(objcnt, cmd['count']) elif special == 'set hostid': hostidpatch = mock.patch(__name__ + '.hostuuid') - hostidpatch.start().return_value = uuid.uuid4() + hid = cmd['hostid'] if 'hostid' in cmd else uuid.uuid4() + hostidpatch.start().return_value = hid patches.append(hostidpatch) elif special == 'iter is unique': objst = ObjectStore.load(storefname)