Class: S3Ranger::SyncCommand
- Inherits:
-
Object
- Object
- S3Ranger::SyncCommand
- Defined in:
- lib/s3ranger/sync.rb
Class Method Summary collapse
- .cmp(list1, list2) ⇒ Object
- .parse_params(args) ⇒ Object
- .process_destination(source, destination) ⇒ Object
- .process_file_destination(source, destination, file = "") ⇒ Object
- .remote_prefix?(prefix) ⇒ Boolean
Instance Method Summary collapse
- #download_files(destination, source, list) ⇒ Object
-
#initialize(args, source, destination) ⇒ SyncCommand
constructor
A new instance of SyncCommand.
- #read_tree_remote(location) ⇒ Object
- #read_trees(source, destination) ⇒ Object
- #remove_files(remote, list) ⇒ Object
- #remove_local_files(destination, source, list) ⇒ Object
- #run ⇒ Object
- #upload_files(remote, list) ⇒ Object
Constructor Details
#initialize(args, source, destination) ⇒ SyncCommand
Returns a new instance of SyncCommand.
146 147 148 149 150 |
# File 'lib/s3ranger/sync.rb', line 146 def initialize args, source, destination @args = args @source = source @destination = destination end |
Class Method Details
.cmp(list1, list2) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/s3ranger/sync.rb', line 124 def self.cmp list1, list2 l1 = {}; list1.each {|e| l1[e.path] = e} l2 = {}; list2.each {|e| l2[e.path] = e} same, to_add_to_2 = [], [] l1.each do |key, value| value2 = l2.delete key if value2.nil? to_add_to_2 << value elsif value2.size == value.size same << value else to_add_to_2 << value end end to_remove_from_2 = l2.values [same, to_add_to_2, to_remove_from_2] end |
.parse_params(args) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/s3ranger/sync.rb', line 183 def self.parse_params args # Reading the arbitrary parameters from the command line and getting # modifiable copies to parse source, destination = args; return nil if source.nil? or destination.nil? # Sync from one s3 to another is currently not supported if remote_prefix? source and remote_prefix? destination raise WrongUsage.new(nil, 'Both arguments can\'t be on S3') end # C'mon, there's rsync out there if !remote_prefix? source and !remote_prefix? destination raise WrongUsage.new(nil, 'One argument must be on S3') end source, destination = process_destination source, destination return [Location.new(*source), Location.new(*destination)] end |
.process_destination(source, destination) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/s3ranger/sync.rb', line 237 def self.process_destination source, destination source, destination = source.dup, destination.dup # don't repeat slashes source.squeeze! '/' destination.squeeze! '/' # Making sure that local paths won't break our stuff later source.gsub!(/^\.\//, '') destination.gsub!(/^\.\//, '') # Parsing the final destination destination = process_file_destination source, destination, "" # here's where we find out what direction we're going source_is_s3 = remote_prefix? source # canonicalize the S3 stuff remote_prefix = source_is_s3 ? source : destination bucket, remote_prefix = remote_prefix.split ":" remote_prefix ||= "" # Just making sure we preserve the direction if source_is_s3 [[remote_prefix, bucket], destination] else [source, [remote_prefix, bucket]] end end |
.process_file_destination(source, destination, file = "") ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/s3ranger/sync.rb', line 208 def self.process_file_destination source, destination, file="" if not file.empty? sub = (remote_prefix? source) ? source.split(":")[1] : source file = file.gsub(/^#{sub}/, '') end # no slash on end of source means we need to append the last src dir to # dst prefix testing for empty isn't good enough here.. needs to be # "empty apart from potentially having 'bucket:'" if source =~ %r{/$} File.join [destination, file] else if remote_prefix? source _, name = source.split ":" File.join [destination, File.basename(name || ""), file] else source = /^\/?(.*)/.match(source)[1] # Corner case: the root of the remote path is empty, we don't want to # add an unnecessary slash here. if destination.end_with? ':' File.join [destination + source, file] else File.join [destination, source, file] end end end end |
.remote_prefix?(prefix) ⇒ Boolean
202 203 204 205 206 |
# File 'lib/s3ranger/sync.rb', line 202 def self.remote_prefix?(prefix) # allow for dos-like things e.g. C:\ to be treated as local even with # colon. prefix.include? ':' and not prefix.match '^[A-Za-z]:[\\\\/]' end |
Instance Method Details
#download_files(destination, source, list) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/s3ranger/sync.rb', line 314 def download_files destination, source, list list.each {|e| path = File.join destination.path, e.path if @args.verbose puts " + #{source}#{e.path} => #{path}" end unless @args.dry_run obj = @args.s3.buckets[source.bucket].objects[e.path] # Making sure this new file will have a safe shelter FileUtils.mkdir_p File.dirname(path) # Downloading and saving the files File.open(path, 'wb') do |file| obj.read do |chunk| file.write chunk end end end } end |
#read_tree_remote(location) ⇒ Object
267 268 269 270 271 272 273 |
# File 'lib/s3ranger/sync.rb', line 267 def read_tree_remote location dir = location.path dir += '/' if not dir.empty? or dir.end_with?('/') @args.s3.buckets[location.bucket].objects.with_prefix(dir || "").to_a.collect {|obj| Node.new location.path, obj.key, obj.content_length } end |
#read_trees(source, destination) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/s3ranger/sync.rb', line 275 def read_trees source, destination if source.local? source_tree = LocalDirectory.new(source.path).list_files destination_tree = read_tree_remote destination else source_tree = read_tree_remote source destination_tree = LocalDirectory.new(destination.path).list_files end [source_tree, destination_tree] end |
#remove_files(remote, list) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/s3ranger/sync.rb', line 301 def remove_files remote, list if @args.verbose list.each {|e| puts " - #{remote}#{e.path}" } end unless @args.dry_run @args.s3.buckets[remote.bucket].objects.delete_if { |obj| list.include? obj.key } end end |
#remove_local_files(destination, source, list) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/s3ranger/sync.rb', line 338 def remove_local_files destination, source, list list.each {|e| path = File.join destination.path, e.path if @args.verbose puts " * #{e.path} => #{path}" end unless @args.dry_run FileUtils.rm_rf path end } end |
#run ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/s3ranger/sync.rb', line 152 def run # Reading the source and destination using our helper method if (source, destination, bucket = self.class.parse_params [@source, @destination]).nil? raise WrongUsage.new(nil, 'Need a source and a destination') end # Getting the trees source_tree, destination_tree = read_trees source, destination # Getting the list of resources to be exchanged between the two peers _, to_add, to_remove = self.class.cmp source_tree, destination_tree # Removing the items matching the exclude pattern if requested to_add.select! { |e| begin (e.path =~ /#{@args.exclude}/).nil? rescue RegexpError => exc raise WrongUsage.new nil, exc. end } if @args.exclude # Calling the methods that perform the actual IO if source.local? upload_files destination, to_add remove_files destination, to_remove unless @args.keep else download_files destination, source, to_add remove_local_files destination, source, to_remove unless @args.keep end end |
#upload_files(remote, list) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/s3ranger/sync.rb', line 287 def upload_files remote, list list.each do |e| if @args.verbose puts " + #{e.full} => #{remote}#{e.path}" end unless @args.dry_run if File.file? e.path @args.s3.buckets[remote.bucket].objects[e.path].write Pathname.new(e.path), :acl => @args.acl end end end end |