Added compute_resource_sync_sites to sync_server_module

This method will be used in integrate_new to logically separate Site Sync parts.
This commit is contained in:
Petr Kalis 2022-03-30 12:35:27 +02:00
parent 572e640303
commit ee885051d9

View file

@ -157,7 +157,6 @@ class SyncServerModule(OpenPypeModule, ITrayModule):
representation_id,
site_name=site_name, force=force)
# public facing API
def remove_site(self, collection, representation_id, site_name,
remove_local_files=False):
"""
@ -184,6 +183,112 @@ class SyncServerModule(OpenPypeModule, ITrayModule):
if remove_local_files:
self._remove_local_file(collection, representation_id, site_name)
def compute_resource_sync_sites(self, project_name):
"""Get available resource sync sites state for publish process.
Returns dict with prepared state of sync sites for 'project_name'.
It checks if Site Sync is enabled, handles alternative sites.
Publish process stores this dictionary as a part of representation
document in DB.
Example:
[
{
'name': '42abbc09-d62a-44a4-815c-a12cd679d2d7',
'created_dt': datetime.datetime(2022, 3, 30, 12, 16, 9, 778637)
},
{'name': 'studio'},
{'name': 'SFTP'}
] -- representation is published locally, artist or Settings have set
remote site as 'studio'. 'SFTP' is alternate site to 'studio'. Eg.
whenever file is on 'studio', it is also on 'SFTP'.
"""
def create_metadata(name, created=True):
"""Create sync site metadata for site with `name`"""
metadata = {"name": name}
if created:
metadata["created_dt"] = datetime.now()
return metadata
if (
not self.sync_system_settings["enabled"] or
not self.sync_project_settings[project_name]["enabled"]):
return [create_metadata(self.DEFAULT_SITE)]
local_site = self.get_active_site(project_name)
remote_site = self.get_remote_site(project_name)
# Attached sites metadata by site name
# That is the local site, remote site, the always accesible sites
# and their alternate sites (alias of sites with different protocol)
attached_sites = dict()
attached_sites[local_site] = create_metadata(local_site)
if remote_site and remote_site not in attached_sites:
attached_sites[remote_site] = create_metadata(remote_site,
created=False)
# add skeleton for sites where it should be always synced to
# usually it would be a backup site which is handled by separate
# background process
for site in self._get_always_accessible_sites(project_name):
if site not in attached_sites:
attached_sites[site] = create_metadata(site, created=False)
attached_sites = self._add_alternative_sites(attached_sites)
return list(attached_sites.values())
def _get_always_accessible_sites(self, project_name):
"""Sites that synced to as a part of background process.
Artist machine doesn't handle those, explicit Tray with that site name
as a local id must be running.
Example is dropbox site serving as a backup solution
"""
always_accessible_sites = (
self.get_sync_project_setting(project_name)["config"].
get("always_accessible_on", [])
)
return [site.strip() for site in always_accessible_sites]
def _add_alternative_sites(self, attached_sites):
"""Add skeleton document for alternative sites
Each new configured site in System Setting could serve as a alternative
site, it's a kind of alias. It means that files on 'a site' are
physically accessible also on 'a alternative' site.
Example is sftp site serving studio files via sftp protocol, physically
file is only in studio, sftp server has this location mounted.
"""
additional_sites = self.sync_system_settings.get("sites", {})
for site_name, site_info in additional_sites.items():
# Get alternate sites (stripped names) for this site name
alt_sites = site_info.get("alternative_sites", [])
alt_sites = [site.strip() for site in alt_sites]
alt_sites = set(alt_sites)
# If no alternative sites we don't need to add
if not alt_sites:
continue
# Take a copy of data of the first alternate site that is already
# defined as an attached site to match the same state.
match_meta = next((attached_sites[site] for site in alt_sites
if site in attached_sites), None)
if not match_meta:
continue
alt_site_meta = copy.deepcopy(match_meta)
alt_site_meta["name"] = site_name
# Note: We change mutable `attached_site` dict in-place
attached_sites[site_name] = alt_site_meta
return attached_sites
def clear_project(self, collection, site_name):
"""
Clear 'collection' of 'site_name' and its local files