Class: Multisync::Runtime

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Runtime

Returns a new instance of Runtime.



11
12
13
# File 'lib/multisync/runtime.rb', line 11

def initialize options
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Runtime options

dryrun: true|false
show: true|false


9
10
11
# File 'lib/multisync/runtime.rb', line 9

def options
  @options
end

Instance Method Details

#check_path(path, type = :source) ⇒ Object

checks a path if path includes a host, the reachability of the host will be checked the existence of the remote path will not be checked if path is a local source path, its existence will be checked if path is a local destination path, the existence of the parent will be checked



97
98
99
100
101
102
103
104
105
106
# File 'lib/multisync/runtime.rb', line 97

def check_path path, type = :source
  if path.include? ':'
    host = path.split(':').first.split('@').last
    Mixlib::ShellOut.new("ping -o -t 1 #{host}").run_command.status.success?
  else
    abs_path = File.expand_path path
    abs_path = File.dirname abs_path if type == :destination
    File.exist? abs_path
  end
end

#dryrun?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/multisync/runtime.rb', line 15

def dryrun?
  options[:dryrun]
end

#quiet?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/multisync/runtime.rb', line 23

def quiet?
  options[:quiet]
end

#run(sync) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/multisync/runtime.rb', line 31

def run sync
  rsync_options = sync.rsync_options.dup
  rsync_options.unshift '--stats'
  rsync_options.unshift '--verbose' unless quiet?
  rsync_options.unshift '--dry-run' if dryrun?

  # escape path by hand, shellescape escapes also ~, but we want to keep its
  # special meaning for home, instead of passing it as literal char
  source, destination = [sync.source, sync.destination].map {|path| path.gsub(/\s+/, '\\ ') }
  cmd = "rsync #{rsync_options.join(' ')} #{source} #{destination}"
  cmd_options = { timeout: timeout }
  cmd_options.merge!({live_stdout: $stdout, live_stderr: $stderr}) unless quiet?
  rsync = Mixlib::ShellOut.new(cmd, cmd_options)
  sync.result[:cmd] = rsync.command

  unless quiet?
    puts
    puts [sync.source_description, sync.destination_description].join(' --> ').color(:cyan)
  end
  
  # Perform all only_if checks, from top to bottom
  sync.checks.each do |check|
    next unless Mixlib::ShellOut.new(check[:cmd]).run_command.error?

    puts check[:cmd] + ' (failed)'
    puts "Skip: ".color(:yellow) + rsync.command
    sync.result[:action] = :skip
    sync.result[:skip_message] = check[:message]
    return
  end
  
  # source check
  if sync.check_source? && ! check_path(sync.source, :source)
    puts "Source #{sync.source} is not accessible"
    puts "Skip: ".color(:yellow) + rsync.command
    sync.result[:action] = :skip
    sync.result[:skip_message] = "Source is not accessible"
    return
  end
  
  # target check
  if sync.check_destination? && ! check_path(sync.destination, :destination)
    puts "Destination #{sync.destination} is not accessible"
    puts "Skip: ".color(:yellow) + rsync.command
    sync.result[:action] = :skip
    sync.result[:skip_message] = "Destination is not accessible"
    return
  end
    
  if show_only?
    puts rsync.command
  else
    sync.result[:action] = :run
    puts rsync.command if dryrun? && !quiet?
    rsync.run_command
    sync.result[:status] = rsync.status
    sync.result[:stdout] = rsync.stdout
    sync.result[:stderr] = rsync.stderr
  end
end

#show_only?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/multisync/runtime.rb', line 19

def show_only?
  options[:print]
end

#timeoutObject



27
28
29
# File 'lib/multisync/runtime.rb', line 27

def timeout
  options[:timeout]
end