Skip to content

Commit

Permalink
Added sync to abstraction
Browse files Browse the repository at this point in the history
Sync is activated by --sync flag.

Ref. Issue #32
  • Loading branch information
hirak99 committed May 8, 2024
1 parent 880882e commit 6fa8510
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
30 changes: 16 additions & 14 deletions src/code/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import argparse
import collections
import datetime
import logging
from typing import Iterable
Expand Down Expand Up @@ -78,30 +79,31 @@ def _parse_args() -> argparse.Namespace:
return args


def _btrfs_sync(mount_paths: set[str]) -> None:
for mount_path in sorted(mount_paths):
if global_flags.FLAGS.dryrun:
os_utils.eprint(f"Would sync {mount_path}")
continue
os_utils.eprint("Syncing ...", flush=True)
os_utils.execute_sh(f"btrfs subvolume sync {mount_path}")
def _sync(configs_to_sync: list[configs.Config]):
paths_to_sync: dict[snap_mechanisms.SnapType, set[str]] = collections.defaultdict(
set
)
for config in configs_to_sync:
paths_to_sync[config.snap_type].add(config.mount_path)
for snap_type, paths in sorted(paths_to_sync.items()):
snap_mechanisms.get(snap_type).sync_paths(paths)


def _delete_snap(configs_iter: Iterable[configs.Config], path_suffix: str, sync: bool):
mount_paths: set[str] = set()
to_sync: list[configs.Config] = []
for config in configs_iter:
snap = snap_operator.find_target(config, path_suffix)
if snap:
snap.delete()
if config.snap_type == snap_mechanisms.SnapType.BTRFS:
mount_paths.add(config.mount_path)
to_sync.append(config)

config.call_post_hooks()

if sync:
_btrfs_sync(mount_paths)
_sync(to_sync)

if not mount_paths:
if not to_sync:
os_utils.eprint(f"Target {path_suffix} not found in any config.")


Expand All @@ -110,7 +112,7 @@ def _config_operation(command: str, source: str, comment: str, sync: bool):
now = datetime.datetime.now()

# Which mount paths to sync.
mount_paths_to_sync: set[str] = set()
to_sync: list[configs.Config] = []

# Commands that need to access existing config.
for config in configs.iterate_configs(source=source):
Expand All @@ -130,12 +132,12 @@ def _config_operation(command: str, source: str, comment: str, sync: bool):

if snapper.snaps_deleted:
if config.snap_type == snap_mechanisms.SnapType.BTRFS:
mount_paths_to_sync.add(config.mount_path)
to_sync.append(config)
if snapper.snaps_created or snapper.snaps_deleted:
config.call_post_hooks()

if sync:
_btrfs_sync(mount_paths_to_sync)
_sync(to_sync)


def main():
Expand Down
15 changes: 13 additions & 2 deletions src/code/mechanisms/abstract_mechanism.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,16 @@ def delete(self, destination: str):

@abc.abstractmethod
def rollback_gen(self, source_dests: list[tuple[str, str]]) -> list[str]:
"""Returns shell lines which when executed will result in a rollback of snapshots."""
# It is okay to leave unimplemented, and raise NotImplementedError().
"""Returns shell lines which when executed will result in a rollback of snapshots.
If it is not possible to roll back this type of snap, or if it is unimplemented,
raise an error such as NotImplementedError().
"""

@abc.abstractmethod
def sync_paths(self, paths: set[str]):
"""Syncs with a set of mount paths that may have been modified.
Create a no-op implementation in case sync is not applicable.
This will be called if the user passes the --sync flag.
"""
8 changes: 8 additions & 0 deletions src/code/mechanisms/btrfs_mechanism.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ def rollback_gen(self, source_dests: list[tuple[str, str]]) -> list[str]:
f"Mount point may no longer be a btrfs volume: {source}"
)
return rollback_btrfs.rollback_gen(source_dests)

def sync_paths(self, paths: set[str]):
for mount_path in sorted(paths):
if global_flags.FLAGS.dryrun:
os_utils.eprint(f"Would sync {mount_path}")
continue
os_utils.eprint("Syncing ...", flush=True)
os_utils.execute_sh(f"btrfs subvolume sync {mount_path}")

0 comments on commit 6fa8510

Please sign in to comment.