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.



6
7
8
9
10
11
12
# File 'lib/quicksync/rsync.rb', line 6

def initialize
  @logger = QuickSync.Logger
  @logger.level = QuickSync::Logger::INFO
  @default_options = QuickSync.Config
  @sync_types = @default_options[:sync_types]

end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#copy_optionsObject

Returns the value of attribute copy_options.



4
5
6
# File 'lib/quicksync/rsync.rb', line 4

def copy_options
  @copy_options
end

#default_optionsObject (readonly)

Returns the value of attribute default_options.



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

def default_options
  @default_options
end

#destObject

Returns the value of attribute dest.



4
5
6
# File 'lib/quicksync/rsync.rb', line 4

def dest
  @dest
end

#excludeObject

Returns the value of attribute exclude.



4
5
6
# File 'lib/quicksync/rsync.rb', line 4

def exclude
  @exclude
end

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/quicksync/rsync.rb', line 4

def host
  @host
end

#includeObject

Returns the value of attribute include.



4
5
6
# File 'lib/quicksync/rsync.rb', line 4

def include
  @include
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#run_methodObject

Returns the value of attribute run_method.



4
5
6
# File 'lib/quicksync/rsync.rb', line 4

def run_method
  @run_method
end

#run_onObject

Returns the value of attribute run_on.



4
5
6
# File 'lib/quicksync/rsync.rb', line 4

def run_on
  @run_on
end

#settingsObject

Returns the value of attribute settings.



4
5
6
# File 'lib/quicksync/rsync.rb', line 4

def settings
  @settings
end

#srcObject

Returns the value of attribute src.



4
5
6
# File 'lib/quicksync/rsync.rb', line 4

def src
  @src
end

#sync_typeObject (readonly)

Returns the value of attribute sync_type.



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

def sync_type
  @sync_type
end

#sync_typesObject

Returns the value of attribute sync_types.



4
5
6
# File 'lib/quicksync/rsync.rb', line 4

def sync_types
  @sync_types
end

#userObject

Returns the value of attribute user.



4
5
6
# File 'lib/quicksync/rsync.rb', line 4

def user
  @user
end

Instance Method Details

#copy_local(src, dest, options = {}) ⇒ Object



46
47
48
49
# File 'lib/quicksync/rsync.rb', line 46

def copy_local(src,dest,options={})
  set_sync_type(__method__)
  return sync(src,dest,options)
end

#copy_options_to_sObject



110
111
112
# File 'lib/quicksync/rsync.rb', line 110

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

#copy_remote(src, dest, options = {}) ⇒ Object



51
52
53
54
# File 'lib/quicksync/rsync.rb', line 51

def copy_remote(src,dest,options={})
  set_sync_type(__method__)
  return sync(src,dest,options)
end

#dest_fully_qualifiedObject



23
24
25
26
27
28
29
# File 'lib/quicksync/rsync.rb', line 23

def dest_fully_qualified
  if sync_type == :pull_from || sync_type == :push_to
    return "#{user}@#{host}:#{dest}"
  else
    return "#{dest}"
  end
end

#exclude_to_sObject



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

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

#generate_cmdObject



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

def generate_cmd
   
  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 << " #{src_fully_qualified}/ #{dest_fully_qualified}"
  return cmd
end

#include_to_sObject



114
115
116
117
118
# File 'lib/quicksync/rsync.rb', line 114

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

#parse_options(src, dest, options) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/quicksync/rsync.rb', line 86

def parse_options(src,dest,options)

  if src.nil? || src.empty?
    raise ArgumentError, ":src can not be empty"
  end

  if dest.nil? || dest.empty?
    raise ArgumentError, ":dest can not be empty"
  end

  @src =  ( ! src.nil? && ! src.empty? ) ? src: default_options[:src]
  @dest = ( ! dest.nil? && ! dest.empty? ) ? dest: default_options[:dest]
  @host = ( ! host.nil? && ! host.empty? ) ? host: default_options[:host]
  @user = ( ! user.nil? && ! user.empty? ) ? user: default_options[:user]
  @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? ? options[:copy_options]: default_options[:copy_options]
  @settings = options.length > 0 && ! options[:settings].nil? ? default_options[:settings].merge(options[:settings]): default_options[:settings]
  @run_method =  options.length > 0 && ! options[:run_method].nil? ? options[:run_method]: default_options[:run_method]

  # logger.important " parse_options: #{self.to_str}"

end

#pull_from(src, dest, options = {}) ⇒ Object



36
37
38
39
# File 'lib/quicksync/rsync.rb', line 36

def pull_from(src,dest,options={})
  set_sync_type(__method__)
  return sync(src,dest,options)
end

#push_to(src, dest, options = {}) ⇒ Object



41
42
43
44
# File 'lib/quicksync/rsync.rb', line 41

def push_to(src,dest,options={})
  set_sync_type(__method__)
  return sync(src,dest,options)
end

#set_sync_type(sync_type) ⇒ Object



14
15
16
# File 'lib/quicksync/rsync.rb', line 14

def set_sync_type(sync_type)
  @sync_type = sync_type.to_sym
end

#src_fully_qualifiedObject



19
20
21
# File 'lib/quicksync/rsync.rb', line 19

def src_fully_qualified
  return src
end

#sync(src, dest, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/quicksync/rsync.rb', line 56

def sync(src,dest,options={})
  parse_options(src,dest,options)
  cmd = generate_cmd
  # logger.info "quicksync command:\n  #{cmd}"
  if run_method == :execute
    logger.info " RSync.pull_from: executing command:\n#  {cmd}"
    system(cmd)
  end

  return cmd
end

#to_strObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/quicksync/rsync.rb', line 68

def to_str
  out = "RSync.to_str:\n"
  out += "  :src=#{src}\n"
  out += "  :dest=#{dest}\n"
  out += "  :dest_fully_qualified=#{dest_fully_qualified}\n"
  out += "  :host=#{host}\n"
  out += "  :user=#{user}\n"
  out += "  :exclude=#{exclude.join(',')}\n" if ! exclude.nil?
  out += "  :include=#{include.join(',')}\n" if ! include.nil?
  out += "  :copy_options=#{copy_options.join(" --")}\n"
  out += "  :settings=#{settings}\n"
  out += "  :sync_type=#{sync_type}\n"
  out += "  :sync_types=#{sync_types}\n"
  out += "  :run_on=#{run_on}\n"
  out += "  :run_method=#{run_method}\n"
  return out
end