Class: Six::Repositories::Rsync::Lib
- Inherits:
-
Object
- Object
- Six::Repositories::Rsync::Lib
- Defined in:
- lib/six/rsync/lib.rb
Constant Summary collapse
- PROTECTED =
false
- WINDRIVE =
/\"(\w)\:/
- DEFAULT_CONFIG =
{:hosts => [], :exclude => []}.to_yaml
- DEFAULT_TIMEOUT =
60
- PARAMS =
if PROTECTED "--dry-run --times -O --no-whole-file -r --delete --progress -h --exclude=.rsync" else "--times -O --no-whole-file -r --delete --progress -h --exclude=.rsync" end
Instance Attribute Summary collapse
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#add(file) ⇒ Object
TODO: WIP.
- #clone(repository, name, opts = {}) ⇒ Object
- #commit ⇒ Object
- #compare_set(typ, host, online = true) ⇒ Object
- #compare_sums(online = true, host = config[:hosts].sample) ⇒ Object
- #init ⇒ Object
-
#initialize(base = nil, logger = nil) ⇒ Lib
constructor
A new instance of Lib.
- #push(host = nil) ⇒ Object
-
#reset(opts = {}) ⇒ Object
TODO: Allow local-self healing, AND remote healing.
- #status ⇒ Object
- #update(cmd, x_opts = [], opts = {}) ⇒ Object
Constructor Details
#initialize(base = nil, logger = nil) ⇒ Lib
Returns a new instance of Lib.
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 |
# File 'lib/six/rsync/lib.rb', line 29 def initialize(base = nil, logger = nil) @rsync_dir = nil @rsync_work_dir = nil @path = nil @stats = false @verbose = true @logger = logger @repos_local = {:pack => Hash.new, :wd => Hash.new, :version => 0} @repos_remote = {:pack => Hash.new, :wd => Hash.new, :version => 0} if base.is_a?(Rsync::Base) @rsync_dir = base.repo.path @rsync_work_dir = base.dir.path if base.dir elsif base.is_a?(Hash) @rsync_dir = base[:repository] @rsync_work_dir = base[:working_directory] end etc = File.join(TOOLS_PATH, 'etc') FileUtils.mkdir_p etc fstab = File.join(etc, 'fstab') str = "" str = File.open(fstab) {|file| file.read} if File.exists?(fstab) unless str[/cygdrive/] str += "\nnone /cygdrive cygdrive user,noacl,posix=0 0 0\n" File.open(fstab, 'w') {|file| file.puts str} end end |
Instance Attribute Details
#verbose ⇒ Object
Returns the value of attribute verbose.
18 19 20 |
# File 'lib/six/rsync/lib.rb', line 18 def verbose @verbose end |
Instance Method Details
#add(file) ⇒ Object
TODO: WIP
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/six/rsync/lib.rb', line 220 def add(file) @logger.error "Please use commit instead!" return @logger.info "Adding #{file}" if (file == ".") load_repos(:remote) @logger.info "Calculating Checksums..." ar = Dir[File.join(@rsync_work_dir, '/**/*')] change = false i = 0 ar.each do |file| i += 1 unless file[/\.gz\Z/] relative = file.clone relative.gsub!(@rsync_work_dir, '') relative.gsub!(/\A[\\|\/]/, '') checksum = md5(file) if checksum != @repos_remote[:wd][relative] change = true @logger.info "Packing #{i}/#{ar.size}: #{file}" gzip(file) @repos_remote[:wd][relative] = checksum @repos_remote[:pack]["#{relative}.gz"] = md5("#{file}.gz") FileUtils.mv("#{file}.gz", pack_path("#{relative}.gz")) end end end save_repos if change else end end |
#clone(repository, name, opts = {}) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/six/rsync/lib.rb', line 113 def clone(repository, name, opts = {}) @path = opts[:path] || '.' @rsync_work_dir = opts[:path] ? File.join(@path, name) : name # TODO: Solve logger mess completely. @logger = opts[:log] if opts[:log] case repository when Array config[:hosts] += repository when String config[:hosts] << repository end begin init # TODO: Eval move to update? arr_opts = [] arr_opts << "-I" if opts[:force] begin update('', arr_opts) rescue RsyncError @logger.error "Unable to sucessfully update, aborting..." @logger.debug "#{$!}" FileUtils.rm_rf @rsync_work_dir if File.exists?(@rsync_work_dir) end rescue RsyncError @logger.error "Unable to initialize" @logger.debug "#{$!}" end opts[:bare] ? {:repository => @rsync_work_dir} : {:working_directory => @rsync_work_dir} end |
#commit ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/six/rsync/lib.rb', line 255 def commit @logger.info "Committing changes on #{@rsync_work_dir}" @config = load_config unless @config @logger.error "Not an Rsync repository!" return end unless config[:hosts].size > 0 @logger.error "No hosts configured!" return end load_repos(:local) load_repos(:remote) @logger.info "Calculating Checksums..." @repos_local[:wd] = calc_sums(:wd) # Added or Changed files ar = Dir[File.join(@rsync_work_dir, '/**/*')] change = false i = 0 @repos_local[:wd].each_pair do |key, value| i += 1 if value != @repos_remote[:wd][key] change = true @logger.info "Packing #{i}/#{@repos_local[:wd].size}: #{key}" file = File.join(@rsync_work_dir, key) file[REGEX_FOLDER] folder = $1 folder.gsub!(@rsync_work_dir, '') gzip(file) @repos_local[:pack]["#{key}.gz"] = md5("#{file}.gz") FileUtils.mkdir_p pack_path(folder) if folder FileUtils.mv("#{file}.gz", pack_path("#{key}.gz")) end end # Deleted files @logger.info "Checking for deleted files..." i = 0 @repos_remote[:wd].each_pair do |key, value| i += 1 if @repos_local[:wd][key].nil? packed = "#{key}.gz" change = true file = pack_path(packed) file[REGEX_FOLDER] folder = $2 @logger.info "Removing: #{packed}" @repos_local[:wd].delete key @repos_local[:pack].delete packed FileUtils.rm_f(file) if File.exists?(file) end end if change @logger.info "Changes found!" save_repos(:local) host = config[:hosts].sample verfile_srv = File.join(".pack", ".repository.yml") verbose = @verbose @verbose = false begin fetch_file(verfile_srv, host) rescue # FIXME: Should never assume that :) @logger.warn "Unable to retrieve version file from server, repository probably doesnt exist!" @logger.debug "#{$!}" # raise RsyncExecuteError end @verbose = verbose load_repos(:remote) if @repos_local[:version] < @repos_remote[:version] # && !force @logger.warn "WARNING, version on server is NEWER, aborting!" raise RsyncError end @repos_local[:version] += 1 @repos_remote[:version] = @repos_local[:version] @repos_remote[:pack] = @repos_local[:pack].clone @repos_remote[:wd] = @repos_local[:wd].clone save_repos(:remote) save_repos(:local) push(host) else @logger.info "No changes found!" end end |
#compare_set(typ, host, online = true) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/six/rsync/lib.rb', line 369 def compare_set(typ, host, online = true) load_repos(:local) load_repos(:remote) #if local[typ][:md5] == remote[typ][:md5] # @logger.info "#{typ} Match!" #else # @logger.info "#{typ} NOT match, updating!" mismatch = [] @repos_remote[typ].each_pair do |key, value| if value == @repos_local[typ][key] #@logger.info "Match! #{key}" else @logger.debug "Mismatch! #{key}" mismatch << key end end if mismatch.size > 0 case typ when :pack # direct unpack of gz into working folder # Update file if online hosts = config[:hosts].clone done = false ## Pack if online b = false while hosts.size > 0 && !done do # FIXME: Nasty if b host = hosts.sample @logger.info "Trying #{host}" end slist = nil b = true hosts -= [host] begin # TODO: Progress bar if mismatch.size > (@repos_remote[typ].size / 2) @logger.info "Many files mismatched (#{mismatch.size}), running full update on .pack folder" arr_opts = [] arr_opts << PARAMS if host[/\A(\w)*\@/] arr_opts << RSH end arr_opts << esc(File.join(host, '.pack/.')) arr_opts << esc(pack_path) command('', arr_opts) else c = mismatch.size @logger.info "Fetching #{mismatch.size} files... Please wait" slist = File.join(TEMP_PATH, ".six-updater_#{rand 9999}-list") slist.gsub!("\\", "/") File.open(slist, 'w') do |f| mismatch.each { |e| f.puts e } end arr_opts = [] arr_opts << PARAMS arr_opts << RSH if host[/\A(\w)*\@/] cyg_slist = "\"#{slist}\"" while cyg_slist[WINDRIVE] do drive = cyg_slist[WINDRIVE] cyg_slist.gsub!(drive, "\"/cygdrive/#{$1}") end arr_opts << "--files-from=#{cyg_slist}" arr_opts << esc(File.join(host, '.pack/.')) arr_opts << esc(pack_path) command('', arr_opts) end done = true rescue @logger.warn "Failure" @logger.debug "#{$!}" ensure FileUtils.rm_f slist if slist end end @logger.warn "There was a problem during updating, please retry!" unless done end end when :wd mismatch.each_with_index do |e, index| # TODO: Nicer progress bar... @logger.info "Unpacking #{index + 1}/#{mismatch.size}: #{e}" unpack(:path => "#{e}.gz") end end end @repos_local[typ].each_pair do |key, value| del_file(key, typ) unless config[:exclude].include?(key) || !@repos_remote[typ][key].nil? end @repos_local[typ] = calc_sums(typ) @repos_local[:version] = @repos_remote[:version] save_repos end |
#compare_sums(online = true, host = config[:hosts].sample) ⇒ Object
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/six/rsync/lib.rb', line 476 def compare_sums(online = true, host = config[:hosts].sample) hosts = config[:hosts].clone done = false ## Pack if online b = false verbose = @verbose @verbose = false while hosts.size > 0 && !done do # FIXME: Nasty host = hosts.sample if b b = true hosts -= [host] @logger.info "Trying #{host}" begin FileUtils.cp(pack_path(".repository.yml"), rsync_path(".repository-pack.yml")) fetch_file(".pack/.repository.yml", host) load_repos(:remote) load_repos(:local) if @repos_local[:version] > @repos_remote[:version] # && !force @logger.warn "WARNING, version on server is OLDER, aborting!" FileUtils.cp(rsync_path(".repository-pack.yml"), pack_path(".repository.yml")) raise RsyncError end done = true rescue @logger.debug "#{$!}" ensure FileUtils.rm(rsync_path(".repository-pack.yml")) end end @verbose = verbose end if done && online # TODO: Don't do actions when not online @logger.info "Verifying Packed files..." compare_set(:pack, host) @logger.info "Verifying Unpacked files..." compare_set(:wd, host) save_repos end end |
#init ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/six/rsync/lib.rb', line 97 def init @logger.info "Processing: #{rsync_path}" if File.exists? rsync_path @logger.error "Seems to already be an Rsync repository, Aborting!" raise RsyncError end if File.exists? @rsync_work_dir @logger.error "Seems to already be a folder, Aborting!" raise RsyncError end FileUtils.mkdir_p pack_path save_config(config) save_repos(:local) save_repos(:remote) end |
#push(host = nil) ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/six/rsync/lib.rb', line 351 def push(host = nil) @logger.info "Pushing..." @config = load_config host = config[:hosts].sample unless host # TODO: UNCLUSTERFUCK arr_opts = [] arr_opts << PARAMS # Upload .pack changes if host[/\A(\w)*\@/] arr_opts << RSH end arr_opts << esc(pack_path('.')) arr_opts << esc(File.join(host, '.pack')) command('', arr_opts) end |
#reset(opts = {}) ⇒ Object
TODO: Allow local-self healing, AND remote healing. reset and fetch?
209 210 211 212 213 214 215 216 217 |
# File 'lib/six/rsync/lib.rb', line 209 def reset(opts = {}) @logger.info "Resetting!" if opts[:hard] @config = load_config calc save_repos compare_sums(false) end end |
#status ⇒ Object
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 91 92 93 94 95 |
# File 'lib/six/rsync/lib.rb', line 59 def status @logger.info "Showing changes on #{@rsync_work_dir}" @config = load_config unless @config @logger.error "Not an Rsync repository!" return end load_repos(:local) load_repos(:remote) @logger.info "Calculating Checksums..." @repos_local[:wd] = calc_sums(:wd) # Added or Changed files ar = Dir[File.join(@rsync_work_dir, '/**/*')] change = false i = 0 @repos_local[:wd].each_pair do |key, value| i += 1 if value != @repos_remote[:wd][key] change = true @logger.info "Modified: #{i}/#{@repos_local[:wd].size}: #{key}" end end # Deleted files @logger.info "Checking for deleted files..." i = 0 @repos_remote[:wd].each_pair do |key, value| i += 1 if @repos_local[:wd][key].nil? @logger.info "Removed: #{packed}" end end end |
#update(cmd, x_opts = [], opts = {}) ⇒ Object
148 149 150 151 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/six/rsync/lib.rb', line 148 def update(cmd, x_opts = [], opts = {}) @logger.debug "Checking for updates..." @config = load_config unless @config @logger.error "Not an Rsync repository!" raise RsyncError end unless config[:hosts].size > 0 @logger.error "No hosts configured!" raise RsyncError end load_repos(:local) load_repos(:remote) hosts = config[:hosts].clone host = hosts.sample if opts[:force] done = false b = false verbose = @verbose @verbose = false while hosts.size > 0 && !done do # FIXME: Nasty host = hosts.sample if b b = true hosts -= [host] @logger.info "Trying #{host}" begin arr_opts = [] arr_opts << PARAMS arr_opts += x_opts if host[/\A(\w)*\@/] arr_opts << RSH #"-e ssh" end arr_opts << esc(File.join(host, '.pack/.')) arr_opts << esc(pack_path) command(cmd, arr_opts) calc save_repos done = true rescue @logger.debug "#{$!}" end end @verbose = verbose raise RsyncError if !done else #reset(:hard => true) calc save_repos # fetch latest sums and only update when changed compare_sums(true, host) end end |