snapsync 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38bab1614373f2385245f2e093bc560c3c7e1dd0
4
- data.tar.gz: 3343524d89264155f5ae303e85aa9a92a47fc594
3
+ metadata.gz: a5da01c8d37c1e84fd79ba60d9264727e5a2823b
4
+ data.tar.gz: 7f6aa1843176471539fb080f2f151ab3d6d33375
5
5
  SHA512:
6
- metadata.gz: c13a2ac4ed65de64035f3a4e212aed9ac4ef6569c9503893f892bdcc59385f39c972319b7372d56d0c9fd89e44cb3b53a3aaa98a0b85b26b1f96ce333ecb6fde
7
- data.tar.gz: 85751adfae9447bfa4b33dd8413097eeb966e050f7f2ab59794417113a80d349e6045d7a1bda5386d7811b8f76502e32d07693c577afa5dce41229c40cf69af4
6
+ metadata.gz: c934f77289b0ba709ee164c5bc21cb67cf5afd1cc6f7a4c7463fbcd5a3938aab1c30b5740ae7921d010ba8c8eb4d31b3b158f93a3b63241578ca410971fde90f
7
+ data.tar.gz: 44060ce27c293a7ac4ceda651beeaec24a53ee7411438377d4907d489bd53cd139b4bdaf6792174e9d76e09e8cd9bb133e67625914167b1111d6b734d6b94ea1
@@ -5,7 +5,7 @@ module Snapsync
5
5
  # partition availability, and will run sync-all on each (declared) targets
6
6
  # when they are available, optionally auto-mounting them
7
7
  class AutoSync
8
- AutoSyncTarget = Struct.new :partition_uuid, :path, :automount
8
+ AutoSyncTarget = Struct.new :partition_uuid, :path, :automount, :name
9
9
 
10
10
  attr_reader :config_dir
11
11
  attr_reader :targets
@@ -26,16 +26,45 @@ module Snapsync
26
26
  conf.each do |hash|
27
27
  target = AutoSyncTarget.new
28
28
  hash.each { |k, v| target[k] = v }
29
+ target.path = Pathname.new(target.path)
29
30
  add(target)
30
31
  end
31
32
  end
32
33
 
34
+ def write_config(path)
35
+ data = each_target.map do |target|
36
+ Hash['partition_uuid' => target.partition_uuid,
37
+ 'path' => target.path.to_s,
38
+ 'automount' => !!target.automount,
39
+ 'name' => target.name]
40
+ end
41
+ File.open(path, 'w') do |io|
42
+ YAML.dump(data, io)
43
+ end
44
+ end
45
+
46
+ def each_target
47
+ return enum_for(__method__) if !block_given?
48
+ targets.each_value do |targets|
49
+ targets.each { |t| yield(t) }
50
+ end
51
+ end
52
+
33
53
  def add(target)
34
54
  targets[target.partition_uuid] ||= Array.new
35
55
  targets[target.partition_uuid] << target
36
56
  partitions.monitor_for(target.partition_uuid)
37
57
  end
38
58
 
59
+ def remove(**matcher)
60
+ targets.delete_if do |uuid, list|
61
+ list.delete_if do |t|
62
+ matcher.all? { |k, v| t[k] == v }
63
+ end
64
+ list.empty?
65
+ end
66
+ end
67
+
39
68
  def run(period: 60)
40
69
  while true
41
70
  partitions.poll
@@ -57,19 +57,96 @@ module Snapsync
57
57
  end
58
58
  end
59
59
 
60
- desc 'init DIR', 'creates a synchronization target with a default policy'
61
- def init(dir)
60
+ desc 'init NAME DIR [POLICY]', 'creates a synchronization target, optionally specifying the synchronization and cleanup policy'
61
+ long_desc <<-EOD
62
+ By default, the default policy is used. To change this, provide additional
63
+ arguments as would be expected by the policy subcommand. Run snapsync help
64
+ policy for more information
65
+ EOD
66
+ option :all, type: :boolean, default: true,
67
+ desc: "if true (the default), create one snapsync target per snapper configuration under DIR, otherwise, initialize only one target directly in DIR"
68
+ option :auto, type: :boolean, default: true,
69
+ desc: "if true (the default), add the newly created target to auto-sync"
70
+ option :automount, type: :boolean, default: true,
71
+ desc: 'whether the supporting partition should be auto-mounted by snapsync when needed or not (the default is yes). Only useful if --no-auto has not been provided on the command line.'
72
+ def init(name, dir, *policy)
62
73
  dir = Pathname.new(dir)
