Sha256: 88d6c2b0ab4b665882c5d87ea3dba91bc810fd0478b95600d07a3d13ca6c56e4

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

module Snapsync
    class Cleanup
        # The underlying timeline policy object that we use to compute which
        # snapshots to delete and which to keep
        attr_reader :policy

        def initialize(policy)
            @policy = policy
        end

        def cleanup(target, dry_run: false)
            snapshots = target.each_snapshot.to_a
            filtered_snapshots = policy.filter_snapshots(snapshots).to_set

            if filtered_snapshots.any? { |s| s.synchronization_point? }
                raise InvalidPolicy, "#{policy} returned a snapsync synchronization point in its results"
            end

            if filtered_snapshots.empty?
                raise InvalidPolicy, "#{policy} returned no snapshots"
            end

            last_sync_point = snapshots.
                sort_by(&:num).reverse.
                find { |s| s.synchronization_point_for?(target) }
            filtered_snapshots << last_sync_point
            filtered_snapshots = filtered_snapshots.to_set

            deleted_snapshots = snapshots.sort_by(&:num).find_all do |s|
                if !filtered_snapshots.include?(s)
                    target.delete(s, dry_run: dry_run)
                    true
                end
            end

            if !deleted_snapshots.empty?
                Snapsync.info "Waiting for subvolumes to be deleted"
                deleted_snapshots.each do |s|
                    IO.popen(["btrfs", "subvolume", "sync", s.subvolume_dir.to_s, err: '/dev/null']).read
                end
            end
        end
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
snapsync-0.2.0 lib/snapsync/cleanup.rb
snapsync-0.1.8 lib/snapsync/cleanup.rb
snapsync-0.1.7 lib/snapsync/cleanup.rb