Class: Constancy::SyncTarget

Inherits:
Object
  • Object
show all
Defined in:
lib/constancy/sync_target.rb

Constant Summary collapse

VALID_CONFIG_KEYS =
%w( name datacenter prefix path exclude chomp delete )
REQUIRED_CONFIG_KEYS =
%w( prefix )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, imperium_config:) ⇒ SyncTarget

Returns a new instance of SyncTarget.



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
# File 'lib/constancy/sync_target.rb', line 10

def initialize(config:, imperium_config:)
  if not config.is_a? Hash
    raise Constancy::ConfigFileInvalid.new("Sync target entries must be specified as hashes")
  end

  if (config.keys - Constancy::SyncTarget::VALID_CONFIG_KEYS) != []
    raise Constancy::ConfigFileInvalid.new("Only the following keys are valid in a sync target entry: #{Constancy::SyncTarget::VALID_CONFIG_KEYS.join(", ")}")
  end

  if (Constancy::SyncTarget::REQUIRED_CONFIG_KEYS - config.keys) != []
    raise Constancy::ConfigFileInvalid.new("The following keys are required for a sync target entry: #{Constancy::SyncTarget::REQUIRED_CONFIG_KEYS.join(", ")}")
  end

  self.datacenter = config['datacenter']
  self.prefix = config['prefix']
  self.path = config['path'] || config['prefix']
  self.name = config['name']
  self.exclude = config['exclude'] || []
  if config.has_key?('chomp')
    @do_chomp = config['chomp'] ? true : false
  end
  if config.has_key?('delete')
    @do_delete = config['delete'] ? true : false
  else
    @do_delete = false
  end

  self.consul = Imperium::KV.new(imperium_config)
end

Instance Attribute Details

#consulObject

Returns the value of attribute consul.



6
7
8
# File 'lib/constancy/sync_target.rb', line 6

def consul
  @consul
end

#datacenterObject

Returns the value of attribute datacenter.



6
7
8
# File 'lib/constancy/sync_target.rb', line 6

def datacenter
  @datacenter
end

#excludeObject

Returns the value of attribute exclude.



6
7
8
# File 'lib/constancy/sync_target.rb', line 6

def exclude
  @exclude
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/constancy/sync_target.rb', line 6

def name
  @name
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/constancy/sync_target.rb', line 6

def path
  @path
end

#prefixObject

Returns the value of attribute prefix.



6
7
8
# File 'lib/constancy/sync_target.rb', line 6

def prefix
  @prefix
end

Instance Method Details

#any_changes?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/constancy/sync_target.rb', line 168

def any_changes?
  self.items_to_change.count > 0
end

#base_dirObject



59
60
61
# File 'lib/constancy/sync_target.rb', line 59

def base_dir
  @base_dir ||= File.join(Constancy.config.base_dir, self.path)
end

#chomp?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/constancy/sync_target.rb', line 40

def chomp?
  @do_chomp
end

#clear_cacheObject



52
53
54
55
56
57
# File 'lib/constancy/sync_target.rb', line 52

def clear_cache
  @base_dir = nil
  @local_files = nil
  @local_items = nil
  @remote_items = nil
end

#delete?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/constancy/sync_target.rb', line 44

def delete?
  @do_delete
end

#descriptionObject



48
49
50
# File 'lib/constancy/sync_target.rb', line 48

def description
  "#{self.name.nil? ? '' : self.name.bold + "\n"}#{'local'.blue}:#{self.path} => #{'consul'.cyan}:#{self.datacenter.green}:#{self.prefix}"
end

#diffObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
# File 'lib/constancy/sync_target.rb', line 97

def diff
  local = self.local_items
  remote = self.remote_items
  all_keys = (local.keys + remote.keys).sort.uniq

  all_keys.collect do |key|
    excluded = false
    op = :noop
    if remote.has_key?(key) and not local.has_key?(key)
      if self.delete?
        op = :delete
      else
        op = :ignore
      end
    elsif local.has_key?(key) and not remote.has_key?(key)
      op = :create
    else
      if remote[key] == local[key]
        op = :noop
      else
        op = :update
      end
    end

    consul_key = [self.prefix, key].compact.join("/")

    if self.exclude.include?(key) or self.exclude.include?(consul_key)
      op = :ignore
      excluded = true
    end

    {
      :op => op,
      :excluded => excluded,
      :relative_path => key,
      :filename => File.join(self.base_dir, key),
      :consul_key => consul_key,
      :local_content => local[key],
      :remote_content => remote[key],
    }
  end
end

#items_to_changeObject



164
165
166
# File 'lib/constancy/sync_target.rb', line 164

def items_to_change
  self.diff.select { |d| [:delete, :update, :create].include?(d[:op]) }
end

#items_to_createObject



148
149
150
# File 'lib/constancy/sync_target.rb', line 148

