mirror of
https://github.com/hyperknot/openfreemap.git
synced 2026-05-21 14:02:15 +00:00
refactor, auto_clean
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import shutil
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from http_host_lib.assets import download_assets
|
||||
@@ -6,7 +7,7 @@ from http_host_lib.config import config
|
||||
from http_host_lib.mount import auto_mount_unmount
|
||||
from http_host_lib.nginx import write_nginx_config
|
||||
from http_host_lib.utils import assert_linux, assert_sudo
|
||||
from http_host_lib.versions import sync_version_files
|
||||
from http_host_lib.versions import fetch_version_files
|
||||
|
||||
|
||||
def full_sync(force=False):
|
||||
@@ -22,18 +23,70 @@ def full_sync(force=False):
|
||||
assert_linux()
|
||||
assert_sudo()
|
||||
|
||||
download_done = False
|
||||
download_done += download_area_version(area='monaco', version='latest')
|
||||
# start
|
||||
|
||||
if not config.host_config.get('skip_planet'):
|
||||
download_done += download_area_version(area='planet', version='latest')
|
||||
|
||||
if download_done or force:
|
||||
auto_mount_unmount()
|
||||
versions_changed = fetch_version_files()
|
||||
|
||||
download_assets()
|
||||
|
||||
versions_changed = sync_version_files()
|
||||
btrfs_downloaded = False
|
||||
|
||||
if download_done or versions_changed or force:
|
||||
# download latest and deployed monaco
|
||||
btrfs_downloaded += download_area_version(area='monaco', version='latest')
|
||||
btrfs_downloaded += download_area_version(area='monaco', version='deployed')
|
||||
|
||||
# download latest and deployed planet
|
||||
if not config.host_config.get('skip_planet'):
|
||||
btrfs_downloaded += download_area_version(area='planet', version='latest')
|
||||
btrfs_downloaded += download_area_version(area='planet', version='deployed')
|
||||
|
||||
if btrfs_downloaded or versions_changed or force:
|
||||
auto_clean_btrfs()
|
||||
auto_mount_unmount()
|
||||
write_nginx_config()
|
||||
|
||||
|
||||
def auto_clean_btrfs():
|
||||
"""
|
||||
Clean old btrfs runs
|
||||
|
||||
For each area we keep max two versions:
|
||||
1. The newest one available locally
|
||||
2. The one currently deployed, specified in /data/ofm/config/deployed_versions
|
||||
3. If there is no deployed version, then we include the second newest one
|
||||
"""
|
||||
|
||||
for area in config.areas:
|
||||
area_dir = config.runs_dir / area
|
||||
if not area_dir.is_dir():
|
||||
continue
|
||||
|
||||
local_versions = sorted([i.name for i in area_dir.iterdir()])
|
||||
|
||||
versions_to_keep = set()
|
||||
|
||||
# add newest version
|
||||
if local_versions:
|
||||
versions_to_keep.add(local_versions[-1])
|
||||
|
||||
# add deployed version
|
||||
try:
|
||||
deployed_version_file = config.deployed_versions_dir / f'{area}.txt'
|
||||
deployed_version = deployed_version_file.read_text().strip()
|
||||
if (config.runs_dir / area / deployed_version).exists():
|
||||
versions_to_keep.add(deployed_version)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# if still only one version, we include the second newest one
|
||||
if len(versions_to_keep) == 1 and len(local_versions) >= 2:
|
||||
versions_to_keep.add(local_versions[-2])
|
||||
|
||||
print(f' keeping versions for {area}: {sorted(versions_to_keep)}')
|
||||
|
||||
versions_to_remove = set(local_versions).difference(versions_to_keep)
|
||||
|
||||
for version in versions_to_remove:
|
||||
print(f' removing version for {area}: {version}')
|
||||
version_dir = config.runs_dir / area / version
|
||||
shutil.rmtree(version_dir)
|
||||
|
||||
Reference in New Issue
Block a user