def is_tape_drive_online(drive): """ Checks if the given tape drive is online Args: drive: Which drive to check Returns: True if the drive is online, false otherwise """ cmd = 'mt -f %s status' % drive p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True) logger.debug('Checking if {drive} is online: {cmd}'.format(drive=drive, cmd=cmd)) out, err = p.communicate() if p.returncode: logger.error( 'Failed to check if {drive} is online, err: {err}, returncode: {rcode}' .format(drive=drive, err=err, rcode=p.returncode)) raise RobotException('%s, return code: %s' % (err, p.returncode)) online = 'ONLINE' in out if online: logger.debug('{drive} is online, out: {out}'.format(drive=drive, out=out)) else: logger.debug('{drive} is not online, out: {out}'.format(drive=drive, out=out)) return online
def is_tape_drive_online(drive): """ Checks if the given tape drive is online Args: drive: Which drive to check Returns: True if the drive is online, false otherwise """ cmd = 'mt -f %s status' % drive p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) out, err = p.communicate() if p.returncode: raise RobotException('%s, return code: %s' % (err, p.returncode)) return 'ONLINE' in out
def robot_inventory(robot): """ Updates the slots and drives in the robot Args: robot: Which robot to get the data from Returns: None """ from ESSArch_Core.storage.models import ( Robot, StorageMedium, TapeDrive, TapeSlot, ) backend_name = settings.ESSARCH_TAPE_IDENTIFICATION_BACKEND backend = get_tape_identification_backend(backend_name) cmd = 'mtx -f %s status' % robot p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True) logger.debug('Inventoring {robot}: {cmd}'.format(robot=robot, cmd=cmd)) out, err = p.communicate() if p.returncode: logger.error( 'Failed to get inventory of {robot}, err: {err}, return code: {code}' .format(robot=robot, err=err, code=p.returncode)) raise RobotException('%s, return code: %s' % (err, p.returncode)) robot = Robot.objects.get(device=robot) re_word = re.compile(r'\W+') for row in out.split( '\n')[::-1]: # Reverse to get (and create) slots first if re.match('Data Transfer Element', row): # Find robot drives logger.debug('Found drive: {row}'.format(row=row)) dt_el = re_word.split(row) drive_id = dt_el[3] status = dt_el[4] try: drive = TapeDrive.objects.get(drive_id=drive_id, robot=robot) logger.debug( 'Drive {row} (drive_id={drive}, robot={robot}) found in database' .format(row=row, drive=drive_id, robot=robot)) if status == 'Full': slot_id = dt_el[7] medium_id = dt_el[10][:6] backend.identify_tape(medium_id) StorageMedium.objects.filter( tape_slot__robot=robot, tape_slot__slot_id=slot_id, medium_id=medium_id).update(tape_drive=drive) else: StorageMedium.objects.filter(tape_drive=drive).update( tape_drive=None) except TapeDrive.DoesNotExist: logger.warning( 'Drive {row} (drive_id={drive}, robot={robot}) not found in database' .format(row=row, drive=drive_id, robot=robot)) if re.match(r'\ *Storage Element', row): # Find robot slots if not re.search('EXPORT', row): logger.debug('Found slot: {row}'.format(row=row)) s_el = re_word.split(row) slot_id = s_el[3] status = s_el[4] if status == 'Full': medium_id = s_el[6][:6] backend.identify_tape(medium_id) slot, created = TapeSlot.objects.update_or_create( robot=robot, slot_id=slot_id, defaults={'medium_id': medium_id}) if created: logger.debug( 'Created tape slot with slot_id={slot}, medium_id={medium}' .format(slot=slot_id, medium=medium_id)) else: logger.debug( 'Updated tape slot with slot_id={slot}, medium_id={medium}' .format(slot=slot_id, medium=medium_id)) StorageMedium.objects.filter(medium_id=medium_id).update( tape_slot=slot) else: slot, created = TapeSlot.objects.get_or_create( robot=robot, slot_id=slot_id) if created: logger.debug( 'Created tape slot with slot_id={slot}'.format( slot=slot_id))