Class: Snapsync::CLI

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

Instance Method Summary collapse

Instance Method Details

#auto_add(name, dir) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/snapsync/cli.rb', line 216

def auto_add(name, dir)
    uuid, mountpoint, relative = partition_of(Snapsync::path(dir))
    conf_path = Pathname.new(options[:config_file])

    autosync = AutoSync.new snapsync_config_file: conf_path
    exists = autosync.each_autosync_target.find do |t|
        t.partition_uuid == uuid && t.mountpoint.cleanpath == mountpoint.cleanpath && t.relative.cleanpath == relative.cleanpath
    end
    if exists
        if !exists.name
            if (exists.automount ^ options[:automount]) && name
                Snapsync.info "already exists without a name, setting the name to #{name}"
            elsif name
                Snapsync.info "already exists without a name and a different automount flag, setting the name to #{name} and updating the automount flag"
            else
                Snapsync.info "already exists with different automount flag, updating"
            end
        elsif exists.automount == options[:automount]
            Snapsync.info "already exists under the name #{exists.name}"
        else
            Snapsync.info "already exists under the name #{exists.name} but with a different automount flag, changing"
            exists.automount = options[:automount]
        end
        exists.name ||= name
    else
        autosync.add AutoSync::AutoSyncTarget.new(uuid, mountpoint, relative, options[:automount], name)
    end
    autosync.write_config(conf_path)
end

#auto_remove(name) ⇒ Object



247
248
249
250
251
252
# File 'lib/snapsync/cli.rb', line 247

def auto_remove(name)
    conf_path = Pathname.new('/etc/snapsync.conf')
    autosync = AutoSync.new snapsync_config_file: conf_path
    autosync.remove(name: name)
    autosync.write_config(conf_path)
end

#auto_syncObject



304
305
306
307
308
309
310
311
312
# File 'lib/snapsync/cli.rb', line 304

def auto_sync
    handle_class_options
    auto = AutoSync.new(SnapperConfig.default_config_dir, snapsync_config_file: Pathname.new(options[:config_file]))
    if options[:one_shot]
        auto.sync
    else
        auto.run
    end
end

#cleanup(dir) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/snapsync/cli.rb', line 94

def cleanup(dir)
    handle_class_options
    target = SyncTarget.new(Snapsync::path(dir))

    if target.cleanup
        target.cleanup.cleanup(target, dry_run: options[:dry_run])
    else
        Snapsync.info "#{target.sync_policy.class.name} policy set, nothing to do"
    end
end

#destroy(dir) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/snapsync/cli.rb', line 287

def destroy(dir)
    handle_class_options
    target_dir = Snapsync::path(dir)

    target = SyncTarget.new(target_dir, create_if_needed: false)
    snapshots = target.each_snapshot.to_a
    snapshots.sort_by(&:num).each do |s|
        target.delete(s)
    end
    target_dir.rmtree
end

#init(*args) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/snapsync/cli.rb', line 140

def init(*args)
    if options[:auto] && !options[:all]
        raise ArgumentError, "cannot use --auto without --all"
    end

    if options[:auto]
        if args.size < 2
            self.class.handle_argument_error(self.class.all_commands['init'], nil, args, 2)
        end
        name, dir, *policy = *args
    else
        if args.size < 1
            self.class.handle_argument_error(self.class.all_commands['init'], nil, args, 1)
        end
        dir, *policy = *args
    end

    dir = Snapsync::path(dir)
    remote = dir.instance_of? RemotePathname

    # Parse the policy option early to avoid breaking later
    begin
        policy = normalize_policy(policy)
    rescue Exception => policy_validation_error
        # Try to see if the user forgot to add the NAME option or added
        # the name option but should not have
        if (args.size > 1) && options[:auto]
            begin
                normalize_policy(args[1..-1])
                raise ArgumentError, "--auto is set but it seems that you did not provide a name"
            rescue InvalidConfiguration
            end
        elsif args.size > 2
            begin
                normalize_policy(args[2..-1])
                raise ArgumentError, "--auto is not set but it seems that you provided a name"
            rescue InvalidConfiguration
            end
        end
        raise policy_validation_error
    end

    dirs = Array.new
    if options[:all]
        SnapperConfig.each_in_dir do |config|
            dirs << dir + config.name
        end
    else
        dirs << dir
    end

    dirs.each do |path|
        begin
            SyncTarget.new(path, create_if_needed: false)
            Snapsync.info "#{path} was already initialized"
        rescue ArgumentError, SyncTarget::NoUUIDError
            path.mkpath
            target = SyncTarget.new(path)
            target.change_policy(*policy)
            target.write_config
            Snapsync.info "initialized #{path} as a snapsync target"
        end
    end

    # We check that both options are set together for some added safety,
    # but it's checked at the top of the method
    if options[:auto] && options[:all]
        auto_add(name, dir)
    end
end

#list(dir = nil) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/snapsync/cli.rb', line 315

def list(dir = nil)
    handle_class_options
    each_target(dir) do |config, target|
        puts "== #{target.dir}"
        puts "UUID: #{target.uuid}"
        puts "Enabled: #{target.enabled?}"
        puts "Autoclean: #{target.autoclean?}"
        puts "Snapper config: #{config.name}"
        print "Policy: "
        pp target.sync_policy

        snapshots_seen = Set.new

        # @type [Snapshot]
        last_snapshot = nil
        puts "Snapshots:"
        target.each_snapshot do |s|
            snapshots_seen.add(s.num)
            last_snapshot = s
            puts "  #{s.num} #{s.to_time}"
        end

        puts " [transferrable:]"
        config.each_snapshot do |s|
            if not snapshots_seen.include? s.num
                delta = s.size_diff_from(last_snapshot)
                puts "  #{s.num} #{s.to_time} => from: #{last_snapshot.num} delta: " \
                    +"#{Snapsync.human_readable_size(delta)}"

                # If the delta was 0, then the data already exists on remote.
                if delta > 0
                    last_snapshot = s
                end
            end
        end
    end
end

#policy(dir, type, *options) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/snapsync/cli.rb', line 271

def policy(dir, type, *options)
    handle_class_options
    dir = Snapsync::path(dir)

    # Parse the policy early to avoid breaking later
    policy = normalize_policy([type, *options])
    each_target(dir) do |_, target|
        target.change_policy(*policy)
        target.write_config
    end
end

#sync(config_name, dir) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/snapsync/cli.rb', line 71

def sync(config_name, dir)
    handle_class_options
    dir = Snapsync::path(dir)

    config = config_from_name(config_name)
    target = SyncTarget.new(dir)
    Sync.new(config, target, autoclean: options[:autoclean]).run
end

#sync_all(dir) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/snapsync/cli.rb', line 84

def sync_all(dir)
    handle_class_options

    dir = Snapsync::path(dir)
    op = SyncAll.new(dir, config_dir: SnapperConfig.default_config_dir, autoclean: options[:autoclean])
    op.run
end