def _attach_volume(self, vol, instance_id, device, timeout):
        """Attaches the volume and blocks until done (or times out)"""
        self.log.info("Attaching %s as %s" % (vol.id, device))
        vol.attach(instance_id, device)

        start_time = time.time()
        end_time = start_time + timeout if timeout else 0
        attached = False

        # block until attached
        while (not timeout) or (time.time() < end_time):
            if vol.update() != "deleting":
                if vol.attachment_state() == "attached":
                    if vol.attach_data.instance_id == instance_id:
                        self.log.info("%s Volume is attached" % self)
                        attached = True
                        break
                    else:
                        raise Fail("Volume is attached to instance %s instead of %s" % (vol.attach_data.instance_id, instance_id))
                else:
                    self.log.debug("Volume is %s" % vol.status)
            else:
                raise Fail("%s Volume %s no longer exists" % (self, vol.id))
            time.sleep(3)

        # block until device is available
        if attached:
            while (not timeout) or (time.time() < end_time):
                if os.path.exists(self.resource.device):
                    return
                time.sleep(1)

        raise Fail("Timed out waiting for volume attachment after %s seconds" % (time.time() - start_time))

    # def detach_volume(self, vol, timeout):
    #     Chef::Log.debug("Detaching #{volume_id}")
    #     vol = volume_by_id(volume_id)
    #     orig_instance_id = vol[:aws_instance_id]
    #     vol.detach()
    #  
    #   # block until detached
    #   begin
    #     Timeout::timeout(timeout) do
    #       while true
    #         vol = volume_by_id(volume_id)
    #         if vol and vol[:aws_status] != "deleting"
    #           if vol[:aws_instance_id] != orig_instance_id
    #             Chef::Log.debug("Volume detached from #{orig_instance_id}")
    #             break
    #           else
    #             Chef::Log.debug("Volume: #{vol.inspect}")
    #           end
    #         else
    #           Chef::Log.debug("Volume #{volume_id} no longer exists")
    #           break
    #         end
    #         sleep 3
    #       end
    #     end
    #   rescue Timeout::Error
    #     raise "Timed out waiting for volume detachment after #{timeout} seconds"
dataset/ETHPy150Open samuel/kokki/kokki/cookbooks/aws/libraries/providers.py/EBSVolumeProvider._attach_volume/original