config_skeleton 0.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bdaa156de0445e8a32b2d49d55ea11308d153d4c73658f6c4ff648a7c32a1440
4
- data.tar.gz: 5dff0715f11dad930b6dd485204d9a1f2b75a47afc9096cb33dd0c4f5a538423
3
+ metadata.gz: 99dfaca5fbeaf8f2bd6c5fd921e854294cd1f01be8e27613ad58e7581e7b1bf5
4
+ data.tar.gz: 552d8592225a20807716bd74ab8eaf1866b25a1d8b7acec75eedb98d22cb95cf
5
5
  SHA512:
6
- metadata.gz: e5c7ffc7621e69441f17349d9e46a9d4f5146ed88a4c5b382524dfd64b7c0aa6b4aedc72bcb3ffac220509299983adff24263f86e8a327072ed34b933095ab3e
7
- data.tar.gz: 50e06b7b3fbe40c4baf522b95595bd447ec3e349c2e14a1cbf7da64500f2db5a64d006ec6b9f15a2320fdbc76f879f733d91ff555c26d7eef7432d1fc46ff059
6
+ metadata.gz: 4748a0d790e28a991555bf8647d79c71f827ba41d7d9a48156d9783a1e8e7ddefbe1acb4417c92667cb128e9c542b1e2aaa000d4adafa68aaf0a058d1e620d2d
7
+ data.tar.gz: 8cd5d2474c55a59255d53c1ed87bfb5c1a5bebe8df32117f9c49c4486ba1647a263c397606a0afb5fd67e22edaffd2df6c436ee8fb866b4bb066ff3d395960f9
@@ -43,6 +43,6 @@ jobs:
43
43
  - uses: actions/checkout@v2
44
44
 
45
45
  - name: Release Gem
46
- uses: CvX/publish-rubygems-action@master
46
+ uses: discourse/publish-rubygems-action@main
47
47
  env:
48
48
  RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Gem::Specification.new do |s|
2
4
  s.name = "config_skeleton"
3
5
 
4
- s.version = "0.3.0"
6
+ s.version = "2.0.0"
5
7
 
6
8
  s.platform = Gem::Platform::RUBY
7
9
 
8
10
  s.summary = "Dynamically generate configs and reload servers"
9
11
 
10
- s.authors = ["Matt Palmer"]
11
- s.email = ["matt.palmer@discourse.org"]
12
+ s.authors = ["Matt Palmer", "Discourse Team"]
13
+ s.email = ["matt.palmer@discourse.org", "team@discourse.org"]
12
14
  s.homepage = "https://github.com/discourse/config_skeleton"
13
15
 
14
16
  s.files = `git ls-files -z`.split("\0").reject { |f| f =~ /^(G|spec|Rakefile)/ }
@@ -16,15 +18,15 @@ Gem::Specification.new do |s|
16
18
  s.required_ruby_version = ">= 2.3.0"
17
19
 
18
20
  s.add_runtime_dependency 'diffy', '~> 3.0'
19
- s.add_runtime_dependency 'frankenstein', '~> 1.0'
20
21
  s.add_runtime_dependency 'rb-inotify', '~> 0.9'
21
- s.add_runtime_dependency 'service_skeleton', '> 0.a'
22
+ s.add_runtime_dependency 'service_skeleton', "~> 2.0"
22
23
 
23
24
  s.add_development_dependency 'bundler'
24
25
  s.add_development_dependency 'github-release'
25
26
  s.add_development_dependency 'rake', "~> 12.0"
26
27
  s.add_development_dependency 'redcarpet'
27
28
  s.add_development_dependency 'rubocop'
29
+ s.add_development_dependency 'rubocop-discourse'
28
30
  s.add_development_dependency 'yard'
29
31
  s.add_development_dependency 'rspec'
30
32
  s.add_development_dependency 'pry'
@@ -1,10 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'diffy'
2
4
  require 'fileutils'
3
5
  require 'frankenstein'
4
6
  require 'logger'
5
- require 'rb-inotify'
6
7
  require 'service_skeleton'
7
8
  require 'tempfile'
