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



33
34
35
36
37
# File 'lib/snapsync/auto_sync.rb', line 33

def add(target)
    targets[target.partition_uuid] ||= Array.new
    targets[target.partition_uuid] << target
    partitions.monitor_for(target.partition_uuid)
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
# 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 }
        add(target)
    end
end

#run(period: 60) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/snapsync/auto_sync.rb', line 39

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