diff --git a/files/gitlab-cookbooks/consul/libraries/watch_helper.rb b/files/gitlab-cookbooks/consul/libraries/watch_helper.rb index 5193dd2ed9c7ff5defaeff3f266d6d98ea8aa6a3_ZmlsZXMvZ2l0bGFiLWNvb2tib29rcy9jb25zdWwvbGlicmFyaWVzL3dhdGNoX2hlbHBlci5yYg==..7109ae4cebc9d7a9d03712d4e90139f4955bcc54_ZmlsZXMvZ2l0bGFiLWNvb2tib29rcy9jb25zdWwvbGlicmFyaWVzL3dhdGNoX2hlbHBlci5yYg== 100644 --- a/files/gitlab-cookbooks/consul/libraries/watch_helper.rb +++ b/files/gitlab-cookbooks/consul/libraries/watch_helper.rb @@ -1,6 +1,8 @@ require 'json' module WatchHelper + WATCHER_FILENAME_PREFIX = 'watcher_'.freeze + class Watcher attr_reader :name, :handler_script, :handler_template, :type, :consul_config_file, :template_variables, :service_name @@ -9,7 +11,7 @@ @handler_script = handler_script @handler_template = handler_template @type = type - @consul_config_file = "#{consul_watch_config_directory}/watcher_#{name}.json" + @consul_config_file = "#{consul_watch_config_directory}/#{WATCHER_FILENAME_PREFIX}#{name}.json" @service_name = "service:#{name}" @template_variables = template_variables.merge({ "watcher_service_name" => @service_name }) end @@ -73,6 +75,6 @@ @all_watchers.select { |watcher| @enabled_watchers.include? watcher.name } end - def excess_configs - enabled_configs = watchers.map { |w| File.basename w.consul_config_file } + def excess_watcher_configs + enabled_watcher_configs = watchers.map { |w| File.basename w.consul_config_file } Dir.glob("#{@consul_config_directory}/*") @@ -78,5 +80,6 @@ Dir.glob("#{@consul_config_directory}/*") - .reject { |c| enabled_configs.include? c } + .reject { |f| !File.basename(f).start_with? WATCHER_FILENAME_PREFIX } + .reject { |f| enabled_watcher_configs.include? f } end def excess_handler_scripts diff --git a/files/gitlab-cookbooks/consul/recipes/watchers.rb b/files/gitlab-cookbooks/consul/recipes/watchers.rb index 5193dd2ed9c7ff5defaeff3f266d6d98ea8aa6a3_ZmlsZXMvZ2l0bGFiLWNvb2tib29rcy9jb25zdWwvcmVjaXBlcy93YXRjaGVycy5yYg==..7109ae4cebc9d7a9d03712d4e90139f4955bcc54_ZmlsZXMvZ2l0bGFiLWNvb2tib29rcy9jb25zdWwvcmVjaXBlcy93YXRjaGVycy5yYg== 100644 --- a/files/gitlab-cookbooks/consul/recipes/watchers.rb +++ b/files/gitlab-cookbooks/consul/recipes/watchers.rb @@ -18,7 +18,7 @@ # Remove excess watcher configurations and handlers to_cleanup = watch_helper.excess_handler_scripts -to_cleanup += watch_helper.excess_configs +to_cleanup += watch_helper.excess_watcher_configs to_cleanup.each do |f| file f do diff --git a/spec/chef/cookbooks/consul/recipes/consul_watcher_spec.rb b/spec/chef/cookbooks/consul/recipes/consul_watcher_spec.rb index 5193dd2ed9c7ff5defaeff3f266d6d98ea8aa6a3_c3BlYy9jaGVmL2Nvb2tib29rcy9jb25zdWwvcmVjaXBlcy9jb25zdWxfd2F0Y2hlcl9zcGVjLnJi..7109ae4cebc9d7a9d03712d4e90139f4955bcc54_c3BlYy9jaGVmL2Nvb2tib29rcy9jb25zdWwvcmVjaXBlcy9jb25zdWxfd2F0Y2hlcl9zcGVjLnJi 100644 --- a/spec/chef/cookbooks/consul/recipes/consul_watcher_spec.rb +++ b/spec/chef/cookbooks/consul/recipes/consul_watcher_spec.rb @@ -4,8 +4,9 @@ let(:chef_run) { ChefSpec::SoloRunner.converge('gitlab-ee::default') } let(:watcher_conf) { '/var/opt/gitlab/consul/config.d/watcher_postgresql.json' } let(:watcher_check) { '/var/opt/gitlab/consul/scripts/failover_postgresql_in_pgbouncer' } + let(:any_service_conf) { '/var/opt/gitlab/consul/config.d/any_service.conf' } let(:excess_watcher_conf) { '/var/opt/gitlab/consul/config.d/watcher_postgresql_old.json' } let(:excess_watcher_check) { '/var/opt/gitlab/consul/scripts/failover_postgresql_old_in_pgbouncer' } def stub_excess_consul_config_files allow(Dir).to receive(:glob).with(anything).and_call_original @@ -7,9 +8,9 @@ let(:excess_watcher_conf) { '/var/opt/gitlab/consul/config.d/watcher_postgresql_old.json' } let(:excess_watcher_check) { '/var/opt/gitlab/consul/scripts/failover_postgresql_old_in_pgbouncer' } def stub_excess_consul_config_files allow(Dir).to receive(:glob).with(anything).and_call_original - allow(Dir).to receive(:glob).with("/var/opt/gitlab/consul/config.d/*").and_return [excess_watcher_conf] + allow(Dir).to receive(:glob).with("/var/opt/gitlab/consul/config.d/*").and_return [excess_watcher_conf, any_service_conf] allow(Dir).to receive(:glob).with("/var/opt/gitlab/consul/scripts/*").and_return [excess_watcher_check] end @@ -48,6 +49,7 @@ it 'removes config and watch files of removed watchers' do expect(chef_run).to delete_file(excess_watcher_conf) expect(chef_run).to delete_file(excess_watcher_check) + expect(chef_run).to_not delete_file(any_service_conf) end it 'creates the watcher handler file' do