9
+ require 'digest/md5'
10
+
11
+ begin
12
+ require 'rb-inotify' unless ENV["DISABLE_INOTIFY"]
13
+ rescue FFI::NotFoundError => e
14
+ STDERR.puts "ERROR: Unable to initialize rb-inotify. To disable, set DISABLE_INOTIFY=1"
15
+ raise
16
+ end
8
17
 
9
18
  # Framework for creating config generation systems.
10
19
  #
@@ -23,19 +32,18 @@ require 'tempfile'
23
32
  #
24
33
  # 1. Implement service-specific config generation and reloading code, by
25
34
  # overriding the private methods #config_file, #config_data, and #reload_server
26
- # (and also potentially #config_ok? and #sleep_duration).
35
+ # (and also potentially #config_ok?, #sleep_duration, #before_regenerate_config, and #after_regenerate_config).
27
36
  # See the documentation for those methods for what they need to do.
28
37
  #
29
38
  # 1. Setup any file watchers you want with .watch and #watch.
30
39
  #
31
- # 1. Instantiate your new class, passing in an environment hash, and then call
32
- # #start. Something like this should do the trick:
40
+ # 1. Use the ServiceSkeleton Runner to start the service. Something like this should do the trick:
33
41
  #
34
42
  # class MyConfigGenerator < ConfigSkeleton
35
43
  # # Implement all the necessary methods
36
44
  # end
37
45
  #
38
- # MyConfigGenerator.new(ENV).start if __FILE__ == $0
46
+ # ServiceSkeleton::Runner.new(MyConfigGenerator, ENV).run if __FILE__ == $0
39
47
  #
40
48
  # 1. Sit back and relax.
41
49
  #
@@ -135,7 +143,8 @@ require 'tempfile'
135
143
  # method, passing one or more strings containing the full path to files or
136
144
  # directories to watch.
137
145
  #
138
- class ConfigSkeleton < ServiceSkeleton
146
+ class ConfigSkeleton
147
+ include ServiceSkeleton
139
148
  # All ConfigSkeleton-related errors will be subclasses of this.
140
149
  class Error < StandardError; end
141
150
 
@@ -154,6 +163,19 @@ class ConfigSkeleton < ServiceSkeleton
154
163
  end
155
164
  end
156
165
 
166
+ def self.inherited(klass)
167
+ klass.gauge :"#{klass.service_name}_last_generation_timestamp", docstring: "When the last config generation run was made"
168
+ klass.gauge :"#{klass.service_name}_last_change_timestamp", docstring: "When the config file was last written to"
169
+ klass.counter :"#{klass.service_name}_reload_total", docstring: "How many times we've asked the server to reload", labels: [:status]
170
+ klass.counter :"#{klass.service_name}_signals_total", docstring: "How many signals have been received (and handled)"
171
+ klass.gauge :"#{klass.service_name}_config_ok", docstring: "Whether the last config change was accepted by the server"
172
+
173
+ klass.hook_signal("HUP") do
174
+ logger.info("SIGHUP") { "received SIGHUP, triggering config regeneration" }
175
+ @trigger_regen_w << "."
176
+ end
177
+ end
178
+
157
179
  # Declare a file watch on all instances of the config generator.
158
180
  #
159
181
  # When you're looking to watch a file whose path is well-known and never-changing, you
@@ -185,20 +207,8 @@ class ConfigSkeleton < ServiceSkeleton
185
207
  @watches || []
186
208
  end
187
209
 
188
- # Create a new config generator.
189
- #
190
- # @param env [Hash<String, String>] the environment in which this config
191
- # generator runs. Typically you'll just pass `ENV` in here, but you can
192
- # pass in any hash you like, for testing purposes.
193
- #
194
- def initialize(env)
210
+ def initialize(*_)
195
211
  super
196
-
197
- hook_signal(:HUP) do
198
- logger.info("SIGHUP") { "received SIGHUP, triggering config regeneration" }
199
- @trigger_regen_w << "."
200
- end
201
-
202
212
  initialize_config_skeleton_metrics
203
213
  @trigger_regen_r, @trigger_regen_w = IO.pipe
