Class: RecordStore::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/record_store/cli.rb

Constant Summary collapse

FORMATS =
%w[file directory]
SKIP_CHECKS =
'SKIP_DEPLOY_VALIDATIONS'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



8
9
10
11
# File 'lib/record_store/cli.rb', line 8

def initialize(*args)
  super
  RecordStore.config_path = options.fetch('config', "#{Dir.pwd}/config.yml")
end

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/record_store/cli.rb', line 14

def exit_on_failure?
  true
end

Instance Method Details

#applyObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/record_store/cli.rb', line 125

def apply
  zones = Zone.modified

  if zones.empty?
    puts "No changes to sync"
    exit
  end

  invalid_zones = zones.select(&:invalid?)
  unless invalid_zones.empty?
    error_message = invalid_zones
      .map { |z| "Attempted to apply invalid zone: #{z.name}: #{z.errors.full_messages.join(',')}" }
      .join("\n")

    abort(error_message)
  end

  zones.each do |zone|
    changesets = zone.build_changesets
    changesets.each(&:apply)
  end

  puts "All zone changes deployed"
end

#assert_empty_diffObject



216
217
218
219
220
221
222
# File 'lib/record_store/cli.rb', line 216

def assert_empty_diff
  zones = Zone.modified.map(&:name)

  unless zones.empty?
    abort("The following zones have diverged: #{zones.join(', ')}")
  end
end

#diff(*zones) ⇒ Object



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
96
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
# File 'lib/record_store/cli.rb', line 67

def diff(*zones)
  puts "Diffing #{zones.any? ? zones.count : Zone.defined.count} zone(s)"

  all = options.fetch('all')

  Zone.each do |name, zone|
    next unless zones.empty? || zones.include?(name)

    changesets = zone.build_changesets(all: all)

    if !options.fetch('verbose') && changesets.all?(&:empty?)
      print_and_flush('.')
      next
    else
      puts "\n"
      puts "Zone: #{name}"
    end

    changesets.each do |changeset|
      next if !options.fetch('verbose') && changeset.changes.empty?

      puts '-' * 20
      puts "Provider: #{changeset.provider}"

      if changeset.additions.any?
        puts "Add:"
        changeset.additions.map(&:record).each do |record|
          puts " - #{record}"
        end
      end

      if changeset.removals.any?
        puts "Remove:"
        changeset.removals.map(&:record).each do |record|
          puts " - #{record}"
        end
      end

      if changeset.updates.any?
        puts "Update:"
        changeset.updates.map(&:record).each do |record|
          puts " - #{record}"
        end
      end

      next unless options.fetch('verbose')

      puts "Unchanged:"
      changeset.unchanged.each do |record|
        puts " - #{record}"
      end
    end
    puts '=' * 20
  end
  puts "\n"
end

#downloadObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/record_store/cli.rb', line 154

def download
  name = options.fetch('name')
  abort('Please omit the period at the end of the zone') if name.ends_with?('.')
  abort('Zone with this name already exists in zones/') if File.exist?("#{RecordStore.zones_path}/#{name}.yml")

  provider = options.fetch('provider', Provider.provider_for(name))
  if provider.nil?
    puts "Could not find valid provider from #{name} SOA record"
    provider = ask("Please enter the provider in which #{name} exists")
  else
    puts "Identified #{provider} as the DNS provider"
  end

  puts "Downloading records for #{name}"
  Zone.download(name, provider)
  puts "Records have been downloaded & can be found in zones/#{name}.yml"
end

#freezeObject



29
30
31
32
33
34
35
# File 'lib/record_store/cli.rb', line 29

def freeze
  Zone.each do |_, zone|
    zone.providers.each do |provider|
      provider.freeze_zone(zone.unrooted_name) if provider.freezable?
    end
  end
end

#infoObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/record_store/cli.rb', line 39

def info
  Zone.each do |name, zone|
    puts "\n"
    puts "Zone: #{name}"
    puts "Providers:"
    zone.config.providers.each { |p| puts "- #{p}" }
    if (delegation = zone.fetch_authority)
      puts "Authoritative nameservers:"
      delegation.each { |d| puts "- #{d}" }
    else
      $stderr.puts "ERROR: Unable to determine delegation (#{name})"
    end
  end
end

#listObject



56
57
58
59
60
61
62
# File 'lib/record_store/cli.rb', line 56

def list
  Zone.each do |name, zone|
    puts "Zone: #{name}"
    records = options.fetch('all') ? zone.all : zone.records
    records.each(&:log!)
  end
end

#reformatObject



174
175
176
177
178
179
180
181
182
# File 'lib/record_store/cli.rb', line 174

def reformat
  name = options['name']
  zones = name ? [Zone.find(name)] : Zone.all

  zones.each do |zone|
    puts "Writing #{zone.name}"
    zone.write
  end
end

#secretsObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/record_store/cli.rb', line 198

