Class: QuickSync::RSync

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRSync

Returns a new instance of RSync.



9
10
11
12
13
14
# File 'lib/quicksync/rsync.rb', line 9

def initialize
  @logger = QuickSync.Logger
  @logger.level = QuickSync::Logger::TRACE
  @default_options = $default_options
  
end

Instance Attribute Details

#copy_optionsObject

Returns the value of attribute copy_options.



6
7
8
# File 'lib/quicksync/rsync.rb', line 6

def copy_options
  @copy_options
end

#default_optionsObject (readonly)

Returns the value of attribute default_options.



7
8
9
# File 'lib/quicksync/rsync.rb', line 7

def default_options
  @default_options
end

#excludeObject

Returns the value of attribute exclude.



6
7
8
# File 'lib/quicksync/rsync.rb', line 6

def exclude
  @exclude
end

#fromObject

Returns the value of attribute from.



6
7
8
# File 'lib/quicksync/rsync.rb', line 6

def from
  @from
end

#includeObject

Returns the value of attribute include.



6
7
8
# File 'lib/quicksync/rsync.rb', line 6

def include
  @include
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/quicksync/rsync.rb', line 7

def logger
  @logger
end

#run_methodObject

Returns the value of attribute run_method.



6
7
8
# File 'lib/quicksync/rsync.rb', line 6

def run_method
  @run_method
end

#settingsObject

Returns the value of attribute settings.



6
7
8
# File 'lib/quicksync/rsync.rb', line 6

def settings
  @settings
end

#toObject

Returns the value of attribute to.



6
7
8
# File 'lib/quicksync/rsync.rb', line 6

def to
  @to
end

Instance Method Details

#copy_options_to_sObject



16
17
18
# File 'lib/quicksync/rsync.rb', line 16

def copy_options_to_s
  return copy_options.map{ |o| "--#{o}"}.join(' ')
end

#exclude_to_sObject



26
27
28
29
30
# File 'lib/quicksync/rsync.rb', line 26

def exclude_to_s
  if exclude.any?
    return exclude.map { |e| "--exclude=\"#{e}\"" }.join(' ')
  end
end

#from_to_sObject



126
127
128
129
130
131
132
133
134
# File 'lib/quicksync/rsync.rb', line 126

def from_to_s 
  
  logger.debug "RSync.from_to_s: from=#{from}"
  if ! from[:host].nil?  && ! from[:host].empty?  && ! from[:host].include?("local")
    return from[:user]+"@"+from[:host] + ":" + from[:dir]
  else 
    return from[:dir]
  end
end

#generate_cmdObject



146
147
148
149
150
151
152
153
154
155
# File 'lib/quicksync/rsync.rb', line 146

def generate_cmd
  logger.debug "QuickSync.generated_cmd: method called"
 
  cmd = "#{settings[:rsync_app]} #{copy_options_to_s}"
  cmd << " #{include_to_s}" if !include.nil? && include.any?
  cmd << " #{exclude_to_s}" if !exclude.nil? && exclude.any?
  cmd << " #{from_to_s}/ #{to_to_s}"
  
  return cmd
end

#include_to_sObject



20
21
22
23
24
# File 'lib/quicksync/rsync.rb', line 20

def include_to_s
  if include.any?
    return include.map { |i| "--include=\"#{i}\"" }.join(' ')
   end
end

#is_local(the_host) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/quicksync/rsync.rb', line 110

def is_local(the_host)
  if ! the_host.nil?  && ! the_host.empty?  && the_host.include?("local")
    return true
  else
    return false
  end
end

#is_remote(the_host) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/quicksync/rsync.rb', line 118

def is_remote(the_host)
  if ! the_host.nil?  && ! the_host.empty?  && ! the_host.include?("local")
    return true
  else
    return false
  end
end

#parse_options(from_v, to_v, options) ⇒ Object



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
# File 'lib/quicksync/rsync.rb', line 36

def parse_options(from_v,to_v,options)
   
  if from_v.nil? || from_v.empty?
    raise ArgumentError, ":from can not be empty"
  end

  if to_v.nil? || to_v.empty?
    raise ArgumentError, ":to can not be empty"
  end
  
  logger.debug "QuickSync.parse_options: from class=#{from_v.class}"

  @from = from_v == String ? default_options[:from].merge({:dir=>from_v}): default_options[:from].merge(from_v)
  @to = to_v == String ? default_options[:to].merge({:dir=>to_v}): default_options[:to].merge(to_v)
  @exclude =  options.length > 0 && ! options[:exclude].nil? ? options[:exclude]: default_options[:exclude]
  @include =  options.length > 0 && ! options[:include].nil? ? options[:include]: default_options[:include]
  @copy_options = options.length > 0 && ! options[:copy_options].nil? ? default_options[:copy_options].concat(options[:copy_options]).uniq!: default_options[:copy_options]
  @settings = options.length > 0 && ! options[:settings].nil? ? default_options[:settings].merge(options[:settings]): default_options[:settings]
  @run_method = default_options[:run_method]
 
  logger.debug "QuickSync.parse_options:"
  logger.debug "  from=#{from}"
  logger.debug "  to=#{to}"
  logger.debug "  exclude=#{exclude}"
  logger.debug "  include=#{include}"
  logger.debug "  copy_options=#{copy_options}"
  logger.debug "  settings=#{settings}"

end

#pull_from(from, to, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/quicksync/rsync.rb', line 66

def pull_from(from,to,options={})
  parse_options(from,to,options)
  # if from[:dir] does not exist then abort as there is nothing to do
  cmd = generate_cmd
  logger.info "RSync.pull_from: run_method=#{run_method}"
  if run_method == :execute
    # run the actual command before returning it
    logger.info "RSync.pull_from: about to execute command"
    system(cmd)
    
  end
  logger.info "quicksync command:\n  #{cmd}"
  return cmd
end

#push_to(from, to, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/quicksync/rsync.rb', line 81

def push_to(from,to,options={})
  parse_options(from,to,options)
  # if from[:dir] does not exist then abort as there is nothing to do
  cmd = generate_cmd
  logger.info "RSync.push_to: run_method=#{run_method}"
  if run_method == :execute
    # run the actual command before returning it
    logger.info "RSync.push_to: about to execute command"
    system(cmd)
    
  end
  logger.info "quicksync command:\n  #{cmd}"
  return cmd
end

#run_onObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/quicksync/rsync.rb', line 96

def run_on
  if is_local(from[:host])  && is_local(to[:host]) 
    return :local
  elsif is_local(from[:host])  && is_remote(to[:host])
    return :local
  elsif is_remote(from[:host])  && is_remote(to[:host])
    return :remote
  elsif is_remote(from[:host])  && is_local(to[:host])
    return :remote
  else
    return :local
  end
end

#to_to_sObject



136
137
138
139
140
141
142
143
# File 'lib/quicksync/rsync.rb', line 136

def to_to_s 
  val = to[:dir]
  if ! to[:host].nil?  && ! to[:host].empty?  && ! to[:host].include?("local")
    val = to[:user]+"@"+to[:host] + ":" + to[:dir]
  else 
    return to[:dir]
  end
end