204
214
  @terminate_r, @terminate_w = IO.pipe
@@ -293,6 +303,7 @@ class ConfigSkeleton < ServiceSkeleton
293
303
  # @see .watch for watching files and directories whose path never changes.
294
304
  #
295
305
  def watch(*files)
306
+ return if ENV["DISABLE_INOTIFY"]
296
307
  files.each do |f|
297
308
  if File.directory?(f)
298
309
  notifier.watch(f, :recursive, :create, :modify, :delete, :move) { |ev| logger.info("#{logloc} watcher") { "detected #{ev.flags.join(", ")} on #{ev.watcher.path}/#{ev.name}; regenerating config" } }
@@ -304,22 +315,11 @@ class ConfigSkeleton < ServiceSkeleton
304
315
 
305
316
  private
306
317
 
307
- # Register metrics in the ServiceSkeleton metrics registry
308
- #
309
- # @return [void]
310
- #
311
318
  def initialize_config_skeleton_metrics
312
- @config_generation = Frankenstein::Request.new("#{service_name}_generation", outgoing: false, description: "config generation", registry: metrics)
313
-
314
- metrics.gauge(:"#{service_name}_last_generation_timestamp", "When the last config generation run was made")
315
- metrics.gauge(:"#{service_name}_last_change_timestamp", "When the config file was last written to")
316
- metrics.counter(:"#{service_name}_reload_total", "How many times we've asked the server to reload")
317
- metrics.counter(:"#{service_name}_signals_total", "How many signals have been received (and handled)")
318
- metrics.gauge(:"#{service_name}_config_ok", "Whether the last config change was accepted by the server")
319
-
320
- metrics.last_generation_timestamp.set({}, 0)
321
- metrics.last_change_timestamp.set({}, 0)
322
- metrics.config_ok.set({}, 0)
319
+ @config_generation = Frankenstein::Request.new("#{self.class.service_name}_generation", outgoing: false, description: "config generation", registry: metrics)
320
+ metrics.last_generation_timestamp.set(0)
321
+ metrics.last_change_timestamp.set(0)
322
+ metrics.config_ok.set(0)
323
323
  end
324
324
 
325
325
  # Write out a config file if one doesn't exist, or do an initial regen run
@@ -334,7 +334,7 @@ class ConfigSkeleton < ServiceSkeleton
334
334
  else
335
335
  logger.info(logloc) { "No existing config file #{config_file} found; writing one" }
336
336
  File.write(config_file, instrumented_config_data)
337
- metrics.last_change_timestamp.set({}, Time.now.to_f)
337
+ metrics.last_change_timestamp.set(Time.now.to_f)
338
338
  end
339
339
  end
340
340
 
@@ -362,6 +362,27 @@ class ConfigSkeleton < ServiceSkeleton
362
362
  raise NotImplementedError, "config_data must be implemented in subclass."
363
363
  end
364
364
 
365
+ # Run code before the config is regenerated and the config_file
366
+ # is written.
367
+ #
368
+ # @param force_reload [Boolean] Whether the regenerate_config was called with force_reload
369
+ # @param existing_config_hash [String] MD5 hash of the config file before regeneration.
370
+ #
371
+ # @note this can optionally be implemented by subclasses.
372
+ #
373
+ def before_regenerate_config(force_reload:, existing_config_hash:, existing_config_data:); end
374
+
375
+ # Run code after the config is regenerated and potentially a new file is written.
376
+ #
377
+ # @param force_reload [Boolean] Whether the regenerate_config was called with force_reload
378
+ # @param config_was_different [Boolean] Whether the diff of the old and new config was different.
379
+ # @param config_was_cycled [Boolean] Whether a new config file was cycled in.
380
+ # @param new_config_hash [String] MD5 hash of the new config file after write.
381
+ #
382
+ # @note this can optionally be implemented by subclasses.
383
+ #
384
+ def after_regenerate_config(force_reload:, config_was_different:, config_was_cycled:, new_config_hash:); end
385
+
365
386
  # Verify that the currently running config is acceptable.
366
387
  #
