diff --git a/.kitchen.yml b/.kitchen.yml index 69e82ae..1f15de1 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -5,12 +5,20 @@ driver: provisioner: name: chef_solo +verifier: + name: inspec + platforms: + - name: ubuntu-16.04 - name: ubuntu-12.04 - - name: centos-6.5 + - name: centos-7.3 suites: - name: default run_list: + - recipe[nodejs::npm] - recipe[statsd::default] attributes: + nodejs: + repo: https://deb.nodesource.com/node_6.x + key: https://deb.nodesource.com/gpgkey/nodesource.gpg.key diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..2bf1c1c --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.3.1 diff --git a/Gemfile b/Gemfile index f1579fa..3f7df9b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' -gem 'berkshelf', '~> 3.0' -gem 'chef' +gem 'berkshelf' gem 'chefspec' gem 'test-kitchen' gem 'kitchen-vagrant' +gem 'kitchen-inspec' diff --git a/Gemfile.lock b/Gemfile.lock index 5f45ded..956aec7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,185 +1,212 @@ GEM remote: https://rubygems.org/ specs: - addressable (2.3.6) - berkshelf (3.1.4) + addressable (2.3.8) + artifactory (2.5.1) + berkshelf (4.2.0) addressable (~> 2.3.4) - berkshelf-api-client (~> 1.2) + berkshelf-api-client (~> 2.0) buff-config (~> 1.0) buff-extensions (~> 1.0) buff-shell_out (~> 0.1) - celluloid (~> 0.16.0.pre) - celluloid-io (~> 0.16.0.pre) + celluloid (= 0.16.0) + celluloid-io (~> 0.16.1) + cleanroom (~> 1.0) faraday (~> 0.9.0) + httpclient (~> 2.6.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) + octokit (~> 4.0) + retryable (~> 2.0) + ridley (~> 4.3) + solve (~> 2.0) + thor (~> 0.19) + berkshelf-api-client (2.0.0) + faraday (~> 0.9.1) + httpclient (~> 2.6.0) buff-config (1.0.1) buff-extensions (~> 1.0) varia_model (~> 0.4) buff-extensions (1.0.0) - buff-ignore (1.1.1) + buff-ignore (1.2.0) buff-ruby_engine (0.1.0) - buff-shell_out (0.1.1) + buff-shell_out (0.2.0) buff-ruby_engine (~> 0.1.0) - celluloid (0.16.0.pre3) + builder (3.2.2) + celluloid (0.16.0) timers (~> 4.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 (4.0.1) - chef (~> 11.12) - fauxhai (~> 2.0) - rspec (~> 3.0) - coderay (1.1.0) - dep-selector-libgecode (1.0.2) - dep_selector (1.0.3) - dep-selector-libgecode (~> 1.0) - ffi (~> 1.9) + celluloid-io (0.16.2) + celluloid (>= 0.16.0) + nio4r (>= 1.1.0) + chefspec (0.0.1) + cleanroom (1.0.0) + coderay (1.1.1) diff-lcs (1.2.5) + docker-api (1.33.1) + excon (>= 0.38.0) + json erubis (2.7.0) - faraday (0.9.0) + excon (0.54.0) + faraday (0.9.2) multipart-post (>= 1.2, < 3) - fauxhai (2.2.0) - net-ssh - ohai - ffi (1.9.3) - hashie (2.1.2) - highline (1.6.21) - hitimes (1.2.2) - ipaddress (0.8.0) - json (1.8.1) - kitchen-vagrant (0.15.0) - test-kitchen (~> 1.0) + ffi (1.9.14) + gssapi (1.2.0) + ffi (>= 1.0.1) + gyoku (1.3.1) + builder (>= 2.1.2) + hashie (3.4.6) + hitimes (1.2.4) + httpclient (2.6.0.1) + inspec (1.8.0) + hashie (~> 3.4) + json (>= 1.8, < 3.0) + method_source (~> 0.8) + mixlib-log + parallel (~> 1.9) + pry (~> 0) + rainbow (~> 2) + rspec (~> 3) + rspec-its (~> 1.2) + rspec_junit_formatter (~> 0.2.3) + rubyzip (~> 1.1) + sslshake (~> 1) + thor (~> 0.19) + train (>= 0.22.0, < 1.0) + json (2.0.2) + kitchen-inspec (0.17.0) + hashie (~> 3.4) + inspec (>= 0.34.0, < 2.0.0) + test-kitchen (~> 1.6) + kitchen-vagrant (0.21.1) + test-kitchen (~> 1.4) + little-plugger (1.1.4) + logging (2.1.0) + little-plugger (~> 1.1) + multi_json (~> 1.10) method_source (0.8.2) - mime-types (1.25.1) minitar (0.5.4) - mixlib-authentication (1.3.0) + mixlib-authentication (1.4.1) mixlib-log - mixlib-cli (1.5.0) - mixlib-config (2.1.0) - mixlib-log (1.6.0) - mixlib-shellout (1.4.0) + mixlib-install (2.1.9) + artifactory + mixlib-shellout + mixlib-versioning + thor + mixlib-log (1.7.1) + mixlib-shellout (2.2.7) + mixlib-versioning (1.1.0) + molinillo (0.4.5) + multi_json (1.12.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 (3.2.0) 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) - 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 - pry (0.10.0) + nio4r (2.0.0) + nori (2.6.0) + octokit (4.6.2) + sawyer (~> 0.8.0, >= 0.5.3) + parallel (1.10.0) + pry (0.10.4) coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) - rack (1.5.2) - 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) + rainbow (2.1.0) + retryable (2.0.4) + ridley (4.4.1) 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) + celluloid (~> 0.16.0) + celluloid-io (~> 0.16.1) erubis faraday (~> 0.9.0) - hashie (>= 2.0.2, < 3.0.0) + hashie (>= 2.0.2, < 4.0.0) + httpclient (~> 2.6) json (>= 1.7.7) mixlib-authentication (>= 1.3.0) - net-http-persistent (>= 2.8) - retryable + retryable (~> 2.0) semverse (~> 1.1) - varia_model (~> 0.4) - rspec (3.0.0) - rspec-core (~> 3.0.0) - rspec-expectations (~> 3.0.0) - rspec-mocks (~> 3.0.0) - rspec-core (3.0.3) - rspec-support (~> 3.0.0) - rspec-expectations (3.0.3) + varia_model (~> 0.4.0) + rspec (3.5.0) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) + rspec-core (3.5.4) + rspec-support (~> 3.5.0) + rspec-expectations (3.5.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.5.0) + rspec-its (1.2.0) + rspec-core (>= 3.0.0) + rspec-expectations (>= 3.0.0) + rspec-mocks (3.5.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.0.0) - rspec-mocks (3.0.3) - rspec-support (~> 3.0.0) - rspec-support (3.0.3) - safe_yaml (1.0.3) - sawyer (0.5.4) - addressable (~> 2.3.5) - faraday (~> 0.8, < 0.10) - semverse (1.1.0) + rspec-support (~> 3.5.0) + rspec-support (3.5.0) + rspec_junit_formatter (0.2.3) + builder (< 4) + rspec-core (>= 2, < 4, != 2.12.0) + rubyntlm (0.6.1) + rubyzip (1.2.0) + safe_yaml (1.0.4) + sawyer (0.8.1) + addressable (>= 2.3.5, < 2.6) + faraday (~> 0.8, < 1.0) + semverse (1.2.1) slop (3.6.0) - solve (1.2.1) - dep_selector (~> 1.0) + solve (2.0.3) + molinillo (~> 0.4.2) semverse (~> 1.1) - systemu (2.5.2) - test-kitchen (1.2.1) - mixlib-shellout (~> 1.2) + sslshake (1.0.13) + test-kitchen (1.14.2) + mixlib-install (>= 1.2, < 3.0) + mixlib-shellout (>= 1.2, < 3.0) net-scp (~> 1.1) - net-ssh (~> 2.7) + net-ssh (>= 2.9, < 4.0) + net-ssh-gateway (~> 1.2.0) safe_yaml (~> 1.0) thor (~> 0.18) - thor (0.19.1) - timers (4.0.0) + thor (0.19.4) + timers (4.0.4) hitimes - varia_model (0.4.0) + train (0.22.0) + docker-api (~> 1.26) + json (>= 1.8, < 3.0) + mixlib-shellout (~> 2.0) + net-scp (~> 1.2) + net-ssh (>= 2.9, < 4.0) + winrm (~> 2.0) + winrm-fs (~> 1.0) + varia_model (0.4.1) buff-extensions (~> 1.0) - hashie (>= 2.0.2, < 3.0.0) - yajl-ruby (1.2.1) + hashie (>= 2.0.2, < 4.0.0) + winrm (2.1.0) + builder (>= 2.1.2) + erubis (~> 2.7) + gssapi (~> 1.2) + gyoku (~> 1.0) + httpclient (~> 2.2, >= 2.2.0.2) + logging (>= 1.6.1, < 3.0) + nori (~> 2.0) + rubyntlm (~> 0.6.0, >= 0.6.1) + winrm-fs (1.0.1) + erubis (~> 2.7) + logging (>= 1.6.1, < 3.0) + rubyzip (~> 1.1) + winrm (~> 2.0) PLATFORMS ruby DEPENDENCIES - berkshelf (~> 3.0) - chef + berkshelf chefspec + kitchen-inspec kitchen-vagrant test-kitchen + +BUNDLED WITH + 1.13.6 diff --git a/attributes/default.rb b/attributes/default.rb index 1e303c8..2723960 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -26,6 +26,14 @@ default['statsd']['pid_file'] = '/var/run/statsd/statsd.pid' default['statsd']['path'] = '/usr/share/statsd' +# Default path for the nodejs cookbook. +case node['platform_family'] +when 'debian' + default['statsd']['node_bin'] = '/usr/bin/nodejs' +when 'rhel' + default['statsd']['node_bin'] = '/bin/node' +end + default['statsd']['user'] = 'statsd' default['statsd']['group'] = 'statsd' @@ -48,6 +56,12 @@ start: true } +if node.has_key?('init_package') && node['init_package'] == 'systemd' + default['statsd']['init_style'] = 'systemd' +else + default['statsd']['init_style'] = 'upstart' +end + # # Add all NPM module backends here. Each backend should be a # hash of the backend's name to the NPM module's version. If we diff --git a/recipes/service.rb b/recipes/service.rb index c0f33e6..582c31f 100644 --- a/recipes/service.rb +++ b/recipes/service.rb @@ -26,19 +26,9 @@ end end -# place the upstart script -template '/etc/init/statsd.conf' do - source 'upstart.conf.erb' - mode 0644 - notifies :restart, 'service[statsd]', :delayed -end +# Set up our service. +include_recipe "statsd::#{node['statsd']['init_style']}_service" -# define the service resource service 'statsd' do - provider Chef::Provider::Service::Upstart - restart_command 'stop statsd; start statsd' - start_command 'start statsd' - stop_command 'stop statsd' - supports restart: true, start: true, stop: true action service_status end diff --git a/recipes/systemd_service.rb b/recipes/systemd_service.rb new file mode 100644 index 0000000..a089aa4 --- /dev/null +++ b/recipes/systemd_service.rb @@ -0,0 +1,29 @@ +# +# Cookbook Name:: statsd +# Recipe:: service +# +# Copyright 2014, Librato, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +template '/etc/systemd/system/statsd.service' do + source 'systemd.service.erb' + action :create +end + +service 'statsd' do + provider Chef::Provider::Service::Systemd + supports restart: true, start: true, stop: true + action :nothing +end diff --git a/recipes/upstart_service.rb b/recipes/upstart_service.rb new file mode 100644 index 0000000..ed9b99b --- /dev/null +++ b/recipes/upstart_service.rb @@ -0,0 +1,35 @@ +# +# Cookbook Name:: statsd +# Recipe:: service +# +# Copyright 2014, Librato, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# Install our init script. +template '/etc/init/statsd.conf' do + source 'upstart.conf.erb' + mode 0644 + notifies :restart, 'service[statsd]', :delayed +end + +# Set up our service. +service 'statsd' do + provider Chef::Provider::Service::Upstart + restart_command 'stop statsd; start statsd' + start_command 'start statsd' + stop_command 'stop statsd' + supports restart: true, start: true, stop: true + action :nothing +end diff --git a/templates/default/systemd.service.erb b/templates/default/systemd.service.erb new file mode 100644 index 0000000..1852bd9 --- /dev/null +++ b/templates/default/systemd.service.erb @@ -0,0 +1,14 @@ +[Unit] +Description=Monitoring daemon, that aggregates events received by udp in 10 second intervals +Documentation=https://github.com/etsy/statsd/ +Wants=network.target + +[Service] +Type=simple +User=<%= node['statsd']['user'] %> +Group=<%= node['statsd']['group'] %> +ExecStart=<%= node['statsd']['node_bin'] %> <%= node['statsd']['path'] %>/stats.js <%= node['statsd']['config_dir'] %>/config.js +PIDFile=<%= node['statsd']['pid_file'] %> + +[Install] +WantedBy=multi-user.target diff --git a/test/integration/default/inspec/default_spec.rb b/test/integration/default/inspec/default_spec.rb new file mode 100644 index 0000000..6af7b31 --- /dev/null +++ b/test/integration/default/inspec/default_spec.rb @@ -0,0 +1,14 @@ +control 'default' do + title 'Ensure statsd is running and available' + + describe service('statsd') do + it { should be_enabled } + it { should be_running } + end + + describe port('8125') do + it { should be_listening } + its('protocols') { should include 'udp' } + its('processes') { should include 'statsd' } + end +end diff --git a/test/integration/default/inspec/systemd_spec.rb b/test/integration/default/inspec/systemd_spec.rb new file mode 100644 index 0000000..f9184e1 --- /dev/null +++ b/test/integration/default/inspec/systemd_spec.rb @@ -0,0 +1,13 @@ +control 'systemd' do + title 'Ensure statsd runs correctly under systemd' + + service_file = '/etc/systemd/system/statsd.service' + + only_if do + file(service_file).exist? + end + + describe file(service_file) do + it { should exist } + end +end diff --git a/test/integration/default/inspec/upstart_spec.rb b/test/integration/default/inspec/upstart_spec.rb new file mode 100644 index 0000000..9cfa296 --- /dev/null +++ b/test/integration/default/inspec/upstart_spec.rb @@ -0,0 +1,13 @@ +control 'upstart' do + title 'Ensure statsd runs correctly under upstart' + + service_file = '/etc/init/statsd.conf' + + only_if do + file(service_file).exist? + end + + describe file(service_file) do + it { should exist } + end +end diff --git a/test/integration/default/serverspec/default_spec.rb b/test/integration/default/serverspec/default_spec.rb deleted file mode 100644 index 794a3b5..0000000 --- a/test/integration/default/serverspec/default_spec.rb +++ /dev/null @@ -1,29 +0,0 @@ -require 'serverspec' - -include Serverspec::Helper::Exec -# Force Debian to properly detect statsd service for upstart on CentOS 6. -include Serverspec::Helper::Debian - -RSpec.configure do |c| - c.before :all do - c.path = '/sbin:/usr/sbin' - end -end - -describe 'statsd service' do - it 'should be configured for upstart' do - expect(file('/etc/init/statsd.conf')).to be_file - end - - it 'should be enabled' do - expect(service('statsd')).to be_enabled - end - - it 'should be running' do - expect(service('statsd')).to be_running - end - - it 'should be listening' do - expect(port('8125')).to be_listening.with('udp') - end -end