Class: Snapsync::AutoSync
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
- #_run ⇒ Object
- #_task_name ⇒ Object
- #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, sync_local: true, sync_remote: true) ⇒ AutoSync
constructor
A new instance of AutoSync.
- #remove(**matcher) ⇒ Object
- #write_config(path) ⇒ Object
Methods inherited from Task
#error, #info, #run, #set_progress, #set_status, #warn
Constructor Details
#initialize(config_dir = SnapperConfig.default_config_dir, snapsync_config_file: DEFAULT_CONFIG_PATH, sync_local: true, sync_remote: true) ⇒ AutoSync
Returns a new instance of AutoSync.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 19 def initialize(config_dir = SnapperConfig.default_config_dir, snapsync_config_file: DEFAULT_CONFIG_PATH, sync_local: true, sync_remote: true) @config_dir = config_dir @targets = Hash.new @sync_local = sync_local @sync_remote = sync_remote @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.
12 13 14 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 12 def config_dir @config_dir end |
#partitions ⇒ Object (readonly)
Returns the value of attribute partitions.
15 16 17 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 15 def partitions @partitions end |
#targets ⇒ Hash<String,Array<AutoSyncTarget>> (readonly)
14 15 16 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 14 def targets @targets end |
Instance Method Details
#_run ⇒ Object
198 199 200 201 202 203 204 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 198 def _run each_available_autosync_target do |path, t| info "sync-all on #{path} (partition #{t.partition_uuid})" op = SyncAll.new(path, config_dir: config_dir) op.run end end |
#_task_name ⇒ Object
194 195 196 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 194 def _task_name 'Auto-Sync' end |
#add(target) ⇒ Object
179 180 181 182 183 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 179 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
109 110 111 112 113 114 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 109 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
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 152 153 154 155 156 157 158 159 160 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 124 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 info "partition #{uuid} is present, but not mounted and automount is false. Ignoring" next end 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 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
168 169 170 171 172 173 174 175 176 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 168 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
185 186 187 188 189 190 191 192 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 185 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 |
#write_config(path) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/snapsync/tasks/AutoSync.rb', line 87 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 |