367
388
  # In the event that a generated config is "bad", it may be possible to detect
@@ -401,7 +422,7 @@ class ConfigSkeleton < ServiceSkeleton
401
422
  #
402
423
  def instrumented_config_data
403
424
  begin
404
- @config_generation.measure { config_data.tap { metrics.last_generation_timestamp.set({}, Time.now.to_f) } }
425
+ @config_generation.measure { config_data.tap { metrics.last_generation_timestamp.set(Time.now.to_f) } }
405
426
  rescue => ex
406
427
  log_exception(ex, logloc) { "Call to config_data raised exception" }
407
428
  nil
@@ -431,6 +452,9 @@ class ConfigSkeleton < ServiceSkeleton
431
452
  #
432
453
  def notifier
433
454
  @notifier ||= INotify::Notifier.new
455
+ rescue NameError
456
+ raise if !ENV["DISABLE_INOTIFY"]
457
+ @notifier ||= Struct.new(:to_io).new(IO.pipe[1]) # Stub for macOS development
434
458
  end
435
459
 
436
460
  # Do the hard yards of actually regenerating the config and performing the reload.
@@ -443,31 +467,54 @@ class ConfigSkeleton < ServiceSkeleton
443
467
  # @return [void]
444
468
  #
445
469
  def regenerate_config(force_reload: false)
470
+ data = File.read(config_file)
471
+ existing_config_hash = Digest::MD5.hexdigest(data)
472
+ before_regenerate_config(
473
+ force_reload: force_reload,
474
+ existing_config_hash: existing_config_hash,
475
+ existing_config_data: data
476
+ )
477
+
446
478
  logger.debug(logloc) { "force? #{force_reload.inspect}" }
447
- tmpfile = Tempfile.new(service_name, File.dirname(config_file))
479
+ tmpfile = Tempfile.new(self.class.service_name, File.dirname(config_file))
448
480
  logger.debug(logloc) { "Tempfile is #{tmpfile.path}" }
449
481
  unless (new_config = instrumented_config_data).nil?
450
482
  File.write(tmpfile.path, new_config)
451
483
  tmpfile.close
452
- logger.debug(logloc) { require 'digest/md5'; "Existing config hash: #{Digest::MD5.hexdigest(File.read(config_file))}, new config hash: #{Digest::MD5.hexdigest(File.read(tmpfile.path))}" }
484
+
485
+ new_config_hash = Digest::MD5.hexdigest(File.read(tmpfile.path))
486
+ logger.debug(logloc) do
487
+ "Existing config hash: #{existing_config_hash}, new config hash: #{new_config_hash}"
488
+ end
453
489
 
454
490
  match_perms(config_file, tmpfile.path)
455
491
 
456
- diff = Diffy::Diff.new(config_file, tmpfile.path, source: 'files', context: 3, include_diff_info: true)
457
- if diff.to_s != ""
458
- logger.info(logloc) { "Config has changed. Diff:\n#{diff.to_s}" }
492
+ diff = Diffy::Diff.new(config_file, tmpfile.path, source: 'files', context: 3, include_diff_info: true).to_s
493
+ config_was_different = diff != ""
494
+
495
+ if config_was_different
496
+ logger.info(logloc) { "Config has changed. Diff:\n#{diff}" }
459
497
  end
460
498
 
461
499
  if force_reload
462
500
  logger.debug(logloc) { "Forcing config reload because force_reload == true" }
463
501
  end
464
502
 
465
- if force_reload || diff.to_s != ""
503
+ config_was_cycled = false
504
+ if force_reload || config_was_different
466
505
  cycle_config(tmpfile.path)
506
+ config_was_cycled = true
467
507
  end
468
508
  end
509
+
510
+ after_regenerate_config(
511
+ force_reload: force_reload,
512
+ config_was_different: config_was_different,
513
+ config_was_cycled: config_was_cycled,
514
+ new_config_hash: new_config_hash
515
+ )
469
516
  ensure
470
- metrics.last_change_timestamp.set({}, File.stat(config_file).mtime.to_f)
517
+ metrics.last_change_timestamp.set(File.stat(config_file).mtime.to_f)
471
518
  tmpfile.close rescue nil
