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)


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

def self.exit_on_failure?
  true
end

Instance Method Details

#applyObject



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

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



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

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



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/record_store/cli.rb', line 65

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



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

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



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

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

#infoObject



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

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



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

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

#reformatObject



171
172
173
174
175
176
177
178
179
# File 'lib/record_store/cli.rb', line 171

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



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

def secrets
  environment = begin
    if ENV['PRODUCTION']
      'production'
    elsif ENV['CI']
      'ci'
    else
      'dev'
    end
  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



183
184
185
186
187
188
189
190
191
192
# File 'lib/record_store/cli.rb', line 183

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



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

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



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

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



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

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



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

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



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

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:"
    zone.errors.each do |field, msg|
      puts " - #{field}: #{msg}"
    end

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

    invalid_records.each do |record|
      puts "    #{record}"
      record.errors.each do |field, msg|
        puts "      - #{field}: #{msg}"
      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