From ad0e3491d321d9c89fc54fc30f7c5784c100973f Mon Sep 17 00:00:00 2001 From: David Howells Date: Wed, 9 Apr 2014 17:44:11 +0100 Subject: [PATCH] Implement "vos listaddrs" Signed-off-by: David Howells --- suite/argparse.py | 5 ++ suite/commands/vos/listaddrs.py | 106 ++++++++++++++++++++++++++++++++ suite/lib/addrcache.py | 22 ++++++- suite/lib/uuid.py | 58 +++++++++++++++++ 4 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 suite/commands/vos/listaddrs.py create mode 100644 suite/lib/uuid.py diff --git a/suite/argparse.py b/suite/argparse.py index d27e4f1..6384a37 100644 --- a/suite/argparse.py +++ b/suite/argparse.py @@ -60,6 +60,11 @@ def get_partition_id(switch, params): def get_auth(switch, params): return params +def get_uuid(switch, params): + from afs.lib.uuid import str2uuid + return str2uuid(params[0]) + + def get_dummy(switch, params): return params diff --git a/suite/commands/vos/listaddrs.py b/suite/commands/vos/listaddrs.py new file mode 100644 index 0000000..68573af --- /dev/null +++ b/suite/commands/vos/listaddrs.py @@ -0,0 +1,106 @@ +# +# AFS Volume management toolkit: Volume location database list +# -*- coding: utf-8 -*- +# + +__copyright__ = """ +Copyright (C) 2014 Red Hat, Inc. All Rights Reserved. +Written by David Howells (dhowells@redhat.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public Licence version 2 as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public Licence for more details. + +You should have received a copy of the GNU General Public Licence +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +""" + +from afs.argparse import * +from afs.lib.debug import debug +from afs.lib.uuid import uuid2str +import afs.lib.addrcache as addrcache +import kafs + +help = "Display all VLDB entries" + +command_arguments = [ + [ "uuid", get_uuid, "os", "" ], + [ "host", get_dummy, "os", "
" ], + [ "printuuid", get_dummy, "fn" ], + [ "cell", get_cell, "os", "" ], + [ "noauth", get_auth, "fn" ], + [ "localauth", get_auth, "fn" ], + [ "verbose", get_dummy, "fn" ], + [ "encrypt", get_dummy, "fn" ], + [ "noresolve", get_dummy, "fn" ], +] + +cant_combine_arguments = [ + ( "cell", "localauth" ), + ( "noauth", "localauth" ), + ( "uuid", "host" ), +] + +description = r""" +Display all VLDB entries +""" + +def list_one(params, vl_conn, attributes): + ret = kafs.VL_GetAddrsU(vl_conn, attributes) + if "printuuid" in params: + print("UUID:", uuid2str(ret.uuidp1)) + + addrs = ret.blkaddrs + for a in addrs: + print(addrcache.resolve(params, a)) + if "printuuid" in params: + print() + +def list_all(params, vl_conn): + ret = kafs.VL_GetAddrs(vl_conn, 0, 0) + print("nentries", ret.nentries) + + found = ret.nentries + limit = ret.nentries * 2 + attributes = kafs.ListAddrByAttributes() + attributes.Mask |= kafs.VLADDR_INDEX + for index in range(1, 65536): + debug("Look up", index) + + attributes.index = index + try: + list_one(params, vl_conn, attributes) + found -= 1 + except kafs.RemoteAbort as msg: + if str(msg) == "Aborted 363524": + continue + elif str(msg) == "Aborted 363549": + break + else: + raise + if found <= 0: + break + +def main(params): + cell = params["cell"] + vl_conn = cell.open_vl_server(params) + + if "uuid" not in params and "host" not in params: + return list_all(params, vl_conn) + + attributes = kafs.ListAddrByAttributes() + attributes.Mask = 0 + if "uuid" in params: + attributes.Mask |= kafs.VLADDR_UUID + attributes.uuid = params["uuid"] + if "host" in params: + attributes.Mask |= kafs.VLADDR_IPADDR + attributes.uuid = params["uuid"] + + list_one(params, vl_conn, attributes) diff --git a/suite/lib/addrcache.py b/suite/lib/addrcache.py index d1e9919..6cfb4d9 100644 --- a/suite/lib/addrcache.py +++ b/suite/lib/addrcache.py @@ -22,7 +22,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ from afs import exception -from socket import inet_pton, AF_INET, AF_INET6; +from socket import inet_pton, inet_ntoa, AF_INET, AF_INET6; import dns.reversename import dns.resolver @@ -105,6 +105,24 @@ def addr2name(addr): return name return None - except dns.resolver.NoAnswer: + except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN): return None return cache_a2n[addr] + +def int2addr(i): + inaddr = bytearray(4) + inaddr[0] = (i >> 24) & 0xff + inaddr[1] = (i >> 16) & 0xff + inaddr[2] = (i >> 8) & 0xff + inaddr[3] = (i >> 0) & 0xff + return inet_ntoa(bytes(inaddr)) + +def resolve(params, addr): + if str(type(addr)) == "": + addr = int2addr(addr) + if "noresolve" in params: + return addr + name = addr2name(addr) + if name == None: + return addr + return name diff --git a/suite/lib/uuid.py b/suite/lib/uuid.py new file mode 100644 index 0000000..35db3ba --- /dev/null +++ b/suite/lib/uuid.py @@ -0,0 +1,58 @@ +# +# AFS Volume management toolkit: UUID handling +# -*- coding: utf-8 -*- +# + +__copyright__ = """ +Copyright (C) 2014 Red Hat, Inc. All Rights Reserved. +Written by David Howells (dhowells@redhat.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public Licence version 2 as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public Licence for more details. + +You should have received a copy of the GNU General Public Licence +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +""" + +def uuid2str(uuid): + """Convert an afsUUID-class object into a UUID string""" + s = "{:08x}-{:04x}-{:04x}-{:02x}-{:02x}-".format(uuid.time_low, + uuid.time_mid, + uuid.time_hi_and_version, + uuid.clock_seq_hi_and_reserved, + uuid.clock_seq_low) + for i in range(0, 6): + s += "{:02x}".format(uuid.node[i]) + return s + +def str2uuid(s): + """Convert a UUID string into an afsUUID-class object""" + if (len(s) != 32 + 5 or + s[ 8] != "-" or + s[13] != "-" or + s[18] != "-" or + s[21] != "-" or + s[24] != "-"): + raise RuntimeError("Invalid UUID format") + + from kafs import afsUUID + uuid = afsUUID() + uuid.time_low = int(s[0:8], 16) + uuid.time_mid = int(s[9:13], 16) + uuid.time_hi_and_version = int(s[14:18], 16) + uuid.clock_seq_hi_and_reserved = int(s[19:21], 16) + uuid.clock_seq_low = int(s[22:24], 16) + uuid.node[0] = int(s[25:27], 16) + uuid.node[1] = int(s[27:29], 16) + uuid.node[2] = int(s[29:31], 16) + uuid.node[3] = int(s[31:33], 16) + uuid.node[4] = int(s[33:35], 16) + uuid.node[5] = int(s[35:37], 16) + return uuid -- 2.49.0