Ejemplo n.º 1
0
def volume_attach_or_detach_task(instance, volume, action, **kwargs):

    if not instance:
        return False
    if not volume:
        return False
    rc = create_rc_by_instance(instance)
    LOG.info("volume attach or detach operation,instance: [%s][%s] - volume: [%s][%s] - action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))

    '''
    action ==‘attach’ attach volume ,update local volume status ,create VolumeAttachment;
    action =='detach' detach volume ,update local volume status ,delete VolumeAttachment
    '''
    if 'attach' == action:
        nova.instance_volume_attach(rc, volume_id=volume.volume_id, instance_id=instance.uuid, device='')
        count = 0
        while True:
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            cinder = volume_task.volume_get(volume)

            if cinder and cinder.status.upper() == u'ERROR':
                volume.status = VOLUME_STATE_ERROR
                volume.save()
                break
            if cinder and cinder.status.upper() == u'IN-USE':
                volume.status = VOLUME_STATE_IN_USE
                volume.instance = instance
                volume.save()
                break
            count += 1
            if count > settings.MAX_COUNT_SYNC:
                LOG.info("volume attach or detach,instance: [%s][%s] - volume: [%s][%s] - timeout for action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))
                break

    elif 'detach' == action:
        nova.instance_volume_detach(rc, instance_id=instance.uuid, att_id=volume.volume_id)

        count = 0
        while True:
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            cinder = volume_task.volume_get(volume)

            if cinder and cinder.status.upper() == u'ERROR':
                volume.status = VOLUME_STATE_ERROR
                volume.save()
                break
            if cinder and cinder.status.upper() == u'AVAILABLE':
                volume.status = VOLUME_STATE_AVAILABLE
                volume.instance = None
                volume.save()
                break
            count += 1
            if count > settings.MAX_COUNT_SYNC:
                LOG.info("volume attach or detach,instance: [%s][%s] - volume: [%s][%s] - timeout for action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))
                break

    return True
Ejemplo n.º 2
0
def detach_volume_from_instance(volume):
    assert volume is not None
    assert volume.instance is not None

    instance = volume.instance
    rc = create_rc_by_instance(instance)
    LOG.info("Volume detach start, [%s][%s].",
             instance, volume)
    
    begin = datetime.datetime.now()
    try:
        nova.instance_volume_detach(rc, instance.uuid, volume.volume_id)
    except Exception:
        volume.change_status(VOLUME_STATE_ERROR)
        end = datetime.datetime.now()
        LOG.exception("Volume detach api call failed, [%s][%s], "
                      "apply [%s] seconds.",
                      instance, volume, (end-begin).seconds)
        return False
    else:
        end = datetime.datetime.now()
        LOG.info("Volume detach api call succeed, [%s][%s], "
                      "apply [%s] seconds.",
                      instance, volume, (end-begin).seconds)

    begin = datetime.datetime.now()
    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        vol = volume_task.volume_get(volume)
        status = vol.status.upper() if vol else 'None'

        end = datetime.datetime.now()
        
        if status == u'ERROR':
            LOG.info("Volume detach failed, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count,instance, volume, status, (end-begin).seconds)
            volume.change_status(VOLUME_STATE_ERROR)
            break
        elif status == u'AVAILABLE':
            LOG.info("Volume detach succeed, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count,instance, volume, status, (end-begin).seconds)
            volume.instance = None
            volume.change_status(VOLUME_STATE_AVAILABLE)
            break
        elif status == "None":
            LOG.info("Volume detach volume is None, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count,instance, volume, status, (end-begin).seconds)
            volume.instance = None
            volume.delete = True
            volume.save()
            break
        else:
            LOG.info("Volume detach synchronize status, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count,instance, volume, status, (end-begin).seconds)

    else:
        end = datetime.datetime.now()
        LOG.info("Volume detach timeout, "
                    "[%s][%s], apply [%s] seconds.",
                    instance, volume, (end-begin).seconds)
        return False

    end = datetime.datetime.now()
    if volume.status == VOLUME_STATE_ERROR:
        LOG.error("Volume detach failed, "
                    "[%s][%s], apply [%s] seconds.",
                    instance, volume, (end-begin).seconds)
        return False
    else:
        return True
Ejemplo n.º 3
0
def attach_volume_to_instance(volume, instance):
    rc = create_rc_by_instance(instance)
    LOG.info("Volume attach start, [%s][%s].",
                    instance, volume)

    begin = datetime.datetime.now()
    try:
        nova.instance_volume_attach(rc, volume_id=volume.volume_id,
                                    instance_id=instance.uuid, device='')
    except Exception:
        volume.change_status(VOLUME_STATE_ERROR)
        end = datetime.datetime.now()
        LOG.exception("Volume attach api call failed, [%s][%s], "
                      "apply [%s] seconds.",
                      instance, volume, (end-begin).seconds)
        return False
    else:
        end = datetime.datetime.now()
        LOG.info("Volume attach api call succeed, [%s][%s], "
                      "apply [%s] seconds.",
                      instance, volume, (end-begin).seconds)

    begin = datetime.datetime.now()
    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        vol = volume_task.volume_get(volume)
        status = vol.status.upper() if vol else "None"
        
        end = datetime.datetime.now()
        if status == u'ERROR':
            volume.change_status(VOLUME_STATE_ERROR)
            LOG.info("Volume attach failed, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count, instance, volume, status, (end-begin).seconds)
            break
        elif status == u'ERROR_ATTACHING':
            volume.instance = instance
            volume.change_status(VOLUME_STATE_ERROR)
            LOG.info("Volume attach failed, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count, instance, volume, status, (end-begin).seconds)
            break
        elif status == u'IN-USE':
            volume.instance = instance
            volume.change_status(VOLUME_STATE_IN_USE)
            LOG.info("Volume attach succeed, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count, instance, volume, status, (end-begin).seconds)
            break
        elif status == "None":
            LOG.info("Volume attach failed, volume is None, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count, instance, volume, status, (end-begin).seconds)
            volume.instance = None
            volume.deleted = True
            volume.save()
            break 
        else:
             LOG.info("Volume attach synchronize status, "
                "[Count:%s][%s][%s][status: %s].",
                 count, instance, volume, status)
    else:
        end = datetime.datetime.now()
        LOG.info("Volume attach timeout, [%s][%s], apply [%s] seconds.",
                 instance, volume, (end-begin).seconds)
        return False

    end = datetime.datetime.now()
    if volume.status == VOLUME_STATE_ERROR:
        LOG.error("Volume attach failed, [%s][%s], apply [%s] seconds.",
                  instance, volume, (end-begin).seconds)
        return False
    else: 
        return True
Ejemplo n.º 4
0
def detach_volume_from_instance(volume):
    assert volume is not None
    assert volume.instance is not None

    instance = volume.instance
    rc = create_rc_by_instance(instance)
    LOG.info("Volume detach start, [%s][%s].", instance, volume)

    begin = datetime.datetime.now()
    try:
        nova.instance_volume_detach(rc, instance.uuid, volume.volume_id)
    except Exception:
        volume.change_status(VOLUME_STATE_ERROR)
        end = datetime.datetime.now()
        LOG.exception(
            "Volume detach api call failed, [%s][%s], "
            "apply [%s] seconds.", instance, volume, (end - begin).seconds)
        return False
    else:
        end = datetime.datetime.now()
        LOG.info(
            "Volume detach api call succeed, [%s][%s], "
            "apply [%s] seconds.", instance, volume, (end - begin).seconds)

    begin = datetime.datetime.now()
    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        vol = volume_task.volume_get(volume)
        status = vol.status.upper() if vol else 'None'

        end = datetime.datetime.now()

        if status == u'ERROR':
            LOG.info(
                "Volume detach failed, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            volume.change_status(VOLUME_STATE_ERROR)
            break
        elif status == u'AVAILABLE':
            LOG.info(
                "Volume detach succeed, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            volume.instance = None
            volume.change_status(VOLUME_STATE_AVAILABLE)
            break
        elif status == "None":
            LOG.info(
                "Volume detach volume is None, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            volume.instance = None
            volume.delete = True
            volume.save()
            break
        else:
            LOG.info(
                "Volume detach synchronize status, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)

    else:
        end = datetime.datetime.now()
        LOG.info("Volume detach timeout, "
                 "[%s][%s], apply [%s] seconds.", instance, volume,
                 (end - begin).seconds)
        return False

    end = datetime.datetime.now()
    if volume.status == VOLUME_STATE_ERROR:
        LOG.error("Volume detach failed, "
                  "[%s][%s], apply [%s] seconds.", instance, volume,
                  (end - begin).seconds)
        return False
    else:
        return True
Ejemplo n.º 5
0
def attach_volume_to_instance(volume, instance):
    rc = create_rc_by_instance(instance)
    LOG.info("Volume attach start, [%s][%s].", instance, volume)

    begin = datetime.datetime.now()
    try:
        nova.instance_volume_attach(rc,
                                    volume_id=volume.volume_id,
                                    instance_id=instance.uuid,
                                    device='')
    except Exception:
        volume.change_status(VOLUME_STATE_ERROR)
        end = datetime.datetime.now()
        LOG.exception(
            "Volume attach api call failed, [%s][%s], "
            "apply [%s] seconds.", instance, volume, (end - begin).seconds)
        return False
    else:
        end = datetime.datetime.now()
        LOG.info(
            "Volume attach api call succeed, [%s][%s], "
            "apply [%s] seconds.", instance, volume, (end - begin).seconds)

    begin = datetime.datetime.now()
    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        vol = volume_task.volume_get(volume)
        status = vol.status.upper() if vol else "None"

        end = datetime.datetime.now()
        if status == u'ERROR':
            volume.change_status(VOLUME_STATE_ERROR)
            LOG.info(
                "Volume attach failed, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            break
        elif status == u'ERROR_ATTACHING':
            volume.instance = instance
            volume.change_status(VOLUME_STATE_ERROR)
            LOG.info(
                "Volume attach failed, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            break
        elif status == u'IN-USE':
            volume.instance = instance
            volume.change_status(VOLUME_STATE_IN_USE)
            LOG.info(
                "Volume attach succeed, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            break
        elif status == "None":
            LOG.info(
                "Volume attach failed, volume is None, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            volume.instance = None
            volume.deleted = True
            volume.save()
            break
        else:
            LOG.info(
                "Volume attach synchronize status, "
                "[Count:%s][%s][%s][status: %s].", count, instance, volume,
                status)
    else:
        end = datetime.datetime.now()
        LOG.info("Volume attach timeout, [%s][%s], apply [%s] seconds.",
                 instance, volume, (end - begin).seconds)
        return False

    end = datetime.datetime.now()
    if volume.status == VOLUME_STATE_ERROR:
        LOG.error("Volume attach failed, [%s][%s], apply [%s] seconds.",
                  instance, volume, (end - begin).seconds)
        return False
    else:
        return True
Ejemplo n.º 6
0
def volume_attach_or_detach_task(instance, volume, action, **kwargs):

    if not instance:
        return False
    if not volume:
        return False
    rc = create_rc_by_instance(instance)
    LOG.info("volume attach or detach operation,instance: [%s][%s] - volume: [%s][%s] - action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))

    '''
    action ==‘attach’ attach volume ,update local volume status ,create VolumeAttachment;
    action =='detach' detach volume ,update local volume status ,delete VolumeAttachment
    '''
    if 'attach' == action:
        nova.instance_volume_attach(rc, volume_id=volume.volume_id, instance_id=instance.uuid, device='')
        count = 0
        while True:
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            cinder = volume_task.volume_get(volume)
            if cinder and cinder.status.upper() == u'ERROR':
                volume.status = VOLUME_STATE_ERROR
                volume.save()
                break
            elif cinder and cinder.status.upper() == u'ERROR_ATTACHING':
                volume.status = VOLUME_STATE_ERROR
                volume.instance = instance
                volume.save()
                break
            elif cinder and cinder.status.upper() == u'IN-USE':
                volume.status = VOLUME_STATE_IN_USE
                volume.instance = instance
                volume.save()
                break
            count += 1
            if count > settings.MAX_COUNT_SYNC:
                LOG.info("volume attach or detach,instance: [%s][%s] - volume: [%s][%s] - timeout for action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))
                break

    elif 'detach' == action:
        nova.instance_volume_detach(rc, instance_id=instance.uuid, att_id=volume.volume_id)

        count = 0
        while True:
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            cinder = volume_task.volume_get(volume)

            if cinder and cinder.status.upper() == u'ERROR':
                volume.status = VOLUME_STATE_ERROR
                volume.save()
                break
            if cinder and cinder.status.upper() == u'AVAILABLE':
                volume.status = VOLUME_STATE_AVAILABLE
                volume.instance = None
                volume.save()
                break
            count += 1
            if count > settings.MAX_COUNT_SYNC:
                LOG.info("volume attach or detach,instance: [%s][%s] - volume: [%s][%s] - timeout for action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))
                break

    return True