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



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/snapsync/cli.rb', line 224

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



255
256
257
258
259
260
# File 'lib/snapsync/cli.rb', line 255

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



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/snapsync/cli.rb', line 314

def auto_sync
    handle_class_options

    if options[:local_only] and options[:remote_only]
        raise ArgumentError, '--local-only and --remote-only cannot be used together'
    end

    while true
        auto = AutoSync.new(SnapperConfig.default_config_dir,
                            snapsync_config_file: Pathname.new(options[:config_file]),
                            sync_local: options[:local_only] || !options[:remote_only],
                            sync_remote: options[:remote_only] || !options[:local_only])
        auto.run
        break if options[:one_shot]

        Snapsync.info "done all declared autosync partitions, sleeping #{period}s"
        sleep 600
    end
end

#cleanup(dir) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/snapsync/cli.rb', line 102

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



295
296
297
298
299
300
301
302
303
304
305
# File 'lib/snapsync/cli.rb', line 295

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



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
210
211
212
213
214
215
216
217
# File 'lib/snapsync/cli.rb', line 148

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



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/snapsync/cli.rb', line 335

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



279
280
281
282
283
284
285
286
287
288
289
# File 'lib/snapsync/cli.rb', line 279

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



79
80
81
82
83
84
85
86
# File 'lib/snapsync/cli.rb', line 79

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



92
93
94
95
96
97
98
# File 'lib/snapsync/cli.rb', line 92

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