def sync_template(remote_repo, template, storage_pool): """Synchronizes local template (cache) with the remote one (master)""" url = c(remote_repo, 'url') vm_type = c(remote_repo, 'type') storage_endpoint = c('general', 'storage-endpoint') localfile = os.path.join(storage_endpoint, storage_pool, vm_type, template) remotefile = os.path.join(url, template) # only download if we don't already have a fresh copy if not is_fresh(localfile, remotefile): # for resilience storage.prepare_storage_pool(storage_pool) download("%s.tar" % remotefile, "%s.tar" % localfile) for h in ['pfff']: r_template_hash = "%s.tar.%s" % (remotefile, h) l_template_hash = "%s.tar.%s" % (localfile, h) download(r_template_hash, l_template_hash) unpack_template(storage_pool, vm_type, localfile)
def sync_template(remote_repo, template, storage_pool, silent=False): """Synchronizes local template (cache) with the remote one (master)""" config = get_config() url = config.getstring(remote_repo, 'url') vm_type = config.getstring(remote_repo, 'type') localfile = os.path.join(storage.get_pool_path(storage_pool), vm_type, template) remotefile = urlparse.urljoin(url.rstrip('/') + '/', template) # only download if we don't already have a fresh copy if is_fresh(localfile, remotefile): return extension = "ova" if _url_exists("%s.ova" % remotefile) else "tar" unfinished_local = "%s.%s.unfinished" % (localfile, extension) unfinished_local_hash = "%s.%s.pfff.unfinished" % (localfile, extension) remote_url = "%s.%s" % (remotefile, extension) if not _url_exists(remote_url): raise TemplateException("Remote template was not found: %s" % remote_url) retries = 5 retry = 0 while not is_fresh(localfile, remotefile, unfinished=True) and retries > retry: # for resilience retry += 1 storage.prepare_storage_pool(storage_pool) download("%s.%s" % (remotefile, extension), unfinished_local, continue_=True, silent=silent) r_template_hash = "%s.%s.pfff" % (remotefile, extension) download(r_template_hash, unfinished_local_hash, continue_=True, silent=silent) os.rename(unfinished_local, '%s.%s' % (localfile, extension)) os.rename(unfinished_local_hash, '%s.%s.pfff' % (localfile, extension)) unpack_template(storage_pool, vm_type, localfile)