def db_lookup_disk(conn, device): """Looks up a disk from the disks database. :param device: The device to search """ print("{}: LookupDisk: disks query for: {}".format(dt.isoformat(dt.now()), device), file=sys.stderr) try: result = r.db('wanwipe').table('disks').get_all(get_disk_serial(device), index='serial_no').run(conn) for document in result: # Look over the returned documents. There should only be one, serial_no is unique. print("{}: LookupDisk: disks query found a matching document: {}".format(dt.isoformat(dt.now()), document), file=sys.stderr) return document.get('id') # Always return the current disk, skipping the below. print("{}: LookupDisk: couldn't find that disk. Creating new disk.".format(dt.isoformat(dt.now())), file=sys.stderr) # We didn't return above, so... return db_register_disk(conn, device) # Register the disk with the disks database. except RqlRuntimeError as kaboom: print("{}: LookupDisk: disks lookup failed somehow: {}".format(dt.isoformat(dt.now()), kaboom), file=sys.stderr)
def db_register_disk(conn, device): """Permanently stores a disk to the disks database. :param device: The device to add """ # First we must populate basic information for the disk. try: inserted = r.db('wanwipe').table('disks').insert( { 'serial_no': get_disk_serial(device), 'device_name': device, 'device_node': "/dev/{}".format(device), 'host_ip': get_global_ip(), 'boot_id': get_boot_id(), 'machine_id': get_dbus_machine_id() } ).run(conn) print("{}: RegisterDisk: disk created: {}".format(dt.isoformat(dt.now()), inserted['generated_keys'][0]), file=sys.stderr) return inserted['generated_keys'][0] except RqlRuntimeError as kaboom: print("{}: RegisterDisk: disk creation failed somehow: {}".format(dt.isoformat(dt.now()), kaboom), file=sys.stderr)