def items_to_create
  self.diff.select { |d| d[:op] == :create }
end

#items_to_deleteObject



140
141
142
# File 'lib/constancy/sync_target.rb', line 140

def items_to_delete
  self.diff.select { |d| d[:op] == :delete }
end

#items_to_excludeObject



156
157
158
# File 'lib/constancy/sync_target.rb', line 156

def items_to_exclude
  self.diff.select { |d| d[:op] == :ignore and d[:excluded] == true }
end

#items_to_ignoreObject



152
153
154
# File 'lib/constancy/sync_target.rb', line 152

def items_to_ignore
  self.diff.select { |d| d[:op] == :ignore }
end

#items_to_noopObject



160
161
162
# File 'lib/constancy/sync_target.rb', line 160

def items_to_noop
  self.diff.select { |d| d[:op] == :noop }
end

#items_to_updateObject



144
145
146
# File 'lib/constancy/sync_target.rb', line 144

def items_to_update
  self.diff.select { |d| d[:op] == :update }
end

#local_filesObject



63
64
65
66
# File 'lib/constancy/sync_target.rb', line 63

def local_files
  # see https://stackoverflow.com/questions/357754/can-i-traverse-symlinked-directories-in-ruby-with-a-glob
  @local_files ||= Dir["#{self.base_dir}/**{,/*/**}/*"].select { |f| File.file?(f) }
end

#local_itemsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/constancy/sync_target.rb', line 68

def local_items
  return @local_items if not @local_items.nil?
  @local_items = {}

  self.local_files.each do |local_file|
    @local_items[local_file.sub(%r{^#{self.base_dir}/?}, '')] = if self.chomp?
                                                                  File.read(local_file).chomp.force_encoding(Encoding::ASCII_8BIT)
                                                                else
                                                                  File.read(local_file).force_encoding(Encoding::ASCII_8BIT)
                                                                end
  end

  @local_items
end


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
207
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/constancy/sync_target.rb', line 172

def print_report
  puts '='*85
  puts self.description

  puts "  Keys scanned: #{self.diff.count}"
  if Constancy.config.verbose?
    puts "  Keys ignored: #{self.items_to_ignore.count}"
    puts "  Keys in sync: #{self.items_to_noop.count}"
  end

  puts if self.any_changes?

  self.diff.each do |item|
    case item[:op]
    when :create
      puts "CREATE".bold.green + " #{item[:consul_key]}"
      puts '-'*85
      # simulate diff but without complaints about line endings
      item[:local_content].each_line do |line|
        puts "+#{line.chomp}".green
      end
      puts '-'*85

    when :update
      puts "UPDATE".bold + " #{item[:consul_key]}"
      puts '-'*85
      puts Diffy::Diff.new(item[:remote_content], item[:local_content]).to_s(:color)
      puts '-'*85

    when :delete
      if self.delete?
        puts "DELETE".bold.red + " #{item[:consul_key]}"
        puts '-'*85
        # simulate diff but without complaints about line endings
        item[:remote_content].each_line do |line|
          puts "-#{line.chomp}".red
        end
        puts '-'*85
      else
        if Constancy.config.verbose?
          puts "IGNORE".bold + " #{item[:consul_key]}"
        end
      end

    when :ignore
      if Constancy.config.verbose?
        puts "IGNORE".bold + " #{item[:consul_key]}"
      end

    when :noop
      if Constancy.config.verbose?
        puts "NO-OP!".bold + " #{item[:consul_key]}"
      end

    else
      if Constancy.config.verbose?
        STDERR.puts "WARNING: unexpected operation '#{item[:op]}' for #{item[:consul_key]}"
      end

    end
  end

  if self.items_to_create.count > 0
    puts
    puts "Keys to create: #{self.items_to_create.count}".bold
    self.items_to_create.each do |item|
      puts "+ #{item[:consul_key]}".green
    end
  end

  if self.items_to_update.count > 0
    puts
    puts "Keys to update: #{self.items_to_update.count}".bold
    self.items_to_update.each do |item|
      puts "~ #{item[:consul_key]}".blue
    end
  end

  if self.delete?
    if self.items_to_delete.count > 0
      puts
      puts "Keys to delete: #{self.items_to_delete.count}".bold
      self.items_to_delete.each do |item|
        puts "- #{item[:consul_key]}".red
      end
    end
  end
end

#remote_itemsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/constancy/sync_target.rb', line 83

def remote_items
  return @remote_items if not @remote_items.nil?
  @remote_items = {}

  resp = self.consul.get(self.prefix, :recurse, dc: self.datacenter)

  return @remote_items if resp.values.nil?
  Constancy::Util.flatten_hash(resp.values).each_pair do |key, value|
    @remote_items[key.join("/")] = (value.nil? ? '' : value)
  end

  @remote_items
end