Class: Multisync::Runtime

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Colors

#as_fail, #as_main, #as_note, #as_skipped, #as_success

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



92
93
94
95
96
97
98
99
100
101
# File 'lib/multisync/runtime.rb', line 92

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
    File.expand_path(path)
      .then { (type == :destination) ? File.dirname(_1) : _1 }
      .then { File.exist? _1 }
  end
end

#dryrun?Boolean

Returns:

  • (Boolean)


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

def dryrun? = options[:dryrun]

#quiet?Boolean

Returns:

  • (Boolean)


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

def quiet? = options[:quiet]

#run(sync) ⇒ Object



23
24
25
26
27
28
29
30
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
# File 'lib/multisync/runtime.rb', line 23

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 { _1.gsub(/\s+/, "\\ ") }
  cmd = "rsync #{rsync_options.join(" ")} #{source} #{destination}"
  cmd_options = {timeout: timeout}
  unless quiet?
    cmd_options[:live_stdout] = $stdout
    cmd_options[:live_stderr] = $stderr
  end
  rsync = Mixlib::ShellOut.new(cmd, cmd_options)
  sync.result[:cmd] = rsync.command

  unless quiet?
    puts
    puts as_main([sync.source_description, sync.destination_description].join(" --> "))
  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 as_skipped("#{check[:cmd]} (failed)")
    puts as_note("Skip: #{rsync.command}")
    sync.result[:action] = :skip
    sync.result[:skip_message] = check[:message]
    return false
  end

  # source check
  if sync.check_source? && !check_path(sync.source, :source)
    puts as_skipped("Source #{sync.source} is not accessible")
    puts as_note("Skip: #{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 as_skipped("Destination #{sync.destination} is not accessible")
    puts as_note("Skip: #{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)


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

def show_only? = options[:print]

#timeoutObject



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

def timeout = options[:timeout]