Class: Snapsync::AutoSync

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

Overview

Implementation of the auto-sync feature

This class implements the ‘snapsync auto’ functionality. It monitors for partition availability, and will run sync-all on each (declared) targets when they are available, optionally auto-mounting them

Defined Under Namespace

Classes: AutoSyncTarget

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_dir = SnapperConfig.default_config_dir) ⇒ AutoSync

Returns a new instance of AutoSync.



14
15
16
17
18
# File 'lib/snapsync/auto_sync.rb', line 14

def initialize(config_dir = SnapperConfig.default_config_dir)
    @config_dir = config_dir
    @targets = Hash.new
    @partitions = PartitionsMonitor.new
end

Instance Attribute Details

#config_dirObject (readonly)

Returns the value of attribute config_dir.



10
11
12
# File 'lib/snapsync/auto_sync.rb', line 10

def config_dir
  @config_dir
end

#partitionsObject (readonly)

Returns the value of attribute partitions.



12
13
14
# File 'lib/snapsync/auto_sync.rb', line 12

def partitions
  @partitions
end

#targetsObject (readonly)

Returns the value of attribute targets.



11
12
13
# File 'lib/snapsync/auto_sync.rb', line 11

def targets
  @targets
end

Instance Method Details

#add(target) ⇒ Object



53
54
55
56
57
# File 'lib/snapsync/auto_sync.rb', line 53

def add(target)
    targets[target.partition_uuid] ||= Array.new
    targets[target.partition_uuid] << target
    partitions.monitor_for(target.partition_uuid)
end

#each_targetObject



46
47
48
49
50
51
# File 'lib/snapsync/auto_sync.rb', line 46

def each_target
    return enum_for(__method__) if !block_given?
    targets.each_value do |targets|
        targets.each { |t| yield(t) }
    end
end

#load_config(path) ⇒ Object



20
21
22
23
# File 'lib/snapsync/auto_sync.rb', line 20

def load_config(path)
    conf = YAML.load(path.read) || Array.new
    parse_config(conf)
end

#parse_config(conf) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/snapsync/auto_sync.rb', line 25

def parse_config(conf)
    conf.each do |hash|
        target = AutoSyncTarget.new
        hash.each { |k, v| target[k] = v }
        target.path = Pathname.new(target.path)
        add(target)
    end
end

#remove(**matcher) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/snapsync/auto_sync.rb', line 59

def remove(**matcher)
    targets.delete_if do |uuid, list|
        list.delete_if do |t|
            matcher.all? { |k, v| t[k] == v }
        end
        list.empty?
    end
end

#run(period: 60) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/snapsync/auto_sync.rb', line 68

def run(period: 60)
    while true
        partitions.poll
        partitions.known_partitions.each do |uuid, fs|
            mp = fs['MountPoints'].first
            targets[uuid].each do |t|
                if !mp
                    if t.automount
                        Snapsync.info "partition #{t.partition_uuid} is present, but not mounted, automounting"
                        mp = fs.Mount([]).first
                        mp = Pathname.new(mp)
                        mounted = true
                    else
                        Snapsync.info "partition #{t.partition_uuid} is present, but not mounted and automount is false. Ignoring"
                        next
                    end
                else
                    mp = Pathname.new(mp[0..-2].pack("U*"))
                end

                full_path = mp + t.path
                Snapsync.info "sync-all on #{mp + t.path} (partition #{t.partition_uuid})"
                op = SyncAll.new(mp + t.path, config_dir: config_dir)
                op.run
                if mounted
                    fs.Unmount([])
                end
            end
        end
        Snapsync.info "done all declared autosync partitions, sleeping #{period}s"
        sleep period
    end
end

#write_config(path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/snapsync/auto_sync.rb', line 34

def write_config(path)
    data = each_target.map do |target|
        Hash['partition_uuid' => target.partition_uuid,
             'path' => target.path.to_s,
             'automount' => !!target.automount,
             'name' => target.name]
    end
    File.open(path, 'w') do |io|
        YAML.dump(data, io)
    end
end