def secrets
  environment = if ENV['PRODUCTION']
    'production'
  elsif ENV['CI']
    'ci'
  else
    'dev'
  end

  secrets = %x(ejson decrypt #{RecordStore.secrets_path.sub(/\.json\z/, ".#{environment}.ejson")})
  if $CHILD_STATUS.success?
    File.write(RecordStore.secrets_path, secrets)
  else
    abort(secrets)
  end
end

#sortObject



186
187
188
189
190
191
192
193
194
195
# File 'lib/record_store/cli.rb', line 186

def sort
  name = options.fetch('name')
  abort("Please omit the period at the end of the zone") if name.ends_with?('.')

  yaml = YAML.load_file("#{RecordStore.zones_path}/#{name}.yml")
  yaml.fetch(name).fetch('records').sort_by! do |r|
    [r.fetch('fqdn'), r.fetch('type'), r['nsdname'] || r['address']]
  end
  File.write("#{RecordStore.zones_path}/#{name}.yml", yaml.deep_stringify_keys.to_yaml.gsub("---\n", ''))
end

#thawObject



20
21
22
23
24
25
26
# File 'lib/record_store/cli.rb', line 20

def thaw
  Zone.each do |_, zone|
    zone.providers.each do |provider|
      provider.thaw_zone(zone.unrooted_name) if provider.thawable?
    end
  end
end

#validate_authority(*zones) ⇒ Object



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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/record_store/cli.rb', line 226

def validate_authority(*zones)
  verbose = options.fetch('verbose')

  Zone.each do |name, zone|
    next unless zones.empty? || zones.include?(name)

    authority = zone.fetch_authority

    delegation = Hash.new { |h, k| h[k] = [] }
    authority.each do |ns|
      delegation[Provider.provider_for(ns)] << ns
    end

    delegated = delegation.keys.sort
    configured = zone.config.providers.sort

    ok = configured & delegated
    missing = configured - delegated
    unconfigured = delegated - configured

    next if !verbose && missing.empty? && unconfigured.empty?

    puts "\n"
    puts "Zone: #{name}"

    if verbose
      ok.each do |provider|
        puts "- #{provider}:"
        delegation[provider].each do |ns|
          puts "  - #{ns.nsdname}"
        end
      end
    end

    missing.each do |provider|
      puts "- #{provider}: authoritative nameservers not found for configured provider"
    end

    unconfigured.each do |provider|
      if provider
        puts "- #{provider}: unexpected authoritative nameservers found"
      else
        puts "- Unknown: unknown authoritative nameservers found"
      end

      delegation[provider].each do |ns|
        puts "  - #{ns.nsdname}"
      end
    end
  end
end

#validate_change_sizeObject



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/record_store/cli.rb', line 308

def validate_change_size
  zones = Zone.modified

  unless zones.empty?
    removals = zones.select do |zone|
      zone.changeset.removals.size > MAXIMUM_REMOVALS
    end

    unless removals.empty?
      error = +"As a safety measure, you cannot remove more than #{MAXIMUM_REMOVALS} "
      error << 'records at a time per zone. '
      error << "(zones failing this: #{removals.map(&:name).join(', ')})"
      abort(error)
    end
  end
end

#validate_initial_stateObject



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/record_store/cli.rb', line 327

def validate_initial_state
  assert_empty_diff
  puts "Deploy will cause no changes, no need to validate initial state"
rescue SystemExit
  if File.exist?(File.expand_path(SKIP_CHECKS, Dir.pwd))
    puts "Found '#{SKIP_CHECKS}', skipping predeploy validations"
  else
    puts "Checkout git SHA #{ENV['LAST_DEPLOYED_SHA']}"
    %x(git checkout #{ENV['LAST_DEPLOYED_SHA']})
    abort("Checkout of old commit failed") unless $CHILD_STATUS.success?

    %x(record-store secrets)
    abort("Decrypt secrets failed") unless $CHILD_STATUS.success?

    %x(record-store assert_empty_diff)
    abort("Dyn status has diverged!") unless $CHILD_STATUS.success?

    puts "Checkout git SHA #{ENV['REVISION']}"
    %x(git checkout #{ENV['REVISION']})
    abort("Checkout of new commit failed") unless $CHILD_STATUS.success?

    %x(record-store secrets)
    abort("Decrypt secrets failed") unless $CHILD_STATUS.success?
  end
end

#validate_recordsObject



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
# File 'lib/record_store/cli.rb', line 279

def validate_records
  invalid_zones = []
  Zone.all.reject(&:valid?).each do |zone|
    invalid_zones << zone.unrooted_name

    puts "#{zone.unrooted_name} definition is not valid:"
    puts zone.errors.full_messages.map { |msg| " - #{msg}" }

    invalid_records = zone.records.reject(&:valid?)
    puts '  Invalid records' unless invalid_records.empty?

    invalid_records.each do |record|
      puts "    #{record}"
      record.errors.messages.each do |field, errors|
        errors.each do |msg|
          puts "      - #{field}: #{msg}"
        end
      end
    end
  end

  if invalid_zones.present?
    abort("The following zones were invalid: #{invalid_zones.join(', ')}")
  else
    puts "All zones have valid definitions."
  end
end