Class: Snapsync::Cleanup

Inherits:
Object
  • Object
show all
Defined in:
lib/snapsync/cleanup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy) ⇒ Cleanup

Returns a new instance of Cleanup.



7
8
9
# File 'lib/snapsync/cleanup.rb', line 7

def initialize(policy)
    @policy = policy
end

Instance Attribute Details

#policyObject (readonly)

The underlying timeline policy object that we use to compute which snapshots to delete and which to keep



5
6
7
# File 'lib/snapsync/cleanup.rb', line 5

def policy
  @policy
end

Instance Method Details

#cleanup(target, dry_run: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/snapsync/cleanup.rb', line 11

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|
            begin
                Btrfs.popen("subvolume", "sync", s.subvolume_dir.to_s)
            rescue Btrfs::Error
            end
        end
    end
end