From 1c83271b7bf069c971050fdd1d166dae809fe115 Mon Sep 17 00:00:00 2001 From: paulczar Date: Sun, 31 Aug 2014 13:45:30 -0500 Subject: [PATCH] fixes for service_restart for issue https://github.com/lusis/chef-logstash/issues/330 --- .gitignore | 3 +- Gemfile.lock | 248 ------------------ providers/config.rb | 3 +- providers/service.rb | 78 +++--- recipes/agent.rb | 1 + recipes/server.rb | 5 +- .../server/serverspec/server_spec.rb | 2 +- test/unit/spec/agent_spec.rb | 1 - test/unit/spec/server_spec.rb | 1 - 9 files changed, 49 insertions(+), 293 deletions(-) delete mode 100644 Gemfile.lock diff --git a/.gitignore b/.gitignore index aa73350..a5d5527 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ vendor/ .kitchen/ .kitchen.local.yml .bundle/ -bin/ \ No newline at end of file +bin/ +Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index f6c303a..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,248 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - addressable (2.3.6) - ast (2.0.0) - berkshelf (3.1.3) - addressable (~> 2.3.4) - berkshelf-api-client (~> 1.2) - buff-config (~> 1.0) - buff-extensions (~> 1.0) - buff-shell_out (~> 0.1) - celluloid (~> 0.16.0.pre) - celluloid-io (~> 0.16.0.pre) - faraday (~> 0.9.0) - minitar (~> 0.5.4) - octokit (~> 3.0) - retryable (~> 1.3.3) - ridley (~> 4.0) - solve (~> 1.1) - thor (~> 0.18) - berkshelf-api-client (1.2.0) - faraday (~> 0.9.0) - buff-config (1.0.0) - buff-extensions (~> 1.0) - varia_model (~> 0.4) - buff-extensions (1.0.0) - buff-ignore (1.1.1) - buff-ruby_engine (0.1.0) - buff-shell_out (0.1.1) - buff-ruby_engine (~> 0.1.0) - celluloid (0.16.0.pre2) - timers (~> 3.0.0) - celluloid-io (0.16.0.pre2) - celluloid (>= 0.16.0.pre) - nio4r (>= 1.0.0) - chef (11.12.8) - chef-zero (>= 2.0.2, < 2.1) - diff-lcs (~> 1.2, >= 1.2.4) - erubis (~> 2.7) - highline (~> 1.6, >= 1.6.9) - json (>= 1.4.4, <= 1.8.1) - mime-types (~> 1.16) - mixlib-authentication (~> 1.3) - mixlib-cli (~> 1.4) - mixlib-config (~> 2.0) - mixlib-log (~> 1.3) - mixlib-shellout (~> 1.4) - net-ssh (~> 2.6) - net-ssh-multi (~> 1.1) - ohai (~> 7.0.4) - pry (~> 0.9) - rest-client (>= 1.0.4, < 1.7.0) - yajl-ruby (~> 1.1) - chef-zero (2.0.2) - hashie (~> 2.0) - json - mixlib-log (~> 1.3) - rack - chefspec (3.4.0) - chef (~> 11.0) - fauxhai (~> 2.0) - rspec (~> 2.14) - coderay (1.1.0) - dep-selector-libgecode (1.0.2) - dep_selector (1.0.3) - dep-selector-libgecode (~> 1.0) - ffi (~> 1.9) - diff-lcs (1.2.5) - erubis (2.7.0) - faraday (0.9.0) - multipart-post (>= 1.2, < 3) - fauxhai (2.1.2) - net-ssh - ohai - ffi (1.9.3) - foodcritic (3.0.3) - erubis - gherkin (~> 2.11.7) - nokogiri (~> 1.5.4) - rake - treetop (~> 1.4.10) - yajl-ruby (~> 1.1.0) - formatador (0.2.5) - gherkin (2.11.8) - multi_json (~> 1.3) - guard (2.6.1) - formatador (>= 0.2.4) - listen (~> 2.7) - lumberjack (~> 1.0) - pry (>= 0.9.12) - thor (>= 0.18.1) - guard-foodcritic (1.0.2) - foodcritic (>= 1.3, < 4.0) - guard (>= 1.0, < 3.0) - guard-rubocop (1.1.0) - guard (~> 2.0) - rubocop (~> 0.20) - hashie (2.1.2) - highline (1.6.21) - hitimes (1.2.1) - ipaddress (0.8.0) - json (1.8.1) - kitchen-vagrant (0.15.0) - test-kitchen (~> 1.0) - listen (2.7.9) - celluloid (>= 0.15.2) - rb-fsevent (>= 0.9.3) - rb-inotify (>= 0.9) - lumberjack (1.0.9) - method_source (0.8.2) - mime-types (1.25.1) - minitar (0.5.4) - mixlib-authentication (1.3.0) - mixlib-log - mixlib-cli (1.5.0) - mixlib-config (2.1.0) - mixlib-log (1.6.0) - mixlib-shellout (1.4.0) - multi_json (1.10.1) - multipart-post (2.0.0) - net-http-persistent (2.9.4) - net-scp (1.2.1) - net-ssh (>= 2.6.5) - net-ssh (2.9.1) - net-ssh-gateway (1.2.0) - net-ssh (>= 2.6.5) - net-ssh-multi (1.2.0) - net-ssh (>= 2.6.5) - net-ssh-gateway (>= 1.2.0) - nio4r (1.0.0) - nokogiri (1.5.11) - octokit (3.2.0) - sawyer (~> 0.5.3) - ohai (7.0.4) - ipaddress - mime-types (~> 1.16) - mixlib-cli - mixlib-config (~> 2.0) - mixlib-log - mixlib-shellout (~> 1.2) - systemu (~> 2.5.2) - yajl-ruby - parser (2.1.9) - ast (>= 1.1, < 3.0) - slop (~> 3.4, >= 3.4.5) - polyglot (0.3.5) - powerpack (0.0.9) - pry (0.10.0) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) - rack (1.5.2) - rainbow (2.0.0) - rake (10.3.2) - rb-fsevent (0.9.4) - rb-inotify (0.9.5) - ffi (>= 0.5.0) - rdoc (4.1.1) - json (~> 1.4) - rest-client (1.6.8) - mime-types (~> 1.16) - rdoc (>= 2.4.2) - retryable (1.3.5) - ridley (4.0.0) - addressable - buff-config (~> 1.0) - buff-extensions (~> 1.0) - buff-ignore (~> 1.1) - buff-shell_out (~> 0.1) - celluloid (~> 0.16.0.pre) - celluloid-io (~> 0.16.0.pre) - erubis - faraday (~> 0.9.0) - hashie (>= 2.0.2, < 3.0.0) - json (>= 1.7.7) - mixlib-authentication (>= 1.3.0) - net-http-persistent (>= 2.8) - retryable - semverse (~> 1.1) - varia_model (~> 0.4) - rspec (2.99.0) - rspec-core (~> 2.99.0) - rspec-expectations (~> 2.99.0) - rspec-mocks (~> 2.99.0) - rspec-core (2.99.1) - rspec-expectations (2.99.1) - diff-lcs (>= 1.1.3, < 2.0) - rspec-its (1.0.1) - rspec-core (>= 2.99.0.beta1) - rspec-expectations (>= 2.99.0.beta1) - rspec-mocks (2.99.1) - rubocop (0.23.0) - json (>= 1.7.7, < 2) - parser (~> 2.1.9) - powerpack (~> 0.0.6) - rainbow (>= 1.99.1, < 3.0) - ruby-progressbar (~> 1.4) - ruby-progressbar (1.5.1) - safe_yaml (1.0.3) - sawyer (0.5.4) - addressable (~> 2.3.5) - faraday (~> 0.8, < 0.10) - semverse (1.1.0) - serverspec (1.9.1) - highline - net-ssh - rspec (~> 2.13) - rspec-its - specinfra (~> 1.18) - slop (3.5.0) - solve (1.2.0) - dep_selector (~> 1.0) - semverse (~> 1.1) - specinfra (1.19.0) - systemu (2.5.2) - test-kitchen (1.2.1) - mixlib-shellout (~> 1.2) - net-scp (~> 1.1) - net-ssh (~> 2.7) - safe_yaml (~> 1.0) - thor (~> 0.18) - thor (0.19.1) - timers (3.0.1) - hitimes - treetop (1.4.15) - polyglot - polyglot (>= 0.3.1) - varia_model (0.4.0) - buff-extensions (~> 1.0) - hashie (>= 2.0.2, < 3.0.0) - yajl-ruby (1.1.0) - -PLATFORMS - ruby - -DEPENDENCIES - berkshelf (> 3) - chef (>= 11.8) - chefspec (>= 3.4) - foodcritic (< 4.0) - guard (>= 2.6) - guard-foodcritic (>= 1.0.2) - guard-rubocop (>= 1.1) - kitchen-vagrant - rake (>= 10.2) - rubocop (= 0.23) - serverspec (>= 1.6) - test-kitchen diff --git a/providers/config.rb b/providers/config.rb index 9f81d31..6cf7ffb 100644 --- a/providers/config.rb +++ b/providers/config.rb @@ -29,6 +29,8 @@ def load_current_resource @service_name = new_resource.service_name || @instance end +use_inline_resources + action :create do conf = conf_vars # Chef::Log.info("config vars: #{conf.inspect}") @@ -40,7 +42,6 @@ def load_current_resource group conf[:group] mode conf[:mode] variables conf[:variables] - notifies :restart, "logstash_service[#{conf[:service_name]}]" action :create end new_resource.updated_by_last_action(tp.updated_by_last_action?) diff --git a/providers/service.rb b/providers/service.rb index d133583..08c087b 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -41,52 +41,22 @@ def load_current_resource @supervisor_gid = attributes['supervisor_gid'] || defaults['supervisor_gid'] end +use_inline_resources + action :restart do - svc = svc_vars - case svc[:method] - when 'native' - sv = service svc[:service_name] do - # provider Chef::Provider::Service::Upstart - action [:restart] - end - new_resource.updated_by_last_action(sv.updated_by_last_action?) - end + service_action(:restart) end action :start do - svc = svc_vars - case svc[:method] - when 'native' - sv = service svc[:service_name] do - # provider Chef::Provider::Service::Upstart - action [:start] - end - new_resource.updated_by_last_action(sv.updated_by_last_action?) - end + service_action(:start) end action :stop do - svc = svc_vars - case svc[:method] - when 'native' - sv = service svc[:service_name] do - # provider Chef::Provider::Service::Upstart - action [:stop] - end - new_resource.updated_by_last_action(sv.updated_by_last_action?) - end + service_action(:stop) end action :reload do - svc = svc_vars - case svc[:method] - when 'native' - sv = service svc[:service_name] do - # provider Chef::Provider::Service::Upstart - action [:reload] - end - new_resource.updated_by_last_action(sv.updated_by_last_action?) - end + service_action(:reload) end action :enable do @@ -243,6 +213,42 @@ def default_args args end +def service_action(action) + svc = svc_vars + case svc[:method] + when 'native' + sv = service svc[:service_name] + case pick_provider + when 'systemd' + sv.provider(Chef::Provider::Service::Systemd) + when 'upstart' + sv.provider(Chef::Provider::Service::Upstart) + else + sv.provider(Chef::Provider::Service::Init) + end + sv.run_action(action) + new_resource.updated_by_last_action(sv.updated_by_last_action?) + end +end + +def pick_provider + if platform_family? 'fedora' + if node['platform_version'] >= '15' + return 'systemd' + else + return 'default' + end + elsif platform_family? 'debian' + if node['platform_version'] >= '12.04' + return 'upstart' + else + return 'default' + end + else + return 'default' + end +end + def svc_vars svc = { name: @instance, diff --git a/recipes/agent.rb b/recipes/agent.rb index 0157648..dfa8e4d 100644 --- a/recipes/agent.rb +++ b/recipes/agent.rb @@ -24,6 +24,7 @@ input_file_name: '/var/log/syslog', input_file_type: 'syslog' ) + notifies :restart, "logstash_service[#{name}]" action [:create] end diff --git a/recipes/server.rb b/recipes/server.rb index cc59e94..53d29ec 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -42,6 +42,7 @@ variables( elasticsearch_embedded: true ) + notifies :restart, "logstash_service[#{name}]" end # ^ see `.kitchen.yml` for example attributes to configure templates. @@ -54,10 +55,6 @@ action [:create] end -logstash_service name do - action [:start] -end - logstash_curator 'server' do action [:create] end diff --git a/test/integration/server/serverspec/server_spec.rb b/test/integration/server/serverspec/server_spec.rb index 4168d4c..dd7610e 100644 --- a/test/integration/server/serverspec/server_spec.rb +++ b/test/integration/server/serverspec/server_spec.rb @@ -39,5 +39,5 @@ # Logstash Curator describe cron do - it { should have_entry('0 * * * * curator --host 127.0.0.1 -d 31 &> /dev/null').with_user('logstash') } + it { should have_entry('0 * * * * curator --host 127.0.0.1 delete --older-than 31 &> /dev/null').with_user('logstash') } end diff --git a/test/unit/spec/agent_spec.rb b/test/unit/spec/agent_spec.rb index 4648138..4ebd5a8 100644 --- a/test/unit/spec/agent_spec.rb +++ b/test/unit/spec/agent_spec.rb @@ -32,7 +32,6 @@ it 'calls the logstash_instance LWRP' do expect(chef_run).to enable_logstash_service('agent') - expect(chef_run).to start_logstash_service('agent') end end diff --git a/test/unit/spec/server_spec.rb b/test/unit/spec/server_spec.rb index 75ca8bf..491d7a2 100644 --- a/test/unit/spec/server_spec.rb +++ b/test/unit/spec/server_spec.rb @@ -32,7 +32,6 @@ it 'calls the logstash_service LWRP' do expect(chef_run).to enable_logstash_service('server') - expect(chef_run).to start_logstash_service('server') end it 'calls the logstash_plugins LWRP' do