2014-09-23 20:09:33 +00:00
|
|
|
import json
|
|
|
|
import logging
|
2014-10-03 19:07:50 +00:00
|
|
|
import zlib
|
2014-09-23 20:09:33 +00:00
|
|
|
|
2014-09-23 18:01:27 +00:00
|
|
|
from data import model
|
|
|
|
from data.database import ImageStorage
|
|
|
|
from app import app, storage as store
|
2014-09-23 20:06:38 +00:00
|
|
|
from data.database import db
|
2014-10-03 19:07:50 +00:00
|
|
|
from util.gzipstream import ZLIB_GZIP_WINDOW
|
2014-09-23 18:01:27 +00:00
|
|
|
|
|
|
|
|
2014-09-23 20:06:38 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-09-26 16:21:50 +00:00
|
|
|
def backfill_sizes_from_data():
|
2014-10-03 19:07:50 +00:00
|
|
|
while True:
|
|
|
|
# Load the record from the DB.
|
2014-09-26 16:21:50 +00:00
|
|
|
try:
|
2014-10-03 19:07:50 +00:00
|
|
|
record = (ImageStorage
|
|
|
|
.select(ImageStorage.uuid)
|
|
|
|
.where(ImageStorage.uncompressed_size == None, ImageStorage.uploading == False)
|
|
|
|
.get())
|
|
|
|
except ImageStorage.DoesNotExist:
|
|
|
|
# We're done!
|
|
|
|
return
|
|
|
|
|
|
|
|
uuid = record.uuid
|
|
|
|
|
|
|
|
# Read the layer from backing storage and calculate the uncompressed size.
|
|
|
|
logger.debug('Loading data: %s (%s bytes)', uuid, with_locations.image_size)
|
|
|
|
decompressor = zlib.decompressobj(ZLIB_GZIP_WINDOW)
|
|
|
|
stream = store.read_stream(with_locations.locations, store.image_layer_path(uuid))
|
|
|
|
|
|
|
|
uncompressed_size = 0
|
|
|
|
CHUNK_SIZE = 512 * 1024 * 1024
|
|
|
|
while True:
|
|
|
|
current_data = stream.read(CHUNK_SIZE)
|
|
|
|
if len(current_data) == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
uncompressed_size += len(decompressor.decompress(current_data))
|
2014-09-26 16:21:50 +00:00
|
|
|
|
|
|
|
# Write the size to the image storage. We do so under a transaction AFTER checking to
|
|
|
|
# make sure the image storage still exists and has not changed.
|
2014-10-03 19:07:50 +00:00
|
|
|
logger.debug('Writing entry: %s. Size: %s', uuid, uncompressed_size)
|
2014-09-26 16:21:50 +00:00
|
|
|
with app.config['DB_TRANSACTION_FACTORY'](db):
|
|
|
|
try:
|
|
|
|
current_record = model.get_storage_by_uuid(uuid)
|
|
|
|
except:
|
|
|
|
# Record no longer exists.
|
|
|
|
continue
|
|
|
|
|
2014-10-03 19:07:50 +00:00
|
|
|
if not current_record.uploading and current_record.uncompressed_size == None:
|
|
|
|
current_record.uncompressed_size = uncompressed_size
|
|
|
|
#current_record.save()
|
2014-09-26 16:21:50 +00:00
|
|
|
|
|
|
|
|
2014-09-23 18:01:27 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logging.getLogger('boto').setLevel(logging.CRITICAL)
|
|
|
|
|
2014-09-26 16:21:50 +00:00
|
|
|
backfill_sizes_from_data()
|