63
- if !dir.exist?
64
- dir.mkpath
74
+
75
+ if policy.empty?
76
+ policy = ['default', Array.new]
77
+ elsif policy.size == 1
78
+ policy << Array.new
65
79
  end
66
80
 
67
- target = LocalTarget.new(dir)
68
- target.change_policy('default', Hash.new)
69
- target.write_config
81
+ dirs = Array.new
82
+ if options[:all]
83
+ SnapperConfig.each_in_dir do |config|
84
+ dirs << dir + config.name
85
+ end
86
+ else
87
+ dirs << dir
88
+ end
89
+
90
+ dirs.each do |path|
91
+ begin
92
+ LocalTarget.new(path, create_if_needed: false)
93
+ Snapsync.info "#{path} was already initialized"
94
+ rescue ArgumentError, LocalTarget::NoUUIDError
95
+ path.mkpath
96
+ target = LocalTarget.new(path)
97
+ target.change_policy(*policy)
98
+ target.write_config
99
+ Snapsync.info "initialized #{path} as a snapsync target"
100
+ end
101
+ end
102
+
103
+ if options[:auto]
104
+ if !options[:all]
105
+ Snapsync.warn "cannot use --auto without --all"
106
+ else
107
+ auto_add(name, dir)
108
+ end
109
+ end
110
+ end
111
+
112
+ desc 'auto-add NAME DIR', "add DIR to the set of targets for auto-sync"
113
+ option :automount, type: :boolean, default: true,
114
+ desc: 'whether the supporting partition should be auto-mounted by snapsync when needed or not (the default is yes)'
115
+ def auto_add(name, dir)
116
+ partitions = PartitionsMonitor.new
117
+ uuid, relative = partitions.partition_of(Pathname.new(dir))
118
+
119
+ conf_path = Pathname.new('/etc/snapsync.conf')
120
+
121
+ autosync = AutoSync.new
122
+ autosync.load_config(conf_path)
123
+ exists = autosync.each_target.find do |t|
124
+ t.partition_uuid == uuid && t.path.cleanpath == relative.cleanpath
125
+ end
126
+ if exists
127
+ if exists.automount == options[:automount]
128
+ Snapsync.info "already exists under the name #{exists.name}"
129
+ else
130
+ Snapsync.info "already exists under the name #{exists.name} but with a different automount flag, changing"
131
+ exists.automount = options[:automount]
132
+ end
133
+ exists.name ||= name
134
+ else
135
+ autosync.add AutoSync::AutoSyncTarget.new(uuid, relative, options[:automount], name)
136
+ end
137
+ autosync.write_config(conf_path)
138
+ end
139
+
140
+ desc 'auto-remove NAME', "remove a target from auto-sync by name"
141
+ def auto_remove(name)
142
+ conf_path = Pathname.new('/etc/snapsync.conf')
143
+ autosync = AutoSync.new
144
+ autosync.load_config(conf_path)
145
+ autosync.remove(name: name)
146
+ autosync.write_config(conf_path)
70
147
  end
71
148
 
72
- desc 'policy DIR TYPE [OPTIONS]', 'sets the synchronization and cleanup policy for the given target'
149
+ desc 'policy DIR TYPE [OPTIONS]', 'sets the synchronization and cleanup policy for the given target or targets'
73
150
  long_desc <<-EOD
74
151
  This command sets the policy used to decide which snapshots to synchronize to
75
152
  the target, and which to not synchronize.
@@ -28,6 +28,28 @@ module Snapsync
28
28
  monitored_partitions << partition_uuid.to_str
29
29
  end
30
30
 
31
+ def partition_of(dir)
32
+ rel = Pathname.new("")
33
+ while !dir.mountpoint?
34
+ rel = dir.basename + rel
35
+ dir = dir.dirname
36
+ end
37
+
38
+ each_partition_with_filesystem do |name, dev|
39
+ partition = dev['org.freedesktop.UDisks2.Block']
40
+ uuid = partition['IdUUID']
41
+
42
+ fs = dev['org.freedesktop.UDisks2.Filesystem']
43
+ mount_points = fs['MountPoints'].map do |str|
44
+ str[0..-2].pack("U*")
45
+ end
46
+ if mount_points.include?(dir.to_s)
47
+ return uuid, rel
48
+ end
49
+ end
50
+ nil
51
+ end
52
+
31
53
  def dirty!
32
54
  dirty.set
33
55
  end
@@ -1,3 +1,3 @@
1
1
  module Snapsync
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snapsync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Joyeux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-03 00:00:00.000000000 Z
11
+ date: 2015-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logging