472
519
  tmpfile.unlink rescue nil
473
520
  end
@@ -517,7 +564,7 @@ class ConfigSkeleton < ServiceSkeleton
517
564
  logger.debug(logloc) { "Restored previous config file" }
518
565
  File.rename(old_copy, config_file)
519
566
  end
520
- metrics.reload_total.increment(status: "failure")
567
+ metrics.reload_total.increment(labels: { status: "failure" })
521
568
 
522
569
  return
523
570
  end
@@ -525,21 +572,21 @@ class ConfigSkeleton < ServiceSkeleton
525
572
  logger.debug(logloc) { "Server reloaded successfully" }
526
573
 
527
574
  if config_ok?
528
- metrics.config_ok.set({}, 1)
575
+ metrics.config_ok.set(1)
529
576
  logger.debug(logloc) { "Configuration successfully updated." }
530
- metrics.reload_total.increment(status: "success")
531
- metrics.last_change_timestamp.set({}, Time.now.to_f)
577
+ metrics.reload_total.increment(labels: { status: "success" })
578
+ metrics.last_change_timestamp.set(Time.now.to_f)
532
579
  else
533
- metrics.config_ok.set({}, 0)
580
+ metrics.config_ok.set(0)
534
581
  if config_was_ok
535
582
  logger.warn(logloc) { "New config file failed config_ok? test; rolling back to previous known-good config" }
536
583
  File.rename(old_copy, config_file)
537
584
  reload_server
538
- metrics.reload_total.increment(status: "bad-config")
585
+ metrics.reload_total.increment(labels: { status: "bad-config" })
539
586
  else
540
587
  logger.warn(logloc) { "New config file failed config_ok? test; leaving new config in place because old config is broken too" }
541
- metrics.reload_total.increment(status: "everything-is-awful")
542
- metrics.last_change_timestamp.set({}, Time.now.to_f)
588
+ metrics.reload_total.increment(labels: { status: "everything-is-awful" })
589
+ metrics.last_change_timestamp.set(Time.now.to_f)
543
590
  end
544
591
  end
545
592
  ensure
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_skeleton
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Palmer
8
+ - Discourse Team
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2020-11-26 00:00:00.000000000 Z
12
+ date: 2021-03-01 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: diffy
@@ -24,20 +25,6 @@ dependencies:
24
25
  - - "~>"
25
26
  - !ruby/object:Gem::Version
26
27
  version: '3.0'
27
- - !ruby/object:Gem::Dependency
28
- name: frankenstein
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.0'
41
28
  - !ruby/object:Gem::Dependency
42
29
  name: rb-inotify
43
30
  requirement: !ruby/object:Gem::Requirement
@@ -56,16 +43,16 @@ dependencies:
56
43
  name: service_skeleton
57
44
  requirement: !ruby/object:Gem::Requirement
58
45
  requirements:
59
- - - ">"
46
+ - - "~>"
60
47
  - !ruby/object:Gem::Version
61
- version: 0.a
48
+ version: '2.0'
62
49
  type: :runtime
63
50
  prerelease: false
64
51
  version_requirements: !ruby/object:Gem::Requirement
65
52
  requirements:
66
- - - ">"
53
+ - - "~>"
67
54
  - !ruby/object:Gem::Version
68
- version: 0.a
55
+ version: '2.0'
69
56
  - !ruby/object:Gem::Dependency
70
57
  name: bundler
71
58
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +123,20 @@ dependencies:
136
123
  - - ">="
137
124
  - !ruby/object:Gem::Version
138
125
  version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rubocop-discourse
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
139
140
  - !ruby/object:Gem::Dependency
140
141
  name: yard
141
142
  requirement: !ruby/object:Gem::Requirement
@@ -195,6 +196,7 @@ dependencies:
195
196
  description:
196
197
  email:
197
198
  - matt.palmer@discourse.org
199
+ - team@discourse.org
198
200
  executables: []
199
201
  extensions: []
200
202
  extra_rdoc_files: []