Class: Zonesync::Sync

Inherits:
Struct
  • Object
show all
Defined in:
lib/zonesync/sync.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#destinationObject

Returns the value of attribute destination



6
7
8
# File 'lib/zonesync/sync.rb', line 6

def destination
  @destination
end

#sourceObject

Returns the value of attribute source



6
7
8
# File 'lib/zonesync/sync.rb', line 6

def source
  @source
end

Instance Method Details

#call(dry_run: false, force: false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zonesync/sync.rb', line 7

def call(dry_run: false, force: false)
  operations = destination.diff!(source, force: force)

  smanifest = source.manifest.generate
  dmanifest = destination.manifest.existing
  if smanifest != dmanifest
    if dmanifest
      operations << [:change, [dmanifest, smanifest]]
    else
      operations << [:add, [smanifest]]
    end
  end

  # Only sync checksums for v1 manifests (v2 manifests provide integrity via hashes)
  source_will_generate_v2 = !source.manifest.existing? # No existing manifest = will generate v2
  dest_has_checksum = destination.manifest.existing_checksum
  dest_has_v1_manifest = destination.manifest.existing? && destination.manifest.v1_format?

  if source_will_generate_v2 && dest_has_checksum
    # Transitioning to v2: remove old checksum
    operations << [:remove, [dest_has_checksum]]
  elsif !source_will_generate_v2 && (source.manifest.v1_format? || dest_has_v1_manifest)
    # Both source and dest use v1 format: sync checksum
    schecksum = source.manifest.generate_checksum
    dchecksum = destination.manifest.existing_checksum
    if schecksum != dchecksum
      if dchecksum
        operations << [:change, [dchecksum, schecksum]]
      else
        operations << [:add, [schecksum]]
      end
    end
  end

  operations.each do |method, records|
    Logger.log(method, records, dry_run: dry_run)
    destination.send(method, *records) unless dry_run
  end
end