Class: Mclone::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/mclone.rb

Defined Under Namespace

Classes: Error, IntactTaskSet, SessionTaskSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSession

Returns a new instance of Session.



528
529
530
531
# File 'lib/mclone.rb', line 528

def initialize
  @volumes = VolumeSet.new
  @tasks = SessionTaskSet.new(self)
end

Instance Attribute Details

#force=(value) ⇒ Object (writeonly)

Sets the attribute force

Parameters:

  • value

    the value to set the attribute force to.



522
523
524
# File 'lib/mclone.rb', line 522

def force=(value)
  @force = value
end

#simulate=(value) ⇒ Object (writeonly)

Sets the attribute simulate

Parameters:

  • value

    the value to set the attribute simulate to.



522
523
524
# File 'lib/mclone.rb', line 522

def simulate=(value)
  @simulate = value
end

#tasksObject (readonly)

Returns the value of attribute tasks.



525
526
527
# File 'lib/mclone.rb', line 525

def tasks
  @tasks
end

#verbose=(value) ⇒ Object (writeonly)

Sets the attribute verbose

Parameters:

  • value

    the value to set the attribute verbose to.



522
523
524
# File 'lib/mclone.rb', line 522

def verbose=(value)
  @verbose = value
end

#volumesObject (readonly)

Returns the value of attribute volumes.



504
505
506
# File 'lib/mclone.rb', line 504

def volumes
  @volumes
end

Instance Method Details

#commit!Object



656
657
658
659
# File 'lib/mclone.rb', line 656

def commit!
  volumes.each { |v| v.commit!(force?) } unless simulate?
  self
end

#create_task!(mode, source, destination, **kws) ⇒ Object

Raises:



564
565
566
567
568
569
570
# File 'lib/mclone.rb', line 564

def create_task!(mode, source, destination, **kws)
  task = Task.new(self, mode, *locate(source), *locate(destination), **kws)
  _task = tasks[task]
  raise(Session::Error, %(refuse to overwrite existing task "#{_task.id}")) unless _task.nil? || force?
  tasks << task
  self
end

#delete_task!(id) ⇒ Object



584
585
586
587
# File 'lib/mclone.rb', line 584

def delete_task!(id)
  tasks >> tasks.task(tasks.resolve(id))
  self
end

#delete_volume!(id) ⇒ Object

Raises:



555
556
557
558
559
560
561
# File 'lib/mclone.rb', line 555

def delete_volume!(id)
  volume = volumes.volume(id = volumes.resolve(id))
  raise(Session::Error, %(refuse to delete non-empty Mclone volume file "#{volume.file}")) unless volume.tasks.empty? || force?
  volumes >> volume
  FileUtils.rm_f(volume.file) unless simulate?
  self
end

#force?Boolean

Returns:

  • (Boolean)


517
518
519
# File 'lib/mclone.rb', line 517

def force?
  @force == true
end

#format_volume!(dir) ⇒ Object

Raises:



534
535
536
537
538
539
540
# File 'lib/mclone.rb', line 534

def format_volume!(dir)
  mclone = File.join(dir, Volume::FILE)
  raise(Session::Error, %(refuse to overwrite existing Mclone volume file "#{mclone}")) if File.exist?(mclone) && !force?
  volumes << (volume = Volume.new(self, mclone))
  volume.commit!(true) unless simulate? # Force creation of a new (empty) volume
  self
end

#intact_tasksObject

Collect all tasks from all loaded volumes which are ready to be executed



639
640
641
# File 'lib/mclone.rb', line 639

def intact_tasks
  IntactTaskSet.new(self).merge!(tasks)
end

#modify_task!(id, mode: nil, include: nil, exclude: nil) ⇒ Object



573
574
575
576
577
578
579
580
581
# File 'lib/mclone.rb', line 573

def modify_task!(id, mode: nil, include: nil, exclude: nil)
  ts = tasks
  task = ts.task(ts.resolve(id)).clone
  task.mode = mode unless mode.nil?
  task.include = include unless include.nil?
  task.exclude = exclude unless exclude.nil?
  tasks << task
  self
end

#process_tasks!(*ids) ⇒ Object

Raises:



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/mclone.rb', line 590

def process_tasks!(*ids)
  failed = false
  intacts = intact_tasks
  ids = intacts.collect(&:id) if ids.empty?
  ids.collect { |id| intacts.task(intacts.resolve(id)) }.each do |task|
    source_path = File.join(volumes.volume(task.source_id).root, task.source_root.nil? || task.source_root.empty? ? '' : task.source_root)
    destination_path = File.join(volumes.volume(task.destination_id).root, task.destination_root.nil? || task.destination_root.empty? ? '' : task.destination_root)
    args = [Mclone.rclone]
    opts = [
      '--config', Mclone.windows? ? 'NUL' : '/dev/null',
      simulate? ? '--dry-run' : nil,
      verbose? ? '--verbose' : nil,
      verbose? ? '--progress' : nil
    ].compact
    opts.append('--crypt-password', task.crypter_token) unless task.crypter_mode.nil?
    case task.crypter_mode
    when :encrypt then opts.append('--crypt-remote', destination_path)
    when :decrypt then opts.append('--crypt-remote', source_path)
    end
    case task.mode
    when :update then args.push('copy', '--update')
    when :synchronize then args << 'sync'
    when :copy then args << 'copy'
    when :move then args << 'move'
    end
    opts.append('--filter', "- /#{Volume::FILE}")
    opts.append('--filter', "- #{task.exclude}") unless task.exclude.nil? || task.exclude.empty?
    opts.append('--filter', "+ #{task.include}") unless task.include.nil? || task.include.empty?
    args.concat(opts)
    case task.crypter_mode
    when nil then args.append(source_path, destination_path)
    when :encrypt then args.append(source_path, ':crypt:')
    when :decrypt then args.append(':crypt:', destination_path)
    end
    $stdout << args.collect(&:escape).join(' ') << "\n" if verbose?
    case system(*args)
    when nil
      $stderr << %(failed to execute "#{args.first}") << "\n" if verbose?
      failed = true
    when false
      $stderr << %(Rclone exited with status #{$?.to_i}) << "\n" if verbose?
      failed = true
    end
  end
  raise(Session::Error, "Rclone execution failure(s)") if failed
  self
end

#restore_volume!(dir) ⇒ Object



543
544
545
546
# File 'lib/mclone.rb', line 543

def restore_volume!(dir)
  volumes << Volume.restore(self, File.join(dir, Volume::FILE))
  self
end

#restore_volumes!Object



549
550
551
552
# File 'lib/mclone.rb', line 549

def restore_volumes!
  (Mclone.environment_mounts + Mclone.system_mounts + [ENV['HOME']]).each { |dir| restore_volume!(dir) rescue Errno::ENOENT }
  self
end

#simulate?Boolean

Returns:

  • (Boolean)


507
508
509
# File 'lib/mclone.rb', line 507

def simulate?
  @simulate == true
end

#verbose?Boolean

Returns:

  • (Boolean)


512
513
514
# File 'lib/mclone.rb', line 512

def verbose?
  @verbose == true
end