Class: Snapsync::AutoSync
- Inherits:
-
Object
- Object
- Snapsync::AutoSync
- 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
Constant Summary collapse
- DEFAULT_CONFIG_PATH =
Pathname.new('/etc/snapsync.conf')
Instance Attribute Summary collapse
-
#config_dir ⇒ Object
readonly
Returns the value of attribute config_dir.
-
#partitions ⇒ Object
readonly
Returns the value of attribute partitions.
- #targets ⇒ Hash<String,Array<AutoSyncTarget>> readonly
Instance Method Summary collapse
- #add(target) ⇒ Object
-
#each_autosync_target {|target| ... } ⇒ void
Enumerates the declared autosync targets.
-
#each_available_autosync_target {|path, target| ... } ⇒ void
Enumerates the available autosync targets.
-
#each_available_target {|target| ... } ⇒ void
Enumerates the available synchronization targets.
-
#initialize(config_dir = SnapperConfig.default_config_dir, snapsync_config_file: DEFAULT_CONFIG_PATH) ⇒ AutoSync
constructor
A new instance of AutoSync.
- #remove(**matcher) ⇒ Object
- #run(period: 600) ⇒ Object
- #sync ⇒ Object
- #write_config(path) ⇒ Object
Constructor Details
#initialize(config_dir = SnapperConfig.default_config_dir, snapsync_config_file: DEFAULT_CONFIG_PATH) ⇒ AutoSync
Returns a new instance of AutoSync.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/snapsync/auto_sync.rb', line 17 def initialize(config_dir = SnapperConfig.default_config_dir, snapsync_config_file: DEFAULT_CONFIG_PATH) @config_dir = config_dir @targets = Hash.new @partitions = PartitionsMonitor.new partitions.poll if snapsync_config_file.exist? load_config snapsync_config_file end end |
Instance Attribute Details
#config_dir ⇒ Object (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 |
#partitions ⇒ Object (readonly)
Returns the value of attribute partitions.
13 14 15 |
# File 'lib/snapsync/auto_sync.rb', line 13 def partitions @partitions end |
#targets ⇒ Hash<String,Array<AutoSyncTarget>> (readonly)
12 13 14 |
# File 'lib/snapsync/auto_sync.rb', line 12 def targets @targets end |
Instance Method Details
#add(target) ⇒ Object
170 171 172 173 174 |
# File 'lib/snapsync/auto_sync.rb', line 170 def add(target) targets[target.partition_uuid] ||= Array.new targets[target.partition_uuid] << target partitions.monitor_for(target.partition_uuid) end |
#each_autosync_target {|target| ... } ⇒ void
This method returns an undefined value.
Enumerates the declared autosync targets
100 101 102 103 104 105 |
# File 'lib/snapsync/auto_sync.rb', line 100 def each_autosync_target return enum_for(__method__) if !block_given? targets.each_value do |targets| targets.each { |t| yield(t) } end end |
#each_available_autosync_target {|path, target| ... } ⇒ void
This method returns an undefined value.
Enumerates the available autosync targets
It may mount partitions as needed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/snapsync/auto_sync.rb', line 115 def each_available_autosync_target return enum_for(__method__) if !block_given? each_autosync_target do |target| # @type [RemotePathname] begin mounted = false mountpoint = target.mountpoint if not mountpoint.mountpoint? if not target.automount Snapsync.info "partition #{uuid} is present, but not mounted and automount is false. Ignoring" next end Snapsync.info "partition #{uuid} is present, but not mounted, automounting" begin if mountpoint.is_a? RemotePathname # TODO: automounting of remote paths raise 'TODO' else fs.Mount([]).first end mounted = true rescue Exception => e Snapsync.warn "failed to mount, ignoring this target" next end end yield(target.mountpoint + target.relative, target) ensure if mounted fs.Unmount([]) end end end end |
#each_available_target {|target| ... } ⇒ void
This method returns an undefined value.
Enumerates the available synchronization targets
It may mount partitions as needed
159 160 161 162 163 164 165 166 167 |
# File 'lib/snapsync/auto_sync.rb', line 159 def each_available_target return enum_for(__method__) if !block_given? each_available_autosync_target do |path, t| op = SyncAll.new(path, config_dir: config_dir) op.each_target do |config, target| yield(config, target) end end end |
#remove(**matcher) ⇒ Object
176 177 178 179 180 181 182 183 |
# File 'lib/snapsync/auto_sync.rb', line 176 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: 600) ⇒ Object
193 194 195 196 197 198 199 |
# File 'lib/snapsync/auto_sync.rb', line 193 def run(period: 600) while true sync Snapsync.info "done all declared autosync partitions, sleeping #{period}s" sleep period end end |
#sync ⇒ Object
185 186 187 188 189 190 191 |
# File 'lib/snapsync/auto_sync.rb', line 185 def sync each_available_autosync_target do |path, t| Snapsync.info "sync-all on #{path} (partition #{t.partition_uuid})" op = SyncAll.new(path, config_dir: config_dir) op.run end end |
#write_config(path) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/snapsync/auto_sync.rb', line 78 def write_config(path) data = { 'version' => 2, 'targets' => each_autosync_target.map do |target| target.to_h do |k,v| if v.is_a? Snapsync::RemotePathname or v.is_a? Pathname [k.to_s,v.to_s] else [k.to_s,v] end end end } File.open(path, 'w') do |io| YAML.dump(data, io) end end |