From faf1436bca5dc3833fdfdbc82e0506c99ed5729d Mon Sep 17 00:00:00 2001 From: "John E. Vincent" Date: Sun, 26 Jan 2014 05:00:02 -0500 Subject: [PATCH 01/55] adding a logstash instance LWRP --- providers/instance.rb | 130 ++++++++++++++++++++++++++++++++++++++++++ resources/instance.rb | 26 +++++++++ 2 files changed, 156 insertions(+) create mode 100644 providers/instance.rb create mode 100644 resources/instance.rb diff --git a/providers/instance.rb b/providers/instance.rb new file mode 100644 index 0000000..fd80b58 --- /dev/null +++ b/providers/instance.rb @@ -0,0 +1,130 @@ +# +# Cookbook Name:: logstash +# Provider:: instance +# Author:: John E. Vincent +# License:: Apache 2.0 +# +# Copyright 2014, John E. Vincent + +require 'chef/mixin/shell_out' +require 'chef/mixin/language' +include Chef::Mixin::ShellOut + +def load_current_resource + @base_directory = new_resource.base_directory + @install_type = new_resource.install_type + @version = new_resource.version || node['logstash']['default_version'] + @checksum = new_resource.checksum || node['logstash']['default_checksum'] + @source_url = new_resource.source_url || "https://download.elasticsearch.org/logstash/logstash/logstash-#{@version}-flatjar.jar" + @repo = new_resource.repo + @sha = new_resource.sha + @java_home = new_resource.java_home + @ls_user = new_resource.user + @ls_group = new_resource.group + @ls_useropts = new_resource.user_opts + @do_symlink = new_resource.auto_symlink + @instance_dir = "#{@base_directory}/#{new_resource.name}" + @updated = false +end + +action :create do + + ur = user @ls_user do + home @ls_useropts[:homedir] + system true + action :create + manage_home true + uid @ls_useropts[:uid] + end + set_updated(ur.updated_by_last_action?) + + gr = group @ls_group do + members @ls_user + append true + system true + end + set_updated(gr.updated_by_last_action?) + + bdr = directory @base_directory do + action :create + mode '0755' + owner @ls_user + group @ls_group + end + set_updated(bdr.updated_by_last_action?) + + idr = directory @instance_dir do + action :create + mode '0755' + owner @ls_user + group @ls_group + end + set_updated(idr.updated_by_last_action?) + + %w{bin etc lib log tmp etc/conf.d etc/patterns}.each do |ldir| + r = directory "#{@instance_dir}/ldir" do + action :create + mode '0755' + owner @ls_user + group @ls_group + end + set_updated(r.updated_by_last_action?) + end + + if @install_type == "jar" + rfr = remote_file "#{@instance_dir}/lib/logstash-#{@version}.jar" do + owner 'root' + group 'root' + mode '0755' + source @source_url + checksum @checksum + end + set_updated(rfr.updated_by_last_action?) + + lr = link "#{@instance_dir}/lib/logstash.jar" do + to "#{@instance_dir}/lib/logstash-#{@version}.jar" + not_if { @do_symlink } + end + set_updated(lr.updated_by_last_action?) + elsif @install_type == "source" + sd = directory "#{@instance_dir}/source" do + action :create + owner @ls_user + group @ls_group + mode '0755' + end + set_updated(sd.updated_by_last_action?) + + gr = git "#{@instance_dir}/source" do + repository @repo + reference @sha + action :sync + user @ls_user + group @ls_group + end + set_updated(gr.updated_by_last_action?) + + source_version = @sha || "v#{@version}" + er = execute "build-logstash" do + cwd "#{@instance_dir}/source" + environment(:JAVA_HOME => @java_home) + user @ls_user # Changed from root cause building as root...WHA? + command "make clean && make VERSION=#{source_version} jar" + action :run + creates "#{@instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" + not_if "test -f #{@instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" + end + set_updated(er.updated_by_last_action?) + lr = link "#{@instance_dir}/lib/logstash.jar" do + to "#{@instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" + end + set_updated(lr.updated_by_last_action?) + else + Chef::Application.fatal!("Unknown install type: #{@install_type}") + end +end + +private +def set_updated(u) + @updated = u unless @updated == true +end diff --git a/resources/instance.rb b/resources/instance.rb new file mode 100644 index 0000000..ef30fdb --- /dev/null +++ b/resources/instance.rb @@ -0,0 +1,26 @@ +# +# Cookbook Name:: logstash +# Resource:: instance +# Author:: John E. Vincent +# Copyright 2014, John E. Vincent +# License:: Apache 2.0 + +actions :create, :delete + +default_action :create if defined?(default_action) + +attribute :name, :kind_of => String, :name_attribute => true +attribute :base_directory, :kind_of => String, :default => "/opt/logstash" +attribute :install_type, :kind_of => String, :equal_to => ["source", "jar"], :default => 'jar' +attribute :auto_symlink, :kind_of => [TrueClass, FalseClass], :default => true +# version/checksum/source_url used by `jar` install_type +attribute :version, :kind_of => String +attribute :checksum, :kind_of => String +attribute :source_url, :kind_of => String +# sha/repo/java_home used by `source` install_type +attribute :sha, :kind_of => String, :default => 'HEAD' +attribute :repo, :kind_of => String, :default => 'git://github.com/logstash/logstash.git' +attribute :java_home, :kind_of => String, :default => '/usr/lib/jvm/java-6-openjdk' #openjdk6 on ubuntu +attribute :user, :kind_of => String, :default => "logstash" +attribute :group, :kind_of => String, :default => "logstash" +attribute :user_opts, :kind_of => Hash, :default => {:homedir => "/var/lib/logstash", :uid => nil, :gid => nil} From 50ff8c66f952293f3805540b766ca5f93c78360b Mon Sep 17 00:00:00 2001 From: "John E. Vincent" Date: Sun, 26 Jan 2014 06:22:41 -0500 Subject: [PATCH 02/55] working instance LWRP for jar installs --- attributes/default.rb | 2 ++ providers/instance.rb | 29 +++++++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/attributes/default.rb b/attributes/default.rb index 0a7e2c7..28b9488 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -4,6 +4,8 @@ default['logstash']['uid'] = nil # set to nil to let system pick default['logstash']['group'] = 'logstash' default['logstash']['gid'] = nil # set to nil to let system pick +default['logstash']['default_version'] = '1.3.2' +default['logstash']['default_checksum'] = '6a6a6e55efd14a182560af8143883eace1936afb11d59d0c35ce4ed5a5576a18' default['logstash']['supervisor_gid'] = node['logstash']['group'] default['logstash']['pid_dir'] = '/var/run/logstash' default['logstash']['create_account'] = true diff --git a/providers/instance.rb b/providers/instance.rb index fd80b58..036c5bc 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -21,24 +21,28 @@ def load_current_resource @java_home = new_resource.java_home @ls_user = new_resource.user @ls_group = new_resource.group - @ls_useropts = new_resource.user_opts - @do_symlink = new_resource.auto_symlink + @ls_useropts = new_resource.user_opts.clone @instance_dir = "#{@base_directory}/#{new_resource.name}" @updated = false end action :create do + ls_homedir = @ls_useropts[:homedir] + ls_uid = @ls_useropts[:uid] + ls_gid = @ls_useropts[:gid] + ur = user @ls_user do - home @ls_useropts[:homedir] + home ls_homedir system true action :create manage_home true - uid @ls_useropts[:uid] + uid ls_uid end set_updated(ur.updated_by_last_action?) gr = group @ls_group do + gid ls_gid members @ls_user append true system true @@ -62,7 +66,7 @@ def load_current_resource set_updated(idr.updated_by_last_action?) %w{bin etc lib log tmp etc/conf.d etc/patterns}.each do |ldir| - r = directory "#{@instance_dir}/ldir" do + r = directory "#{@instance_dir}/#{ldir}" do action :create mode '0755' owner @ls_user @@ -72,18 +76,21 @@ def load_current_resource end if @install_type == "jar" - rfr = remote_file "#{@instance_dir}/lib/logstash-#{@version}.jar" do + ls_source_url = @source_url + ls_version = @version + ls_checksum = @checksum + rfr = remote_file "#{@instance_dir}/lib/logstash-#{ls_version}.jar" do owner 'root' group 'root' mode '0755' - source @source_url - checksum @checksum + source ls_source_url + checksum ls_checksum end set_updated(rfr.updated_by_last_action?) lr = link "#{@instance_dir}/lib/logstash.jar" do - to "#{@instance_dir}/lib/logstash-#{@version}.jar" - not_if { @do_symlink } + to "#{@instance_dir}/lib/logstash-#{ls_version}.jar" + only_if { new_resource.auto_symlink } end set_updated(lr.updated_by_last_action?) elsif @install_type == "source" @@ -117,11 +124,13 @@ def load_current_resource set_updated(er.updated_by_last_action?) lr = link "#{@instance_dir}/lib/logstash.jar" do to "#{@instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" + only_if { new_resource.auto_symlink } end set_updated(lr.updated_by_last_action?) else Chef::Application.fatal!("Unknown install type: #{@install_type}") end + new_resource.updated_by_last_action(@updated) end private From 2a3d8e32db5b848a1dc51781cf70f480c4b8420f Mon Sep 17 00:00:00 2001 From: "John E. Vincent" Date: Sun, 26 Jan 2014 23:38:42 -0500 Subject: [PATCH 03/55] small symlink bug --- providers/instance.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/providers/instance.rb b/providers/instance.rb index 036c5bc..29f98b4 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -22,7 +22,7 @@ def load_current_resource @ls_user = new_resource.user @ls_group = new_resource.group @ls_useropts = new_resource.user_opts.clone - @instance_dir = "#{@base_directory}/#{new_resource.name}" + @instance_dir = "#{@base_directory}/#{new_resource.name}".clone @updated = false end @@ -79,7 +79,9 @@ def load_current_resource ls_source_url = @source_url ls_version = @version ls_checksum = @checksum - rfr = remote_file "#{@instance_dir}/lib/logstash-#{ls_version}.jar" do + ls_instance_dir = @instance_dir + + rfr = remote_file "#{ls_instance_dir}/lib/logstash-#{ls_version}.jar" do owner 'root' group 'root' mode '0755' @@ -88,8 +90,8 @@ def load_current_resource end set_updated(rfr.updated_by_last_action?) - lr = link "#{@instance_dir}/lib/logstash.jar" do - to "#{@instance_dir}/lib/logstash-#{ls_version}.jar" + lr = link "#{ls_instance_dir}/lib/logstash.jar" do + to "#{ls_instance_dir}/lib/logstash-#{ls_version}.jar" only_if { new_resource.auto_symlink } end set_updated(lr.updated_by_last_action?) From d5c950b6141ac4277cdce8445f1b23fc90678e31 Mon Sep 17 00:00:00 2001 From: paulczar Date: Sat, 15 Mar 2014 18:35:30 -0500 Subject: [PATCH 04/55] first pass at server recipe --- Vagrantfile | 4 +- attributes/default.rb | 7 +- attributes/server.rb | 40 ++-- metadata.rb | 2 +- providers/instance.rb | 142 +++++++++----- recipes/default.rb | 43 ----- recipes/server.rb | 186 +++++++------------ recipes/test.rb | 12 ++ resources/instance.rb | 6 +- templates/default/logstash_server.conf.erb | 4 +- templates/default/sv-logstash_server-run.erb | 18 +- templates/default/sv-logstash_web-run.erb | 15 +- 12 files changed, 236 insertions(+), 243 deletions(-) create mode 100644 recipes/test.rb diff --git a/Vagrantfile b/Vagrantfile index a9b6d34..7a321b9 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -7,7 +7,6 @@ log_level = :info chef_run_list = %w[ java::default logstash::server - logstash::agent ] # curl::default # minitest-handler::default @@ -88,6 +87,9 @@ Vagrant.configure('2') do |config| end config.vm.define :precise64 do |dist_config| + if Vagrant.has_plugin?("vagrant-cachier") + dist_config.cache.scope = :box + end dist_config.vm.box = 'opscode-ubuntu-12.04' dist_config.vm.box_url = 'https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box' diff --git a/attributes/default.rb b/attributes/default.rb index 438a7d8..876c8ce 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -4,8 +4,10 @@ default['logstash']['uid'] = nil # set to nil to let system pick default['logstash']['group'] = 'logstash' default['logstash']['gid'] = nil # set to nil to let system pick -default['logstash']['default_version'] = '1.3.2' -default['logstash']['default_checksum'] = '6a6a6e55efd14a182560af8143883eace1936afb11d59d0c35ce4ed5a5576a18' +default['logstash']['default_version'] = '1.4.0.rc1' +default['logstash']['default_checksum'] = 'b015fa130d589af957c9a48e6f59754f5c0954835abf44bd013547a6b6520e59' +default['logstash']['default_source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.rc1.tar.gz' +default['logstash']['default_install_type'] = 'tarball' default['logstash']['supervisor_gid'] = node['logstash']['group'] default['logstash']['pid_dir'] = '/var/run/logstash' default['logstash']['create_account'] = true @@ -40,3 +42,4 @@ default['logstash']['logging']['maxBackup'] = 10 default['logstash']['logging']['maxSize'] = '10M' default['logstash']['logging']['useFileSize'] = false + diff --git a/attributes/server.rb b/attributes/server.rb index fbbded9..5d5a68d 100644 --- a/attributes/server.rb +++ b/attributes/server.rb @@ -1,25 +1,27 @@ # Encoding: utf-8 -default['logstash']['server']['version'] = '1.3.3' -default['logstash']['server']['home'] = "#{node['logstash']['basedir']}/server" -default['logstash']['server']['log_file'] = '/var/log/logstash/server.log' -default['logstash']['server']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.3.3-flatjar.jar' -default['logstash']['server']['checksum'] = 'a83503bd2aa32e1554b98f812d0b411fbc5f7b6b21cebb48b7d344474f2dfc6d' -default['logstash']['server']['install_method'] = 'jar' # Either `source` or `jar` -default['logstash']['server']['patterns_dir'] = 'etc/patterns' -default['logstash']['server']['config_dir'] = 'etc/conf.d' -default['logstash']['server']['config_file'] = 'logstash.conf' +default['logstash']['server']['name'] = 'server' +default['logstash']['server']['home'] = "/opt/logstash/#{node['logstash']['server']['name']}" +default['logstash']['server']['version'] = '1.4.0.rc1' +default['logstash']['server']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.rc1.tar.gz' +default['logstash']['server']['checksum'] = 'b015fa130d589af957c9a48e6f59754f5c0954835abf44bd013547a6b6520e59' +default['logstash']['server']['install_method'] = 'tarball' +default['logstash']['server']['config_file'] = 'logstash.conf' +default['logstash']['server']['log_file'] = 'server.log' +default['logstash']['server']['base_config'] = 'server.conf.erb' # set blank if don't want data driven config + +default['logstash']['server']['xms'] = '1024M' +default['logstash']['server']['xmx'] = '1024M' +default['logstash']['server']['java_opts'] = '' +default['logstash']['server']['gc_opts'] = '-XX:+UseParallelOldGC' +default['logstash']['server']['ipv4_only'] = false +default['logstash']['server']['debug'] = false +default['logstash']['server']['workers'] = 1 +default['logstash']['server']['patterns'] = [] + +default['logstash']['server']['base_config_cookbook'] = 'logstash' default['logstash']['server']['config_templates'] = [] default['logstash']['server']['config_templates_cookbook'] = 'logstash' default['logstash']['server']['config_templates_variables'] = {} -default['logstash']['server']['base_config'] = 'server.conf.erb' # set blank if don't want data driven config -default['logstash']['server']['base_config_cookbook'] = 'logstash' -default['logstash']['server']['xms'] = '1024M' -default['logstash']['server']['xmx'] = '1024M' -default['logstash']['server']['java_opts'] = '' -default['logstash']['server']['gc_opts'] = '-XX:+UseParallelOldGC' -default['logstash']['server']['ipv4_only'] = false -default['logstash']['server']['debug'] = false -default['logstash']['server']['workers'] = 1 # allow control over the upstart config default['logstash']['server']['upstart_with_sudo'] = false @@ -36,4 +38,4 @@ default['logstash']['server']['web']['address'] = '0.0.0.0' default['logstash']['server']['web']['port'] = '9292' -default['logstash']['server']['logrotate']['options'] = %w{ missingok notifempty compress copytruncate } +default['logstash']['server']['logrotate']['options'] = %w{ missingok notifempty compress copytruncate } \ No newline at end of file diff --git a/metadata.rb b/metadata.rb index defdb9b..a25a8f8 100644 --- a/metadata.rb +++ b/metadata.rb @@ -11,7 +11,7 @@ supports os end -%w{ build-essential runit git ant java logrotate rabbitmq yum python }.each do |ckbk| +%w{ build-essential runit git ant java logrotate rabbitmq yum python ark }.each do |ckbk| depends ckbk end diff --git a/providers/instance.rb b/providers/instance.rb index 29f98b4..6bf24f4 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -11,28 +11,37 @@ include Chef::Mixin::ShellOut def load_current_resource - @base_directory = new_resource.base_directory - @install_type = new_resource.install_type + @base_directory = new_resource.base_directory || node['logstash']['basedir'] + @install_type = new_resource.install_type || node['logstash']['default_install_type'] @version = new_resource.version || node['logstash']['default_version'] @checksum = new_resource.checksum || node['logstash']['default_checksum'] - @source_url = new_resource.source_url || "https://download.elasticsearch.org/logstash/logstash/logstash-#{@version}-flatjar.jar" + @source_url = new_resource.source_url || node['logstash']['default_source_url'] @repo = new_resource.repo @sha = new_resource.sha @java_home = new_resource.java_home - @ls_user = new_resource.user - @ls_group = new_resource.group - @ls_useropts = new_resource.user_opts.clone + @user = new_resource.user + @group = new_resource.group + @useropts = new_resource.user_opts.clone @instance_dir = "#{@base_directory}/#{new_resource.name}".clone @updated = false + @name = new_resource.name end action :create do - ls_homedir = @ls_useropts[:homedir] - ls_uid = @ls_useropts[:uid] - ls_gid = @ls_useropts[:gid] + ls_homedir = @useropts[:homedir] + ls_uid = @useropts[:uid] + ls_gid = @useropts[:gid] + ls_source_url = @source_url + ls_version = @version + ls_checksum = @checksum + ls_basedir = @base_directory + ls_user = @user + ls_group = @group + ls_dir = @dir + ls_name = @name - ur = user @ls_user do + ur = user ls_user do home ls_homedir system true action :create @@ -41,45 +50,65 @@ def load_current_resource end set_updated(ur.updated_by_last_action?) - gr = group @ls_group do + gr = group ls_group do gid ls_gid - members @ls_user + members ls_user append true system true end set_updated(gr.updated_by_last_action?) - bdr = directory @base_directory do - action :create - mode '0755' - owner @ls_user - group @ls_group - end - set_updated(bdr.updated_by_last_action?) + case @install_type + when 'tarball' + @run_context.include_recipe "ark::default" + arkit = ark ls_name do + url ls_source_url + checksum ls_checksum + owner ls_user + group ls_group + mode 0755 + version ls_version + path "#{ls_basedir}" + action :put + end + set_updated(arkit.updated_by_last_action?) - idr = directory @instance_dir do - action :create - mode '0755' - owner @ls_user - group @ls_group - end - set_updated(idr.updated_by_last_action?) + %w{bin etc lib log tmp etc/conf.d patterns}.each do |ldir| + r = directory "#{@instance_dir}/#{ldir}" do + action :create + mode '0755' + owner ls_user + group ls_group + end + set_updated(r.updated_by_last_action?) + end - %w{bin etc lib log tmp etc/conf.d etc/patterns}.each do |ldir| - r = directory "#{@instance_dir}/#{ldir}" do + when "jar" + bdr = directory @base_directory do action :create mode '0755' - owner @ls_user - group @ls_group + owner ls_user + group ls_group end - set_updated(r.updated_by_last_action?) - end + set_updated(bdr.updated_by_last_action?) + + idr = directory @instance_dir do + action :create + mode '0755' + owner ls_user + group ls_group + end + set_updated(idr.updated_by_last_action?) - if @install_type == "jar" - ls_source_url = @source_url - ls_version = @version - ls_checksum = @checksum - ls_instance_dir = @instance_dir + %w{bin etc lib log tmp etc/conf.d patterns}.each do |ldir| + r = directory "#{@instance_dir}/#{ldir}" do + action :create + mode '0755' + owner ls_user + group ls_group + end + set_updated(r.updated_by_last_action?) + end rfr = remote_file "#{ls_instance_dir}/lib/logstash-#{ls_version}.jar" do owner 'root' @@ -95,11 +124,38 @@ def load_current_resource only_if { new_resource.auto_symlink } end set_updated(lr.updated_by_last_action?) - elsif @install_type == "source" + + when "source" + bdr = directory @base_directory do + action :create + mode '0755' + owner ls_user + group ls_group + end + set_updated(bdr.updated_by_last_action?) + + idr = directory @instance_dir do + action :create + mode '0755' + owner ls_user + group ls_group + end + set_updated(idr.updated_by_last_action?) + + %w{bin etc lib log tmp etc/conf.d patterns}.each do |ldir| + r = directory "#{@instance_dir}/#{ldir}" do + action :create + mode '0755' + owner ls_user + group ls_group + end + set_updated(r.updated_by_last_action?) + end + sd = directory "#{@instance_dir}/source" do action :create - owner @ls_user - group @ls_group + owner ls_user + group ls_group mode '0755' end set_updated(sd.updated_by_last_action?) @@ -108,8 +164,8 @@ def load_current_resource repository @repo reference @sha action :sync - user @ls_user - group @ls_group + user ls_user + group ls_group end set_updated(gr.updated_by_last_action?) @@ -117,7 +173,7 @@ def load_current_resource er = execute "build-logstash" do cwd "#{@instance_dir}/source" environment(:JAVA_HOME => @java_home) - user @ls_user # Changed from root cause building as root...WHA? + user ls_user # Changed from root cause building as root...WHA? command "make clean && make VERSION=#{source_version} jar" action :run creates "#{@instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" diff --git a/recipes/default.rb b/recipes/default.rb index cce024f..d5d94ae 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -3,46 +3,3 @@ # Cookbook Name:: logstash # Recipe:: default # -include_recipe 'runit' unless node['platform_version'] >= '12.04' - -if node['logstash']['create_account'] - - group node['logstash']['group'] do - system true - gid node['logstash']['gid'] - end - - user node['logstash']['user'] do - group node['logstash']['group'] - home node['logstash']['homedir'] - system true - action :create - manage_home true - uid node['logstash']['uid'] - end - -else - directory node['logstash']['homedir'] do - recursive true - action :create - group node['logstash']['group'] - owner node['logstash']['user'] - mode '0755' - end -end - -directory node['logstash']['basedir'] do - action :create - owner 'root' - group 'root' - mode '0755' -end - -node['logstash']['join_groups'].each do |grp| - group grp do - members node['logstash']['user'] - action :modify - append true - only_if "grep -q '^#{grp}:' /etc/group" - end -end diff --git a/recipes/server.rb b/recipes/server.rb index f6310f3..9158bdd 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -10,29 +10,26 @@ # # -include_recipe 'logstash::default' -include_recipe 'logrotate' +# install logstash 'server' +logstash_instance node['logstash']['server']['name'] do + base_directory node['logstash']['server']['basedir'] + version node['logstash']['server']['version'] + checksum node['logstash']['server']['checksum'] + source_url node['logstash']['server']['source_url'] + install_type node['logstash']['server']['install_type'] + user node['logstash']['user'] + group node['logstash']['group'] +end -include_recipe 'rabbitmq' if node['logstash']['install_rabbitmq'] +# check for rabbit / zero install +include_recipe 'rabbitmq' if node['logstash']['install_rabbitmq'] if node['logstash']['install_zeromq'] include_recipe 'logstash::zero_mq_repo' node['logstash']['zeromq_packages'].each { |p| package p } end -if node['logstash']['server']['init_method'] == 'runit' - include_recipe 'runit' - service_resource = 'runit_service[logstash_server]' -else - service_resource = 'service[logstash_server]' -end - -if node['logstash']['server']['patterns_dir'][0] == '/' - patterns_dir = node['logstash']['server']['patterns_dir'] -else - patterns_dir = node['logstash']['server']['home'] + '/' + node['logstash']['server']['patterns_dir'] -end - +# fix search if chef solo if Chef::Config[:solo] es_server_ip = node['logstash']['elasticsearch_ip'] graphite_server_ip = node['logstash']['graphite_ip'] @@ -53,63 +50,9 @@ end end -# Create directory for logstash -directory node['logstash']['server']['home'] do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] -end - -%w{bin etc lib log tmp }.each do |ldir| - directory "#{node['logstash']['server']['home']}/#{ldir}" do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] - end -end - -# installation -if node['logstash']['server']['install_method'] == 'jar' - remote_file "#{node['logstash']['server']['home']}/lib/logstash-#{node['logstash']['server']['version']}.jar" do - owner 'root' - group 'root' - mode '0755' - source node['logstash']['server']['source_url'] - checksum node['logstash']['server']['checksum'] - action :create_if_missing - end - - link "#{node['logstash']['server']['home']}/lib/logstash.jar" do - to "#{node['logstash']['server']['home']}/lib/logstash-#{node['logstash']['server']['version']}.jar" - notifies :restart, service_resource - end -else - include_recipe 'logstash::source' - - logstash_version = node['logstash']['source']['sha'] || "v#{node['logstash']['server']['version']}" - link "#{node['logstash']['server']['home']}/lib/logstash.jar" do - to "#{node['logstash']['basedir']}/source/build/logstash-#{logstash_version}-monolithic.jar" - notifies :restart, service_resource - end -end - -directory "#{node['logstash']['server']['home']}/etc/conf.d" do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] -end - -directory patterns_dir do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] -end - -node['logstash']['patterns'].each do |file, hash| +# add in any custom patterns +patterns_dir = "#{node['logstash']['server']['home']}/patterns" +node['logstash']['server']['patterns'].each do |file, hash| template_name = patterns_dir + '/' + file template template_name do source 'patterns.erb' @@ -121,49 +64,15 @@ end end -log_dir = ::File.dirname node['logstash']['server']['log_file'] -directory log_dir do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] - recursive true -end - -template "#{node['logstash']['server']['home']}/#{node['logstash']['server']['config_dir']}/#{node['logstash']['server']['config_file']}" do - source node['logstash']['server']['base_config'] - cookbook node['logstash']['server']['base_config_cookbook'] - owner node['logstash']['user'] - group node['logstash']['group'] - mode '0644' - variables( - :graphite_server_ip => graphite_server_ip, - :es_server_ip => es_server_ip, - :enable_embedded_es => node['logstash']['server']['enable_embedded_es'], - :es_cluster => node['logstash']['elasticsearch_cluster'], - :patterns_dir => patterns_dir - ) - notifies :restart, service_resource - action :create - only_if { node['logstash']['server']['config_file'] } -end - -unless node['logstash']['server']['config_templates'].empty? || node['logstash']['server']['config_templates'].nil? - node['logstash']['server']['config_templates'].each do |config_template| - template "#{node['logstash']['server']['home']}/#{node['logstash']['server']['config_dir']}/#{config_template}.conf" do - source "#{config_template}.conf.erb" - cookbook node['logstash']['server']['config_templates_cookbook'] - owner node['logstash']['user'] - group node['logstash']['group'] - mode '0644' - variables node['logstash']['server']['config_templates_variables'][config_template] - notifies :restart, service_resource - action :create - end - end +# services are hard! +if node['logstash']['server']['init_method'] == 'runit' + include_recipe 'runit' + service_resource = 'runit_service[logstash_server]' +else + service_resource = 'service[logstash_server]' end -services = ['server'] +services = [node['logstash']['server']['name']] services << 'web' if node['logstash']['server']['web']['enable'] services.each do |type| @@ -212,12 +121,12 @@ owner 'root' group 'root' mode '0774' - variables(:config_file => node['logstash']['server']['config_dir'], - :home => node['logstash']['server']['home'], - :name => type, - :log_file => node['logstash']['server']['log_file'], - :max_heap => node['logstash']['server']['xmx'], - :min_heap => node['logstash']['server']['xms'] + variables(:config_file => node['logstash']['server']['config_dir'], + :home => node['logstash']['server']['home'], + :name => type, + :log_file => "#{node['logstash']['server']['home']/log/log_file}", + :max_heap => node['logstash']['server']['xmx'], + :min_heap => node['logstash']['server']['xms'] ) end @@ -231,6 +140,43 @@ end end + +# config files +template "#{node['logstash']['server']['home']}/etc/conf.d/#{node['logstash']['server']['config_file']}" do + source node['logstash']['server']['base_config'] + cookbook node['logstash']['server']['base_config_cookbook'] + owner node['logstash']['user'] + group node['logstash']['group'] + mode '0644' + variables( + :graphite_server_ip => graphite_server_ip, + :es_server_ip => es_server_ip, + :enable_embedded_es => node['logstash']['server']['enable_embedded_es'], + :es_cluster => node['logstash']['elasticsearch_cluster'], + :patterns_dir => patterns_dir + ) + notifies :restart, service_resource + action :create + only_if { node['logstash']['server']['config_file'] } +end + +unless node['logstash']['server']['config_templates'].empty? || node['logstash']['server']['config_templates'].nil? + node['logstash']['server']['config_templates'].each do |config_template| + template "#{node['logstash']['server']['home']}/etc/conf.d/#{config_template}.conf" do + source "#{config_template}.conf.erb" + cookbook node['logstash']['server']['config_templates_cookbook'] + owner node['logstash']['user'] + group node['logstash']['group'] + mode '0644' + variables node['logstash']['server']['config_templates_variables'][config_template] + notifies :restart, service_resource + action :create + end + end +end + +# set up logrotate +include_recipe 'logrotate' logrotate_app 'logstash_server' do path "#{log_dir}/*.log" size node['logstash']['logging']['maxSize'] if node['logstash']['logging']['useFileSize'] diff --git a/recipes/test.rb b/recipes/test.rb new file mode 100644 index 0000000..665bee2 --- /dev/null +++ b/recipes/test.rb @@ -0,0 +1,12 @@ +# Encoding: utf-8 +# +# Cookbook Name:: logstash +# Recipe:: default +# + +logstash_instance 'tarball' do + version '1.4.0.rc1' + checksum 'b015fa130d589af957c9a48e6f59754f5c0954835abf44bd013547a6b6520e59' + source_url 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.rc1.tar.gz' + install_type 'tarball' +end diff --git a/resources/instance.rb b/resources/instance.rb index ef30fdb..0e3c943 100644 --- a/resources/instance.rb +++ b/resources/instance.rb @@ -10,10 +10,10 @@ default_action :create if defined?(default_action) attribute :name, :kind_of => String, :name_attribute => true -attribute :base_directory, :kind_of => String, :default => "/opt/logstash" -attribute :install_type, :kind_of => String, :equal_to => ["source", "jar"], :default => 'jar' +attribute :base_directory, :kind_of => String +attribute :install_type, :kind_of => String attribute :auto_symlink, :kind_of => [TrueClass, FalseClass], :default => true -# version/checksum/source_url used by `jar` install_type +# version/checksum/source_url used by `jar`, `tarball` install_type attribute :version, :kind_of => String attribute :checksum, :kind_of => String attribute :source_url, :kind_of => String diff --git a/templates/default/logstash_server.conf.erb b/templates/default/logstash_server.conf.erb index 94dc3c2..ba4291f 100644 --- a/templates/default/logstash_server.conf.erb +++ b/templates/default/logstash_server.conf.erb @@ -22,7 +22,7 @@ script export HOME=$LOGSTASH_HOME export GC_OPTS="<%= node['logstash']['server']['gc_opts'] %>" export JAVA_OPTS="-server -Xms<%= node['logstash']['server']['xms'] %> -Xmx<%= node['logstash']['server']['xmx'] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= node['logstash']['server']['java_opts'] %> <%= '-Djava.net.preferIPv4Stack=true' if node['logstash']['agent']['ipv4_only'] %>" - export LOGSTASH_OPTS="agent -f <%= node['logstash']['server']['config_dir'] %>" + export LOGSTASH_OPTS="agent -f <%= node['logstash']['server']['home'] %>/etc/conf.d" <% if node['logstash']['server']['pluginpath'] -%> export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['server']['pluginpath'] %>" <% end -%> @@ -30,7 +30,7 @@ script export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['server']['workers'] %>" <% end %> <% if node['logstash']['server']['log_file'] -%> - export LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= node['logstash']['server']['log_file'] %>" + export LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= node['logstash']['server']['home'] %>/log/<%= node['logstash']['server']['log_file'] %>" <% end -%> <% if node['logstash']['server']['debug'] -%> export LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" diff --git a/templates/default/sv-logstash_server-run.erb b/templates/default/sv-logstash_server-run.erb index 77173e2..628e04e 100644 --- a/templates/default/sv-logstash_server-run.erb +++ b/templates/default/sv-logstash_server-run.erb @@ -9,7 +9,7 @@ exec 2>&1 LOGSTASH_HOME="<%= node['logstash']['server']['home'] %>" GC_OPTS="<%= node['logstash']['server']['gc_opts'] %>" JAVA_OPTS="-server -Xms<%= node['logstash']['server']['xms'] %> -Xmx<%= node['logstash']['server']['xmx'] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= node['logstash']['server']['java_opts'] %> <%= '-Djava.net.preferIPv4Stack=true' if node['logstash']['agent']['ipv4_only'] %>" -LOGSTASH_OPTS="agent -f <%= node['logstash']['server']['config_dir'] %>" +LOGSTASH_OPTS="agent -f <%= node['logstash']['server']['home'] %>/etc/conf.d" <% if node['logstash']['server']['pluginpath'] -%> LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['server']['pluginpath'] %>" <% end -%> @@ -19,11 +19,19 @@ LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" <% if node['logstash']['server']['log_file'] -%> LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= node['logstash']['server']['log_file'] %>" <% end -%> -<% if node['logstash']['server']['workers'] %> +<% if node['logstash']['server']['workers'] -%> export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['server']['workers'] %>" -<% end %> -<% if node['logstash']['supervisor_gid'] -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node["logstash"]["user"] %>:<%= node["logstash"]["supervisor_gid"] %> java $JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS +<% end -%> +<% if node['logstash']['server']['install_method'] == 'tarball' -%> +<% if node['logstash']['supervisor_gid'] -%> +HOME=$LOGSTASH_HOME exec chpst -u <%= node["logstash"]["user"] %>:<%= node["logstash"]["supervisor_gid"] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS +<% else -%> +HOME=$LOGSTASH_HOME exec chpst -u <%= node["logstash"]["user"] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS +<% end -%> <% else -%> +<% if node['logstash']['supervisor_gid'] -%> +HOME=$LOGSTASH_HOME exec chpst -u <%= node["logstash"]["user"] %>:<%= node["logstash"]["supervisor_gid"] %> java $JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS +<% else -%> HOME=$LOGSTASH_HOME exec chpst -u <%= node["logstash"]["user"] %> java $JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS +<% end -%> <% end -%> diff --git a/templates/default/sv-logstash_web-run.erb b/templates/default/sv-logstash_web-run.erb index 3ade8b6..98bba71 100644 --- a/templates/default/sv-logstash_web-run.erb +++ b/templates/default/sv-logstash_web-run.erb @@ -11,9 +11,16 @@ GC_OPTS="<%= node['logstash']['server']['gc_opts'] %>" LOGSTASH_OPTS="web -a <%= node['logstash']['server']['web']['address'] %> -p <%= node['logstash']['server']['web']['port'] %>" - -<% if ! node['logstash']['supervisor_gid'].empty? -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node['logstash']['user'] %>:<%= node['logstash']['supervisor_gid'] %> java $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS +<% if node['logstash']['server']['install_method'] == 'tarball' -%> +<% if ! node['logstash']['supervisor_gid'].empty? -%> +HOME=$LOGSTASH_HOME exec chpst -u <%= node['logstash']['user'] %>:<%= node['logstash']['supervisor_gid'] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS +<% else -%> +HOME=$LOGSTASH_HOME exec chpst -u <%= node['logstash']['user'] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS +<% end -%> <% else -%> +<% if ! node['logstash']['supervisor_gid'].empty? -%> +HOME=$LOGSTASH_HOME exec chpst -u <%= node['logstash']['user'] %>:<%= node['logstash']['supervisor_gid'] %> java $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS +<% else -%> HOME=$LOGSTASH_HOME exec chpst -u <%= node['logstash']['user'] %> java $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS -<% end -%> +<% end -%> +<% end -%> \ No newline at end of file From 505d92312e7103fb4617aafa9116b0c31eb96a16 Mon Sep 17 00:00:00 2001 From: paulczar Date: Sun, 16 Mar 2014 16:17:13 -0500 Subject: [PATCH 05/55] use pleaserun for services, cleanup of testing, remove zeromq/rabbit BS, move templates --- .rubocop.yml | 9 + .travis.yml | 6 +- Berksfile | 1 + Gemfile | 24 +- Gemfile.lock | 224 ++++++++++++++++-- Guardfile | 14 ++ Rakefile | 87 +++---- Vagrantfile | 104 ++++---- attributes/agent.rb | 2 +- attributes/beaver.rb | 2 +- attributes/default.rb | 87 ++++--- attributes/server.rb | 43 +--- libraries/logstash_conf.rb | 12 +- metadata.rb | 6 +- providers/instance.rb | 72 +++--- recipes/agent.rb | 44 ++-- recipes/beaver.rb | 30 +-- recipes/pyshipper.rb | 2 +- recipes/server.rb | 179 ++++---------- recipes/source.rb | 2 +- recipes/zero_mq_repo.rb | 37 --- resources/instance.rb | 28 +-- .../default/{ => config}/apache.conf.erb | 6 +- templates/default/config/stdout.conf.erb | 5 + .../default/{ => config}/syslog.conf.erb | 11 +- templates/default/patterns.erb | 3 - templates/default/patterns/patterns.erb | 3 + 27 files changed, 555 insertions(+), 488 deletions(-) create mode 100644 Guardfile delete mode 100644 recipes/zero_mq_repo.rb rename templates/default/{ => config}/apache.conf.erb (84%) create mode 100644 templates/default/config/stdout.conf.erb rename templates/default/{ => config}/syslog.conf.erb (60%) delete mode 100644 templates/default/patterns.erb create mode 100644 templates/default/patterns/patterns.erb diff --git a/.rubocop.yml b/.rubocop.yml index 33c419c..695bf01 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -17,3 +17,12 @@ LineLength: MethodLength: Enabled: true Max: 20 + +AccessorMethodName: + Enabled: false + +ClassAndModuleChildren: + Enabled: false + +UselessAssignment: + Enabled: false diff --git a/.travis.yml b/.travis.yml index e88cf5e..6d9c270 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: ruby gemfile: - - test/support/Gemfile rvm: - 1.9.3 -install: BUNDLE_GEMFILE=$PWD/test/support/Gemfile bundle install -script: BUNDLE_GEMFILE=$PWD/test/support/Gemfile bundle exec rake strainer \ No newline at end of file +install: bundle install +script: bundle exec berks install +script: bundle exec rake style spec \ No newline at end of file diff --git a/Berksfile b/Berksfile index 6544a42..16965fd 100644 --- a/Berksfile +++ b/Berksfile @@ -4,6 +4,7 @@ metadata cookbook 'rabbitmq', git: 'git://github.com/opscode-cookbooks/rabbitmq.git' cookbook 'java' +cookbook 'pleaserun', git: 'https://github.com/paulczar/chef-pleaserun.git' group :test do cookbook 'minitest-handler', git: 'git://github.com/btm/minitest-handler-cookbook.git' diff --git a/Gemfile b/Gemfile index 79c2e71..54eb03d 100644 --- a/Gemfile +++ b/Gemfile @@ -2,4 +2,26 @@ source 'https://rubygems.org' -gem 'berkshelf' +gem 'chefspec', '~> 3.2' +gem 'foodcritic', '~> 3.0' +gem 'rubocop', '~> 0.18' +gem 'chef', '~> 11.8' +gem 'berkshelf', '~> 2.0' +gem 'test-kitchen', '~> 1.1' +gem 'serverspec', '~> 0.14' +gem 'rake' + +group :chef_gems do + gem 'pleaserun' +end + +group :development do + gem 'guard', '~> 1.8' + gem 'guard-rubocop', '~> 0.2' + gem 'guard-foodcritic' +end + +group :infinite_loop_solver do + gem 'celluloid', '~> 0.14' + gem 'celluloid-io', '~> 0.14' +end diff --git a/Gemfile.lock b/Gemfile.lock index 2bfa194..bf02e09 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,19 +1,23 @@ GEM remote: https://rubygems.org/ specs: - activesupport (3.2.15) + activesupport (3.2.17) i18n (~> 0.6, >= 0.6.4) multi_json (~> 1.0) addressable (2.3.5) - akami (1.2.0) + akami (1.2.1) gyoku (>= 0.4.0) - nokogiri (>= 1.4.0) - berkshelf (2.0.10) + nokogiri + ast (1.1.0) + atomic (1.1.15) + avl_tree (1.1.3) + berkshelf (2.0.14) activesupport (~> 3.2.0) addressable (~> 2.3.4) buff-shell_out (~> 0.1) chozo (>= 0.6.1) - faraday (>= 0.8.5) + faraday (~> 0.8.0) + faraday (~> 0.8.5) hashie (>= 2.0.2) minitar (~> 0.5.4) rbzip2 (~> 0.2.0) @@ -25,54 +29,173 @@ GEM buff-extensions (~> 0.3) varia_model (~> 0.1) buff-extensions (0.5.0) - buff-ignore (1.1.0) + buff-ignore (1.1.1) buff-ruby_engine (0.1.0) buff-shell_out (0.1.1) buff-ruby_engine (~> 0.1.0) builder (3.2.2) + cabin (0.6.1) celluloid (0.14.1) timers (>= 1.0.0) celluloid-io (0.14.1) celluloid (>= 0.14.1) nio4r (>= 0.4.5) + chef (11.10.4) + chef-zero (~> 1.7, >= 1.7.2) + 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.3) + net-ssh (~> 2.6) + net-ssh-multi (~> 1.1) + ohai (~> 6.0) + pry (~> 0.9) + puma (~> 1.6) + rest-client (>= 1.0.4, < 1.7.0) + yajl-ruby (~> 1.1) + chef-zero (1.7.3) + hashie (~> 2.0) + json + mixlib-log (~> 1.3) + moneta (< 0.7.0) + rack + chefspec (3.4.0) + chef (~> 11.0) + fauxhai (~> 2.0) + rspec (~> 2.14) chozo (0.6.1) activesupport (>= 3.2.0) hashie (>= 2.0.2) multi_json (>= 1.3.0) + clamp (0.6.3) + coderay (1.1.0) + diff-lcs (1.2.5) erubis (2.7.0) - faraday (0.8.8) + faraday (0.8.9) multipart-post (~> 1.2.0) - ffi (1.9.0) + fauxhai (2.1.0) + 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.4) + gherkin (2.11.8) + multi_json (~> 1.3) gssapi (1.0.3) ffi (>= 1.0.1) - gyoku (1.1.0) + guard (1.8.3) + formatador (>= 0.2.4) + listen (~> 1.3) + lumberjack (>= 1.0.2) + pry (>= 0.9.10) + thor (>= 0.14.6) + guard-foodcritic (1.0.2) + foodcritic (>= 1.3, < 4.0) + guard (>= 1.0, < 3.0) + guard-rubocop (0.2.2) + guard (~> 1.8) + rubocop (~> 0.10) + gyoku (1.1.1) builder (>= 2.1.2) hashie (2.0.5) + highline (1.6.21) + hitimes (1.2.1) httpclient (2.3.4.1) httpi (0.9.7) rack - i18n (0.6.5) - json (1.7.7) + i18n (0.6.9) + insist (1.0.0) + ipaddress (0.8.0) + json (1.8.1) + listen (1.3.1) + rb-fsevent (>= 0.9.3) + rb-inotify (>= 0.9) + rb-kqueue (>= 0.2) little-plugger (1.1.3) - logging (1.8.1) + logging (1.8.2) little-plugger (>= 1.1.3) - multi_json (>= 1.3.6) - mini_portile (0.5.2) + multi_json (>= 1.8.4) + lumberjack (1.0.4) + method_source (0.8.2) + metriks (0.9.9.6) + atomic (~> 1.0) + avl_tree (~> 1.1.2) + hitimes (~> 1.1) + mime-types (1.25.1) minitar (0.5.4) mixlib-authentication (1.3.0) mixlib-log + mixlib-cli (1.4.0) + mixlib-config (2.1.0) mixlib-log (1.6.0) - multi_json (1.8.2) + mixlib-shellout (1.3.0) + moneta (0.6.0) + multi_json (1.9.0) multipart-post (1.2.0) - net-http-persistent (2.9) - net-ssh (2.7.0) - nio4r (0.5.0) - nokogiri (1.6.0) - mini_portile (~> 0.5.0) + mustache (0.99.5) + net-http-persistent (2.9.4) + net-scp (1.1.2) + net-ssh (>= 2.6.5) + net-ssh (2.8.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) + nokogiri (1.5.11) nori (1.1.5) + ohai (6.20.0) + ipaddress + mixlib-cli + mixlib-config + mixlib-log + mixlib-shellout + systemu (~> 2.5.2) + yajl-ruby + parser (2.1.7) + ast (~> 1.1) + slop (~> 3.4, >= 3.4.5) + pleaserun (0.0.4) + cabin (> 0) + cabin + clamp + insist + mustache + ohai (~> 6.20) + stud + polyglot (0.3.4) + powerpack (0.0.9) + pry (0.9.12.6) + coderay (~> 1.0) + method_source (~> 0.8) + slop (~> 3.4) + puma (1.6.3) + rack (~> 1.2) rack (1.5.2) + rainbow (2.0.0) + rake (10.1.1) + rb-fsevent (0.9.4) + rb-inotify (0.9.3) + ffi (>= 0.5.0) + rb-kqueue (0.2.2) + ffi (>= 0.5.0) rbzip2 (0.2.0) - retryable (1.3.3) + rest-client (1.6.7) + mime-types (>= 1.16) + retryable (1.3.5) ridley (1.5.3) addressable buff-config (~> 0.2) @@ -93,7 +216,23 @@ GEM solve (>= 0.4.4) varia_model (~> 0.1) winrm (~> 1.1.0) + rspec (2.14.1) + rspec-core (~> 2.14.0) + rspec-expectations (~> 2.14.0) + rspec-mocks (~> 2.14.0) + rspec-core (2.14.8) + rspec-expectations (2.14.5) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.14.6) + rubocop (0.19.0) + json (>= 1.7.7, < 2) + parser (~> 2.1.7) + powerpack (~> 0.0.6) + rainbow (>= 1.99.1, < 3.0) + ruby-progressbar (~> 1.4) + ruby-progressbar (1.4.2) rubyntlm (0.1.1) + safe_yaml (1.0.1) savon (0.9.5) akami (~> 1.0) builder (>= 2.1.2) @@ -102,11 +241,32 @@ GEM nokogiri (>= 1.4.0) nori (~> 1.0) wasabi (~> 1.0) - solve (0.8.1) + serverspec (0.15.4) + highline + net-ssh + rspec (>= 2.13.0) + specinfra (>= 0.7.1) + slop (3.5.0) + solve (0.8.2) + specinfra (0.7.1) + stud (0.0.17) + ffi + metriks + 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.18.1) - timers (1.1.0) + timers (2.0.0) + hitimes + treetop (1.4.15) + polyglot + polyglot (>= 0.3.1) uuidtools (2.1.4) - varia_model (0.2.0) + varia_model (0.3.2) buff-extensions (~> 0.2) hashie (>= 2.0.2) wasabi (1.0.0) @@ -119,9 +279,23 @@ GEM rubyntlm (~> 0.1.1) savon (= 0.9.5) uuidtools (~> 2.1.2) + yajl-ruby (1.1.0) PLATFORMS ruby DEPENDENCIES - berkshelf + berkshelf (~> 2.0) + celluloid (~> 0.14) + celluloid-io (~> 0.14) + chef (~> 11.8) + chefspec (~> 3.2) + foodcritic (~> 3.0) + guard (~> 1.8) + guard-foodcritic + guard-rubocop (~> 0.2) + pleaserun + rake + rubocop (~> 0.18) + serverspec (~> 0.14) + test-kitchen (~> 1.1) diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..f7b7ef8 --- /dev/null +++ b/Guardfile @@ -0,0 +1,14 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme + +guard :rubocop do + watch(%r{.+\.rb$}) + watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) } +end + +guard "foodcritic" do + watch(%r{attributes/.+\.rb$}) + watch(%r{providers/.+\.rb$}) + watch(%r{recipes/.+\.rb$}) + watch(%r{resources/.+\.rb$}) +end diff --git a/Rakefile b/Rakefile index 6452486..7201d99 100644 --- a/Rakefile +++ b/Rakefile @@ -1,73 +1,40 @@ -#!/usr/bin/env rake # Encoding: utf-8 +require 'bundler/setup' -@cookbook_path = '/tmp/logstash-cookbooks' -@cookbook = 'logstash' -@gemfile = "#{File.dirname(__FILE__)}/test/support/Gemfile" +namespace :style do + require 'rubocop/rake_task' + desc 'Run Ruby style checks' + Rubocop::RakeTask.new(:ruby) -desc 'install dependencies using Berkshelf' -task :install_deps do - install_deps + require 'foodcritic' + desc 'Run Chef style checks' + FoodCritic::Rake::LintTask.new(:chef) end -desc 'Runs foodcritic linter' -task :foodcritic do - if Gem::Version.new('1.9.2') <= Gem::Version.new(RUBY_VERSION.dup) - sandbox = File.join(File.dirname(__FILE__), %w{tmp foodcritic}, @cookbook) - prepare_test_sandbox(sandbox) +desc 'Run all style checks' +task style: ['style:chef', 'style:ruby'] - sh "foodcritic --epic-fail any #{File.dirname(sandbox)}" - else - puts "WARN: foodcritic run is skipped as Ruby #{RUBY_VERSION} is < 1.9.2." +require 'kitchen' +desc 'Run Test Kitchen integration tests' +task :integration do + Kitchen.logger = Kitchen.default_file_logger + Kitchen::Config.new.instances.each do |instance| + instance.test(:always) end end -desc 'Runs Strainer' -task :strainer do - if Gem::Version.new('1.9.2') <= Gem::Version.new(RUBY_VERSION.dup) - # sandbox = '/tmp/cookbook_logstash' - # prepare_test_sandbox(sandbox) - # rm_rf '/tmp/strainer' - install_deps - # puts gemfile - puts "BUNDLE_GEMFILE=#{@gemfile} bundle exec strainer test --sandbox=/tmp/strainer --cookbooks-path=#{@cookbook_path}" - system({ 'BUNDLE_GEMFILE' => @gemfile }, "bundle exec strainer test --sandbox=/tmp/strainer --cookbooks-path=#{@cookbook_path}") - else - puts "WARN: strainer run is skipped as Ruby #{RUBY_VERSION} is < 1.9.2." - end -end - -desc 'Runs Test Kitchen' -task :kitchen do - begin - require 'kitchen/rake_tasks' - Kitchen::RakeTasks.new - rescue LoadError - puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI'] - end - if Gem::Version.new('1.9.2') <= Gem::Version.new(RUBY_VERSION.dup) - install_deps - system({ 'BUNDLE_GEMFILE' => @gemfile }, 'bundle exec kitchen test --destroy=always') - end +require 'rspec/core/rake_task' +desc 'Run ChefSpec unit tests' +RSpec::Core::RakeTask.new(:spec) do |t, args| + t.rspec_opts = 'test/unit/spec' end -task default: 'strainer' - -private - -def install_deps - puts "BUNDLE_GEMFILE=#{@gemfile} bundle exec berks install --path=#{@cookbook_path}" - system({ 'BUNDLE_GEMFILE' => @gemfile }, "bundle exec berks install --path=#{@cookbook_path}") -end - -private - -def prepare_test_sandbox(sandbox) - files = %w{ *.md *.rb attributes definitions files providers Strainerfile .rubocop* - recipes resources templates test/integration test/serverspec } +# The default rake task should just run it all +task default: %w(style spec integration) - rm_rf sandbox - mkdir_p sandbox - cp_r Dir.glob("{#{files.join(',')}}"), sandbox - puts "\n\n" +begin + require 'kitchen/rake_tasks' + Kitchen::RakeTasks.new +rescue LoadError + puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI'] end diff --git a/Vagrantfile b/Vagrantfile index 7a321b9..1e0f74b 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -4,10 +4,10 @@ log_level = :info -chef_run_list = %w[ - java::default - logstash::server -] +chef_run_list = %w( + java::default + logstash::server +) # curl::default # minitest-handler::default # logstash::server @@ -17,57 +17,57 @@ chef_run_list = %w[ # ] chef_json = { - java: { - jdk_version: '7' + java: { + jdk_version: '7' + }, + kibana: { + webserver_listen: '0.0.0.0', + webserver: 'nginx', + install_type: 'file' }, - kibana: { - webserver_listen: '0.0.0.0', - webserver: 'nginx', - install_type: 'file' - }, - logstash: { - supervisor_gid: 'adm', - agent: { - server_ipaddress: '127.0.0.1', - xms: '128m', - xmx: '128m', - enable_embedded_es: false, - inputs: [ - file: { - type: 'syslog', - path: ['/var/log/syslog', '/var/log/messages'], - start_position: 'beginning' - } + logstash: { + supervisor_gid: 'adm', + agent: { + server_ipaddress: '127.0.0.1', + xms: '128m', + xmx: '128m', + enable_embedded_es: false, + inputs: [ + file: { + type: 'syslog', + path: ['/var/log/syslog', '/var/log/messages'], + start_position: 'beginning' + } ], - filters: [ - { - condition: 'if [type] == "syslog"', - block: { - grok: { - match: [ - 'message', - "%{SYSLOGTIMESTAMP:timestamp} %{IPORHOST:host} (?:%{PROG:program}(?:\[%{POSINT:pid}\])?: )?%{GREEDYDATA:message}" - ] - }, - date: { - match: [ - 'timestamp', - 'MMM d HH:mm:ss', - 'MMM dd HH:mm:ss', - 'ISO8601' - ] - } - } + filters: [ + { + condition: 'if [type] == "syslog"', + block: { + grok: { + match: [ + 'message', + "%{SYSLOGTIMESTAMP:timestamp} %{IPORHOST:host} (?:%{PROG:program}(?:\[%{POSINT:pid}\])?: )?%{GREEDYDATA:message}" + ] + }, + date: { + match: [ + 'timestamp', + 'MMM d HH:mm:ss', + 'MMM dd HH:mm:ss', + 'ISO8601' + ] } + } + } ] }, - server: { - xms: '128m', - xmx: '128m', - enable_embedded_es: true, - config_templates: ['apache'], - config_templates_variables: { apache: { type: 'apache' } }, - web: { enable: true } + server: { + xms: '128m', + xmx: '128m', + enable_embedded_es: true, + config_templates: ['apache'], + config_templates_variables: { apache: { type: 'apache' } }, + web: { enable: true } } } } @@ -87,9 +87,7 @@ Vagrant.configure('2') do |config| end config.vm.define :precise64 do |dist_config| - if Vagrant.has_plugin?("vagrant-cachier") - dist_config.cache.scope = :box - end + dist_config.cache.scope = :box if Vagrant.has_plugin?('vagrant-cachier') dist_config.vm.box = 'opscode-ubuntu-12.04' dist_config.vm.box_url = 'https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box' diff --git a/attributes/agent.rb b/attributes/agent.rb index 5f33809..7a843f3 100644 --- a/attributes/agent.rb +++ b/attributes/agent.rb @@ -31,7 +31,7 @@ default['logstash']['server']['upstart_with_sudo'] = false # logrotate options for logstash agent -default['logstash']['agent']['logrotate']['options'] = %w{ missingok notifempty } +default['logstash']['agent']['logrotate']['options'] = %w(missingok notifempty) # stop/start on logrotate? default['logstash']['agent']['logrotate']['stopstartprepost'] = false diff --git a/attributes/beaver.rb b/attributes/beaver.rb index 773fb8a..f1dbcce 100644 --- a/attributes/beaver.rb +++ b/attributes/beaver.rb @@ -9,5 +9,5 @@ default['logstash']['beaver']['outputs'] = [] default['logstash']['beaver']['format'] = 'json' -default['logstash']['beaver']['logrotate']['options'] = %w{ missingok notifempty compress copytruncate } +default['logstash']['beaver']['logrotate']['options'] = %w(missingok notifempty compress copytruncate) default['logstash']['beaver']['logrotate']['postrotate'] = 'invoke-rc.d logstash_beaver force-reload >/dev/null 2>&1 || true' diff --git a/attributes/default.rb b/attributes/default.rb index 876c8ce..c3eee27 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -1,18 +1,4 @@ # Encoding: utf-8 -default['logstash']['basedir'] = '/opt/logstash' -default['logstash']['user'] = 'logstash' -default['logstash']['uid'] = nil # set to nil to let system pick -default['logstash']['group'] = 'logstash' -default['logstash']['gid'] = nil # set to nil to let system pick -default['logstash']['default_version'] = '1.4.0.rc1' -default['logstash']['default_checksum'] = 'b015fa130d589af957c9a48e6f59754f5c0954835abf44bd013547a6b6520e59' -default['logstash']['default_source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.rc1.tar.gz' -default['logstash']['default_install_type'] = 'tarball' -default['logstash']['supervisor_gid'] = node['logstash']['group'] -default['logstash']['pid_dir'] = '/var/run/logstash' -default['logstash']['create_account'] = true -default['logstash']['join_groups'] = [] -default['logstash']['homedir'] = '/var/lib/logstash' # roles/flags for various search/discovery default['logstash']['graphite_role'] = 'graphite_server' @@ -24,22 +10,63 @@ default['logstash']['elasticsearch_port'] = '' default['logstash']['graphite_ip'] = '' -default['logstash']['patterns'] = {} -default['logstash']['install_zeromq'] = false -default['logstash']['install_rabbitmq'] = false +# Default logstash instance variables +default['logstash']['default']['basedir'] = '/opt/logstash' +default['logstash']['default']['user'] = 'logstash' +default['logstash']['default']['uid'] = nil # set to nil to let system pick +default['logstash']['default']['group'] = 'logstash' +default['logstash']['default']['gid'] = nil # set to nil to let system pick +default['logstash']['default']['supervisor_gid'] = node['logstash']['group'] +default['logstash']['default']['pid_dir'] = '/var/run/logstash' +default['logstash']['default']['create_account'] = true +default['logstash']['default']['join_groups'] = [] +default['logstash']['default']['homedir'] = '/var/lib/logstash' -case node['platform_family'] -when 'rhel' - default['logstash']['zeromq_packages'] = %w{ zeromq zeromq-devel } -when 'fedora' - default['logstash']['zeromq_packages'] = %w{ zeromq zeromq-devel } -when 'debian' - default['logstash']['zeromq_packages'] = %w{ libzmq3-dbg libzmq3-dev libzmq3 } -end +default['logstash']['default']['name'] = 'server' +default['logstash']['default']['home'] = "/opt/logstash/#{node['logstash']['default']['name']}" +default['logstash']['default']['version'] = '1.4.0.rc1' +default['logstash']['default']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.rc1.tar.gz' +default['logstash']['default']['checksum'] = 'b015fa130d589af957c9a48e6f59754f5c0954835abf44bd013547a6b6520e59' +default['logstash']['default']['install_type'] = 'tarball' +default['logstash']['default']['log_file'] = 'server.log' +default['logstash']['default']['base_config'] = 'server.conf.erb' # set blank if don't want data driven config -# Logging features -default['logstash']['logging']['rotateFrequency'] = 'daily' -default['logstash']['logging']['maxBackup'] = 10 -default['logstash']['logging']['maxSize'] = '10M' -default['logstash']['logging']['useFileSize'] = false +default['logstash']['default']['xms'] = '1024M' +default['logstash']['default']['xmx'] = '1024M' +default['logstash']['default']['java_opts'] = '' +default['logstash']['default']['gc_opts'] = '-XX:+UseParallelOldGC' +default['logstash']['default']['ipv4_only'] = false +default['logstash']['default']['debug'] = false +default['logstash']['default']['workers'] = 1 + +default['logstash']['default']['patterns_templates_cookbook'] = 'logstash' +default['logstash']['default']['patterns_templates'] = {} + +default['logstash']['default']['base_config_cookbook'] = 'logstash' +default['logstash']['default']['config_file'] = '' +default['logstash']['default']['config_templates'] = {} +default['logstash']['default']['config_templates_cookbook'] = 'logstash' +default['logstash']['default']['config_templates_variables'] = {} + +# allow control over the upstart config +default['logstash']['default']['upstart_with_sudo'] = false + +default['logstash']['default']['init_method'] = 'native' # native or runit +# roles/flags for various autoconfig/discovery components +default['logstash']['default']['enable_embedded_es'] = true +default['logstash']['default']['inputs'] = [] +default['logstash']['default']['filters'] = [] +default['logstash']['default']['outputs'] = [] + +default['logstash']['default']['web']['enable'] = false +default['logstash']['default']['web']['address'] = '0.0.0.0' +default['logstash']['default']['web']['port'] = '9292' + +default['logstash']['default']['logrotate']['options'] = %w(missingok notifempty compress copytruncate) + +# Logging features +default['logstash']['default']['logging']['rotateFrequency'] = 'daily' +default['logstash']['default']['logging']['maxBackup'] = 10 +default['logstash']['default']['logging']['maxSize'] = '10M' +default['logstash']['default']['logging']['useFileSize'] = false \ No newline at end of file diff --git a/attributes/server.rb b/attributes/server.rb index 5d5a68d..5cb371f 100644 --- a/attributes/server.rb +++ b/attributes/server.rb @@ -1,41 +1,6 @@ # Encoding: utf-8 -default['logstash']['server']['name'] = 'server' -default['logstash']['server']['home'] = "/opt/logstash/#{node['logstash']['server']['name']}" -default['logstash']['server']['version'] = '1.4.0.rc1' -default['logstash']['server']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.rc1.tar.gz' -default['logstash']['server']['checksum'] = 'b015fa130d589af957c9a48e6f59754f5c0954835abf44bd013547a6b6520e59' -default['logstash']['server']['install_method'] = 'tarball' -default['logstash']['server']['config_file'] = 'logstash.conf' -default['logstash']['server']['log_file'] = 'server.log' -default['logstash']['server']['base_config'] = 'server.conf.erb' # set blank if don't want data driven config +default['logstash']['server'] = node['logstash']['default'] -default['logstash']['server']['xms'] = '1024M' -default['logstash']['server']['xmx'] = '1024M' -default['logstash']['server']['java_opts'] = '' -default['logstash']['server']['gc_opts'] = '-XX:+UseParallelOldGC' -default['logstash']['server']['ipv4_only'] = false -default['logstash']['server']['debug'] = false -default['logstash']['server']['workers'] = 1 -default['logstash']['server']['patterns'] = [] - -default['logstash']['server']['base_config_cookbook'] = 'logstash' -default['logstash']['server']['config_templates'] = [] -default['logstash']['server']['config_templates_cookbook'] = 'logstash' -default['logstash']['server']['config_templates_variables'] = {} - -# allow control over the upstart config -default['logstash']['server']['upstart_with_sudo'] = false - -default['logstash']['server']['init_method'] = 'native' # native or runit -# roles/flags for various autoconfig/discovery components -default['logstash']['server']['enable_embedded_es'] = true - -default['logstash']['server']['inputs'] = [] -default['logstash']['server']['filters'] = [] -default['logstash']['server']['outputs'] = [] - -default['logstash']['server']['web']['enable'] = false -default['logstash']['server']['web']['address'] = '0.0.0.0' -default['logstash']['server']['web']['port'] = '9292' - -default['logstash']['server']['logrotate']['options'] = %w{ missingok notifempty compress copytruncate } \ No newline at end of file +override['logstash']['server']['config_templates'] = { 'syslog' => 'config/syslog.conf', 'stdout' => 'config/stdout.conf' } +override['logstash']['default']['patterns_templates'] = { 'default' => 'patterns/patterns' } +# override any defaults here. diff --git a/libraries/logstash_conf.rb b/libraries/logstash_conf.rb index 95a7619..e74e8f5 100644 --- a/libraries/logstash_conf.rb +++ b/libraries/logstash_conf.rb @@ -23,7 +23,7 @@ def self.hash_to_str(h, indent = 0) h.each do |k, v| case v when Hash, Mash - result << k + " {" + result << k + ' {' result << hash_to_str(v, indent) else indent += 4 @@ -39,9 +39,9 @@ def self.value_to_str(v, indent = 0) when String, Symbol, Fixnum, Float "\"#{v}\"" when Array - "[#{v.map { |e| value_to_str(e,indent) }.join(", ")}]" + "[#{v.map { |e| value_to_str(e, indent) }.join(", ")}]" when Hash, Mash - hash_to_str(v, indent) + "\n" + indent(indent + 4) + "}" + hash_to_str(v, indent) + "\n" + indent(indent + 4) + '}' when TrueClass, FalseClass v.to_s else @@ -51,7 +51,7 @@ def self.value_to_str(v, indent = 0) def self.key_value_to_str(k, v, indent = 0) if !v.nil? - #k.inspect + " => " + v.inspect + # k.inspect + " => " + v.inspect key_to_str(k) + ' => ' + value_to_str(v, indent) else key_to_str(k) @@ -105,7 +105,7 @@ def self.section_to_str(section, version = nil, patterns_dir = nil, indent = 0) end def indent(i) - res = String.new - i.times { res << " " } + res = '' + i.times { res << ' ' } res end diff --git a/metadata.rb b/metadata.rb index a25a8f8..fef32d1 100644 --- a/metadata.rb +++ b/metadata.rb @@ -7,14 +7,14 @@ long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.8.1' -%w{ ubuntu debian redhat centos scientific amazon fedora }.each do |os| +%w(ubuntu debian redhat centos scientific amazon fedora).each do |os| supports os end -%w{ build-essential runit git ant java logrotate rabbitmq yum python ark }.each do |ckbk| +%w(build-essential runit git ant java logrotate rabbitmq yum python ark pleaserun).each do |ckbk| depends ckbk end -%w{ yum apt }.each do |ckbk| +%w(yum apt).each do |ckbk| recommends ckbk end diff --git a/providers/instance.rb b/providers/instance.rb index 6bf24f4..cbf11b0 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -1,4 +1,4 @@ -# +# Encoding: utf-8 # Cookbook Name:: logstash # Provider:: instance # Author:: John E. Vincent @@ -11,11 +11,11 @@ include Chef::Mixin::ShellOut def load_current_resource - @base_directory = new_resource.base_directory || node['logstash']['basedir'] - @install_type = new_resource.install_type || node['logstash']['default_install_type'] - @version = new_resource.version || node['logstash']['default_version'] - @checksum = new_resource.checksum || node['logstash']['default_checksum'] - @source_url = new_resource.source_url || node['logstash']['default_source_url'] + @base_directory = new_resource.base_directory || node['logstash']['default']['basedir'] + @install_type = new_resource.install_type || node['logstash']['default']['install_type'] + @version = new_resource.version || node['logstash']['default']['version'] + @checksum = new_resource.checksum || node['logstash']['default']['checksum'] + @source_url = new_resource.source_url || node['logstash']['default']['source_url'] @repo = new_resource.repo @sha = new_resource.sha @java_home = new_resource.java_home @@ -23,7 +23,6 @@ def load_current_resource @group = new_resource.group @useropts = new_resource.user_opts.clone @instance_dir = "#{@base_directory}/#{new_resource.name}".clone - @updated = false @name = new_resource.name end @@ -38,7 +37,6 @@ def load_current_resource ls_basedir = @base_directory ls_user = @user ls_group = @group - ls_dir = @dir ls_name = @name ur = user ls_user do @@ -48,7 +46,7 @@ def load_current_resource manage_home true uid ls_uid end - set_updated(ur.updated_by_last_action?) + new_resource.updated_by_last_action(ur.updated_by_last_action?) gr = group ls_group do gid ls_gid @@ -56,11 +54,11 @@ def load_current_resource append true system true end - set_updated(gr.updated_by_last_action?) + new_resource.updated_by_last_action(gr.updated_by_last_action?) case @install_type when 'tarball' - @run_context.include_recipe "ark::default" + @run_context.include_recipe 'ark::default' arkit = ark ls_name do url ls_source_url checksum ls_checksum @@ -68,29 +66,29 @@ def load_current_resource group ls_group mode 0755 version ls_version - path "#{ls_basedir}" + path ls_basedir action :put end - set_updated(arkit.updated_by_last_action?) + new_resource.updated_by_last_action(arkit.updated_by_last_action?) - %w{bin etc lib log tmp etc/conf.d patterns}.each do |ldir| + %w(bin etc lib log tmp etc/conf.d patterns).each do |ldir| r = directory "#{@instance_dir}/#{ldir}" do action :create mode '0755' owner ls_user group ls_group end - set_updated(r.updated_by_last_action?) + new_resource.updated_by_last_action(r.updated_by_last_action?) end - when "jar" + when 'jar' bdr = directory @base_directory do action :create mode '0755' owner ls_user group ls_group end - set_updated(bdr.updated_by_last_action?) + new_resource.updated_by_last_action(bdr.updated_by_last_action?) idr = directory @instance_dir do action :create @@ -98,16 +96,16 @@ def load_current_resource owner ls_user group ls_group end - set_updated(idr.updated_by_last_action?) + new_resource.updated_by_last_action(idr.updated_by_last_action?) - %w{bin etc lib log tmp etc/conf.d patterns}.each do |ldir| + %w(bin etc lib log tmp etc/conf.d patterns).each do |ldir| r = directory "#{@instance_dir}/#{ldir}" do action :create mode '0755' owner ls_user group ls_group end - set_updated(r.updated_by_last_action?) + new_resource.updated_by_last_action(r.updated_by_last_action?) end rfr = remote_file "#{ls_instance_dir}/lib/logstash-#{ls_version}.jar" do @@ -117,22 +115,22 @@ def load_current_resource source ls_source_url checksum ls_checksum end - set_updated(rfr.updated_by_last_action?) + new_resource.updated_by_last_action(rfr.updated_by_last_action?) lr = link "#{ls_instance_dir}/lib/logstash.jar" do to "#{ls_instance_dir}/lib/logstash-#{ls_version}.jar" only_if { new_resource.auto_symlink } end - set_updated(lr.updated_by_last_action?) + new_resource.updated_by_last_action(lr.updated_by_last_action?) - when "source" + when 'source' bdr = directory @base_directory do action :create mode '0755' owner ls_user group ls_group end - set_updated(bdr.updated_by_last_action?) + new_resource.updated_by_last_action(bdr.updated_by_last_action?) idr = directory @instance_dir do action :create @@ -140,16 +138,16 @@ def load_current_resource owner ls_user group ls_group end - set_updated(idr.updated_by_last_action?) + new_resource.updated_by_last_action(idr.updated_by_last_action?) - %w{bin etc lib log tmp etc/conf.d patterns}.each do |ldir| + %w(bin etc lib log tmp etc/conf.d patterns).each do |ldir| r = directory "#{@instance_dir}/#{ldir}" do action :create mode '0755' owner ls_user group ls_group end - set_updated(r.updated_by_last_action?) + new_resource.updated_by_last_action(r.updated_by_last_action?) end sd = directory "#{@instance_dir}/source" do @@ -158,7 +156,7 @@ def load_current_resource group ls_group mode '0755' end - set_updated(sd.updated_by_last_action?) + new_resource.updated_by_last_action(sd.updated_by_last_action?) gr = git "#{@instance_dir}/source" do repository @repo @@ -167,31 +165,25 @@ def load_current_resource user ls_user group ls_group end - set_updated(gr.updated_by_last_action?) + new_resource.updated_by_last_action(gr.updated_by_last_action?) source_version = @sha || "v#{@version}" - er = execute "build-logstash" do + er = execute 'build-logstash' do cwd "#{@instance_dir}/source" - environment(:JAVA_HOME => @java_home) + environment(JAVA_HOME: @java_home) user ls_user # Changed from root cause building as root...WHA? command "make clean && make VERSION=#{source_version} jar" action :run creates "#{@instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" not_if "test -f #{@instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" end - set_updated(er.updated_by_last_action?) + new_resource.updated_by_last_action(er.updated_by_last_action?) lr = link "#{@instance_dir}/lib/logstash.jar" do to "#{@instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" only_if { new_resource.auto_symlink } end - set_updated(lr.updated_by_last_action?) + new_resource.updated_by_last_action(lr.updated_by_last_action?) else Chef::Application.fatal!("Unknown install type: #{@install_type}") end - new_resource.updated_by_last_action(@updated) -end - -private -def set_updated(u) - @updated = u unless @updated == true -end +end \ No newline at end of file diff --git a/recipes/agent.rb b/recipes/agent.rb index 000bded..73a0980 100644 --- a/recipes/agent.rb +++ b/recipes/agent.rb @@ -44,7 +44,7 @@ group node['logstash']['group'] end -%w{bin etc lib tmp log}.each do |ldir| +%w(bin etc lib tmp log).each do |ldir| directory "#{node['logstash']['agent']['home']}/#{ldir}" do action :create mode '0755' @@ -71,17 +71,17 @@ group node['logstash']['group'] end -node['logstash']['patterns'].each do |file, hash| - template_name = patterns_dir + '/' + file - template template_name do - source 'patterns.erb' - owner node['logstash']['user'] - group node['logstash']['group'] - variables(:patterns => hash) - mode '0644' - notifies :restart, service_resource - end -end +# node['logstash']['patterns'].each do |file, hash| +# template_name = patterns_dir + '/' + file +# template template_name do +# source 'patterns.erb' +# owner node['logstash']['user'] +# group node['logstash']['group'] +# variables(patterns: hash) +# mode '0644' +# notifies :restart, service_resource +# end +# end if node['logstash']['agent']['install_method'] == 'jar' remote_file "#{node['logstash']['agent']['home']}/lib/logstash-#{node['logstash']['agent']['version']}.jar" do @@ -114,8 +114,8 @@ group node['logstash']['group'] mode '0644' variables( - :logstash_server_ip => logstash_server_ip, - :patterns_dir => patterns_dir) + logstash_server_ip: logstash_server_ip, + patterns_dir: patterns_dir) notifies :restart, service_resource only_if { node['logstash']['agent']['config_file'] } end @@ -162,7 +162,7 @@ else Chef::Log.fatal("Please set node['logstash']['agent']['init_method'] to 'runit' for #{node['platform_version']}") end - elsif platform_family? 'fedora' && node['platform_version'] >= '15' + elsif platform_family?('fedora') && node['platform_version'] >= '15' execute 'reload-systemd' do command 'systemctl --system daemon-reload' action :nothing @@ -189,17 +189,17 @@ group 'root' mode '0774' variables( - :config_file => node['logstash']['agent']['config_dir'], - :home => node['logstash']['agent']['home'], - :log_file => node['logstash']['agent']['log_file'], - :name => 'agent', - :max_heap => node['logstash']['agent']['xmx'], - :min_heap => node['logstash']['agent']['xms'] + config_file: node['logstash']['agent']['config_dir'], + home: node['logstash']['agent']['home'], + log_file: node['logstash']['agent']['log_file'], + name: 'agent', + max_heap: node['logstash']['agent']['xmx'], + min_heap: node['logstash']['agent']['xms'] ) end service 'logstash_agent' do - supports :restart => true, :reload => true, :status => true + supports restart: true, reload: true, status: true action :enable end end diff --git a/recipes/beaver.rb b/recipes/beaver.rb index 08dc735..ffbc548 100644 --- a/recipes/beaver.rb +++ b/recipes/beaver.rb @@ -45,7 +45,7 @@ [ File.dirname(conf_file), File.dirname(log_file), - File.dirname(pid_file), + File.dirname(pid_file) ].each do |dir| directory dir do owner node['logstash']['user'] @@ -139,8 +139,8 @@ owner node['logstash']['user'] group node['logstash']['group'] variables( - :conf => conf, - :files => files + conf: conf, + files: files ) notifies :restart, 'service[logstash_beaver]' end @@ -163,17 +163,17 @@ mode '0644' source 'logstash_beaver.conf.erb' variables( - :cmd => cmd, - :group => node['logstash']['supervisor_gid'], - :user => node['logstash']['user'], - :log => log_file, - :supports_setuid => supports_setuid + cmd: cmd, + group: node['logstash']['supervisor_gid'], + user: node['logstash']['user'], + log: log_file, + supports_setuid: supports_setuid ) notifies :restart, 'service[logstash_beaver]' end service 'logstash_beaver' do - supports :restart => true, :reload => false + supports restart: true, reload: false action [:enable, :start] provider Chef::Provider::Service::Upstart end @@ -182,17 +182,17 @@ mode '0755' source 'init-beaver.erb' variables( - :cmd => cmd, - :pid_file => pid_file, - :user => node['logstash']['user'], - :log => log_file, - :platform => node['platform'] + cmd: cmd, + pid_file: pid_file, + user: node['logstash']['user'], + log: log_file, + platform: node['platform'] ) notifies :restart, 'service[logstash_beaver]' end service 'logstash_beaver' do - supports :restart => true, :reload => false, :status => true + supports restart: true, reload: false, status: true action [:enable, :start] end end diff --git a/recipes/pyshipper.rb b/recipes/pyshipper.rb index 50516ba..d5a9fe8 100644 --- a/recipes/pyshipper.rb +++ b/recipes/pyshipper.rb @@ -20,7 +20,7 @@ action :sync end -%w{pyzmq-static simplejson argparse}.each do |ppkg| +%w(pyzmq-static simplejson argparse).each do |ppkg| python_pip ppkg do action :install end diff --git a/recipes/server.rb b/recipes/server.rb index 9158bdd..0dc32cb 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -11,23 +11,18 @@ # # install logstash 'server' + logstash_instance node['logstash']['server']['name'] do base_directory node['logstash']['server']['basedir'] version node['logstash']['server']['version'] checksum node['logstash']['server']['checksum'] source_url node['logstash']['server']['source_url'] install_type node['logstash']['server']['install_type'] - user node['logstash']['user'] - group node['logstash']['group'] + user node['logstash']['server']['user'] + group node['logstash']['server']['group'] end - -# check for rabbit / zero install -include_recipe 'rabbitmq' if node['logstash']['install_rabbitmq'] -if node['logstash']['install_zeromq'] - include_recipe 'logstash::zero_mq_repo' - node['logstash']['zeromq_packages'].each { |p| package p } -end +logstash_home = "#{node['logstash']['server']['basedir']}/#{node['logstash']['server']['name']}" # fix search if chef solo if Chef::Config[:solo] @@ -50,138 +45,66 @@ end end +# services are hard! +include_recipe 'pleaserun::default' +service_resource = "service[logstash_#{node['logstash']['server']['name']}]" + +logstash_args = ['agent', '-f', "#{node['logstash']['server']['home']}/etc/conf.d/"] +logstash_args.concat ['--pluginpath', node['logstash']['server']['pluginpath']] if node['logstash']['server']['pluginpath'] +logstash_args.concat ['-vv'] if node['logstash']['server']['debug'] +logstash_args.concat ['-l', "#{logstash_home}/log/#{node['logstash']['server']['log_file']}"] if node['logstash']['server']['log_file'] +logstash_args.concat ['-w', node['logstash']['server']['workers'].to_s ] if node['logstash']['server']['workers'] + +pleaserun "logstash_#{node['logstash']['server']['name']}" do + name "logstash_#{node['logstash']['server']['name']}" + program "#{logstash_home}/bin/logstash" + args logstash_args + description "logstash_#{node['logstash']['server']['name']}" + chdir logstash_home + user node['logstash']['server']['user'] + group node['logstash']['server']['group'] + action :create +end + # add in any custom patterns -patterns_dir = "#{node['logstash']['server']['home']}/patterns" -node['logstash']['server']['patterns'].each do |file, hash| - template_name = patterns_dir + '/' + file - template template_name do - source 'patterns.erb' - owner node['logstash']['user'] - group node['logstash']['group'] - variables(:patterns => hash) +node['logstash']['server']['patterns_templates'].each do |name, template| + template "#{node['logstash']['server']['home']}/patterns/#{File.basename(template)}" do + source "#{template}.erb" + cookbook node['logstash']['server']['patterns_templates_cookbook'] + owner node['logstash']['server']['user'] + group node['logstash']['server']['group'] mode '0644' notifies :restart, service_resource + not_if { node['logstash']['server']['patterns_templates'].empty? } end end -# services are hard! -if node['logstash']['server']['init_method'] == 'runit' - include_recipe 'runit' - service_resource = 'runit_service[logstash_server]' -else - service_resource = 'service[logstash_server]' -end - -services = [node['logstash']['server']['name']] -services << 'web' if node['logstash']['server']['web']['enable'] - -services.each do |type| - if node['logstash']['server']['init_method'] == 'runit' - runit_service("logstash_#{type}") - elsif node['logstash']['server']['init_method'] == 'native' - if platform_family? 'debian' - if node['platform_version'] >= '12.04' - template "/etc/init/logstash_#{type}.conf" do - mode '0644' - source "logstash_#{type}.conf.erb" - end - - service "logstash_#{type}" do - provider Chef::Provider::Service::Upstart - action [:enable, :start] - end - else - Chef::Log.fatal("Please set node['logstash']['server']['init_method'] to 'runit' for #{node['platform_version']}") - end - - elsif platform_family? 'fedora' && node['platform_version'] >= '15' - execute 'reload-systemd' do - command 'systemctl --system daemon-reload' - action :nothing - end - - template '/etc/systemd/system/logstash_server.service' do - source 'logstash_server.service.erb' - owner 'root' - group 'root' - mode '0755' - notifies :run, 'execute[reload-systemd]', :immediately - notifies :restart, 'service[logstash_server]', :delayed - end - - service 'logstash_server' do - service_name 'logstash_server.service' - provider Chef::Provider::Service::Systemd - action [:enable, :start] - end - - elsif platform_family? 'rhel', 'fedora' - template "/etc/init.d/logstash_#{type}" do - source "init.logstash_#{type}.erb" - owner 'root' - group 'root' - mode '0774' - variables(:config_file => node['logstash']['server']['config_dir'], - :home => node['logstash']['server']['home'], - :name => type, - :log_file => "#{node['logstash']['server']['home']/log/log_file}", - :max_heap => node['logstash']['server']['xmx'], - :min_heap => node['logstash']['server']['xms'] - ) - end - - service "logstash_#{type}" do - supports :restart => true, :reload => true, :status => true - action [:enable, :start] - end - end - else - Chef::Log.fatal("Unsupported init method: #{node['logstash']['server']['init_method']}") +node['logstash']['server']['config_templates'].each do |name, template| + template "#{node['logstash']['server']['home']}/etc/conf.d/#{File.basename(template)}" do + source "#{template}.erb" + cookbook node['logstash']['server']['config_templates_cookbook'] + owner node['logstash']['server']['user'] + group node['logstash']['server']['group'] + mode '0644' + # variables node['logstash']['server']['config_templates_variables'][config_template] + notifies :restart, service_resource + action :create + not_if { node['logstash']['server']['config_templates'].empty? } end end - -# config files -template "#{node['logstash']['server']['home']}/etc/conf.d/#{node['logstash']['server']['config_file']}" do - source node['logstash']['server']['base_config'] - cookbook node['logstash']['server']['base_config_cookbook'] - owner node['logstash']['user'] - group node['logstash']['group'] - mode '0644' - variables( - :graphite_server_ip => graphite_server_ip, - :es_server_ip => es_server_ip, - :enable_embedded_es => node['logstash']['server']['enable_embedded_es'], - :es_cluster => node['logstash']['elasticsearch_cluster'], - :patterns_dir => patterns_dir - ) - notifies :restart, service_resource - action :create - only_if { node['logstash']['server']['config_file'] } -end - -unless node['logstash']['server']['config_templates'].empty? || node['logstash']['server']['config_templates'].nil? - node['logstash']['server']['config_templates'].each do |config_template| - template "#{node['logstash']['server']['home']}/etc/conf.d/#{config_template}.conf" do - source "#{config_template}.conf.erb" - cookbook node['logstash']['server']['config_templates_cookbook'] - owner node['logstash']['user'] - group node['logstash']['group'] - mode '0644' - variables node['logstash']['server']['config_templates_variables'][config_template] - notifies :restart, service_resource - action :create - end - end +service "logstash_#{node['logstash']['server']['name']}" do + supports restart: true, reload: true, start: true, enable: true + action [:enable, :start] end # set up logrotate include_recipe 'logrotate' -logrotate_app 'logstash_server' do +logrotate_app "logstash_#{node['logstash']['server']['name']}" do path "#{log_dir}/*.log" - size node['logstash']['logging']['maxSize'] if node['logstash']['logging']['useFileSize'] - frequency node['logstash']['logging']['rotateFrequency'] - rotate node['logstash']['logging']['maxBackup'] + size node['logstash']['server']['logging']['maxSize'] if node['logstash']['server']['logging']['useFileSize'] + frequency node['logstash']['server']['logging']['rotateFrequency'] + rotate node['logstash']['server']['logging']['maxBackup'] options node['logstash']['server']['logrotate']['options'] - create "664 #{node['logstash']['user']} #{node['logstash']['group']}" + create "664 #{node['logstash']['server']['user']} #{node['logstash']['server']['group']}" end diff --git a/recipes/source.rb b/recipes/source.rb index aa395ee..d8828c8 100644 --- a/recipes/source.rb +++ b/recipes/source.rb @@ -26,7 +26,7 @@ execute 'build-logstash' do cwd "#{node['logstash']['basedir']}/source" environment( - :JAVA_HOME => node['logstash']['source']['java_home'] + JAVA_HOME: node['logstash']['source']['java_home'] ) user 'root' # This variant is useful for troubleshooting stupid environment problems diff --git a/recipes/zero_mq_repo.rb b/recipes/zero_mq_repo.rb deleted file mode 100644 index f5247e9..0000000 --- a/recipes/zero_mq_repo.rb +++ /dev/null @@ -1,37 +0,0 @@ -# Encoding: utf-8 -# -# Cookbook Name:: logstash -# Recipe:: zero_mq_repo -# -# -include_recipe 'yum::default' - -major_version = node['platform_version'].split('.').first.to_i - -case -when platform_family?('rhel') - yum_repository 'zeromq' do - description 'zeromq repo' - baseurl "http://download.opensuse.org/repositories/home:/fengshuo:/zeromq/CentOS_CentOS-#{major_version}/" - gpgkey "http://download.opensuse.org/repositories/home:/fengshuo:/zeromq/CentOS_CentOS-#{major_version}/repodata/repomd.xml.key" - action :create - end -when platform_family?('debian') - apt_repository 'zeromq-ppa' do - uri 'http://ppa.launchpad.net/chris-lea/zeromq/ubuntu' - distribution node['lsb']['codename'] - components ['main'] - keyserver 'keyserver.ubuntu.com' - key 'C7917B12' - action :add - end - apt_repository 'libpgm-ppa' do - uri 'http://ppa.launchpad.net/chris-lea/libpgm/ubuntu' - distribution node['lsb']['codename'] - components ['main'] - keyserver 'keyserver.ubuntu.com' - key 'C7917B12' - action :add - notifies :run, 'execute[apt-get update]', :immediately - end -end diff --git a/resources/instance.rb b/resources/instance.rb index 0e3c943..d1b9415 100644 --- a/resources/instance.rb +++ b/resources/instance.rb @@ -1,4 +1,4 @@ -# +# Encoding: utf-8 # Cookbook Name:: logstash # Resource:: instance # Author:: John E. Vincent @@ -9,18 +9,18 @@ default_action :create if defined?(default_action) -attribute :name, :kind_of => String, :name_attribute => true -attribute :base_directory, :kind_of => String -attribute :install_type, :kind_of => String -attribute :auto_symlink, :kind_of => [TrueClass, FalseClass], :default => true +attribute :name, kind_of: String, name_attribute: true +attribute :base_directory, kind_of: String +attribute :install_type, kind_of: String +attribute :auto_symlink, kind_of: [TrueClass, FalseClass], default: true # version/checksum/source_url used by `jar`, `tarball` install_type -attribute :version, :kind_of => String -attribute :checksum, :kind_of => String -attribute :source_url, :kind_of => String +attribute :version, kind_of: String +attribute :checksum, kind_of: String +attribute :source_url, kind_of: String # sha/repo/java_home used by `source` install_type -attribute :sha, :kind_of => String, :default => 'HEAD' -attribute :repo, :kind_of => String, :default => 'git://github.com/logstash/logstash.git' -attribute :java_home, :kind_of => String, :default => '/usr/lib/jvm/java-6-openjdk' #openjdk6 on ubuntu -attribute :user, :kind_of => String, :default => "logstash" -attribute :group, :kind_of => String, :default => "logstash" -attribute :user_opts, :kind_of => Hash, :default => {:homedir => "/var/lib/logstash", :uid => nil, :gid => nil} +attribute :sha, kind_of: String, default: 'HEAD' +attribute :repo, kind_of: String, default: 'git://github.com/logstash/logstash.git' +attribute :java_home, kind_of: String, default: '/usr/lib/jvm/java-6-openjdk' # openjdk6 on ubuntu +attribute :user, kind_of: String, default: 'logstash' +attribute :group, kind_of: String, default: 'logstash' +attribute :user_opts, kind_of: Hash, default: { homedir: '/var/lib/logstash', uid: nil, gid: nil } diff --git a/templates/default/apache.conf.erb b/templates/default/config/apache.conf.erb similarity index 84% rename from templates/default/apache.conf.erb rename to templates/default/config/apache.conf.erb index 4fc9b95..01e628a 100644 --- a/templates/default/apache.conf.erb +++ b/templates/default/config/apache.conf.erb @@ -7,8 +7,8 @@ filter { date { match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z", "dd/MMM/yyyy:HH:mm:ss" ] } - geoip { - source => "clientip" - } +# geoip { +# source => "clientip" +# } } } \ No newline at end of file diff --git a/templates/default/config/stdout.conf.erb b/templates/default/config/stdout.conf.erb new file mode 100644 index 0000000..ae89df0 --- /dev/null +++ b/templates/default/config/stdout.conf.erb @@ -0,0 +1,5 @@ +output { + stdout { + codec => rubydebug + } +} \ No newline at end of file diff --git a/templates/default/syslog.conf.erb b/templates/default/config/syslog.conf.erb similarity index 60% rename from templates/default/syslog.conf.erb rename to templates/default/config/syslog.conf.erb index 0fb5220..1ae8174 100644 --- a/templates/default/syslog.conf.erb +++ b/templates/default/config/syslog.conf.erb @@ -1,10 +1,17 @@ +input { + tcp { + port => "5959" + type => "syslog" + } +} + filter { - if [type] == "<%= @type %>" { + if [type] == "syslog" { grok { overwrite => "message" match => [ "message", - "?%{SYSLOGTIMESTAMP:timestamp} %{IPORHOST:host} (?:%{PROG:program}(?:\[%{POSINT:pid}\])?: )?%{GREEDYDATA:message}" + "%{SYSLOGTIMESTAMP:timestamp} %{IPORHOST:host} (?:%{PROG:program}(?:\[%{POSINT:pid}\])?: )?%{GREEDYDATA:message}" ] } syslog_pri { } diff --git a/templates/default/patterns.erb b/templates/default/patterns.erb deleted file mode 100644 index 448ee7c..0000000 --- a/templates/default/patterns.erb +++ /dev/null @@ -1,3 +0,0 @@ -<% @patterns.sort.each do |name, pattern| -%> -<%= name %> <%= pattern %> -<% end -%> diff --git a/templates/default/patterns/patterns.erb b/templates/default/patterns/patterns.erb new file mode 100644 index 0000000..93ee935 --- /dev/null +++ b/templates/default/patterns/patterns.erb @@ -0,0 +1,3 @@ +# this file was created and managed by chef. +# see https://github.com/elasticsearch/logstash/tree/master/patterns +# for pattern examples. From 12f1eb1b3dee39f2900cc69250b209171a47f995 Mon Sep 17 00:00:00 2001 From: paulczar Date: Sun, 16 Mar 2014 18:36:16 -0500 Subject: [PATCH 06/55] logstash service creation lwrp --- Gemfile | 1 + Gemfile.lock | 3 ++ attributes/default.rb | 108 +++++++++++++++++++++--------------------- attributes/server.rb | 6 +-- providers/instance.rb | 70 ++++++++++++++++++++------- providers/service.rb | 62 ++++++++++++++++++++++++ recipes/server.rb | 100 ++++++++++++++++++-------------------- resources/service.rb | 20 ++++++++ 8 files changed, 241 insertions(+), 129 deletions(-) create mode 100644 providers/service.rb create mode 100644 resources/service.rb diff --git a/Gemfile b/Gemfile index 54eb03d..1533eca 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gem 'rubocop', '~> 0.18' gem 'chef', '~> 11.8' gem 'berkshelf', '~> 2.0' gem 'test-kitchen', '~> 1.1' +gem 'kitchen-vagrant' gem 'serverspec', '~> 0.14' gem 'rake' diff --git a/Gemfile.lock b/Gemfile.lock index bf02e09..fc7a881 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -119,6 +119,8 @@ GEM insist (1.0.0) ipaddress (0.8.0) json (1.8.1) + kitchen-vagrant (0.14.0) + test-kitchen (~> 1.0) listen (1.3.1) rb-fsevent (>= 0.9.3) rb-inotify (>= 0.9) @@ -294,6 +296,7 @@ DEPENDENCIES guard (~> 1.8) guard-foodcritic guard-rubocop (~> 0.2) + kitchen-vagrant pleaserun rake rubocop (~> 0.18) diff --git a/attributes/default.rb b/attributes/default.rb index c3eee27..5ceb3eb 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -1,72 +1,72 @@ # Encoding: utf-8 # roles/flags for various search/discovery -default['logstash']['graphite_role'] = 'graphite_server' -default['logstash']['graphite_query'] = "roles:#{node['logstash']['graphite_role']} AND chef_environment:#{node.chef_environment}" -default['logstash']['elasticsearch_role'] = 'elasticsearch_server' -default['logstash']['elasticsearch_query'] = "roles:#{node['logstash']['elasticsearch_role']} AND chef_environment:#{node.chef_environment}" -default['logstash']['elasticsearch_cluster'] = 'logstash' -default['logstash']['elasticsearch_ip'] = '' -default['logstash']['elasticsearch_port'] = '' -default['logstash']['graphite_ip'] = '' +default['logstash']['instance']['default']['graphite_role'] = 'graphite_server' +default['logstash']['instance']['default']['graphite_query'] = "roles:#{node['logstash']['graphite_role']} AND chef_environment:#{node.chef_environment}" +default['logstash']['instance']['default']['elasticsearch_role'] = 'elasticsearch_server' +default['logstash']['instance']['default']['elasticsearch_query'] = "roles:#{node['logstash']['elasticsearch_role']} AND chef_environment:#{node.chef_environment}" +default['logstash']['instance']['default']['elasticsearch_cluster'] = 'logstash' +default['logstash']['instance']['default']['elasticsearch_ip'] = '' +default['logstash']['instance']['default']['elasticsearch_port'] = '' +default['logstash']['instance']['default']['graphite_ip'] = '' # Default logstash instance variables -default['logstash']['default']['basedir'] = '/opt/logstash' -default['logstash']['default']['user'] = 'logstash' -default['logstash']['default']['uid'] = nil # set to nil to let system pick -default['logstash']['default']['group'] = 'logstash' -default['logstash']['default']['gid'] = nil # set to nil to let system pick -default['logstash']['default']['supervisor_gid'] = node['logstash']['group'] -default['logstash']['default']['pid_dir'] = '/var/run/logstash' -default['logstash']['default']['create_account'] = true -default['logstash']['default']['join_groups'] = [] -default['logstash']['default']['homedir'] = '/var/lib/logstash' +default['logstash']['instance']['default']['basedir'] = '/opt/logstash' +default['logstash']['instance']['default']['user'] = 'logstash' +default['logstash']['instance']['default']['uid'] = nil # set to nil to let system pick +default['logstash']['instance']['default']['group'] = 'logstash' +default['logstash']['instance']['default']['gid'] = nil # set to nil to let system pick +default['logstash']['instance']['default']['supervisor_gid'] = node['logstash']['group'] +default['logstash']['instance']['default']['pid_dir'] = '/var/run/logstash' +default['logstash']['instance']['default']['create_account'] = true +default['logstash']['instance']['default']['join_groups'] = [] +default['logstash']['instance']['default']['homedir'] = '/var/lib/logstash' -default['logstash']['default']['name'] = 'server' -default['logstash']['default']['home'] = "/opt/logstash/#{node['logstash']['default']['name']}" -default['logstash']['default']['version'] = '1.4.0.rc1' -default['logstash']['default']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.rc1.tar.gz' -default['logstash']['default']['checksum'] = 'b015fa130d589af957c9a48e6f59754f5c0954835abf44bd013547a6b6520e59' -default['logstash']['default']['install_type'] = 'tarball' -default['logstash']['default']['log_file'] = 'server.log' -default['logstash']['default']['base_config'] = 'server.conf.erb' # set blank if don't want data driven config +default['logstash']['instance']['default']['name'] = 'server' +default['logstash']['instance']['default']['home'] = "/opt/logstash/#{node['logstash']['instance']['default']['name']}" +default['logstash']['instance']['default']['version'] = '1.4.0.rc1' +default['logstash']['instance']['default']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.rc1.tar.gz' +default['logstash']['instance']['default']['checksum'] = 'b015fa130d589af957c9a48e6f59754f5c0954835abf44bd013547a6b6520e59' +default['logstash']['instance']['default']['install_type'] = 'tarball' +default['logstash']['instance']['default']['log_file'] = 'server.log' +default['logstash']['instance']['default']['base_config'] = 'server.conf.erb' # set blank if don't want data driven config -default['logstash']['default']['xms'] = '1024M' -default['logstash']['default']['xmx'] = '1024M' -default['logstash']['default']['java_opts'] = '' -default['logstash']['default']['gc_opts'] = '-XX:+UseParallelOldGC' -default['logstash']['default']['ipv4_only'] = false -default['logstash']['default']['debug'] = false -default['logstash']['default']['workers'] = 1 +default['logstash']['instance']['default']['xms'] = '1024M' +default['logstash']['instance']['default']['xmx'] = '1024M' +default['logstash']['instance']['default']['java_opts'] = '' +default['logstash']['instance']['default']['gc_opts'] = '-XX:+UseParallelOldGC' +default['logstash']['instance']['default']['ipv4_only'] = false +default['logstash']['instance']['default']['debug'] = false +default['logstash']['instance']['default']['workers'] = 1 -default['logstash']['default']['patterns_templates_cookbook'] = 'logstash' -default['logstash']['default']['patterns_templates'] = {} +default['logstash']['instance']['default']['patterns_templates_cookbook'] = 'logstash' +default['logstash']['instance']['default']['patterns_templates'] = {} -default['logstash']['default']['base_config_cookbook'] = 'logstash' -default['logstash']['default']['config_file'] = '' -default['logstash']['default']['config_templates'] = {} -default['logstash']['default']['config_templates_cookbook'] = 'logstash' -default['logstash']['default']['config_templates_variables'] = {} +default['logstash']['instance']['default']['base_config_cookbook'] = 'logstash' +default['logstash']['instance']['default']['config_file'] = '' +default['logstash']['instance']['default']['config_templates'] = {} +default['logstash']['instance']['default']['config_templates_cookbook'] = 'logstash' +default['logstash']['instance']['default']['config_templates_variables'] = {} # allow control over the upstart config -default['logstash']['default']['upstart_with_sudo'] = false +default['logstash']['instance']['default']['upstart_with_sudo'] = false -default['logstash']['default']['init_method'] = 'native' # native or runit +default['logstash']['instance']['default']['init_method'] = 'native' # native or runit # roles/flags for various autoconfig/discovery components -default['logstash']['default']['enable_embedded_es'] = true +default['logstash']['instance']['default']['enable_embedded_es'] = true -default['logstash']['default']['inputs'] = [] -default['logstash']['default']['filters'] = [] -default['logstash']['default']['outputs'] = [] +default['logstash']['instance']['default']['inputs'] = [] +default['logstash']['instance']['default']['filters'] = [] +default['logstash']['instance']['default']['outputs'] = [] -default['logstash']['default']['web']['enable'] = false -default['logstash']['default']['web']['address'] = '0.0.0.0' -default['logstash']['default']['web']['port'] = '9292' +default['logstash']['instance']['default']['web']['enable'] = false +default['logstash']['instance']['default']['web']['address'] = '0.0.0.0' +default['logstash']['instance']['default']['web']['port'] = '9292' -default['logstash']['default']['logrotate']['options'] = %w(missingok notifempty compress copytruncate) +default['logstash']['instance']['default']['logrotate']['options'] = %w(missingok notifempty compress copytruncate) # Logging features -default['logstash']['default']['logging']['rotateFrequency'] = 'daily' -default['logstash']['default']['logging']['maxBackup'] = 10 -default['logstash']['default']['logging']['maxSize'] = '10M' -default['logstash']['default']['logging']['useFileSize'] = false \ No newline at end of file +default['logstash']['instance']['default']['logging']['rotateFrequency'] = 'daily' +default['logstash']['instance']['default']['logging']['maxBackup'] = 10 +default['logstash']['instance']['default']['logging']['maxSize'] = '10M' +default['logstash']['instance']['default']['logging']['useFileSize'] = false diff --git a/attributes/server.rb b/attributes/server.rb index 5cb371f..beec82e 100644 --- a/attributes/server.rb +++ b/attributes/server.rb @@ -1,6 +1,6 @@ # Encoding: utf-8 -default['logstash']['server'] = node['logstash']['default'] +default['logstash']['instance']['server'] = node['logstash']['instance']['default'] -override['logstash']['server']['config_templates'] = { 'syslog' => 'config/syslog.conf', 'stdout' => 'config/stdout.conf' } -override['logstash']['default']['patterns_templates'] = { 'default' => 'patterns/patterns' } +override['logstash']['instance']['server']['config_templates'] = { 'syslog' => 'config/syslog.conf', 'stdout' => 'config/stdout.conf' } +override['logstash']['instance']['server']['patterns_templates'] = { 'default' => 'patterns/patterns' } # override any defaults here. diff --git a/providers/instance.rb b/providers/instance.rb index cbf11b0..86c9370 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -11,11 +11,11 @@ include Chef::Mixin::ShellOut def load_current_resource - @base_directory = new_resource.base_directory || node['logstash']['default']['basedir'] - @install_type = new_resource.install_type || node['logstash']['default']['install_type'] - @version = new_resource.version || node['logstash']['default']['version'] - @checksum = new_resource.checksum || node['logstash']['default']['checksum'] - @source_url = new_resource.source_url || node['logstash']['default']['source_url'] + @base_directory = new_resource.base_directory || node['logstash']['instance']['default']['basedir'] + @install_type = new_resource.install_type || node['logstash']['instance']['default']['install_type'] + @version = new_resource.version || node['logstash']['instance']['default']['version'] + @checksum = new_resource.checksum || node['logstash']['instance']['default']['checksum'] + @source_url = new_resource.source_url || node['logstash']['instance']['default']['source_url'] @repo = new_resource.repo @sha = new_resource.sha @java_home = new_resource.java_home @@ -26,6 +26,39 @@ def load_current_resource @name = new_resource.name end +action :delete do + ls_instance_dir = @instance_dir + ls_name = @name + ls_user = @user + ls_group = @group + + idr = directory ls_instance_dir do + recursive true + action :delete + end + new_resource.updated_by_last_action(idr.updated_by_last_action?) + + delete_user = true + delete_group = true + node['logstash']['instance'].each do |instance, contents| + next if instance == ls_name + delete_user = false if node['logstash']['instance'][instance]['user'] + delete_group = false if node['logstash']['instance'][instance]['group'] + end + + ur = user ls_user do + action :remove + only_if { delete_user } + end + new_resource.updated_by_last_action(ur.updated_by_last_action?) + + gr = group ls_group do + action :remove + only_if { delete_group } + end + new_resource.updated_by_last_action(gr.updated_by_last_action?) +end + action :create do ls_homedir = @useropts[:homedir] @@ -38,6 +71,7 @@ def load_current_resource ls_user = @user ls_group = @group ls_name = @name + ls_instance_dir = @instance_dir ur = user ls_user do home ls_homedir @@ -72,7 +106,7 @@ def load_current_resource new_resource.updated_by_last_action(arkit.updated_by_last_action?) %w(bin etc lib log tmp etc/conf.d patterns).each do |ldir| - r = directory "#{@instance_dir}/#{ldir}" do + r = directory "#{ls_instance_dir}/#{ldir}" do action :create mode '0755' owner ls_user @@ -90,7 +124,7 @@ def load_current_resource end new_resource.updated_by_last_action(bdr.updated_by_last_action?) - idr = directory @instance_dir do + idr = directory ls_instance_dir do action :create mode '0755' owner ls_user @@ -99,7 +133,7 @@ def load_current_resource new_resource.updated_by_last_action(idr.updated_by_last_action?) %w(bin etc lib log tmp etc/conf.d patterns).each do |ldir| - r = directory "#{@instance_dir}/#{ldir}" do + r = directory "#{ls_instance_dir}/#{ldir}" do action :create mode '0755' owner ls_user @@ -132,7 +166,7 @@ def load_current_resource end new_resource.updated_by_last_action(bdr.updated_by_last_action?) - idr = directory @instance_dir do + idr = directory ls_instance_dir do action :create mode '0755' owner ls_user @@ -141,7 +175,7 @@ def load_current_resource new_resource.updated_by_last_action(idr.updated_by_last_action?) %w(bin etc lib log tmp etc/conf.d patterns).each do |ldir| - r = directory "#{@instance_dir}/#{ldir}" do + r = directory "#{ls_instance_dir}/#{ldir}" do action :create mode '0755' owner ls_user @@ -150,7 +184,7 @@ def load_current_resource new_resource.updated_by_last_action(r.updated_by_last_action?) end - sd = directory "#{@instance_dir}/source" do + sd = directory "#{ls_instance_dir}/source" do action :create owner ls_user group ls_group @@ -158,7 +192,7 @@ def load_current_resource end new_resource.updated_by_last_action(sd.updated_by_last_action?) - gr = git "#{@instance_dir}/source" do + gr = git "#{ls_instance_dir}/source" do repository @repo reference @sha action :sync @@ -169,21 +203,21 @@ def load_current_resource source_version = @sha || "v#{@version}" er = execute 'build-logstash' do - cwd "#{@instance_dir}/source" + cwd "#{ls_instance_dir}/source" environment(JAVA_HOME: @java_home) user ls_user # Changed from root cause building as root...WHA? command "make clean && make VERSION=#{source_version} jar" action :run - creates "#{@instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" - not_if "test -f #{@instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" + creates "#{ls_instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" + not_if "test -f #{ls_instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" end new_resource.updated_by_last_action(er.updated_by_last_action?) - lr = link "#{@instance_dir}/lib/logstash.jar" do - to "#{@instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" + lr = link "#{ls_instance_dir}/lib/logstash.jar" do + to "#{ls_instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" only_if { new_resource.auto_symlink } end new_resource.updated_by_last_action(lr.updated_by_last_action?) else Chef::Application.fatal!("Unknown install type: #{@install_type}") end -end \ No newline at end of file +end diff --git a/providers/service.rb b/providers/service.rb new file mode 100644 index 0000000..facc0eb --- /dev/null +++ b/providers/service.rb @@ -0,0 +1,62 @@ +# Encoding: utf-8 +# Cookbook Name:: logstash +# Provider:: service +# Author:: John E. Vincent +# License:: Apache 2.0 +# +# Copyright 2014, John E. Vincent + +require 'chef/mixin/shell_out' +require 'chef/mixin/language' +include Chef::Mixin::ShellOut + +def load_current_resource + @instance = new_resource.instance + @service_name = new_resource.service_name || "logstash_#{@instance}" + @home = "#{node['logstash']['instance'][@instance]['basedir']}/#{@instance}" + @method = new_resource.method + @command = new_resource.command || "#{@home}/bin/logstash" + @args = new_resource.args || default_args + @description = new_resource.description || @service_name + @chdir = new_resource.chdir || @home + @user = new_resource.user || node['logstash']['instance'][@instance]['user'] + @group = new_resource.group || node['logstash']['instance'][@instance]['group'] +end + +action :create do + @run_context.include_recipe 'pleaserun::default' + svc_name = @instance + svc_service_name = @service_name + svc_home = @home + svc_method = @method + svc_command = @command + svc_args = @args + svc_description = @description + svc_chdir = @chdir + svc_user = @user + svc_group = @group + pr = pleaserun svc_service_name do + name svc_service_name + program svc_command + args svc_args + description svc_description + chdir svc_chdir + user svc_user + group svc_group + action :create + not_if { ::File.exists?("/etc/init.d/#{svc_service_name}") } + end + new_resource.updated_by_last_action(pr.updated_by_last_action?) +end + +private + +def default_args + logstash_home = "#{node['logstash']['instance'][@instance]['basedir']}/#{@instance}" + args = ['agent', '-f', "#{node['logstash']['instance'][@instance]['home']}/etc/conf.d/"] + args.concat ['--pluginpath', node['logstash']['instance'][@instance]['pluginpath']] if node['logstash']['instance'][@instance]['pluginpath'] + args.concat ['-vv'] if node['logstash']['instance'][@instance]['debug'] + args.concat ['-l', "#{logstash_home}/log/#{node['logstash']['instance'][@instance]['log_file']}"] if node['logstash']['instance'][@instance]['log_file'] + args.concat ['-w', node['logstash']['instance'][@instance]['workers'].to_s] if node['logstash']['instance'][@instance]['workers'] + args +end diff --git a/recipes/server.rb b/recipes/server.rb index 0dc32cb..b33b6db 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -12,99 +12,91 @@ # install logstash 'server' -logstash_instance node['logstash']['server']['name'] do - base_directory node['logstash']['server']['basedir'] - version node['logstash']['server']['version'] - checksum node['logstash']['server']['checksum'] - source_url node['logstash']['server']['source_url'] - install_type node['logstash']['server']['install_type'] - user node['logstash']['server']['user'] - group node['logstash']['server']['group'] +name = 'server' +node.override['logstash']['instance'][name]['name'] = name +node.save unless Chef::Config[:solo] + +# these should all default correctly. listing out for example. +logstash_instance name do + base_directory node['logstash']['instance'][name]['basedir'] + version node['logstash']['instance'][name]['version'] + checksum node['logstash']['instance'][name]['checksum'] + source_url node['logstash']['instance'][name]['source_url'] + install_type node['logstash']['instance'][name]['install_type'] + user node['logstash']['instance'][name]['user'] + group node['logstash']['instance'][name]['group'] + action :create # :delete to remove end -logstash_home = "#{node['logstash']['server']['basedir']}/#{node['logstash']['server']['name']}" +logstash_home = "#{node['logstash']['instance'][name]['basedir']}/#{name}" # fix search if chef solo if Chef::Config[:solo] - es_server_ip = node['logstash']['elasticsearch_ip'] - graphite_server_ip = node['logstash']['graphite_ip'] + es_server_ip = node['logstash']['instance'][name]['elasticsearch_ip'] + graphite_server_ip = node['logstash']['instance'][name]['graphite_ip'] else - es_results = search(:node, node['logstash']['elasticsearch_query']) - graphite_results = search(:node, node['logstash']['graphite_query']) + es_results = search(:node, node['logstash']['instance'][name]['elasticsearch_query']) + graphite_results = search(:node, node['logstash']['instance'][name]['graphite_query']) if !es_results.empty? es_server_ip = es_results[0]['ipaddress'] else - es_server_ip = node['logstash']['elasticsearch_ip'] + es_server_ip = node['logstash']['instance'][name]['elasticsearch_ip'] end if !graphite_results.empty? graphite_server_ip = graphite_results[0]['ipaddress'] else - graphite_server_ip = node['logstash']['graphite_ip'] + graphite_server_ip = node['logstash']['instance'][name]['graphite_ip'] end end # services are hard! -include_recipe 'pleaserun::default' -service_resource = "service[logstash_#{node['logstash']['server']['name']}]" - -logstash_args = ['agent', '-f', "#{node['logstash']['server']['home']}/etc/conf.d/"] -logstash_args.concat ['--pluginpath', node['logstash']['server']['pluginpath']] if node['logstash']['server']['pluginpath'] -logstash_args.concat ['-vv'] if node['logstash']['server']['debug'] -logstash_args.concat ['-l', "#{logstash_home}/log/#{node['logstash']['server']['log_file']}"] if node['logstash']['server']['log_file'] -logstash_args.concat ['-w', node['logstash']['server']['workers'].to_s ] if node['logstash']['server']['workers'] +service_resource = "service[logstash_#{name}]" -pleaserun "logstash_#{node['logstash']['server']['name']}" do - name "logstash_#{node['logstash']['server']['name']}" - program "#{logstash_home}/bin/logstash" - args logstash_args - description "logstash_#{node['logstash']['server']['name']}" - chdir logstash_home - user node['logstash']['server']['user'] - group node['logstash']['server']['group'] +logstash_service name do action :create end # add in any custom patterns -node['logstash']['server']['patterns_templates'].each do |name, template| - template "#{node['logstash']['server']['home']}/patterns/#{File.basename(template)}" do - source "#{template}.erb" - cookbook node['logstash']['server']['patterns_templates_cookbook'] - owner node['logstash']['server']['user'] - group node['logstash']['server']['group'] +node['logstash']['instance'][name]['patterns_templates'].each do |template, file| + template "#{node['logstash']['instance'][name]['home']}/patterns/#{File.basename(file)}" do + source "#{file}.erb" + cookbook node['logstash']['instance'][name]['patterns_templates_cookbook'] + owner node['logstash']['instance'][name]['user'] + group node['logstash']['instance'][name]['group'] mode '0644' notifies :restart, service_resource - not_if { node['logstash']['server']['patterns_templates'].empty? } + not_if { node['logstash']['instance'][name]['patterns_templates'].empty? } end end -node['logstash']['server']['config_templates'].each do |name, template| - template "#{node['logstash']['server']['home']}/etc/conf.d/#{File.basename(template)}" do - source "#{template}.erb" - cookbook node['logstash']['server']['config_templates_cookbook'] - owner node['logstash']['server']['user'] - group node['logstash']['server']['group'] +node['logstash']['instance'][name]['config_templates'].each do |template, file| + template "#{node['logstash']['instance'][name]['home']}/etc/conf.d/#{File.basename(file)}" do + source "#{file}.erb" + cookbook node['logstash']['instance'][name]['config_templates_cookbook'] + owner node['logstash']['instance'][name]['user'] + group node['logstash']['instance'][name]['group'] mode '0644' - # variables node['logstash']['server']['config_templates_variables'][config_template] + # variables node['logstash']['instance'][name]['config_templates_variables'][config_template] notifies :restart, service_resource action :create - not_if { node['logstash']['server']['config_templates'].empty? } + not_if { node['logstash']['instance'][name]['config_templates'].empty? } end end -service "logstash_#{node['logstash']['server']['name']}" do +service "logstash_#{name}" do supports restart: true, reload: true, start: true, enable: true - action [:enable, :start] + action [:enable] end # set up logrotate include_recipe 'logrotate' -logrotate_app "logstash_#{node['logstash']['server']['name']}" do +logrotate_app "logstash_#{name}" do path "#{log_dir}/*.log" - size node['logstash']['server']['logging']['maxSize'] if node['logstash']['server']['logging']['useFileSize'] - frequency node['logstash']['server']['logging']['rotateFrequency'] - rotate node['logstash']['server']['logging']['maxBackup'] - options node['logstash']['server']['logrotate']['options'] - create "664 #{node['logstash']['server']['user']} #{node['logstash']['server']['group']}" + size node['logstash']['instance'][name]['logging']['maxSize'] if node['logstash']['instance'][name]['logging']['useFileSize'] + frequency node['logstash']['instance'][name]['logging']['rotateFrequency'] + rotate node['logstash']['instance'][name]['logging']['maxBackup'] + options node['logstash']['instance'][name]['logrotate']['options'] + create "664 #{node['logstash']['instance'][name]['user']} #{node['logstash']['instance'][name]['group']}" end diff --git a/resources/service.rb b/resources/service.rb new file mode 100644 index 0000000..b8fde05 --- /dev/null +++ b/resources/service.rb @@ -0,0 +1,20 @@ +# Encoding: utf-8 +# Cookbook Name:: logstash +# Resource:: instance +# Author:: John E. Vincent +# Copyright 2014, John E. Vincent +# License:: Apache 2.0 + +actions :create, :delete + +default_action :create if defined?(default_action) + +attribute :instance, kind_of: String, name_attribute: true +attribute :service_name, kind_of: String +attribute :method, kind_of: String, default: 'pleaserun' +attribute :command, kind_of: String +attribute :args, kind_of: Array +attribute :description, kind_of: String +attribute :chdir, kind_of: String +attribute :user, kind_of: String +attribute :group, kind_of: String From 86bc09e7195df0835ac20cf9d8020355b239a762 Mon Sep 17 00:00:00 2001 From: paulczar Date: Tue, 18 Mar 2014 09:14:39 -0500 Subject: [PATCH 07/55] integrate service methods into new provider --- Vagrantfile | 36 +--------- attributes/default.rb | 2 +- providers/service.rb | 107 +++++++++++++++++++++++++---- recipes/server.rb | 13 ++-- resources/service.rb | 2 +- templates/default/init/upstart.erb | 47 +++++++++++++ 6 files changed, 153 insertions(+), 54 deletions(-) create mode 100644 templates/default/init/upstart.erb diff --git a/Vagrantfile b/Vagrantfile index 1e0f74b..fda1241 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -31,43 +31,13 @@ chef_json = { server_ipaddress: '127.0.0.1', xms: '128m', xmx: '128m', - enable_embedded_es: false, - inputs: [ - file: { - type: 'syslog', - path: ['/var/log/syslog', '/var/log/messages'], - start_position: 'beginning' - } - ], - filters: [ - { - condition: 'if [type] == "syslog"', - block: { - grok: { - match: [ - 'message', - "%{SYSLOGTIMESTAMP:timestamp} %{IPORHOST:host} (?:%{PROG:program}(?:\[%{POSINT:pid}\])?: )?%{GREEDYDATA:message}" - ] - }, - date: { - match: [ - 'timestamp', - 'MMM d HH:mm:ss', - 'MMM dd HH:mm:ss', - 'ISO8601' - ] - } - } - } - ] - }, + enable_embedded_es: false + }, server: { xms: '128m', xmx: '128m', enable_embedded_es: true, - config_templates: ['apache'], - config_templates_variables: { apache: { type: 'apache' } }, - web: { enable: true } + web: { enable: false } } } } diff --git a/attributes/default.rb b/attributes/default.rb index 5ceb3eb..5af0a18 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -51,7 +51,7 @@ # allow control over the upstart config default['logstash']['instance']['default']['upstart_with_sudo'] = false -default['logstash']['instance']['default']['init_method'] = 'native' # native or runit +default['logstash']['instance']['default']['init_method'] = 'native' # pleaserun or native or runit # roles/flags for various autoconfig/discovery components default['logstash']['instance']['default']['enable_embedded_es'] = true diff --git a/providers/service.rb b/providers/service.rb index facc0eb..bea5d01 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -14,7 +14,7 @@ def load_current_resource @instance = new_resource.instance @service_name = new_resource.service_name || "logstash_#{@instance}" @home = "#{node['logstash']['instance'][@instance]['basedir']}/#{@instance}" - @method = new_resource.method + @method = new_resource.method || node['logstash']['instance'][@instance]['init_method'] @command = new_resource.command || "#{@home}/bin/logstash" @args = new_resource.args || default_args @description = new_resource.description || @service_name @@ -24,7 +24,6 @@ def load_current_resource end action :create do - @run_context.include_recipe 'pleaserun::default' svc_name = @instance svc_service_name = @service_name svc_home = @home @@ -35,18 +34,100 @@ def load_current_resource svc_chdir = @chdir svc_user = @user svc_group = @group - pr = pleaserun svc_service_name do - name svc_service_name - program svc_command - args svc_args - description svc_description - chdir svc_chdir - user svc_user - group svc_group - action :create - not_if { ::File.exists?("/etc/init.d/#{svc_service_name}") } + services = [svc_name] + services << 'web' if node['logstash']['server']['web']['enable'] + Chef::Log.info("Using init method #{svc_method} for #{svc_service_name}") + case svc_method + when 'pleaserun' + @run_context.include_recipe 'pleaserun::default' + pr = pleaserun svc_service_name do + name svc_service_name + program svc_command + args svc_args + description svc_description + chdir svc_chdir + user svc_user + group svc_group + action :create + not_if { ::File.exists?("/etc/init.d/#{svc_service_name}") } + end + new_resource.updated_by_last_action(pr.updated_by_last_action?) + when 'runit' + @run_context.include_recipe 'runit::default' + ri = runit_service(svc_service_name) + new_resource.updated_by_last_action(ri.updated_by_last_action?) + when 'native' + if platform_family? 'debian' + if node['platform_version'] >= '12.04' + tp = template "/etc/init/#{svc_service_name}.conf" do + mode '0644' + source 'init/upstart.erb' + variables( + home: "#{node['logstash']['instance'][svc_name]['basedir']}/#{svc_name}", + name: svc_name, + command: svc_command + + ) + end + new_resource.updated_by_last_action(tp.updated_by_last_action?) + #sv = service "#{svc_service_name}" do + # provider Chef::Provider::Service::Upstart + # action [:enable, :start] + #end + #new_resource.updated_by_last_action(sv.updated_by_last_action?) + else + Chef::Log.fatal("Please set node['logstash']['server']['init_method'] to 'runit' for #{node['platform_version']}") + end + + elsif (platform_family? 'fedora') && (node['platform_version'] >= '15') + ex = execute 'reload-systemd' do + command 'systemctl --system daemon-reload' + action :nothing + end + new_resource.updated_by_last_action(ex.updated_by_last_action?) + + template '/etc/systemd/system/logstash_server.service' do + tp = source 'logstash_server.service.erb' + owner 'root' + group 'root' + mode '0755' + notifies :run, 'execute[reload-systemd]', :immediately + notifies :restart, 'service[logstash_server]', :delayed + end + new_resource.updated_by_last_action(tp.updated_by_last_action?) + + sv = service 'logstash_server' do + service_name 'logstash_server.service' + provider Chef::Provider::Service::Systemd + action [:enable, :start] + end + new_resource.updated_by_last_action(sv.updated_by_last_action?) + + elsif platform_family? 'rhel', 'fedora' + tp = template "/etc/init.d/#{svc_service_name}" do + source "init.#{svc_service_name}.erb" + owner 'root' + group 'root' + mode '0774' + variables(config_file: node['logstash']['server']['config_dir'], + home: node['logstash']['server']['home'], + name: svc_name, + log_file: node['logstash']['server']['log_file'], + max_heap: node['logstash']['server']['xmx'], + min_heap: node['logstash']['server']['xms'] + ) + end + new_resource.updated_by_last_action(tp.updated_by_last_action?) + + sv = service "#{svc_service_name}" do + supports restart: true, reload: true, status: true + action [:enable, :start] + end + new_resource.updated_by_last_action(sv.updated_by_last_action?) + end + else + Chef::Log.fatal("Unsupported init method: #{@svc_method}") end - new_resource.updated_by_last_action(pr.updated_by_last_action?) end private diff --git a/recipes/server.rb b/recipes/server.rb index b33b6db..4ad0710 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -66,7 +66,7 @@ owner node['logstash']['instance'][name]['user'] group node['logstash']['instance'][name]['group'] mode '0644' - notifies :restart, service_resource + # notifies :restart, service_resource not_if { node['logstash']['instance'][name]['patterns_templates'].empty? } end end @@ -79,16 +79,17 @@ group node['logstash']['instance'][name]['group'] mode '0644' # variables node['logstash']['instance'][name]['config_templates_variables'][config_template] - notifies :restart, service_resource + # notifies :restart, service_resource action :create not_if { node['logstash']['instance'][name]['config_templates'].empty? } end end -service "logstash_#{name}" do - supports restart: true, reload: true, start: true, enable: true - action [:enable] -end +# service "logstash_#{name}" do +# supports restart: true, reload: true, start: true, enable: true +# method = node['logstash']['instance'][name]['init_method'] +# action [:enable] +# end # set up logrotate include_recipe 'logrotate' diff --git a/resources/service.rb b/resources/service.rb index b8fde05..6b0b240 100644 --- a/resources/service.rb +++ b/resources/service.rb @@ -11,7 +11,7 @@ attribute :instance, kind_of: String, name_attribute: true attribute :service_name, kind_of: String -attribute :method, kind_of: String, default: 'pleaserun' +attribute :method, kind_of: String attribute :command, kind_of: String attribute :args, kind_of: Array attribute :description, kind_of: String diff --git a/templates/default/init/upstart.erb b/templates/default/init/upstart.erb new file mode 100644 index 0000000..2cf704b --- /dev/null +++ b/templates/default/init/upstart.erb @@ -0,0 +1,47 @@ +description "Logstash" +author "Chef" + +start on (filesystem and net-device-up) +stop on runlevel [!2345] + +respawn +respawn limit 5 30 +limit nofile 65550 65550 + +chdir <%= @home %> + +<% unless node['logstash']['instance'][@name]['upstart_with_sudo'] -%> +setuid <%= node['logstash']['instance'][@name]['user'] %> + <% unless node['logstash']['instance'][@name]['supervisor_gid'].to_s.empty? -%> +setgid <%= node['logstash']['instance'][@name]['supervisor_gid'] %> + <% end -%> +<% end -%> + +script + export LOGSTASH_HOME="<%= @home %>" + export HOME=$LOGSTASH_HOME + export GC_OPTS="<%= node['logstash']['instance'][@name]['gc_opts'] %>" + export JAVA_OPTS="-server -Xms<%= node['logstash']['instance'][@name]['xms'] %> -Xmx<%= node['logstash']['instance'][@name]['xmx'] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= node['logstash']['instance'][@name]['java_opts'] %> <%= '-Djava.net.preferIPv4Stack=true' if node['logstash']['instance'][@name]['ipv4_only'] %>" + export LOGSTASH_OPTS="agent -f <%= node['logstash']['instance'][@name]['home'] %>/etc/conf.d" + <% if node['logstash']['instance'][@name]['pluginpath'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['instance'][@name]['pluginpath'] %>" + <% end -%> + <% if node['logstash']['instance'][@name]['workers'] %> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['instance'][@name]['workers'] %>" + <% end %> + <% if node['logstash']['instance'][@name]['log_file'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= @home %>/log/<%= node['logstash']['instance'][@name]['log_file'] %>" + <% end -%> + <% if node['logstash']['instance'][@name]['debug'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" + <% end -%> + export OPTS="$JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS" + + <% if node['logstash']['instance'][@name]['upstart_with_sudo'] -%> + exec sudo -u <%= node['logstash']['instance'][@name]['user'] %> /usr/bin/java $OPTS + <% else -%> + exec /usr/bin/java $OPTS + <% end -%> +end script + +emits logstash-server-running From 27c7539662b2a6059dffb0b883118b8a22ac2547 Mon Sep 17 00:00:00 2001 From: Paul Czarkowski Date: Wed, 19 Mar 2014 15:54:59 -0500 Subject: [PATCH 08/55] service LWRP handles *some* native inits --- attributes/default.rb | 1 + providers/instance.rb | 30 +++- providers/service.rb | 150 ++++++++++++------ recipes/server.rb | 49 +----- resources/instance.rb | 1 + resources/service.rb | 4 +- templates/default/init/binary_upstart.erb | 45 ++++++ .../init/{upstart.erb => java_upstart.erb} | 1 + 8 files changed, 183 insertions(+), 98 deletions(-) create mode 100644 templates/default/init/binary_upstart.erb rename templates/default/init/{upstart.erb => java_upstart.erb} (99%) diff --git a/attributes/default.rb b/attributes/default.rb index 5af0a18..efb9a41 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -63,6 +63,7 @@ default['logstash']['instance']['default']['web']['address'] = '0.0.0.0' default['logstash']['instance']['default']['web']['port'] = '9292' +default['logstash']['instance']['default']['logrotate']['enable'] = true default['logstash']['instance']['default']['logrotate']['options'] = %w(missingok notifempty compress copytruncate) # Logging features diff --git a/providers/instance.rb b/providers/instance.rb index 86c9370..05931db 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -11,11 +11,13 @@ include Chef::Mixin::ShellOut def load_current_resource - @base_directory = new_resource.base_directory || node['logstash']['instance']['default']['basedir'] - @install_type = new_resource.install_type || node['logstash']['instance']['default']['install_type'] - @version = new_resource.version || node['logstash']['instance']['default']['version'] - @checksum = new_resource.checksum || node['logstash']['instance']['default']['checksum'] - @source_url = new_resource.source_url || node['logstash']['instance']['default']['source_url'] + @name = new_resource.name || 'default' + @base_directory = new_resource.base_directory || node['logstash']['instance'][@name]['basedir'] + @install_type = new_resource.install_type || node['logstash']['instance'][@name]['install_type'] + @version = new_resource.version || node['logstash']['instance'][@name]['version'] + @checksum = new_resource.checksum || node['logstash']['instance'][@name]['checksum'] + @source_url = new_resource.source_url || node['logstash']['instance'][@name]['source_url'] + @enable_logrotate = new_resource.enable_logrotate || node['logstash']['instance']['default']['enable_logrotate'] @repo = new_resource.repo @sha = new_resource.sha @java_home = new_resource.java_home @@ -23,7 +25,6 @@ def load_current_resource @group = new_resource.group @useropts = new_resource.user_opts.clone @instance_dir = "#{@base_directory}/#{new_resource.name}".clone - @name = new_resource.name end action :delete do @@ -72,6 +73,7 @@ def load_current_resource ls_group = @group ls_name = @name ls_instance_dir = @instance_dir + ls_enable_logrotate = @enable_logrotate ur = user ls_user do home ls_homedir @@ -220,4 +222,20 @@ def load_current_resource else Chef::Application.fatal!("Unknown install type: #{@install_type}") end + logrotate(ls_name) + +end + +private + +def logrotate(name) + @run_context.include_recipe 'logrotate::default' + logrotate_app "logstash_#{name}" do + path "#{log_dir}/*.log" + size node['logstash']['instance'][name]['logging']['maxSize'] if node['logstash']['instance'][name]['logging']['useFileSize'] + frequency node['logstash']['instance'][name]['logging']['rotateFrequency'] + rotate node['logstash']['instance'][name]['logging']['maxBackup'] + options node['logstash']['instance'][name]['logrotate']['options'] + create "664 #{node['logstash']['instance'][name]['user']} #{node['logstash']['instance'][name]['group']}" + end end diff --git a/providers/service.rb b/providers/service.rb index bea5d01..38a3649 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -23,60 +23,104 @@ def load_current_resource @group = new_resource.group || node['logstash']['instance'][@instance]['group'] end -action :create do - svc_name = @instance - svc_service_name = @service_name - svc_home = @home - svc_method = @method - svc_command = @command - svc_args = @args - svc_description = @description - svc_chdir = @chdir - svc_user = @user - svc_group = @group - services = [svc_name] - services << 'web' if node['logstash']['server']['web']['enable'] - Chef::Log.info("Using init method #{svc_method} for #{svc_service_name}") - case svc_method +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 +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 +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 +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 +end + +action :enable do + svc = svc_vars + Chef::Log.info("Using init method #{svc[:method]} for #{svc[:service_name]} - #{svc.inspect}") + case svc[:method] when 'pleaserun' @run_context.include_recipe 'pleaserun::default' - pr = pleaserun svc_service_name do - name svc_service_name - program svc_command - args svc_args - description svc_description - chdir svc_chdir - user svc_user - group svc_group + pr = pleaserun svc[:service_name] do + name svc[:service_name] + program svc[:command] + args svc[:args] + description svc[:description] + chdir svc[:chdir] + user svc[:user] + group svc[:group] action :create - not_if { ::File.exists?("/etc/init.d/#{svc_service_name}") } + not_if { ::File.exists?("/etc/init.d/#{svc[:service_name]}") } end new_resource.updated_by_last_action(pr.updated_by_last_action?) when 'runit' @run_context.include_recipe 'runit::default' - ri = runit_service(svc_service_name) + ri = runit_service(svc[:service_name]) new_resource.updated_by_last_action(ri.updated_by_last_action?) when 'native' if platform_family? 'debian' if node['platform_version'] >= '12.04' - tp = template "/etc/init/#{svc_service_name}.conf" do - mode '0644' - source 'init/upstart.erb' - variables( - home: "#{node['logstash']['instance'][svc_name]['basedir']}/#{svc_name}", - name: svc_name, - command: svc_command - - ) + if node['logstash']['instance'][svc[:name]]['install_type'] == 'tarball' + tp_source = 'init/binary_upstart.erb' + else + tp_source = 'init/java_upstart.erb' end + + tp = template "/etc/init/#{svc[:service_name]}.conf" do + mode '0644' + source tp_source + variables( home: "#{node['logstash']['instance'][svc[:name]]['basedir']}/#{svc[:name]}", + name: svc[:name], + command: svc[:command] + ) + end + new_resource.updated_by_last_action(tp.updated_by_last_action?) - #sv = service "#{svc_service_name}" do - # provider Chef::Provider::Service::Upstart - # action [:enable, :start] - #end - #new_resource.updated_by_last_action(sv.updated_by_last_action?) + sv = service "#{svc[:service_name]}" do + provider Chef::Provider::Service::Upstart + supports :restart => true, :reload => true, :start => true, :stop => true + action [:enable] + end + new_resource.updated_by_last_action(sv.updated_by_last_action?) + else - Chef::Log.fatal("Please set node['logstash']['server']['init_method'] to 'runit' for #{node['platform_version']}") + Chef::Log.fatal("Please set node['logstash']['instance']['server']['init_method'] to 'runit' for #{node['platform_version']}") end elsif (platform_family? 'fedora') && (node['platform_version'] >= '15') @@ -104,14 +148,14 @@ def load_current_resource new_resource.updated_by_last_action(sv.updated_by_last_action?) elsif platform_family? 'rhel', 'fedora' - tp = template "/etc/init.d/#{svc_service_name}" do - source "init.#{svc_service_name}.erb" + tp = template "/etc/init.d/#{svc[:service_name]}" do + source "init.#{svc[:service_name]}.erb" owner 'root' group 'root' mode '0774' variables(config_file: node['logstash']['server']['config_dir'], home: node['logstash']['server']['home'], - name: svc_name, + name: svc[:name], log_file: node['logstash']['server']['log_file'], max_heap: node['logstash']['server']['xmx'], min_heap: node['logstash']['server']['xms'] @@ -119,14 +163,14 @@ def load_current_resource end new_resource.updated_by_last_action(tp.updated_by_last_action?) - sv = service "#{svc_service_name}" do + sv = service "#{svc[:service_name]}" do supports restart: true, reload: true, status: true action [:enable, :start] end new_resource.updated_by_last_action(sv.updated_by_last_action?) end else - Chef::Log.fatal("Unsupported init method: #{@svc_method}") + Chef::Log.fatal("Unsupported init method: #{@svc[:method]}") end end @@ -141,3 +185,19 @@ def default_args args.concat ['-w', node['logstash']['instance'][@instance]['workers'].to_s] if node['logstash']['instance'][@instance]['workers'] args end + +def svc_vars + svc = { + name: @instance, + service_name: @service_name, + home: @home, + method: @method, + command: @command, + args: @args, + description: @description, + chdir: @chdir, + user: @user, + group: @group + } + svc +end diff --git a/recipes/server.rb b/recipes/server.rb index 4ad0710..b068cd6 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -25,37 +25,13 @@ install_type node['logstash']['instance'][name]['install_type'] user node['logstash']['instance'][name]['user'] group node['logstash']['instance'][name]['group'] - action :create # :delete to remove + enable_logrotate node['logstash']['instance'][name]['enable_logrotate'] + action :create end -logstash_home = "#{node['logstash']['instance'][name]['basedir']}/#{name}" - -# fix search if chef solo -if Chef::Config[:solo] - es_server_ip = node['logstash']['instance'][name]['elasticsearch_ip'] - graphite_server_ip = node['logstash']['instance'][name]['graphite_ip'] -else - es_results = search(:node, node['logstash']['instance'][name]['elasticsearch_query']) - graphite_results = search(:node, node['logstash']['instance'][name]['graphite_query']) - - if !es_results.empty? - es_server_ip = es_results[0]['ipaddress'] - else - es_server_ip = node['logstash']['instance'][name]['elasticsearch_ip'] - end - - if !graphite_results.empty? - graphite_server_ip = graphite_results[0]['ipaddress'] - else - graphite_server_ip = node['logstash']['instance'][name]['graphite_ip'] - end -end - -# services are hard! -service_resource = "service[logstash_#{name}]" - +# services are hard! Let's go LWRP'ing. FIREBALL! FIREBALL! FIREBALL! logstash_service name do - action :create + action [:enable, :start] end # add in any custom patterns @@ -84,20 +60,3 @@ not_if { node['logstash']['instance'][name]['config_templates'].empty? } end end - -# service "logstash_#{name}" do -# supports restart: true, reload: true, start: true, enable: true -# method = node['logstash']['instance'][name]['init_method'] -# action [:enable] -# end - -# set up logrotate -include_recipe 'logrotate' -logrotate_app "logstash_#{name}" do - path "#{log_dir}/*.log" - size node['logstash']['instance'][name]['logging']['maxSize'] if node['logstash']['instance'][name]['logging']['useFileSize'] - frequency node['logstash']['instance'][name]['logging']['rotateFrequency'] - rotate node['logstash']['instance'][name]['logging']['maxBackup'] - options node['logstash']['instance'][name]['logrotate']['options'] - create "664 #{node['logstash']['instance'][name]['user']} #{node['logstash']['instance'][name]['group']}" -end diff --git a/resources/instance.rb b/resources/instance.rb index d1b9415..7f06876 100644 --- a/resources/instance.rb +++ b/resources/instance.rb @@ -24,3 +24,4 @@ attribute :user, kind_of: String, default: 'logstash' attribute :group, kind_of: String, default: 'logstash' attribute :user_opts, kind_of: Hash, default: { homedir: '/var/lib/logstash', uid: nil, gid: nil } +attribute :enable_logrotate, kind_of: [TrueClass, FalseClass] diff --git a/resources/service.rb b/resources/service.rb index 6b0b240..fc117c8 100644 --- a/resources/service.rb +++ b/resources/service.rb @@ -5,9 +5,9 @@ # Copyright 2014, John E. Vincent # License:: Apache 2.0 -actions :create, :delete +actions :enable, :start, :restart, :reload, :stop -default_action :create if defined?(default_action) +default_action :enable if defined?(default_action) attribute :instance, kind_of: String, name_attribute: true attribute :service_name, kind_of: String diff --git a/templates/default/init/binary_upstart.erb b/templates/default/init/binary_upstart.erb new file mode 100644 index 0000000..3ef546d --- /dev/null +++ b/templates/default/init/binary_upstart.erb @@ -0,0 +1,45 @@ +description "Logstash" +author "Chef" + +start on (filesystem and net-device-up) +stop on runlevel [!2345] + +respawn +respawn limit 5 30 +limit nofile 65550 65550 + +chdir <%= @home %> + +<% unless node['logstash']['instance'][@name]['upstart_with_sudo'] -%> +setuid <%= node['logstash']['instance'][@name]['user'] %> + <% unless node['logstash']['instance'][@name]['supervisor_gid'].to_s.empty? -%> +setgid <%= node['logstash']['instance'][@name]['supervisor_gid'] %> + <% end -%> +<% end -%> + +script + export LOGSTASH_HOME="<%= @home %>" + export HOME=$LOGSTASH_HOME + export LOGSTASH_OPTS="agent -f <%= node['logstash']['instance'][@name]['home'] %>/etc/conf.d" + <% if node['logstash']['instance'][@name]['pluginpath'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['instance'][@name]['pluginpath'] %>" + <% end -%> + <% if node['logstash']['instance'][@name]['workers'] %> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['instance'][@name]['workers'] %>" + <% end %> + <% if node['logstash']['instance'][@name]['log_file'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= @home %>/log/<%= node['logstash']['instance'][@name]['log_file'] %>" + <% end -%> + <% if node['logstash']['instance'][@name]['debug'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" + <% end -%> + export OPTS="$JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS" + + <% if node['logstash']['instance'][@name]['upstart_with_sudo'] -%> + exec sudo -u <%= node['logstash']['instance'][@name]['user'] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS + <% else -%> + exec $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS + <% end -%> +end script + +emits logstash-server-running diff --git a/templates/default/init/upstart.erb b/templates/default/init/java_upstart.erb similarity index 99% rename from templates/default/init/upstart.erb rename to templates/default/init/java_upstart.erb index 2cf704b..75e8437 100644 --- a/templates/default/init/upstart.erb +++ b/templates/default/init/java_upstart.erb @@ -20,6 +20,7 @@ setgid <%= node['logstash']['instance'][@name]['supervisor_gid'] %> script export LOGSTASH_HOME="<%= @home %>" export HOME=$LOGSTASH_HOME + export GC_OPTS="<%= node['logstash']['instance'][@name]['gc_opts'] %>" export JAVA_OPTS="-server -Xms<%= node['logstash']['instance'][@name]['xms'] %> -Xmx<%= node['logstash']['instance'][@name]['xmx'] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= node['logstash']['instance'][@name]['java_opts'] %> <%= '-Djava.net.preferIPv4Stack=true' if node['logstash']['instance'][@name]['ipv4_only'] %>" export LOGSTASH_OPTS="agent -f <%= node['logstash']['instance'][@name]['home'] %>/etc/conf.d" From 74f0f3e071e41a7387588f860f7e61b365ae40f4 Mon Sep 17 00:00:00 2001 From: paulczar Date: Tue, 18 Mar 2014 09:14:39 -0500 Subject: [PATCH 09/55] integrate service methods into new provider --- CHANGELOG.md | 4 + Vagrantfile | 36 +--- attributes/default.rb | 10 +- attributes/server.rb | 2 +- providers/config.rb | 71 +++++++ providers/instance.rb | 30 ++- providers/service.rb | 184 +++++++++++++++--- recipes/server.rb | 87 +++------ resources/config.rb | 19 ++ resources/instance.rb | 1 + resources/service.rb | 6 +- rubocop-todo.yml | 10 + .../default/config/elasticsearch.conf.erb | 7 + templates/default/init/binary_upstart.erb | 45 +++++ templates/default/init/java_upstart.erb | 48 +++++ templates/default/init/upstart.erb | 47 +++++ 16 files changed, 477 insertions(+), 130 deletions(-) create mode 100644 providers/config.rb create mode 100644 resources/config.rb create mode 100644 rubocop-todo.yml create mode 100644 templates/default/config/elasticsearch.conf.erb create mode 100644 templates/default/init/binary_upstart.erb create mode 100644 templates/default/init/java_upstart.erb create mode 100644 templates/default/init/upstart.erb diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ef4b1e..f1aeb82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ This file is used to list changes made in each version of chef-logstash. +## 0.8.0 +* major refactor towards being a library cookbook +* support for Logstash 1.4 + ## 0.7.7 * Support for new beaver config [#239](https://github.com/lusis/chef-logstash/pull/239) * Support for multiline codec [#240](https://github.com/lusis/chef-logstash/pull/240) diff --git a/Vagrantfile b/Vagrantfile index 1e0f74b..fda1241 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -31,43 +31,13 @@ chef_json = { server_ipaddress: '127.0.0.1', xms: '128m', xmx: '128m', - enable_embedded_es: false, - inputs: [ - file: { - type: 'syslog', - path: ['/var/log/syslog', '/var/log/messages'], - start_position: 'beginning' - } - ], - filters: [ - { - condition: 'if [type] == "syslog"', - block: { - grok: { - match: [ - 'message', - "%{SYSLOGTIMESTAMP:timestamp} %{IPORHOST:host} (?:%{PROG:program}(?:\[%{POSINT:pid}\])?: )?%{GREEDYDATA:message}" - ] - }, - date: { - match: [ - 'timestamp', - 'MMM d HH:mm:ss', - 'MMM dd HH:mm:ss', - 'ISO8601' - ] - } - } - } - ] - }, + enable_embedded_es: false + }, server: { xms: '128m', xmx: '128m', enable_embedded_es: true, - config_templates: ['apache'], - config_templates_variables: { apache: { type: 'apache' } }, - web: { enable: true } + web: { enable: false } } } } diff --git a/attributes/default.rb b/attributes/default.rb index 5ceb3eb..3bde883 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -22,14 +22,11 @@ default['logstash']['instance']['default']['join_groups'] = [] default['logstash']['instance']['default']['homedir'] = '/var/lib/logstash' -default['logstash']['instance']['default']['name'] = 'server' -default['logstash']['instance']['default']['home'] = "/opt/logstash/#{node['logstash']['instance']['default']['name']}" default['logstash']['instance']['default']['version'] = '1.4.0.rc1' default['logstash']['instance']['default']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.rc1.tar.gz' default['logstash']['instance']['default']['checksum'] = 'b015fa130d589af957c9a48e6f59754f5c0954835abf44bd013547a6b6520e59' default['logstash']['instance']['default']['install_type'] = 'tarball' -default['logstash']['instance']['default']['log_file'] = 'server.log' -default['logstash']['instance']['default']['base_config'] = 'server.conf.erb' # set blank if don't want data driven config +default['logstash']['instance']['default']['log_file'] = 'logstash.log' default['logstash']['instance']['default']['xms'] = '1024M' default['logstash']['instance']['default']['xmx'] = '1024M' @@ -43,6 +40,8 @@ default['logstash']['instance']['default']['patterns_templates'] = {} default['logstash']['instance']['default']['base_config_cookbook'] = 'logstash' +default['logstash']['instance']['default']['base_config'] = '' # set if want data driven + default['logstash']['instance']['default']['config_file'] = '' default['logstash']['instance']['default']['config_templates'] = {} default['logstash']['instance']['default']['config_templates_cookbook'] = 'logstash' @@ -51,7 +50,7 @@ # allow control over the upstart config default['logstash']['instance']['default']['upstart_with_sudo'] = false -default['logstash']['instance']['default']['init_method'] = 'native' # native or runit +default['logstash']['instance']['default']['init_method'] = 'native' # pleaserun or native or runit # roles/flags for various autoconfig/discovery components default['logstash']['instance']['default']['enable_embedded_es'] = true @@ -63,6 +62,7 @@ default['logstash']['instance']['default']['web']['address'] = '0.0.0.0' default['logstash']['instance']['default']['web']['port'] = '9292' +default['logstash']['instance']['default']['logrotate']['enable'] = true default['logstash']['instance']['default']['logrotate']['options'] = %w(missingok notifempty compress copytruncate) # Logging features diff --git a/attributes/server.rb b/attributes/server.rb index beec82e..899a5ef 100644 --- a/attributes/server.rb +++ b/attributes/server.rb @@ -1,6 +1,6 @@ # Encoding: utf-8 default['logstash']['instance']['server'] = node['logstash']['instance']['default'] -override['logstash']['instance']['server']['config_templates'] = { 'syslog' => 'config/syslog.conf', 'stdout' => 'config/stdout.conf' } +override['logstash']['instance']['server']['config_templates'] = { 'syslog' => 'config/syslog.conf.erb', 'stdout' => 'config/stdout.conf.erb' } override['logstash']['instance']['server']['patterns_templates'] = { 'default' => 'patterns/patterns' } # override any defaults here. diff --git a/providers/config.rb b/providers/config.rb new file mode 100644 index 0000000..607f07a --- /dev/null +++ b/providers/config.rb @@ -0,0 +1,71 @@ +# Encoding: utf-8 +# Cookbook Name:: logstash +# Provider:: service +# Author:: John E. Vincent +# License:: Apache 2.0 +# +# Copyright 2014, John E. Vincent + +require 'chef/mixin/shell_out' +require 'chef/mixin/language' +include Chef::Mixin::ShellOut + +def load_current_resource + @instance = new_resource.instance + if node['logstash']['instance'].key?(@instance) + attributes = node['logstash']['instance'][@instance] + else + attributes = node['logstash']['instance']['default'] + end + @templates = new_resource.templates || attributes['config_templates'] + @templates_cookbook = new_resource.templates_cookbook || attributes['config_templates_cookbook'] + @variables = new_resource.variables || attributes['config_templates_variables'] + @home = new_resource.home || "#{attributes['base_dir']}/#{@instance}" + @owner = new_resource.owner || attributes['user'] + @group = new_resource.group || attributes['group'] + @mode = new_resource.mode || '0644' +end + +action :create do + conf = conf_vars + # Chef::Log.info("config vars: #{conf.inspect}") + conf[:templates].each do |template, file| + home = "#{node['logstash']['instance'][conf[:instance]]['basedir']}/#{conf[:instance]}" + tp = template"#{home}/etc/conf.d/#{::File.basename(file).chomp(::File.extname(file))}" do + source file + cookbook conf[:templates_cookbook] + owner conf[:owner] + group conf[:group] + mode conf[:mode] + variables conf[:variables] + notifies :restart, "logstash_service[#{conf[:instance]}]" + action :create + end + new_resource.updated_by_last_action(tp.updated_by_last_action?) + end +end + +action :remove do + conf[:templates].each do |template, file| + tp = template template do + action :remove + end + new_resource.updated_by_last_action(tp.updated_by_last_action?) + end +end + +private + +def conf_vars + conf = { + instance: @instance, + templates: @templates, + variables: @variables, + home: @home, + owner: @owner, + group: @group, + mode: @mode, + templates_cookbook: @templates_cookbook + } + conf +end diff --git a/providers/instance.rb b/providers/instance.rb index 86c9370..05931db 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -11,11 +11,13 @@ include Chef::Mixin::ShellOut def load_current_resource - @base_directory = new_resource.base_directory || node['logstash']['instance']['default']['basedir'] - @install_type = new_resource.install_type || node['logstash']['instance']['default']['install_type'] - @version = new_resource.version || node['logstash']['instance']['default']['version'] - @checksum = new_resource.checksum || node['logstash']['instance']['default']['checksum'] - @source_url = new_resource.source_url || node['logstash']['instance']['default']['source_url'] + @name = new_resource.name || 'default' + @base_directory = new_resource.base_directory || node['logstash']['instance'][@name]['basedir'] + @install_type = new_resource.install_type || node['logstash']['instance'][@name]['install_type'] + @version = new_resource.version || node['logstash']['instance'][@name]['version'] + @checksum = new_resource.checksum || node['logstash']['instance'][@name]['checksum'] + @source_url = new_resource.source_url || node['logstash']['instance'][@name]['source_url'] + @enable_logrotate = new_resource.enable_logrotate || node['logstash']['instance']['default']['enable_logrotate'] @repo = new_resource.repo @sha = new_resource.sha @java_home = new_resource.java_home @@ -23,7 +25,6 @@ def load_current_resource @group = new_resource.group @useropts = new_resource.user_opts.clone @instance_dir = "#{@base_directory}/#{new_resource.name}".clone - @name = new_resource.name end action :delete do @@ -72,6 +73,7 @@ def load_current_resource ls_group = @group ls_name = @name ls_instance_dir = @instance_dir + ls_enable_logrotate = @enable_logrotate ur = user ls_user do home ls_homedir @@ -220,4 +222,20 @@ def load_current_resource else Chef::Application.fatal!("Unknown install type: #{@install_type}") end + logrotate(ls_name) + +end + +private + +def logrotate(name) + @run_context.include_recipe 'logrotate::default' + logrotate_app "logstash_#{name}" do + path "#{log_dir}/*.log" + size node['logstash']['instance'][name]['logging']['maxSize'] if node['logstash']['instance'][name]['logging']['useFileSize'] + frequency node['logstash']['instance'][name]['logging']['rotateFrequency'] + rotate node['logstash']['instance'][name]['logging']['maxBackup'] + options node['logstash']['instance'][name]['logrotate']['options'] + create "664 #{node['logstash']['instance'][name]['user']} #{node['logstash']['instance'][name]['group']}" + end end diff --git a/providers/service.rb b/providers/service.rb index facc0eb..fb09013 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -14,7 +14,7 @@ def load_current_resource @instance = new_resource.instance @service_name = new_resource.service_name || "logstash_#{@instance}" @home = "#{node['logstash']['instance'][@instance]['basedir']}/#{@instance}" - @method = new_resource.method + @method = new_resource.method || node['logstash']['instance'][@instance]['init_method'] @command = new_resource.command || "#{@home}/bin/logstash" @args = new_resource.args || default_args @description = new_resource.description || @service_name @@ -23,30 +23,150 @@ def load_current_resource @group = new_resource.group || node['logstash']['instance'][@instance]['group'] end -action :create do - @run_context.include_recipe 'pleaserun::default' - svc_name = @instance - svc_service_name = @service_name - svc_home = @home - svc_method = @method - svc_command = @command - svc_args = @args - svc_description = @description - svc_chdir = @chdir - svc_user = @user - svc_group = @group - pr = pleaserun svc_service_name do - name svc_service_name - program svc_command - args svc_args - description svc_description - chdir svc_chdir - user svc_user - group svc_group - action :create - not_if { ::File.exists?("/etc/init.d/#{svc_service_name}") } +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 +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 +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 +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 +end + +action :enable do + svc = svc_vars + Chef::Log.info("Using init method #{svc[:method]} for #{svc[:service_name]} - #{svc.inspect}") + case svc[:method] + when 'pleaserun' + @run_context.include_recipe 'pleaserun::default' + pr = pleaserun svc[:service_name] do + name svc[:service_name] + program svc[:command] + args svc[:args] + description svc[:description] + chdir svc[:chdir] + user svc[:user] + group svc[:group] + action :create + not_if { ::File.exists?("/etc/init.d/#{svc[:service_name]}") } + end + new_resource.updated_by_last_action(pr.updated_by_last_action?) + when 'runit' + @run_context.include_recipe 'runit::default' + ri = runit_service(svc[:service_name]) + new_resource.updated_by_last_action(ri.updated_by_last_action?) + when 'native' + if platform_family? 'debian' + if node['platform_version'] >= '12.04' + if node['logstash']['instance'][svc[:name]]['install_type'] == 'tarball' + tp_source = 'init/binary_upstart.erb' + else + tp_source = 'init/java_upstart.erb' + end + tp = template "/etc/init/#{svc[:service_name]}.conf" do + mode '0644' + source tp_source + variables(home: "#{node['logstash']['instance'][svc[:name]]['basedir']}/#{svc[:name]}", + name: svc[:name], + command: svc[:command] + ) + end + new_resource.updated_by_last_action(tp.updated_by_last_action?) + sv = service svc[:service_name] do + provider Chef::Provider::Service::Upstart + supports restart: true, reload: true, start: true, stop: true + action [:enable] + end + new_resource.updated_by_last_action(sv.updated_by_last_action?) + else + Chef::Log.fatal("Please set node['logstash']['instance']['server']['init_method'] to 'runit' for #{node['platform_version']}") + end + elsif (platform_family? 'fedora') && (node['platform_version'] >= '15') + ex = execute 'reload-systemd' do + command 'systemctl --system daemon-reload' + action :nothing + end + new_resource.updated_by_last_action(ex.updated_by_last_action?) + template '/etc/systemd/system/logstash_server.service' do + tp = source 'logstash_server.service.erb' + owner 'root' + group 'root' + mode '0755' + notifies :run, 'execute[reload-systemd]', :immediately + notifies :restart, 'service[logstash_server]', :delayed + end + new_resource.updated_by_last_action(tp.updated_by_last_action?) + + sv = service 'logstash_server' do + service_name 'logstash_server.service' + provider Chef::Provider::Service::Systemd + action [:enable, :start] + end + new_resource.updated_by_last_action(sv.updated_by_last_action?) + + elsif platform_family? 'rhel', 'fedora' + tp = template "/etc/init.d/#{svc[:service_name]}" do + source "init.#{svc[:service_name]}.erb" + owner 'root' + group 'root' + mode '0774' + variables(config_file: node['logstash']['server']['config_dir'], + home: node['logstash']['server']['home'], + name: svc[:name], + log_file: node['logstash']['server']['log_file'], + max_heap: node['logstash']['server']['xmx'], + min_heap: node['logstash']['server']['xms'] + ) + end + new_resource.updated_by_last_action(tp.updated_by_last_action?) + + sv = service svc[:service_name] do + supports restart: true, reload: true, status: true + action [:enable, :start] + end + new_resource.updated_by_last_action(sv.updated_by_last_action?) + end + else + Chef::Log.fatal("Unsupported init method: #{@svc[:method]}") end - new_resource.updated_by_last_action(pr.updated_by_last_action?) end private @@ -60,3 +180,19 @@ def default_args args.concat ['-w', node['logstash']['instance'][@instance]['workers'].to_s] if node['logstash']['instance'][@instance]['workers'] args end + +def svc_vars + svc = { + name: @instance, + service_name: @service_name, + home: @home, + method: @method, + command: @command, + args: @args, + description: @description, + chdir: @chdir, + user: @user, + group: @group + } + svc +end diff --git a/recipes/server.rb b/recipes/server.rb index b33b6db..69f33cb 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -13,90 +13,61 @@ # install logstash 'server' name = 'server' -node.override['logstash']['instance'][name]['name'] = name -node.save unless Chef::Config[:solo] +attributes = node['logstash']['instance'][name] # these should all default correctly. listing out for example. logstash_instance name do - base_directory node['logstash']['instance'][name]['basedir'] - version node['logstash']['instance'][name]['version'] - checksum node['logstash']['instance'][name]['checksum'] - source_url node['logstash']['instance'][name]['source_url'] - install_type node['logstash']['instance'][name]['install_type'] - user node['logstash']['instance'][name]['user'] - group node['logstash']['instance'][name]['group'] - action :create # :delete to remove + base_directory attributes['basedir'] + version attributes['version'] + checksum attributes['checksum'] + source_url attributes['source_url'] + install_type attributes['install_type'] + user attributes['user'] + group attributes['group'] + enable_logrotate attributes['enable_logrotate'] + action :create end -logstash_home = "#{node['logstash']['instance'][name]['basedir']}/#{name}" - # fix search if chef solo if Chef::Config[:solo] - es_server_ip = node['logstash']['instance'][name]['elasticsearch_ip'] - graphite_server_ip = node['logstash']['instance'][name]['graphite_ip'] + es_server_ip = attributes['elasticsearch_ip'] + graphite_server_ip = attributes['graphite_ip'] else - es_results = search(:node, node['logstash']['instance'][name]['elasticsearch_query']) - graphite_results = search(:node, node['logstash']['instance'][name]['graphite_query']) + es_results = search(:node, attributes['elasticsearch_query']) + graphite_results = search(:node, attributes['graphite_query']) if !es_results.empty? es_server_ip = es_results[0]['ipaddress'] else - es_server_ip = node['logstash']['instance'][name]['elasticsearch_ip'] + es_server_ip = attributes['elasticsearch_ip'] end if !graphite_results.empty? graphite_server_ip = graphite_results[0]['ipaddress'] else - graphite_server_ip = node['logstash']['instance'][name]['graphite_ip'] + graphite_server_ip = attributes['graphite_ip'] end end -# services are hard! -service_resource = "service[logstash_#{name}]" - +# services are hard! Let's go LWRP'ing. FIREBALL! FIREBALL! FIREBALL! logstash_service name do - action :create + action [:enable, :start] end -# add in any custom patterns -node['logstash']['instance'][name]['patterns_templates'].each do |template, file| - template "#{node['logstash']['instance'][name]['home']}/patterns/#{File.basename(file)}" do - source "#{file}.erb" - cookbook node['logstash']['instance'][name]['patterns_templates_cookbook'] - owner node['logstash']['instance'][name]['user'] - group node['logstash']['instance'][name]['group'] - mode '0644' - notifies :restart, service_resource - not_if { node['logstash']['instance'][name]['patterns_templates'].empty? } - end +logstash_config name do + action [:create] + not_if { attributes['config_templates'].empty? } end -node['logstash']['instance'][name]['config_templates'].each do |template, file| - template "#{node['logstash']['instance'][name]['home']}/etc/conf.d/#{File.basename(file)}" do +# add in any custom patterns +attributes['patterns_templates'].each do |template, file| + template "#{attributes['basedir']}/#{name}/patterns/#{File.basename(file)}" do source "#{file}.erb" - cookbook node['logstash']['instance'][name]['config_templates_cookbook'] - owner node['logstash']['instance'][name]['user'] - group node['logstash']['instance'][name]['group'] + cookbook attributes['patterns_templates_cookbook'] + owner attributes['user'] + group attributes['group'] mode '0644' - # variables node['logstash']['instance'][name]['config_templates_variables'][config_template] - notifies :restart, service_resource - action :create - not_if { node['logstash']['instance'][name]['config_templates'].empty? } + # notifies :restart, service_resource + not_if { attributes['patterns_templates'].empty? } end end - -service "logstash_#{name}" do - supports restart: true, reload: true, start: true, enable: true - action [:enable] -end - -# set up logrotate -include_recipe 'logrotate' -logrotate_app "logstash_#{name}" do - path "#{log_dir}/*.log" - size node['logstash']['instance'][name]['logging']['maxSize'] if node['logstash']['instance'][name]['logging']['useFileSize'] - frequency node['logstash']['instance'][name]['logging']['rotateFrequency'] - rotate node['logstash']['instance'][name]['logging']['maxBackup'] - options node['logstash']['instance'][name]['logrotate']['options'] - create "664 #{node['logstash']['instance'][name]['user']} #{node['logstash']['instance'][name]['group']}" -end diff --git a/resources/config.rb b/resources/config.rb new file mode 100644 index 0000000..6c62ddc --- /dev/null +++ b/resources/config.rb @@ -0,0 +1,19 @@ +# Encoding: utf-8 +# Cookbook Name:: logstash +# Resource:: instance +# Author:: John E. Vincent +# Copyright 2014, John E. Vincent +# License:: Apache 2.0 + +actions :create, :delete + +default_action :create if defined?(default_action) + +attribute :instance, kind_of: String, name_attribute: true +attribute :templates, kind_of: Array +attribute :variables, kind_of: Array +attribute :home, kind_of: String +attribute :owner, kind_of: String +attribute :group, kind_of: String +attribute :mode, kind_of: String +attribute :templates_cookbook, kind_of: String diff --git a/resources/instance.rb b/resources/instance.rb index d1b9415..7f06876 100644 --- a/resources/instance.rb +++ b/resources/instance.rb @@ -24,3 +24,4 @@ attribute :user, kind_of: String, default: 'logstash' attribute :group, kind_of: String, default: 'logstash' attribute :user_opts, kind_of: Hash, default: { homedir: '/var/lib/logstash', uid: nil, gid: nil } +attribute :enable_logrotate, kind_of: [TrueClass, FalseClass] diff --git a/resources/service.rb b/resources/service.rb index b8fde05..fc117c8 100644 --- a/resources/service.rb +++ b/resources/service.rb @@ -5,13 +5,13 @@ # Copyright 2014, John E. Vincent # License:: Apache 2.0 -actions :create, :delete +actions :enable, :start, :restart, :reload, :stop -default_action :create if defined?(default_action) +default_action :enable if defined?(default_action) attribute :instance, kind_of: String, name_attribute: true attribute :service_name, kind_of: String -attribute :method, kind_of: String, default: 'pleaserun' +attribute :method, kind_of: String attribute :command, kind_of: String attribute :args, kind_of: Array attribute :description, kind_of: String diff --git a/rubocop-todo.yml b/rubocop-todo.yml new file mode 100644 index 0000000..ce6aca0 --- /dev/null +++ b/rubocop-todo.yml @@ -0,0 +1,10 @@ +# This configuration was generated by `rubocop --auto-gen-config` +# on 2014-03-22 15:56:34 -0500 using RuboCop version 0.19.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +BlockNesting: + Max: 4 diff --git a/templates/default/config/elasticsearch.conf.erb b/templates/default/config/elasticsearch.conf.erb new file mode 100644 index 0000000..0941bc1 --- /dev/null +++ b/templates/default/config/elasticsearch.conf.erb @@ -0,0 +1,7 @@ +output { + elasticsearch { +<% if es_server_ip -%> + host => <%= es_server_ip %> +<% end -%> + } +} \ No newline at end of file diff --git a/templates/default/init/binary_upstart.erb b/templates/default/init/binary_upstart.erb new file mode 100644 index 0000000..3ef546d --- /dev/null +++ b/templates/default/init/binary_upstart.erb @@ -0,0 +1,45 @@ +description "Logstash" +author "Chef" + +start on (filesystem and net-device-up) +stop on runlevel [!2345] + +respawn +respawn limit 5 30 +limit nofile 65550 65550 + +chdir <%= @home %> + +<% unless node['logstash']['instance'][@name]['upstart_with_sudo'] -%> +setuid <%= node['logstash']['instance'][@name]['user'] %> + <% unless node['logstash']['instance'][@name]['supervisor_gid'].to_s.empty? -%> +setgid <%= node['logstash']['instance'][@name]['supervisor_gid'] %> + <% end -%> +<% end -%> + +script + export LOGSTASH_HOME="<%= @home %>" + export HOME=$LOGSTASH_HOME + export LOGSTASH_OPTS="agent -f <%= node['logstash']['instance'][@name]['home'] %>/etc/conf.d" + <% if node['logstash']['instance'][@name]['pluginpath'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['instance'][@name]['pluginpath'] %>" + <% end -%> + <% if node['logstash']['instance'][@name]['workers'] %> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['instance'][@name]['workers'] %>" + <% end %> + <% if node['logstash']['instance'][@name]['log_file'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= @home %>/log/<%= node['logstash']['instance'][@name]['log_file'] %>" + <% end -%> + <% if node['logstash']['instance'][@name]['debug'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" + <% end -%> + export OPTS="$JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS" + + <% if node['logstash']['instance'][@name]['upstart_with_sudo'] -%> + exec sudo -u <%= node['logstash']['instance'][@name]['user'] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS + <% else -%> + exec $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS + <% end -%> +end script + +emits logstash-server-running diff --git a/templates/default/init/java_upstart.erb b/templates/default/init/java_upstart.erb new file mode 100644 index 0000000..75e8437 --- /dev/null +++ b/templates/default/init/java_upstart.erb @@ -0,0 +1,48 @@ +description "Logstash" +author "Chef" + +start on (filesystem and net-device-up) +stop on runlevel [!2345] + +respawn +respawn limit 5 30 +limit nofile 65550 65550 + +chdir <%= @home %> + +<% unless node['logstash']['instance'][@name]['upstart_with_sudo'] -%> +setuid <%= node['logstash']['instance'][@name]['user'] %> + <% unless node['logstash']['instance'][@name]['supervisor_gid'].to_s.empty? -%> +setgid <%= node['logstash']['instance'][@name]['supervisor_gid'] %> + <% end -%> +<% end -%> + +script + export LOGSTASH_HOME="<%= @home %>" + export HOME=$LOGSTASH_HOME + + export GC_OPTS="<%= node['logstash']['instance'][@name]['gc_opts'] %>" + export JAVA_OPTS="-server -Xms<%= node['logstash']['instance'][@name]['xms'] %> -Xmx<%= node['logstash']['instance'][@name]['xmx'] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= node['logstash']['instance'][@name]['java_opts'] %> <%= '-Djava.net.preferIPv4Stack=true' if node['logstash']['instance'][@name]['ipv4_only'] %>" + export LOGSTASH_OPTS="agent -f <%= node['logstash']['instance'][@name]['home'] %>/etc/conf.d" + <% if node['logstash']['instance'][@name]['pluginpath'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['instance'][@name]['pluginpath'] %>" + <% end -%> + <% if node['logstash']['instance'][@name]['workers'] %> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['instance'][@name]['workers'] %>" + <% end %> + <% if node['logstash']['instance'][@name]['log_file'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= @home %>/log/<%= node['logstash']['instance'][@name]['log_file'] %>" + <% end -%> + <% if node['logstash']['instance'][@name]['debug'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" + <% end -%> + export OPTS="$JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS" + + <% if node['logstash']['instance'][@name]['upstart_with_sudo'] -%> + exec sudo -u <%= node['logstash']['instance'][@name]['user'] %> /usr/bin/java $OPTS + <% else -%> + exec /usr/bin/java $OPTS + <% end -%> +end script + +emits logstash-server-running diff --git a/templates/default/init/upstart.erb b/templates/default/init/upstart.erb new file mode 100644 index 0000000..2cf704b --- /dev/null +++ b/templates/default/init/upstart.erb @@ -0,0 +1,47 @@ +description "Logstash" +author "Chef" + +start on (filesystem and net-device-up) +stop on runlevel [!2345] + +respawn +respawn limit 5 30 +limit nofile 65550 65550 + +chdir <%= @home %> + +<% unless node['logstash']['instance'][@name]['upstart_with_sudo'] -%> +setuid <%= node['logstash']['instance'][@name]['user'] %> + <% unless node['logstash']['instance'][@name]['supervisor_gid'].to_s.empty? -%> +setgid <%= node['logstash']['instance'][@name]['supervisor_gid'] %> + <% end -%> +<% end -%> + +script + export LOGSTASH_HOME="<%= @home %>" + export HOME=$LOGSTASH_HOME + export GC_OPTS="<%= node['logstash']['instance'][@name]['gc_opts'] %>" + export JAVA_OPTS="-server -Xms<%= node['logstash']['instance'][@name]['xms'] %> -Xmx<%= node['logstash']['instance'][@name]['xmx'] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= node['logstash']['instance'][@name]['java_opts'] %> <%= '-Djava.net.preferIPv4Stack=true' if node['logstash']['instance'][@name]['ipv4_only'] %>" + export LOGSTASH_OPTS="agent -f <%= node['logstash']['instance'][@name]['home'] %>/etc/conf.d" + <% if node['logstash']['instance'][@name]['pluginpath'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['instance'][@name]['pluginpath'] %>" + <% end -%> + <% if node['logstash']['instance'][@name]['workers'] %> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['instance'][@name]['workers'] %>" + <% end %> + <% if node['logstash']['instance'][@name]['log_file'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= @home %>/log/<%= node['logstash']['instance'][@name]['log_file'] %>" + <% end -%> + <% if node['logstash']['instance'][@name]['debug'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" + <% end -%> + export OPTS="$JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS" + + <% if node['logstash']['instance'][@name]['upstart_with_sudo'] -%> + exec sudo -u <%= node['logstash']['instance'][@name]['user'] %> /usr/bin/java $OPTS + <% else -%> + exec /usr/bin/java $OPTS + <% end -%> +end script + +emits logstash-server-running From 3240cfab32b19f254cb91afe267e7110f270cc9f Mon Sep 17 00:00:00 2001 From: paulczar Date: Sun, 23 Mar 2014 16:29:17 -0500 Subject: [PATCH 10/55] LWRPs for pattern and config updated documentation --- .rubocop.yml | 6 + .tailor | 25 -- CHANGELOG.md | 16 +- README.md | 212 +++++----------- Vagrantfile | 27 +-- attributes/agent.rb | 53 +--- attributes/default.rb | 7 +- attributes/server.rb | 13 +- libraries/search.rb | 27 +++ providers/config.rb | 18 +- providers/instance.rb | 15 +- providers/pattern.rb | 61 +++++ providers/service.rb | 41 ++-- recipes/agent.rb | 229 ++---------------- recipes/haproxy.rb | 45 ---- recipes/server.rb | 49 +--- resources/config.rb | 8 +- resources/pattern.rb | 19 ++ rubocop-todo.yml | 10 - .../default/config/elasticsearch.conf.erb | 7 - ...apache.conf.erb => filter_apache.conf.erb} | 0 templates/default/config/input_file.conf.erb | 6 + ...{syslog.conf.erb => input_syslog.conf.erb} | 0 .../config/output_elasticsearch.conf.erb | 10 + ...stdout.conf.erb => output_stdout.conf.erb} | 0 templates/default/init/binary_upstart.erb | 4 +- templates/default/init/java_upstart.erb | 4 +- .../{patterns.erb => custom_patterns.erb} | 0 28 files changed, 299 insertions(+), 613 deletions(-) delete mode 100644 .tailor create mode 100644 libraries/search.rb create mode 100644 providers/pattern.rb delete mode 100644 recipes/haproxy.rb create mode 100644 resources/pattern.rb delete mode 100644 rubocop-todo.yml delete mode 100644 templates/default/config/elasticsearch.conf.erb rename templates/default/config/{apache.conf.erb => filter_apache.conf.erb} (100%) create mode 100644 templates/default/config/input_file.conf.erb rename templates/default/config/{syslog.conf.erb => input_syslog.conf.erb} (100%) create mode 100644 templates/default/config/output_elasticsearch.conf.erb rename templates/default/config/{stdout.conf.erb => output_stdout.conf.erb} (100%) rename templates/default/patterns/{patterns.erb => custom_patterns.erb} (100%) diff --git a/.rubocop.yml b/.rubocop.yml index 695bf01..5cb7bae 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -26,3 +26,9 @@ ClassAndModuleChildren: UselessAssignment: Enabled: false + +BlockNesting: + Max: 4 + +Documentation: + Enabled: false diff --git a/.tailor b/.tailor deleted file mode 100644 index f54fac6..0000000 --- a/.tailor +++ /dev/null @@ -1,25 +0,0 @@ -Tailor.config do |config| - config.formatters "text" - config.file_set '**/*.rb' do |style| - style.max_line_length 80, level: :off - style.allow_camel_case_methods false, level: :error - style.allow_hard_tabs false, level: :error - style.allow_screaming_snake_case_classes false, level: :error - style.allow_trailing_line_spaces false, level: :error - style.allow_invalid_ruby false, level: :warn - style.indentation_spaces 2, level: :error - style.max_code_lines_in_class 300, level: :error - style.max_code_lines_in_method 30, level: :error - style.spaces_after_comma 1, level: :error - style.spaces_after_lbrace 1, level: :error - style.spaces_after_lbracket 0, level: :error - style.spaces_after_lparen 0, level: :error - style.spaces_before_comma 0, level: :error - style.spaces_before_lbrace 1, level: :error - style.spaces_before_rbrace 1, level: :error - style.spaces_before_rbracket 0, level: :error - style.spaces_before_rparen 0, level: :error - style.spaces_in_empty_braces 0, level: :error - style.trailing_newlines 1, level: :error - end -end \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index f1aeb82..4e9f9f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,17 +2,26 @@ This file is used to list changes made in each version of chef-logstash. -## 0.8.0 -* major refactor towards being a library cookbook +## 0.8.0: + +_this will almost certainly break backwards compatibility_ + * support for Logstash 1.4 +* major refactor towards being a library cookbook + * instance LWRP + * service LWRP + * pattern LWP + * config LWP + +## 0.7.7: -## 0.7.7 * Support for new beaver config [#239](https://github.com/lusis/chef-logstash/pull/239) * Support for multiline codec [#240](https://github.com/lusis/chef-logstash/pull/240) * Parameterize /var/lib/logstash [#242](https://github.com/lusis/chef-logstash/pull/242) * Fix parameter spacing option [#244](https://github.com/lusis/chef-logstash/pull/244) ## 0.7.6: + * introduced more testing * Strainer: rubocop, knife test, foodcritic, chefspec * lots of style fixes for rubocop @@ -20,6 +29,7 @@ This file is used to list changes made in each version of chef-logstash. * testkitchen + server spec ## 0.7.5: + * added fedora systemd support * moved zeromq repos to own recipe diff --git a/README.md b/README.md index 264bba6..05a7b00 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,12 @@ Description This is the semi-official 'all-in-one' Logstash cookbook. -If you are using logstash < 1.2 you might want to use the 0.6.1 branch. +This cookbook is in transition from being a regular cookbook to following the Library Cookbook pattern. +While you can still use the `agent` and `server` recipes, the power of this cookbook now comes from the +`LWRPs`. + +If you are using logstash < 1.2 you might want to use the 0.6.x branch. +If you are using logstash < 1.4 you might want to use the 0.7.x branch. Requirements ============ @@ -23,156 +28,19 @@ see the Berksfile for more details * [Heavywater Graphite Cookbook](https://github.com/hw-cookbooks/graphite) - This is the one I use * [Karmi's ElasticSearch Cookbook](https://github.com/elasticsearch/cookbook-elasticsearch) * [RiotGames RBENV cookbook](https://github.com/RiotGames/rbenv-cookbook) - - +* [@lusis Kibana cookbook](https://github.com/lusis/chef-kibana) Attributes ========== ## Default -* `node['logstash']['homedir']` - the home directory of the logstash user -* `node['logstash']['basedir']` - the base directory for all the - Logstash components -* `node['logstash']['user']` - the owner for all Logstash components -* `node['logstash']['group']` - the group for all Logstash components -* `node['logstash']['supervisor_gid']` - set gid to run logstash as in supervisor ( runit, upstart ). - Useful for Ubuntu where logstash or beaver needs to run as group `adm` to read syslog -* `node['logstash']['graphite_role']` - the Chef role to search for - discovering your preexisting Graphite server -* `node['logstash']['graphite_query']` - the search query used for - discovering your preexisting Graphite server. Defaults to - node['logstash']['graphite_role'] in the current node environment -* `node['logstash']['elasticsearch_role']` - the Chef role to search - for discovering your preexisting ElasticSearch cluster. -* `node['logstash']['elasticsearch_query']` - the search query used for - discovering your preexisting ElasticSearch cluster. Defaults to - node['logstash']['elasticsearch_role'] in the current node environment -* `node['logstash']['elasticsearch_cluster']` - the cluster name - assigned to your preexisting ElasticSearch cluster. Only applies to - external ES clusters. -* `node['logstash']['elasticsearch_ip']` - the IP address that will be - used for your elasticsearch server in case you are using Chef-solo -* `node['logstash']['graphite_ip']` - the IP address that will be used - for your graphite server in case you are using Chef-solo -* `node['logstash']['join_groups']` - An array of Operating System - groups to join. Usefull to gain read privileges on some logfiles. -* `node['logstash']['patterns']` - A hash with grok patterns to be - used on grok and multiline filters. -* `node['logstash']['create_account']` - create the account info from - `user` and `group`; this is `true` by default. Disable it to use an - existing account! -* `node['logstash']['install_zeromq']` - Should this - recipe install zeromq packages? -* `node['logstash']['install_rabbitmq']` - Should this - recipe install rabbitmq packages? -* `node['logstash']['zeromq_packages']` - zeromq_packages to install - if you use zeromq - -## Agent - -* `node['logstash']['agent']['install_method']` - The method to - install logstash - either `jar` or `source`, defaults to `jar` -* `node['logstash']['agent']['version']` - The version of Logstash to - install. Only applies to `jar` install method. -* `node['logstash']['agent']['source_url']` - The URL of the Logstash - jar to download. Only applies to `jar` install method. -* `node['logstash']['agent']['checksum']` - The checksum of the jar - file. Only applies to `jar` install method. -* `node['logstash']['agent']['base_config']` - The name of the - template to use for `logstash.conf` as a base config. -* `node['logstash']['agent']['base_config_cookbook']` - Where to find - the base\_config template. -* `node['logstash']['agent']['workers']` - Number of workers for filter processing. -* `node['logstash']['agent']['xms']` - The minimum memory to assign - the JVM. -* `node['logstash']['agent']['xmx']` - The maximum memory to assign - the JVM. -* `node['logstash']['agent']['java_opts']` - Additional params you - want to pass to the JVM -* `node['logstash']['agent']['gc_opts']` - Specify your garbage - collection options to pass to the JVM -* `node['logstash']['agent']['ipv4_only']` - Add jvm option - preferIPv4Stack? -* `node['logstash']['agent']['debug']` - Run logstash with `-v` - option? -* `node['logstash']['agent']['server_role']` - The role of the node - behaving as a Logstash `server`/`indexer` -* `node['logstash']['agent']['inputs']` - Array of input plugins - configuration. -* `node['logstash']['agent']['filters']` - Array of filter plugins - configuration. -* `node['logstash']['agent']['outputs']` - Array of output plugins - configuration. -* `node['logstash']['agent']['patterns_dir']` - The patterns directory - where pattern files will be generated. Relative to the basedir or - absolute. -* `node['logstash']['agent']['home']` - home dir of logstash agent -* `node['logstash']['agent']['config_dir']` - location of conf.d style config dir -* `node['logstash']['agent']['config_file']` - name for base config file ( in conf.d dir ) -* `node['logstash']['agent']['upstart_with_sudo']` - use sudo with upstart - - -## Server - -* `node['logstash']['server']['install_method']` - The method to - install logstash - either `jar` or `source` -* `node['logstash']['server']['version']` - The version of Logstash to - install. Only applies to `jar` install method. -* `node['logstash']['server']['source_url']` - The URL of the Logstash - jar to download. Only applies to `jar` install method. -* `node['logstash']['server']['checksum']` - The checksum of the jar - file. Only applies to `jar` install method. -* `node['logstash']['server']['base_config']` - The name of the - template to use for `logstash.conf` as a base config. -* `node['logstash']['server']['base_config_cookbook']` - Where to find - the base config template. -* `node['logstash']['server']['xms']` - The minimum memory to assign - the JVM. -* `node['logstash']['server']['xmx']` - The maximum memory to assign - the JVM. -* `node['logstash']['server']['java_opts']` - Additional params you - want to pass to the JVM -* `node['logstash']['server']['gc_opts']` - Specify your garbage - collection options to pass to the JVM -* `node['logstash']['server']['ipv4_only']` - Add jvm option - preferIPv4Stack? -* `node['logstash']['server']['debug']` - Run logstash with `-v` - option? -* `node['logstash']['server']['enable_embedded_es']` - Should Logstash - run with the embedded ElasticSearch server or not? -* `node['logstash']['server']['inputs']` - Array of input plugins - configuration. -* `node['logstash']['server']['filters']` - Array of filter plugins - configuration. -* `node['logstash']['server']['outputs']` - Array of output plugins - configuration. -* `node['logstash']['server']['patterns_dir']` - The patterns - directory where pattern files will be generated. Relative to the - basedir or absolute. -* `node['logstash']['server']['home']` - home dir of logstash agent -* `node['logstash']['server']['config_dir']` - location of conf.d style config dir -* `node['logstash']['server']['config_file']` - name for base config file ( in conf.d dir ) -* `node['logstash']['server']['workers']` - Number of workers for filter processing. -* `node['logstash']['server']['web']['enable']` - true to enable embedded kibana ( may be behind in features ) -* `node['logstash']['server']['web']['address']` - IP Address to listen on -* `node['logstash']['server']['web']['port']` - port to listen on. -* `node['logstash']['server']['upstart_with_sudo']` - use sudo with upstart - -## Kibana - -Kibana can be run from the embedded version in elasticsearch. -It is not recommended that you use this outside of basic testing. This is for several reasons: - -- Kibana is a fast moving target -- It violates SRP -- It's not very secure when run this way -- There are two solid cookbooks for using Kibana now - - Kibana2 (Ruby version): https://github.com/realityforge/chef-kibana - - Kibana3 (HTML/JS version): https://github.com/lusis/chef-kibana +see [attributes/default.rb](attributes/default.rb) ## Beaver (alternative to Logstash Agent) +_This will be depreciated soon in favor of an external library cookbook._ + * `node['logstash']['beaver']['repo']` - URL or repository to install beaver from (using pip). * `node['logstash']['beaver']['server_role']` - The role of the node @@ -236,6 +104,43 @@ It is not recommended that you use this outside of basic testing. This is for se * `node['logstash']['index_cleaner']['cron']['log_file']` - Path to direct the index_cleaner cron job's stdout and stderr +Lightweight Resource Providers +=================== + +These now do all the heavy lifting. + +## logstash_instance + +This will install a logstash instance. It will take defaults from attributes for most attributes. + +see [resources/instance.rb](resources/instance.rb) + +## logstash_service + +This will create system init scripts for managing logstash instance. It will take defaults from attributes for most attributes. + +see [resources/service.rb](resources/service.rb) + +_experimental support for pleaserun has been added. Only `native` for `Ubuntu 12.04` has been thoroughly tested._ + +## logstash_config + +This will create logstash config files. It will take defaults from attributes for most attributes. + +see [resources/config.rb](resources/config.rb) + +## logstash_pattern + +This will install custom grok patterns for logstash. It will take defaults from attributes for most attributes: + +see [resources/pattern.rb](resources/pattern.rb) + +Searching +====== + +There is a search helper library `libraries/search.rb` which will help you search for values such as `elasticsearch_ip`. see the `server` recipe for an example of its usage. + + Testing ======= @@ -245,16 +150,10 @@ Testing vagrant up precise64 ``` -## Strainer +## Rubocop, FoodCritic, Rspec, Test-Kitchen ``` -rake strainer -``` - -## Test-Kitchen + ServerSpec - -``` -rake kitchen +bundle exec rake ``` Contributing @@ -269,10 +168,10 @@ Usage A proper readme is forthcoming but in the interim.... -There are 2 recipes you need to concern yourself with: +These two recipes show how to install and configure logstash instances via the provided `LWRPs` -* server - This would be your indexer node -* agent - This would be a local host's agent for collection +* [recipe/server.rb](recipe/server.rb) - This would be your indexer node +* [recipe/agent.rb](recipe/agent.rb) - This would be a local host's agent for collection Every attempt (and I mean this) was made to ensure that the following @@ -375,6 +274,11 @@ node['logstash']['agent']['config_templates_variables'] = { apache: { type: 'apa ## Letting data drive your templates +*DEPRECIATED!* + +While this may work ... it is no longer being actively supported by the maintainers of this cookbook. +We will accept `PRs`. + The current templates for the agent and server are written so that you can provide ruby hashes in your roles that map to inputs, filters, and outputs. Here is a role for logstash_server. @@ -630,11 +534,13 @@ Logstash will listen for syslog messages on tcp/5140 - Author: Richard Clamp (@richardc) - Author: Juanje Ojeda (@juanje) - Author: @benattar +- Author: Paul Czarkowski (@pczarkowski) - Copyright: 2012, John E. Vincent - Copyright: 2012, Bryan W. Berry - Copyright: 2012, Richard Clamp - Copyright: 2012, Juanje Ojeda - Copyright: 2012, @benattar +- Copyright: 2014, Paul Czarkowski Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/Vagrantfile b/Vagrantfile index fda1241..c826766 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -6,15 +6,12 @@ log_level = :info chef_run_list = %w( java::default + curl::default logstash::server + logstash::agent ) -# curl::default # minitest-handler::default -# logstash::server -# logstash::agent -# ark::default # kibana::default -# ] chef_json = { java: { @@ -24,22 +21,7 @@ chef_json = { webserver_listen: '0.0.0.0', webserver: 'nginx', install_type: 'file' - }, - logstash: { - supervisor_gid: 'adm', - agent: { - server_ipaddress: '127.0.0.1', - xms: '128m', - xmx: '128m', - enable_embedded_es: false - }, - server: { - xms: '128m', - xmx: '128m', - enable_embedded_es: true, - web: { enable: false } - } - } + } } Vagrant.configure('2') do |config| @@ -68,7 +50,6 @@ Vagrant.configure('2') do |config| chef.run_list = chef_run_list chef.json = chef_json chef.run_list.unshift('apt') - chef.json[:logstash][:server][:init_method] = 'runit' end end @@ -83,7 +64,6 @@ Vagrant.configure('2') do |config| chef.run_list = chef_run_list chef.json = chef_json chef.run_list.unshift('apt') - chef.json[:logstash][:server][:init_method] = 'runit' end end config.vm.define :lucid32 do |dist_config| @@ -96,7 +76,6 @@ Vagrant.configure('2') do |config| chef.run_list = chef_run_list chef.json = chef_json chef.run_list.unshift('apt') - chef.json[:logstash][:server][:init_method] = 'runit' end end diff --git a/attributes/agent.rb b/attributes/agent.rb index 7a843f3..1622db2 100644 --- a/attributes/agent.rb +++ b/attributes/agent.rb @@ -1,46 +1,19 @@ # Encoding: utf-8 -default['logstash']['agent']['version'] = '1.3.3' -default['logstash']['agent']['log_file'] = '/var/log/logstash/agent.log' -default['logstash']['agent']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.3.3-flatjar.jar' -default['logstash']['agent']['checksum'] = 'a83503bd2aa32e1554b98f812d0b411fbc5f7b6b21cebb48b7d344474f2dfc6d' -default['logstash']['agent']['install_method'] = 'jar' # Either `source` or `jar` -default['logstash']['agent']['home'] = "#{node['logstash']['basedir']}/agent" -default['logstash']['agent']['patterns_dir'] = 'etc/patterns' -default['logstash']['agent']['config_dir'] = 'etc/conf.d' -default['logstash']['agent']['config_file'] = 'logstash.conf' -default['logstash']['agent']['config_templates'] = [] -default['logstash']['agent']['config_templates_cookbook'] = 'logstash' -default['logstash']['agent']['config_templates_variables'] = {} -default['logstash']['agent']['base_config'] = 'agent.conf.erb' -default['logstash']['agent']['base_config_cookbook'] = 'logstash' -default['logstash']['agent']['xms'] = '384M' -default['logstash']['agent']['xmx'] = '384M' -default['logstash']['agent']['java_opts'] = '' -default['logstash']['agent']['gc_opts'] = '-XX:+UseParallelOldGC' -default['logstash']['agent']['ipv4_only'] = false -default['logstash']['agent']['debug'] = false -# allow control over the upstart config -default['logstash']['agent']['upstart_with_sudo'] = false -default['logstash']['agent']['upstart_respawn_count'] = 5 -default['logstash']['agent']['upstart_respawn_timeout'] = 30 -default['logstash']['agent']['init_method'] = 'native' # native or runit -default['logstash']['agent']['init_method'] = 'native' -default['logstash']['agent']['workers'] = 1 -# allow control over the upstart config -default['logstash']['server']['upstart_with_sudo'] = false +# slurp in defaults to named instance +default['logstash']['instance']['agent'] = node['logstash']['instance']['default'] -# logrotate options for logstash agent -default['logstash']['agent']['logrotate']['options'] = %w(missingok notifempty) -# stop/start on logrotate? -default['logstash']['agent']['logrotate']['stopstartprepost'] = false +# override any defaults here. -# roles/flasgs for various autoconfig/discovery components -default['logstash']['agent']['server_role'] = 'logstash_server' +override['logstash']['instance']['agent']['config_templates'] = { + 'input_file' => 'config/input_file.conf.erb', + 'output_stdout' => 'config/output_stdout.conf.erb' +} -# for use in case recipe used w/ chef-solo, default to self -default['logstash']['agent']['server_ipaddress'] = '' +override['logstash']['instance']['default']['config_templates_variables'] = { + input_file_name: '/var/log/syslog', + input_file_type: 'syslog' +} -default['logstash']['agent']['inputs'] = [] -default['logstash']['agent']['filters'] = [] -default['logstash']['agent']['outputs'] = [] +override['logstash']['instance']['agent']['enable_embedded_es'] = false +override['logstash']['instance']['agent']['supervisor_gid'] = 'adm' diff --git a/attributes/default.rb b/attributes/default.rb index 3bde883..6785e7d 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -36,8 +36,9 @@ default['logstash']['instance']['default']['debug'] = false default['logstash']['instance']['default']['workers'] = 1 -default['logstash']['instance']['default']['patterns_templates_cookbook'] = 'logstash' -default['logstash']['instance']['default']['patterns_templates'] = {} +default['logstash']['instance']['default']['pattern_templates_cookbook'] = 'logstash' +default['logstash']['instance']['default']['pattern_templates'] = {} +default['logstash']['instance']['default']['pattern_templates_variables'] = {} default['logstash']['instance']['default']['base_config_cookbook'] = 'logstash' default['logstash']['instance']['default']['base_config'] = '' # set if want data driven @@ -52,7 +53,7 @@ default['logstash']['instance']['default']['init_method'] = 'native' # pleaserun or native or runit # roles/flags for various autoconfig/discovery components -default['logstash']['instance']['default']['enable_embedded_es'] = true +default['logstash']['instance']['default']['enable_embedded_es'] = false default['logstash']['instance']['default']['inputs'] = [] default['logstash']['instance']['default']['filters'] = [] diff --git a/attributes/server.rb b/attributes/server.rb index 899a5ef..2cd1cf2 100644 --- a/attributes/server.rb +++ b/attributes/server.rb @@ -1,6 +1,15 @@ # Encoding: utf-8 + +# slurp in defaults to named instance default['logstash']['instance']['server'] = node['logstash']['instance']['default'] -override['logstash']['instance']['server']['config_templates'] = { 'syslog' => 'config/syslog.conf.erb', 'stdout' => 'config/stdout.conf.erb' } -override['logstash']['instance']['server']['patterns_templates'] = { 'default' => 'patterns/patterns' } # override any defaults here. + +override['logstash']['instance']['server']['config_templates'] = { + 'input_syslog' => 'config/input_syslog.conf.erb', + 'output_stdout' => 'config/output_stdout.conf.erb', + 'output_elasticsearch' => 'config/output_elasticsearch.conf.erb' +} +override['logstash']['instance']['server']['pattern_templates'] = { 'default' => 'patterns/custom_patterns.erb' } +override['logstash']['instance']['server']['elasticsearch_ip'] = '127.0.0.1' +override['logstash']['instance']['server']['enable_embedded_es'] = true diff --git a/libraries/search.rb b/libraries/search.rb new file mode 100644 index 0000000..b63bef4 --- /dev/null +++ b/libraries/search.rb @@ -0,0 +1,27 @@ +# Encoding: utf-8 +# rubocop:disable RedundantReturn +require 'rubygems' + +# extend recipe class for library +class Chef + class Recipe + def service_ip(instance = 'default', service = 'elasticsearch') + if node['logstash']['instance'].key?(instance) + attributes = node['logstash']['instance'][instance] + else + attributes = node['logstash']['instance']['default'] + end + if Chef::Config[:solo] + service_ip = attributes["#{service}_ip"] + else + results = search(:node, attributes["#{service}_query"]) + if !results.empty? + service_ip = results[0]['ipaddress'] + else + service_ip = attributes["#{service}_ip"] + end + end + service_ip + end + end +end diff --git a/providers/config.rb b/providers/config.rb index 607f07a..afdaafc 100644 --- a/providers/config.rb +++ b/providers/config.rb @@ -1,6 +1,6 @@ # Encoding: utf-8 # Cookbook Name:: logstash -# Provider:: service +# Provider:: config # Author:: John E. Vincent # License:: Apache 2.0 # @@ -20,7 +20,7 @@ def load_current_resource @templates = new_resource.templates || attributes['config_templates'] @templates_cookbook = new_resource.templates_cookbook || attributes['config_templates_cookbook'] @variables = new_resource.variables || attributes['config_templates_variables'] - @home = new_resource.home || "#{attributes['base_dir']}/#{@instance}" + @path = new_resource.path || "#{attributes['basedir']}/#{@instance}/etc/conf.d" @owner = new_resource.owner || attributes['user'] @group = new_resource.group || attributes['group'] @mode = new_resource.mode || '0644' @@ -30,8 +30,7 @@ def load_current_resource conf = conf_vars # Chef::Log.info("config vars: #{conf.inspect}") conf[:templates].each do |template, file| - home = "#{node['logstash']['instance'][conf[:instance]]['basedir']}/#{conf[:instance]}" - tp = template"#{home}/etc/conf.d/#{::File.basename(file).chomp(::File.extname(file))}" do + tp = template"#{conf[:path]}/#{::File.basename(file).chomp(::File.extname(file))}" do source file cookbook conf[:templates_cookbook] owner conf[:owner] @@ -45,15 +44,6 @@ def load_current_resource end end -action :remove do - conf[:templates].each do |template, file| - tp = template template do - action :remove - end - new_resource.updated_by_last_action(tp.updated_by_last_action?) - end -end - private def conf_vars @@ -61,7 +51,7 @@ def conf_vars instance: @instance, templates: @templates, variables: @variables, - home: @home, + path: @path, owner: @owner, group: @group, mode: @mode, diff --git a/providers/instance.rb b/providers/instance.rb index 05931db..9a05232 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -12,11 +12,16 @@ def load_current_resource @name = new_resource.name || 'default' - @base_directory = new_resource.base_directory || node['logstash']['instance'][@name]['basedir'] - @install_type = new_resource.install_type || node['logstash']['instance'][@name]['install_type'] - @version = new_resource.version || node['logstash']['instance'][@name]['version'] - @checksum = new_resource.checksum || node['logstash']['instance'][@name]['checksum'] - @source_url = new_resource.source_url || node['logstash']['instance'][@name]['source_url'] + if node['logstash']['instance'].key?(@name) + attributes = node['logstash']['instance'][@name] + else + attributes = node['logstash']['instance']['default'] + end + @base_directory = new_resource.base_directory || attributes['basedir'] + @install_type = new_resource.install_type || attributes['install_type'] + @version = new_resource.version || attributes['version'] + @checksum = new_resource.checksum || attributes['checksum'] + @source_url = new_resource.source_url || attributes['source_url'] @enable_logrotate = new_resource.enable_logrotate || node['logstash']['instance']['default']['enable_logrotate'] @repo = new_resource.repo @sha = new_resource.sha diff --git a/providers/pattern.rb b/providers/pattern.rb new file mode 100644 index 0000000..57ae41e --- /dev/null +++ b/providers/pattern.rb @@ -0,0 +1,61 @@ +# Encoding: utf-8 +# Cookbook Name:: logstash +# Provider:: patterns +# Author:: John E. Vincent +# License:: Apache 2.0 +# +# Copyright 2014, John E. Vincent + +require 'chef/mixin/shell_out' +require 'chef/mixin/language' +include Chef::Mixin::ShellOut + +def load_current_resource + @instance = new_resource.instance + if node['logstash']['instance'].key?(@instance) + attributes = node['logstash']['instance'][@instance] + else + attributes = node['logstash']['instance']['default'] + end + @templates = new_resource.templates || attributes['pattern_templates'] + @variables = new_resource.variables || attributes['pattern_templates_variables'] + @path = new_resource.path || "#{attributes['basedir']}/#{@instance}/patterns" + @owner = new_resource.owner || attributes['user'] + @group = new_resource.group || attributes['group'] + @mode = new_resource.mode || '0644' + @templates_cookbook = new_resource.templates_cookbook || attributes['pattern_templates_cookbook'] +end + +action :create do + pattern = pattern_vars + # Chef::Log.info("config vars: #{conf.inspect}") + pattern[:templates].each do |template, file| + tp = template"#{pattern[:path]}/#{::File.basename(file).chomp(::File.extname(file))}" do + source file + cookbook pattern[:templates_cookbook] + owner pattern[:owner] + group pattern[:group] + mode pattern[:mode] + variables pattern[:variables] + notifies :restart, "logstash_service[#{pattern[:instance]}]" + action :create + end + new_resource.updated_by_last_action(tp.updated_by_last_action?) + end +end + +private + +def pattern_vars + pattern = { + instance: @instance, + templates: @templates, + variables: @variables, + path: @path, + owner: @owner, + group: @group, + mode: @mode, + templates_cookbook: @templates_cookbook + } + pattern +end diff --git a/providers/service.rb b/providers/service.rb index fb09013..2b2d3f5 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -12,15 +12,20 @@ def load_current_resource @instance = new_resource.instance + if node['logstash']['instance'].key?(@instance) + @attributes = node['logstash']['instance'][@instance] + else + @attributes = node['logstash']['instance']['default'] + end @service_name = new_resource.service_name || "logstash_#{@instance}" - @home = "#{node['logstash']['instance'][@instance]['basedir']}/#{@instance}" - @method = new_resource.method || node['logstash']['instance'][@instance]['init_method'] + @home = "#{@attributes['basedir']}/#{@instance}" + @method = new_resource.method || @attributes['init_method'] @command = new_resource.command || "#{@home}/bin/logstash" @args = new_resource.args || default_args @description = new_resource.description || @service_name @chdir = new_resource.chdir || @home - @user = new_resource.user || node['logstash']['instance'][@instance]['user'] - @group = new_resource.group || node['logstash']['instance'][@instance]['group'] + @user = new_resource.user || @attributes['user'] + @group = new_resource.group || @attributes['group'] end action :restart do @@ -73,7 +78,7 @@ def load_current_resource action :enable do svc = svc_vars - Chef::Log.info("Using init method #{svc[:method]} for #{svc[:service_name]} - #{svc.inspect}") + Chef::Log.info("Using init method #{svc[:method]} for #{svc[:service_name]}") case svc[:method] when 'pleaserun' @run_context.include_recipe 'pleaserun::default' @@ -96,7 +101,7 @@ def load_current_resource when 'native' if platform_family? 'debian' if node['platform_version'] >= '12.04' - if node['logstash']['instance'][svc[:name]]['install_type'] == 'tarball' + if @attributes['install_type'] == 'tarball' tp_source = 'init/binary_upstart.erb' else tp_source = 'init/java_upstart.erb' @@ -104,7 +109,7 @@ def load_current_resource tp = template "/etc/init/#{svc[:service_name]}.conf" do mode '0644' source tp_source - variables(home: "#{node['logstash']['instance'][svc[:name]]['basedir']}/#{svc[:name]}", + variables(home: svc[:home], name: svc[:name], command: svc[:command] ) @@ -148,12 +153,12 @@ def load_current_resource owner 'root' group 'root' mode '0774' - variables(config_file: node['logstash']['server']['config_dir'], - home: node['logstash']['server']['home'], + variables(config_file: @attributes['config_dir'], + home: svc[:home], name: svc[:name], - log_file: node['logstash']['server']['log_file'], - max_heap: node['logstash']['server']['xmx'], - min_heap: node['logstash']['server']['xms'] + log_file: @attributes['log_file'], + max_heap: @attributes['xmx'], + min_heap: @attributes['xms'] ) end new_resource.updated_by_last_action(tp.updated_by_last_action?) @@ -172,12 +177,12 @@ def load_current_resource private def default_args - logstash_home = "#{node['logstash']['instance'][@instance]['basedir']}/#{@instance}" - args = ['agent', '-f', "#{node['logstash']['instance'][@instance]['home']}/etc/conf.d/"] - args.concat ['--pluginpath', node['logstash']['instance'][@instance]['pluginpath']] if node['logstash']['instance'][@instance]['pluginpath'] - args.concat ['-vv'] if node['logstash']['instance'][@instance]['debug'] - args.concat ['-l', "#{logstash_home}/log/#{node['logstash']['instance'][@instance]['log_file']}"] if node['logstash']['instance'][@instance]['log_file'] - args.concat ['-w', node['logstash']['instance'][@instance]['workers'].to_s] if node['logstash']['instance'][@instance]['workers'] + svc = svc_vars + args = ['agent', '-f', "#{svc[:home]}/etc/conf.d/"] + args.concat ['--pluginpath', @attributes['pluginpath']] if @attributes['pluginpath'] + args.concat ['-vv'] if @attributes['debug'] + args.concat ['-l', "#{svc[:home]}/log/#{@attributes['log_file']}"] if @attributes['log_file'] + args.concat ['-w', @attributes['workers'].to_s] if @attributes['workers'] args end diff --git a/recipes/agent.rb b/recipes/agent.rb index 73a0980..927be08 100644 --- a/recipes/agent.rb +++ b/recipes/agent.rb @@ -4,228 +4,27 @@ # Recipe:: agent # # -include_recipe 'logstash::default' -include_recipe 'yum::default' -if node['logstash']['agent']['init_method'] == 'runit' - include_recipe 'runit' - service_resource = 'runit_service[logstash_agent]' -else - service_resource = 'service[logstash_agent]' -end - -if node['logstash']['agent']['patterns_dir'][0] == '/' - patterns_dir = node['logstash']['agent']['patterns_dir'] -else - patterns_dir = node['logstash']['agent']['home'] + '/' + node['logstash']['agent']['patterns_dir'] -end - -if node['logstash']['install_zeromq'] - include_recipe 'logstash::zero_mq_repo' - node['logstash']['zeromq_packages'].each { |p| package p } -end - -# check if running chef-solo. If not, detect the logstash server/ip by role. If I can't do that, fall back to using ['logstash']['agent']['server_ipaddress'] -if Chef::Config[:solo] - logstash_server_ip = node['logstash']['agent']['server_ipaddress'] -else - logstash_server_results = search(:node, "roles:#{node['logstash']['agent']['server_role']}") - if !logstash_server_results.empty? - logstash_server_ip = logstash_server_results[0]['ipaddress'] - else - logstash_server_ip = node['logstash']['agent']['server_ipaddress'] - end -end +name = 'agent' -directory node['logstash']['agent']['home'] do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] +# these should all default correctly. listing out for example. +logstash_instance name do + action :create end -%w(bin etc lib tmp log).each do |ldir| - directory "#{node['logstash']['agent']['home']}/#{ldir}" do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] - end - - link "#{node['logstash']['homedir']}/#{ldir}" do - to "#{node['logstash']['agent']['home']}/#{ldir}" - end -end - -directory "#{node['logstash']['agent']['home']}/etc/conf.d" do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] +# services are hard! Let's go LWRP'ing. FIREBALL! FIREBALL! FIREBALL! +logstash_service name do + action [:enable, :start] end -directory patterns_dir do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] -end - -# node['logstash']['patterns'].each do |file, hash| -# template_name = patterns_dir + '/' + file -# template template_name do -# source 'patterns.erb' -# owner node['logstash']['user'] -# group node['logstash']['group'] -# variables(patterns: hash) -# mode '0644' -# notifies :restart, service_resource -# end -# end - -if node['logstash']['agent']['install_method'] == 'jar' - remote_file "#{node['logstash']['agent']['home']}/lib/logstash-#{node['logstash']['agent']['version']}.jar" do - owner 'root' - group 'root' - mode '0755' - source node['logstash']['agent']['source_url'] - checksum node['logstash']['agent']['checksum'] - action :create_if_missing - end - - link "#{node['logstash']['agent']['home']}/lib/logstash.jar" do - to "#{node['logstash']['agent']['home']}/lib/logstash-#{node['logstash']['agent']['version']}.jar" - notifies :restart, service_resource - end -else - include_recipe 'logstash::source' - - logstash_version = node['logstash']['source']['sha'] || "v#{node['logstash']['server']['version']}" - link "#{node['logstash']['agent']['home']}/lib/logstash.jar" do - to "#{node['logstash']['basedir']}/source/build/logstash-#{logstash_version}-monolithic.jar" - notifies :restart, service_resource - end -end - -template "#{node['logstash']['agent']['home']}/#{node['logstash']['agent']['config_dir']}/#{node['logstash']['agent']['config_file']}" do - source node['logstash']['agent']['base_config'] - cookbook node['logstash']['agent']['base_config_cookbook'] - owner node['logstash']['user'] - group node['logstash']['group'] - mode '0644' +logstash_config name do variables( - logstash_server_ip: logstash_server_ip, - patterns_dir: patterns_dir) - notifies :restart, service_resource - only_if { node['logstash']['agent']['config_file'] } -end - -unless node['logstash']['agent']['config_templates'].empty? || node['logstash']['agent']['config_templates'].nil? - node['logstash']['agent']['config_templates'].each do |config_template| - template "#{node['logstash']['agent']['home']}/#{node['logstash']['agent']['config_dir']}/#{config_template}.conf" do - source "#{config_template}.conf.erb" - cookbook node['logstash']['agent']['config_templates_cookbook'] - owner node['logstash']['user'] - group node['logstash']['group'] - mode '0644' - variables node['logstash']['agent']['config_templates_variables'][config_template] - notifies :restart, service_resource - action :create - end - end -end - -log_dir = ::File.dirname node['logstash']['agent']['log_file'] -directory log_dir do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] - recursive true -end - -if node['logstash']['agent']['init_method'] == 'runit' - runit_service 'logstash_agent' -elsif node['logstash']['agent']['init_method'] == 'native' - if platform_family? 'debian' - if node['platform_version'] >= '12.04' - template '/etc/init/logstash_agent.conf' do - mode '0644' - source 'logstash_agent.conf.erb' - notifies :restart, service_resource - end - - service 'logstash_agent' do - provider Chef::Provider::Service::Upstart - action [:enable, :start] - end - else - Chef::Log.fatal("Please set node['logstash']['agent']['init_method'] to 'runit' for #{node['platform_version']}") - end - elsif platform_family?('fedora') && node['platform_version'] >= '15' - execute 'reload-systemd' do - command 'systemctl --system daemon-reload' - action :nothing - end - - template '/etc/systemd/system/logstash_agent.service' do - source 'logstash_agent.service.erb' - owner 'root' - group 'root' - mode '0755' - notifies :run, 'execute[reload-systemd]', :immediately - notifies :restart, 'service[logstash_agent]', :delayed - end - - service 'logstash_agent' do - service_name 'logstash_agent.service' - provider Chef::Provider::Service::Systemd - action [:enable, :start] - end - elsif platform_family? 'rhel', 'fedora' - template '/etc/init.d/logstash_agent' do - source 'init.logstash_server.erb' - owner 'root' - group 'root' - mode '0774' - variables( - config_file: node['logstash']['agent']['config_dir'], - home: node['logstash']['agent']['home'], - log_file: node['logstash']['agent']['log_file'], - name: 'agent', - max_heap: node['logstash']['agent']['xmx'], - min_heap: node['logstash']['agent']['xms'] - ) - end - - service 'logstash_agent' do - supports restart: true, reload: true, status: true - action :enable - end - end -else - Chef::Log.fatal("Unsupported init method: #{node['logstash']['server']['init_method']}") + input_file_name: '/var/log/syslog', + input_file_type: 'syslog' + ) + action [:create] end -logrotate_app 'logstash' do - path "#{log_dir}/*.log" - if node['logstash']['logging']['useFileSize'] - size node['logstash']['logging']['maxSize'] - else - frequency node['logstash']['logging']['rotateFrequency'] - end - rotate node['logstash']['logging']['maxBackup'] - options node['logstash']['agent']['logrotate']['options'] - create "664 #{node['logstash']['user']} #{node['logstash']['group']}" - notifies :restart, 'service[rsyslog]' - if node['logstash']['agent']['logrotate']['stopstartprepost'] - prerotate <<-EOF - service logstash_agent stop - logger stopped logstash_agent service for log rotation - EOF - postrotate <<-EOF - service logstash_agent start - logger started logstash_agent service after log rotation - EOF - end +logstash_pattern name do + action [:create] end diff --git a/recipes/haproxy.rb b/recipes/haproxy.rb deleted file mode 100644 index 84e38c2..0000000 --- a/recipes/haproxy.rb +++ /dev/null @@ -1,45 +0,0 @@ -# Encoding: utf-8 -# this recipe lets output haproxy logs to file as if they were apache -# virtual logs, in order to interface with legacy traffic measuring -# applications like AWstats -# Also requires changes to your haproxy configuration -# and a file output on your logstash_server -# I have no idea if it meets anyone's needs other than my own -# only for those crazy enough to replace apache or Nginx as their -# main front-end server - Bryan W. Berry 28 June 2012 - -include_recipe 'logrotate' - -directory "#{node['logstash']['server']['home']}/apache_logs" do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] -end - -apache_logs = "#{node['logstash']['homedir']}/apache_logs" -link apache_logs do - to "#{node['logstash']['server']['home']}/apache_logs" -end - -directory "#{node['logstash']['server']['home']}/etc/patterns" do - owner node['logstash']['user'] - group node['logstash']['group'] - mode '0774' -end - -# create pattern_file for haproxy -cookbook_file "#{node['logstash']['server']['home']}/etc/patterns/haproxy" do - source 'haproxy' - owner node['logstash']['user'] - group node['logstash']['group'] - mode '0774' -end - -# set logrotate for /opt/logstash/server/apache_logs -logrotate_app 'apache_logs' do - path node['logstash']['server']['logrotate_target'] - frequency 'daily' - create "664 #{node['logstash']['user']} #{node['logstash']['user']}" - rotate '30' -end diff --git a/recipes/server.rb b/recipes/server.rb index 69f33cb..87a8ce1 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -17,57 +17,24 @@ # these should all default correctly. listing out for example. logstash_instance name do - base_directory attributes['basedir'] - version attributes['version'] - checksum attributes['checksum'] - source_url attributes['source_url'] - install_type attributes['install_type'] - user attributes['user'] - group attributes['group'] - enable_logrotate attributes['enable_logrotate'] action :create end -# fix search if chef solo -if Chef::Config[:solo] - es_server_ip = attributes['elasticsearch_ip'] - graphite_server_ip = attributes['graphite_ip'] -else - es_results = search(:node, attributes['elasticsearch_query']) - graphite_results = search(:node, attributes['graphite_query']) - - if !es_results.empty? - es_server_ip = es_results[0]['ipaddress'] - else - es_server_ip = attributes['elasticsearch_ip'] - end - - if !graphite_results.empty? - graphite_server_ip = graphite_results[0]['ipaddress'] - else - graphite_server_ip = attributes['graphite_ip'] - end -end - # services are hard! Let's go LWRP'ing. FIREBALL! FIREBALL! FIREBALL! logstash_service name do action [:enable, :start] end +es_ip = service_ip(name, 'elasticsearch') + logstash_config name do action [:create] - not_if { attributes['config_templates'].empty? } + variables( + elasticsearch_ip: es_ip, + elasticsearch_embedded: true + ) end -# add in any custom patterns -attributes['patterns_templates'].each do |template, file| - template "#{attributes['basedir']}/#{name}/patterns/#{File.basename(file)}" do - source "#{file}.erb" - cookbook attributes['patterns_templates_cookbook'] - owner attributes['user'] - group attributes['group'] - mode '0644' - # notifies :restart, service_resource - not_if { attributes['patterns_templates'].empty? } - end +logstash_pattern name do + action [:create] end diff --git a/resources/config.rb b/resources/config.rb index 6c62ddc..434864e 100644 --- a/resources/config.rb +++ b/resources/config.rb @@ -1,19 +1,19 @@ # Encoding: utf-8 # Cookbook Name:: logstash -# Resource:: instance +# Resource:: config # Author:: John E. Vincent # Copyright 2014, John E. Vincent # License:: Apache 2.0 -actions :create, :delete +actions :create default_action :create if defined?(default_action) attribute :instance, kind_of: String, name_attribute: true attribute :templates, kind_of: Array -attribute :variables, kind_of: Array -attribute :home, kind_of: String +attribute :variables, kind_of: Hash attribute :owner, kind_of: String attribute :group, kind_of: String attribute :mode, kind_of: String +attribute :path, kind_of: String attribute :templates_cookbook, kind_of: String diff --git a/resources/pattern.rb b/resources/pattern.rb new file mode 100644 index 0000000..dae946d --- /dev/null +++ b/resources/pattern.rb @@ -0,0 +1,19 @@ +# Encoding: utf-8 +# Cookbook Name:: logstash +# Resource:: patterns +# Author:: John E. Vincent +# Copyright 2014, John E. Vincent +# License:: Apache 2.0 + +actions :create + +default_action :create if defined?(default_action) + +attribute :instance, kind_of: String, name_attribute: true +attribute :templates, kind_of: Array +attribute :variables, kind_of: Hash +attribute :path, kind_of: String +attribute :owner, kind_of: String +attribute :group, kind_of: String +attribute :mode, kind_of: String +attribute :templates_cookbook, kind_of: String diff --git a/rubocop-todo.yml b/rubocop-todo.yml deleted file mode 100644 index ce6aca0..0000000 --- a/rubocop-todo.yml +++ /dev/null @@ -1,10 +0,0 @@ -# This configuration was generated by `rubocop --auto-gen-config` -# on 2014-03-22 15:56:34 -0500 using RuboCop version 0.19.0. -# The point is for the user to remove these configuration records -# one by one as the offenses are removed from the code base. -# Note that changes in the inspected code, or installation of new -# versions of RuboCop, may require this file to be generated again. - -# Offense count: 1 -BlockNesting: - Max: 4 diff --git a/templates/default/config/elasticsearch.conf.erb b/templates/default/config/elasticsearch.conf.erb deleted file mode 100644 index 0941bc1..0000000 --- a/templates/default/config/elasticsearch.conf.erb +++ /dev/null @@ -1,7 +0,0 @@ -output { - elasticsearch { -<% if es_server_ip -%> - host => <%= es_server_ip %> -<% end -%> - } -} \ No newline at end of file diff --git a/templates/default/config/apache.conf.erb b/templates/default/config/filter_apache.conf.erb similarity index 100% rename from templates/default/config/apache.conf.erb rename to templates/default/config/filter_apache.conf.erb diff --git a/templates/default/config/input_file.conf.erb b/templates/default/config/input_file.conf.erb new file mode 100644 index 0000000..9a5df6d --- /dev/null +++ b/templates/default/config/input_file.conf.erb @@ -0,0 +1,6 @@ +input { + file { + path => ["<%= @input_file_name %>"] + type => ["<%= @input_file_type %>"] + } +} diff --git a/templates/default/config/syslog.conf.erb b/templates/default/config/input_syslog.conf.erb similarity index 100% rename from templates/default/config/syslog.conf.erb rename to templates/default/config/input_syslog.conf.erb diff --git a/templates/default/config/output_elasticsearch.conf.erb b/templates/default/config/output_elasticsearch.conf.erb new file mode 100644 index 0000000..1a137a4 --- /dev/null +++ b/templates/default/config/output_elasticsearch.conf.erb @@ -0,0 +1,10 @@ +output { + elasticsearch { +<% if @elasticsearch_ip -%> + host => "<%= @elasticsearch_ip %>" +<% end -%> +<% if @elasticsearch_embedded -%> + embedded => true +<% end -%> + } +} \ No newline at end of file diff --git a/templates/default/config/stdout.conf.erb b/templates/default/config/output_stdout.conf.erb similarity index 100% rename from templates/default/config/stdout.conf.erb rename to templates/default/config/output_stdout.conf.erb diff --git a/templates/default/init/binary_upstart.erb b/templates/default/init/binary_upstart.erb index 3ef546d..3d76d29 100644 --- a/templates/default/init/binary_upstart.erb +++ b/templates/default/init/binary_upstart.erb @@ -20,7 +20,7 @@ setgid <%= node['logstash']['instance'][@name]['supervisor_gid'] %> script export LOGSTASH_HOME="<%= @home %>" export HOME=$LOGSTASH_HOME - export LOGSTASH_OPTS="agent -f <%= node['logstash']['instance'][@name]['home'] %>/etc/conf.d" + export LOGSTASH_OPTS="agent -f $LOGSTASH_HOME/etc/conf.d" <% if node['logstash']['instance'][@name]['pluginpath'] -%> export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['instance'][@name]['pluginpath'] %>" <% end -%> @@ -28,7 +28,7 @@ script export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['instance'][@name]['workers'] %>" <% end %> <% if node['logstash']['instance'][@name]['log_file'] -%> - export LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= @home %>/log/<%= node['logstash']['instance'][@name]['log_file'] %>" + export LOGSTASH_OPTS="$LOGSTASH_OPTS -l $LOGSTASH_HOME/log/<%= node['logstash']['instance'][@name]['log_file'] %>" <% end -%> <% if node['logstash']['instance'][@name]['debug'] -%> export LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" diff --git a/templates/default/init/java_upstart.erb b/templates/default/init/java_upstart.erb index 75e8437..8027232 100644 --- a/templates/default/init/java_upstart.erb +++ b/templates/default/init/java_upstart.erb @@ -23,7 +23,7 @@ script export GC_OPTS="<%= node['logstash']['instance'][@name]['gc_opts'] %>" export JAVA_OPTS="-server -Xms<%= node['logstash']['instance'][@name]['xms'] %> -Xmx<%= node['logstash']['instance'][@name]['xmx'] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= node['logstash']['instance'][@name]['java_opts'] %> <%= '-Djava.net.preferIPv4Stack=true' if node['logstash']['instance'][@name]['ipv4_only'] %>" - export LOGSTASH_OPTS="agent -f <%= node['logstash']['instance'][@name]['home'] %>/etc/conf.d" + export LOGSTASH_OPTS="agent -f $LOGSTASH_HOME/etc/conf.d" <% if node['logstash']['instance'][@name]['pluginpath'] -%> export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['instance'][@name]['pluginpath'] %>" <% end -%> @@ -31,7 +31,7 @@ script export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['instance'][@name]['workers'] %>" <% end %> <% if node['logstash']['instance'][@name]['log_file'] -%> - export LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= @home %>/log/<%= node['logstash']['instance'][@name]['log_file'] %>" + export LOGSTASH_OPTS="$LOGSTASH_OPTS -l $LOGSTASH_HOME/log/<%= node['logstash']['instance'][@name]['log_file'] %>" <% end -%> <% if node['logstash']['instance'][@name]['debug'] -%> export LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" diff --git a/templates/default/patterns/patterns.erb b/templates/default/patterns/custom_patterns.erb similarity index 100% rename from templates/default/patterns/patterns.erb rename to templates/default/patterns/custom_patterns.erb From b39867f0e8ce861701b729bf968d90413682c425 Mon Sep 17 00:00:00 2001 From: Paul Czarkowski Date: Mon, 24 Mar 2014 11:49:43 -0500 Subject: [PATCH 11/55] adding chefspec tests --- attributes/server.rb | 8 ++--- libraries/matchers.rb | 45 +++++++++++++++++++++++++++++ metadata.rb | 4 +-- providers/pattern.rb | 2 +- test/unit/spec/agent_spec.rb | 26 ++++++++++++++--- test/unit/spec/lwrp_config_spec.rb | 45 +++++++++++++++++++++++++++++ test/unit/spec/lwrp_pattern_spec.rb | 38 ++++++++++++++++++++++++ test/unit/spec/server_spec.rb | 26 ++++++++++++++--- test/unit/spec/spec_helper.rb | 29 +++++++++++++++---- 9 files changed, 202 insertions(+), 21 deletions(-) create mode 100644 libraries/matchers.rb create mode 100644 test/unit/spec/lwrp_config_spec.rb create mode 100644 test/unit/spec/lwrp_pattern_spec.rb diff --git a/attributes/server.rb b/attributes/server.rb index 2cd1cf2..8df32a0 100644 --- a/attributes/server.rb +++ b/attributes/server.rb @@ -5,11 +5,11 @@ # override any defaults here. -override['logstash']['instance']['server']['config_templates'] = { +set['logstash']['instance']['server']['config_templates'] = { 'input_syslog' => 'config/input_syslog.conf.erb', 'output_stdout' => 'config/output_stdout.conf.erb', 'output_elasticsearch' => 'config/output_elasticsearch.conf.erb' } -override['logstash']['instance']['server']['pattern_templates'] = { 'default' => 'patterns/custom_patterns.erb' } -override['logstash']['instance']['server']['elasticsearch_ip'] = '127.0.0.1' -override['logstash']['instance']['server']['enable_embedded_es'] = true +set['logstash']['instance']['server']['pattern_templates'] = { 'default' => 'patterns/custom_patterns.erb' } +set['logstash']['instance']['server']['elasticsearch_ip'] = '127.0.0.1' +set['logstash']['instance']['server']['enable_embedded_es'] = true diff --git a/libraries/matchers.rb b/libraries/matchers.rb new file mode 100644 index 0000000..bfd699b --- /dev/null +++ b/libraries/matchers.rb @@ -0,0 +1,45 @@ +# Encoding: utf-8 +# used by ChefSpec for LWRPs + +if defined?(ChefSpec) + # LWRP - Instance + def create_logstash_instance(name) + ChefSpec::Matchers::ResourceMatcher.new(:logstash_instance, :create, name) + end + + def delete_logstash_instance(name) + ChefSpec::Matchers::ResourceMatcher.new(:logstash_instance, :delete, name) + end + + # LWRP - Config + def create_logstash_config(name) + ChefSpec::Matchers::ResourceMatcher.new(:logstash_config, :create, name) + end + + # LWRP - Pattern + def create_logstash_pattern(name) + ChefSpec::Matchers::ResourceMatcher.new(:logstash_pattern, :create, name) + end + + # LWRP - Service + def enable_logstash_service(name) + ChefSpec::Matchers::ResourceMatcher.new(:logstash_service, :enable, name) + end + + def restart_logstash_service(name) + ChefSpec::Matchers::ResourceMatcher.new(:logstash_service, :restart, name) + end + + def start_logstash_service(name) + ChefSpec::Matchers::ResourceMatcher.new(:logstash_service, :start, name) + end + + def reload_logstash_service(name) + ChefSpec::Matchers::ResourceMatcher.new(:logstash_service, :reload, name) + end + + def stop_logstash_service(name) + ChefSpec::Matchers::ResourceMatcher.new(:logstash_service, :stop, name) + end + +end diff --git a/metadata.rb b/metadata.rb index fef32d1..5a45ee0 100644 --- a/metadata.rb +++ b/metadata.rb @@ -5,13 +5,13 @@ license 'Apache 2.0' description 'Installs/Configures logstash' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '0.8.1' +version '0.9.0' %w(ubuntu debian redhat centos scientific amazon fedora).each do |os| supports os end -%w(build-essential runit git ant java logrotate rabbitmq yum python ark pleaserun).each do |ckbk| +%w(build-essential runit git ant java logrotate yum python ark pleaserun).each do |ckbk| depends ckbk end diff --git a/providers/pattern.rb b/providers/pattern.rb index 57ae41e..56e17c5 100644 --- a/providers/pattern.rb +++ b/providers/pattern.rb @@ -30,7 +30,7 @@ def load_current_resource pattern = pattern_vars # Chef::Log.info("config vars: #{conf.inspect}") pattern[:templates].each do |template, file| - tp = template"#{pattern[:path]}/#{::File.basename(file).chomp(::File.extname(file))}" do + tp = template "#{pattern[:path]}/#{::File.basename(file).chomp(::File.extname(file))}" do source file cookbook pattern[:templates_cookbook] owner pattern[:owner] diff --git a/test/unit/spec/agent_spec.rb b/test/unit/spec/agent_spec.rb index b122224..3c82b00 100644 --- a/test/unit/spec/agent_spec.rb +++ b/test/unit/spec/agent_spec.rb @@ -2,12 +2,30 @@ require_relative 'spec_helper' describe 'logstash::agent' do - before { logstash_stubs } describe 'ubuntu' do - let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) } + let(:runner) { ChefSpec::Runner.new(::UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + # runner.node.set['logstash'] ... + runner.converge(described_recipe) + end + include_context 'stubs-common' + + it 'calls the logstash_instance LWPR' do + expect(chef_run).to create_logstash_instance('agent') + end + + it 'calls the logstash_config LWPR' do + expect(chef_run).to create_logstash_config('agent') + end + + it 'calls the logstash_pattern LWPR' do + expect(chef_run).to create_logstash_pattern('agent') + end - it 'writes some chefspec code' do - pending 'todo' + it 'calls the logstash_instance LWPR' 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/lwrp_config_spec.rb b/test/unit/spec/lwrp_config_spec.rb new file mode 100644 index 0000000..bae33b4 --- /dev/null +++ b/test/unit/spec/lwrp_config_spec.rb @@ -0,0 +1,45 @@ +# Encoding: utf-8 +require_relative 'spec_helper' +require_relative 'server_spec' + +::LWRP = { + step_into: ['logstash_config'] +}.merge(::UBUNTU_OPTS) + +describe 'logstash::server' do + describe 'ubuntu' do + let(:runner) { ChefSpec::Runner.new(::LWRP) } + let(:node) { runner.node } + let(:chef_run) do + runner.node.set['logstash']['instance']['server']['config_templates'] = { + output_stdout: 'config/output_stdout.conf.erb' + } + runner.node.set['logstash']['instance']['server']['config_templates_variables'] = { + elasticsearch_ip: '127.0.0.1', + elasticsearch_embedded: true + } + runner.node.set['logstash']['instance']['server']['basedir'] = '/opt/logstash' + runner.node.set['logstash']['instance']['server']['user'] = 'logstash' + runner.node.set['logstash']['instance']['server']['group'] = 'logstash' + runner.node.set['logstash']['instance']['server']['config_templates_cookbook'] = 'logstash' + runner.converge(described_recipe) + end + include_context 'stubs-common' + + it 'installs the config template' do + expect(chef_run).to create_template('/opt/logstash/server/etc/conf.d/output_stdout.conf').with( + source: 'config/output_stdout.conf.erb', + cookbook: 'logstash', + owner: 'logstash', + group: 'logstash', + mode: '0644', + variables: { + elasticsearch_ip: '127.0.0.1', + elasticsearch_embedded: true + }, + action: [:create] + ) + end + + end +end diff --git a/test/unit/spec/lwrp_pattern_spec.rb b/test/unit/spec/lwrp_pattern_spec.rb new file mode 100644 index 0000000..2238d22 --- /dev/null +++ b/test/unit/spec/lwrp_pattern_spec.rb @@ -0,0 +1,38 @@ +# Encoding: utf-8 +require_relative 'spec_helper' +require_relative 'server_spec' + +describe 'logstash::server' do + describe 'ubuntu' do + let(:runner) { ChefSpec::Runner.new(step_into: ['logstash_pattern']) } + let(:node) { runner.node } + let(:chef_run) do + runner.node.merge(::UBUNTU_OPTS) + runner.node.set['logstash']['instance']['server']['pattern_templates'] = { + 'default' => 'patterns/custom_patterns.erb' + } + runner.node.set['logstash']['instance']['server']['pattern_templates_variables'] = { + 'test' => true + } + runner.node.set['logstash']['instance']['server']['basedir'] = '/opt/logstash' + runner.node.set['logstash']['instance']['server']['user'] = 'logstash' + runner.node.set['logstash']['instance']['server']['group'] = 'logstash' + runner.node.set['logstash']['instance']['server']['pattern_templates_cookbook'] = 'logstash' + runner.converge(described_recipe) + end + include_context 'stubs-common' + + it 'installs the pattern template' do + expect(chef_run).to create_template('/opt/logstash/server/patterns/custom_patterns').with( + source: 'patterns/custom_patterns.erb', + cookbook: 'logstash', + owner: 'logstash', + group: 'logstash', + mode: '0644', + variables: { 'test' => true }, + action: [:create] + ) + end + + end +end diff --git a/test/unit/spec/server_spec.rb b/test/unit/spec/server_spec.rb index 0934e77..2fba609 100644 --- a/test/unit/spec/server_spec.rb +++ b/test/unit/spec/server_spec.rb @@ -2,12 +2,30 @@ require_relative 'spec_helper' describe 'logstash::server' do - before { logstash_stubs } describe 'ubuntu' do - let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) } + let(:runner) { ChefSpec::Runner.new(::UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + # runner.node.set['logstash'] ... + runner.converge(described_recipe) + end + include_context 'stubs-common' + + it 'calls the logstash_instance LWPR' do + expect(chef_run).to create_logstash_instance('server') + end + + it 'calls the logstash_config LWPR' do + expect(chef_run).to create_logstash_config('server') + end + + it 'calls the logstash_pattern LWPR' do + expect(chef_run).to create_logstash_pattern('server') + end - it 'writes some chefspec code' do - pending 'todo' + it 'calls the logstash_instance LWPR' do + expect(chef_run).to enable_logstash_service('server') + expect(chef_run).to start_logstash_service('server') end end diff --git a/test/unit/spec/spec_helper.rb b/test/unit/spec/spec_helper.rb index cee8990..c5b8b75 100644 --- a/test/unit/spec/spec_helper.rb +++ b/test/unit/spec/spec_helper.rb @@ -1,18 +1,35 @@ # Encoding: utf-8 require 'chefspec' require 'chefspec/berkshelf' +# require 'chefspec/server' require 'chef/application' ::LOG_LEVEL = :fatal +::REDHAT_OPTS = { + platform: 'redhat', + version: '6.4', + log_level: ::LOG_LEVEL +} ::UBUNTU_OPTS = { - platform: 'ubuntu', - version: '12.04', + platform: 'ubuntu', + version: '12.04', log_level: ::LOG_LEVEL } -def logstash_stubs - stub_command("update-alternatives --display java | grep '/usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java - priority 1061'").and_return(true) - stub_command("/usr/bin/python -c 'import setuptools'").and_return(true) - stub_command('test -f /opt/logstash/source/build/logstash-v1.3.2-monolithic.jar').and_return(true) +shared_context 'stubs-common' do + before do + Chef::Application.stub(:fatal!).and_return('fatal') + stub_command("update-alternatives --display java | grep '/usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java - priority 1061'").and_return(true) + stub_command("/usr/bin/python -c 'import setuptools'").and_return(true) + stub_command('test -f /opt/logstash/source/build/logstash-v1.3.2-monolithic.jar').and_return(true) + end +end + +shared_examples 'example' do +# it 'does not include example recipe by default' do +# expect(chef_run).not_to include_recipe('example::default') +# end end + +at_exit { ChefSpec::Coverage.report! } From 70c6cfc7d1f1aa270a05e562fde5139ad3fe535a Mon Sep 17 00:00:00 2001 From: paulczar Date: Mon, 31 Mar 2014 08:28:00 -0500 Subject: [PATCH 12/55] added plugins LWRP --- README.md | 6 ++++ Vagrantfile | 6 ++-- attributes/default.rb | 15 ++++++--- providers/plugins.rb | 76 +++++++++++++++++++++++++++++++++++++++++++ recipes/server.rb | 5 +++ resources/plugins.rb | 21 ++++++++++++ 6 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 providers/plugins.rb create mode 100644 resources/plugins.rb diff --git a/README.md b/README.md index 05a7b00..13433bf 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,12 @@ This will install custom grok patterns for logstash. It will take defaults fro see [resources/pattern.rb](resources/pattern.rb) +## logstash_plugns + +This will install the logstash community plugins: + +see [resources/plugins.rb](resources/plugins.rb) + Searching ====== diff --git a/Vagrantfile b/Vagrantfile index c826766..2bbbaaa 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -8,10 +8,10 @@ chef_run_list = %w( java::default curl::default logstash::server - logstash::agent ) -# minitest-handler::default -# kibana::default +# logstash::agent +# minitest-handler::default +# kibana::default chef_json = { java: { diff --git a/attributes/default.rb b/attributes/default.rb index 6785e7d..129e160 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -22,12 +22,19 @@ default['logstash']['instance']['default']['join_groups'] = [] default['logstash']['instance']['default']['homedir'] = '/var/lib/logstash' -default['logstash']['instance']['default']['version'] = '1.4.0.rc1' -default['logstash']['instance']['default']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.rc1.tar.gz' -default['logstash']['instance']['default']['checksum'] = 'b015fa130d589af957c9a48e6f59754f5c0954835abf44bd013547a6b6520e59' +default['logstash']['instance']['default']['version'] = '1.4.0' +default['logstash']['instance']['default']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.tar.gz' +default['logstash']['instance']['default']['checksum'] = 'ab62394bb56da10cb20ee106badf22734402b21435977ec4f9aa65257627c629' default['logstash']['instance']['default']['install_type'] = 'tarball' -default['logstash']['instance']['default']['log_file'] = 'logstash.log' +default['logstash']['instance']['default']['plugins']['install_type'] = 'native' # native|tarball +default['logstash']['instance']['default']['plugins']['version'] = '1.4.0' +default['logstash']['instance']['default']['plugins']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-contrib-1.4.0.tar.gz' +default['logstash']['instance']['default']['plugins']['checksum'] = '23aa397a7832d6e3553eba8360ff5d4ccfff98aac1e4a1b8201c21755e8d77c9' +default['logstash']['instance']['default']['plugins']['install_type'] = 'tarball' +default['logstash']['instance']['default']['plugins']['check_if_installed'] = 'lib/logstash/filters/translate.rb' + +default['logstash']['instance']['default']['log_file'] = 'logstash.log' default['logstash']['instance']['default']['xms'] = '1024M' default['logstash']['instance']['default']['xmx'] = '1024M' default['logstash']['instance']['default']['java_opts'] = '' diff --git a/providers/plugins.rb b/providers/plugins.rb new file mode 100644 index 0000000..83f6d6a --- /dev/null +++ b/providers/plugins.rb @@ -0,0 +1,76 @@ +# Encoding: utf-8 +# Cookbook Name:: logstash +# Provider:: plugins +# Author:: John E. Vincent +# License:: Apache 2.0 +# +# Copyright 2014, John E. Vincent + +require 'chef/mixin/shell_out' +require 'chef/mixin/language' +include Chef::Mixin::ShellOut + +def load_current_resource + @name = new_resource.name || 'contrib' + @instance = new_resource.instance || 'default' + if node['logstash']['instance'].key?(@instance) + attributes = node['logstash']['instance'][@instance] + else + attributes = node['logstash']['instance']['default'] + end + @base_directory = new_resource.base_directory || attributes['basedir'] + @version = new_resource.version || attributes['plugins']['version'] + @checksum = new_resource.checksum || attributes['plugins']['checksum'] + @source_url = new_resource.source_url || attributes['plugins']['source_url'] + @user = new_resource.user || attributes['user'] + @group = new_resource.group || attributes['group'] + @instance_dir = "#{@base_directory}/#{@instance}" + @install_type = new_resource.install_type || attributes['plugins']['install_type'] + @install_check = new_resource.install_check || attributes['plugins']['check_if_installed'] +end + +action :create do + ls_version = @version + ls_checksum = @checksum + ls_source_url = @source_url + ls_basedir = @base_directory + ls_user = @user + ls_group = @group + ls_name = @name + ls_instance = @instance + ls_instance_dir = @instance_dir + ls_install_check = @install_check + + case @install_type + when 'native' + ex = execute "bin/plugin install #{ls_name}" do + command "bin/plugin install #{ls_name}" + user ls_user + group ls_group + cwd ls_instance_dir + notifies :restart, "logstash_service[#{ls_instance}]" + # this is a temp workaround to make the plugin command idempotent. + not_if { ::File.exists?("#{ls_instance_dir}/#{ls_install_check}") } + end + new_resource.updated_by_last_action(ex.updated_by_last_action?) + when 'tarball' + @run_context.include_recipe 'ark::default' + arkit = ark "#{ls_instance}_contrib" do + name ls_instance + url ls_source_url + checksum ls_checksum + owner ls_user + group ls_group + mode 0755 + version ls_version + path ls_basedir + action [:put] + notifies :restart, "logstash_service[#{ls_instance}]" + # this is a temp workaround to ensure idempotent. + not_if { ::File.exists?("#{ls_instance_dir}/#{ls_install_check}") } + end + new_resource.updated_by_last_action(arkit.updated_by_last_action?) + else + Chef::Application.fatal!("Unknown install type: #{@install_type}") + end +end \ No newline at end of file diff --git a/recipes/server.rb b/recipes/server.rb index 87a8ce1..9587dda 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -35,6 +35,11 @@ ) end +logstash_plugins 'contrib' do + instance name + action [:create] +end + logstash_pattern name do action [:create] end diff --git a/resources/plugins.rb b/resources/plugins.rb new file mode 100644 index 0000000..0caeaba --- /dev/null +++ b/resources/plugins.rb @@ -0,0 +1,21 @@ +# Encoding: utf-8 +# Cookbook Name:: logstash +# Resource:: instance +# Author:: John E. Vincent +# Copyright 2014, John E. Vincent +# License:: Apache 2.0 + +actions :create + +default_action :create if defined?(default_action) + +attribute :name, kind_of: String, name_attribute: true +attribute :instance, kind_of: String +attribute :version, kind_of: String +attribute :checksum, kind_of: String +attribute :source_url, kind_of: String +attribute :user, kind_of: String +attribute :group, kind_of: String +attribute :base_directory, kind_of: String +attribute :install_type, kind_of: String, default: 'native' +attribute :install_check, kind_of: String \ No newline at end of file From 16d1cf4aae809615e084d84d5e176c682c4c5b95 Mon Sep 17 00:00:00 2001 From: "Peter Burkholder (@pburkholder)" Date: Fri, 11 Apr 2014 19:43:44 -0400 Subject: [PATCH 13/55] templates change to Hash to match default attribs With Array, it dies at https://github.com/lusis/chef-logstash/blob/logstash_1.4/providers/config.rb#L32 --- resources/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/config.rb b/resources/config.rb index 434864e..971fd6c 100644 --- a/resources/config.rb +++ b/resources/config.rb @@ -10,7 +10,7 @@ default_action :create if defined?(default_action) attribute :instance, kind_of: String, name_attribute: true -attribute :templates, kind_of: Array +attribute :templates, kind_of: Hash attribute :variables, kind_of: Hash attribute :owner, kind_of: String attribute :group, kind_of: String From f2edd115c85699bd690e264627f8b7545fb59002 Mon Sep 17 00:00:00 2001 From: paulczar Date: Sun, 13 Apr 2014 16:15:33 -0500 Subject: [PATCH 14/55] cleanup of attribute systems --- .kitchen.yml | 51 ++---- .rubocop.yml | 3 + README.md | 8 + attributes/agent.rb | 19 -- attributes/default.rb | 28 ++- attributes/server.rb | 15 -- libraries/matchers.rb | 5 + libraries/search.rb | 8 +- providers/config.rb | 16 +- providers/instance.rb | 203 +++++++++++----------- providers/pattern.rb | 16 +- providers/plugins.rb | 19 +- providers/service.rb | 72 +++++--- recipes/agent.rb | 2 + recipes/server.rb | 15 +- resources/instance.rb | 5 +- resources/plugins.rb | 2 +- resources/service.rb | 1 - templates/default/init/binary_upstart.erb | 27 +-- test/unit/spec/agent_spec.rb | 8 +- test/unit/spec/beaver_spec.rb | 11 +- test/unit/spec/default_spec.rb | 11 +- test/unit/spec/haproxy_spec.rb | 11 +- test/unit/spec/index_cleaer_spec.rb | 14 -- test/unit/spec/index_cleaner_spec.rb | 19 ++ test/unit/spec/lwrp_config_spec.rb | 6 +- test/unit/spec/pyshipper_spec.rb | 12 +- test/unit/spec/server_spec.rb | 12 +- test/unit/spec/source_spec.rb | 11 +- test/unit/spec/zero_mq_repo.rb | 11 +- 30 files changed, 322 insertions(+), 319 deletions(-) delete mode 100644 attributes/agent.rb delete mode 100644 attributes/server.rb delete mode 100644 test/unit/spec/index_cleaer_spec.rb create mode 100644 test/unit/spec/index_cleaner_spec.rb diff --git a/.kitchen.yml b/.kitchen.yml index d850083..15c8c13 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -20,46 +20,15 @@ suites: - recipe[apt::default] - recipe[java::default] - recipe[logstash::server] - - recipe[logstash::agent] - - recipe[kibana::default] attributes: - java: - jdk_version: 7 - oracle: - accept_oracle_download_terms: true - install_flavor: oracle logstash: - supervisor_gid: 'adm' - agent: - server_ipaddress: 127.0.0.1 - xms: 128m - xmx: 128m - enable_embedded_es: false - inputs: - - file: - type: syslog - path: - - /var/log/syslog - - /var/log/messages - start_position: beginning - filters: - - condition: 'if [type] == "syslog"' - block: - grok: - match: - - 'message' - - '%{SYSLOGTIMESTAMP:timestamp} %{IPORHOST:host} (?:%{PROG:program}(?:\[%{POSINT:pid}\])?: )?%{GREEDYDATA:message}' - date: - match: - - 'timestamp' - - 'MMM d HH:mm:ss' - - 'MMM dd HH:mm:ss' - - 'ISO8601' - server: - enable_embedded_es: true - web: - enabled: true - kibana: - webserver_listen: '0.0.0.0' - webserver: 'nginx' - install_type: 'file' + instance: + server: + config_templates: + input_syslog: 'config/input_syslog.conf.erb' + output_stdout: 'config/output_stdout.conf.erb' + output_elasticsearch: 'config/output_elasticsearch.conf.erb' + pattern_templates: + default: 'patterns/custom_patterns.erb' + elasticsearch_ip: '127.0.0.1' + enable_embedded_es: true \ No newline at end of file diff --git a/.rubocop.yml b/.rubocop.yml index 5cb7bae..c6166b1 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -32,3 +32,6 @@ BlockNesting: Documentation: Enabled: false + +MethodLength: + Max: 26 diff --git a/README.md b/README.md index 13433bf..50f2dfb 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,14 @@ This will install the logstash community plugins: see [resources/plugins.rb](resources/plugins.rb) +## attribute precidence in logstash LWRPs + +We've done our best to make this intuitive and easy to use. + +1. the value directly in the resource call. +2. the value from the hash node['logstash']['instance'][name] +3. the value from the hash node['logstash']['instance']['default'] + Searching ====== diff --git a/attributes/agent.rb b/attributes/agent.rb deleted file mode 100644 index 1622db2..0000000 --- a/attributes/agent.rb +++ /dev/null @@ -1,19 +0,0 @@ -# Encoding: utf-8 - -# slurp in defaults to named instance -default['logstash']['instance']['agent'] = node['logstash']['instance']['default'] - -# override any defaults here. - -override['logstash']['instance']['agent']['config_templates'] = { - 'input_file' => 'config/input_file.conf.erb', - 'output_stdout' => 'config/output_stdout.conf.erb' -} - -override['logstash']['instance']['default']['config_templates_variables'] = { - input_file_name: '/var/log/syslog', - input_file_type: 'syslog' -} - -override['logstash']['instance']['agent']['enable_embedded_es'] = false -override['logstash']['instance']['agent']['supervisor_gid'] = 'adm' diff --git a/attributes/default.rb b/attributes/default.rb index 129e160..491d73c 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -13,9 +13,8 @@ # Default logstash instance variables default['logstash']['instance']['default']['basedir'] = '/opt/logstash' default['logstash']['instance']['default']['user'] = 'logstash' -default['logstash']['instance']['default']['uid'] = nil # set to nil to let system pick default['logstash']['instance']['default']['group'] = 'logstash' -default['logstash']['instance']['default']['gid'] = nil # set to nil to let system pick +default['logstash']['instance']['default']['user_opts'] = { homedir: '/var/lib/logstash', uid: nil, gid: nil } default['logstash']['instance']['default']['supervisor_gid'] = node['logstash']['group'] default['logstash']['instance']['default']['pid_dir'] = '/var/run/logstash' default['logstash']['instance']['default']['create_account'] = true @@ -27,12 +26,12 @@ default['logstash']['instance']['default']['checksum'] = 'ab62394bb56da10cb20ee106badf22734402b21435977ec4f9aa65257627c629' default['logstash']['instance']['default']['install_type'] = 'tarball' -default['logstash']['instance']['default']['plugins']['install_type'] = 'native' # native|tarball -default['logstash']['instance']['default']['plugins']['version'] = '1.4.0' -default['logstash']['instance']['default']['plugins']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-contrib-1.4.0.tar.gz' -default['logstash']['instance']['default']['plugins']['checksum'] = '23aa397a7832d6e3553eba8360ff5d4ccfff98aac1e4a1b8201c21755e8d77c9' -default['logstash']['instance']['default']['plugins']['install_type'] = 'tarball' -default['logstash']['instance']['default']['plugins']['check_if_installed'] = 'lib/logstash/filters/translate.rb' +default['logstash']['instance']['default']['plugins_install_type'] = 'native' # native|tarball +default['logstash']['instance']['default']['plugins_version'] = '1.4.0' +default['logstash']['instance']['default']['plugins_source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-contrib-1.4.0.tar.gz' +default['logstash']['instance']['default']['plugins_checksum'] = '23aa397a7832d6e3553eba8360ff5d4ccfff98aac1e4a1b8201c21755e8d77c9' +default['logstash']['instance']['default']['plugins_install_type'] = 'tarball' +default['logstash']['instance']['default']['plugins_check_if_installed'] = 'lib/logstash/filters/translate.rb' default['logstash']['instance']['default']['log_file'] = 'logstash.log' default['logstash']['instance']['default']['xms'] = '1024M' @@ -70,11 +69,10 @@ default['logstash']['instance']['default']['web']['address'] = '0.0.0.0' default['logstash']['instance']['default']['web']['port'] = '9292' -default['logstash']['instance']['default']['logrotate']['enable'] = true -default['logstash']['instance']['default']['logrotate']['options'] = %w(missingok notifempty compress copytruncate) - # Logging features -default['logstash']['instance']['default']['logging']['rotateFrequency'] = 'daily' -default['logstash']['instance']['default']['logging']['maxBackup'] = 10 -default['logstash']['instance']['default']['logging']['maxSize'] = '10M' -default['logstash']['instance']['default']['logging']['useFileSize'] = false +default['logstash']['instance']['default']['logrotate_enable'] = true +default['logstash']['instance']['default']['logrotate_options'] = %w(missingok notifempty compress copytruncate) +default['logstash']['instance']['default']['logrotate_frequency'] = 'daily' +default['logstash']['instance']['default']['logrotate_max_backup'] = 10 +default['logstash']['instance']['default']['logrotate_max_size'] = '10M' +default['logstash']['instance']['default']['logrotate_use_filesize'] = false diff --git a/attributes/server.rb b/attributes/server.rb deleted file mode 100644 index 8df32a0..0000000 --- a/attributes/server.rb +++ /dev/null @@ -1,15 +0,0 @@ -# Encoding: utf-8 - -# slurp in defaults to named instance -default['logstash']['instance']['server'] = node['logstash']['instance']['default'] - -# override any defaults here. - -set['logstash']['instance']['server']['config_templates'] = { - 'input_syslog' => 'config/input_syslog.conf.erb', - 'output_stdout' => 'config/output_stdout.conf.erb', - 'output_elasticsearch' => 'config/output_elasticsearch.conf.erb' -} -set['logstash']['instance']['server']['pattern_templates'] = { 'default' => 'patterns/custom_patterns.erb' } -set['logstash']['instance']['server']['elasticsearch_ip'] = '127.0.0.1' -set['logstash']['instance']['server']['enable_embedded_es'] = true diff --git a/libraries/matchers.rb b/libraries/matchers.rb index bfd699b..009cd76 100644 --- a/libraries/matchers.rb +++ b/libraries/matchers.rb @@ -21,6 +21,11 @@ def create_logstash_pattern(name) ChefSpec::Matchers::ResourceMatcher.new(:logstash_pattern, :create, name) end + # LWRP - Plugins + def create_logstash_plugins(name) + ChefSpec::Matchers::ResourceMatcher.new(:logstash_plugins, :create, name) + end + # LWRP - Service def enable_logstash_service(name) ChefSpec::Matchers::ResourceMatcher.new(:logstash_service, :enable, name) diff --git a/libraries/search.rb b/libraries/search.rb index b63bef4..96d4ab0 100644 --- a/libraries/search.rb +++ b/libraries/search.rb @@ -8,17 +8,19 @@ class Recipe def service_ip(instance = 'default', service = 'elasticsearch') if node['logstash']['instance'].key?(instance) attributes = node['logstash']['instance'][instance] + defaults = node['logstash']['instance'][instance] else attributes = node['logstash']['instance']['default'] end if Chef::Config[:solo] - service_ip = attributes["#{service}_ip"] + service_ip = attributes["#{service}_ip"] || defaults["#{service}_ip"] + service_query = attributes["#{service}_query"] || defaults["#{service}_query"] else - results = search(:node, attributes["#{service}_query"]) + results = search(:node, service_query) if !results.empty? service_ip = results[0]['ipaddress'] else - service_ip = attributes["#{service}_ip"] + service_ip = attributes["#{service}_ip"] || defaults["#{service}_ip"] end end service_ip diff --git a/providers/config.rb b/providers/config.rb index afdaafc..421624d 100644 --- a/providers/config.rb +++ b/providers/config.rb @@ -14,16 +14,18 @@ def load_current_resource @instance = new_resource.instance if node['logstash']['instance'].key?(@instance) attributes = node['logstash']['instance'][@instance] + defaults = node['logstash']['instance']['default'] else attributes = node['logstash']['instance']['default'] end - @templates = new_resource.templates || attributes['config_templates'] - @templates_cookbook = new_resource.templates_cookbook || attributes['config_templates_cookbook'] - @variables = new_resource.variables || attributes['config_templates_variables'] - @path = new_resource.path || "#{attributes['basedir']}/#{@instance}/etc/conf.d" - @owner = new_resource.owner || attributes['user'] - @group = new_resource.group || attributes['group'] - @mode = new_resource.mode || '0644' + @basedir = attributes['basedir'] || defaults['basedir'] + @templates = new_resource.templates || attributes['config_templates'] || defaults['config_templates'] + @templates_cookbook = new_resource.templates_cookbook || attributes['config_templates_cookbook'] || defaults['config_templates_cookbook'] + @variables = new_resource.variables || attributes['config_templates_variables'] || defaults['config_templates_variables'] + @owner = new_resource.owner || attributes['user'] || defaults['user'] + @group = new_resource.group || attributes['group'] || defaults['group'] + @mode = new_resource.mode || '0644' + @path = new_resource.path || "#{@basedir}/#{@instance}/etc/conf.d" end action :create do diff --git a/providers/instance.rb b/providers/instance.rb index 9a05232..262e729 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -14,84 +14,56 @@ def load_current_resource @name = new_resource.name || 'default' if node['logstash']['instance'].key?(@name) attributes = node['logstash']['instance'][@name] + defaults = node['logstash']['instance']['default'] else attributes = node['logstash']['instance']['default'] end - @base_directory = new_resource.base_directory || attributes['basedir'] - @install_type = new_resource.install_type || attributes['install_type'] - @version = new_resource.version || attributes['version'] - @checksum = new_resource.checksum || attributes['checksum'] - @source_url = new_resource.source_url || attributes['source_url'] - @enable_logrotate = new_resource.enable_logrotate || node['logstash']['instance']['default']['enable_logrotate'] + @base_directory = new_resource.base_directory || attributes['basedir'] || defaults['basedir'] + @install_type = new_resource.install_type || attributes['install_type'] || defaults['install_type'] + @version = new_resource.version || attributes['version'] || defaults['version'] + @checksum = new_resource.checksum || attributes['checksum'] || defaults['checksum'] + @source_url = new_resource.source_url || attributes['source_url'] || defaults['source_url'] + @enable_logrotate = new_resource.enable_logrotate || attributes['enable_logrotate'] || defaults['enable_logrotate'] @repo = new_resource.repo @sha = new_resource.sha @java_home = new_resource.java_home - @user = new_resource.user - @group = new_resource.group - @useropts = new_resource.user_opts.clone + @user = new_resource.user || attributes['user'] || defaults['user'] + @group = new_resource.group || attributes['group'] || defaults['group'] + @useropts = attributes['user_opts'] || defaults['user_opts'] @instance_dir = "#{@base_directory}/#{new_resource.name}".clone + @logrotate_size = attributes['logrotate_max_size'] || defaults['logrotate_max_size'] + @logrotate_use_filesize = attributes['logrotate_use_filesize'] || defaults['logrotate_use_filesize'] + @logrotate_rotate_frequency = attributes['logrotate_rotate_frequency'] || defaults['logrotate_rotate_frequency'] + @logrotate_max_backup = attributes['logrotate_max_backup'] || defaults['logrotate_max_backup'] + @logrotate_options = attributes['logrotate_options'] || defaults['logrotate_options'] + @logrotate_enable = attributes['logrotate_enable'] || defaults['logrotate_enable'] end action :delete do - ls_instance_dir = @instance_dir - ls_name = @name - ls_user = @user - ls_group = @group + ls = ls_vars - idr = directory ls_instance_dir do + idr = directory ls[:instance_dir] do recursive true action :delete end new_resource.updated_by_last_action(idr.updated_by_last_action?) - - delete_user = true - delete_group = true - node['logstash']['instance'].each do |instance, contents| - next if instance == ls_name - delete_user = false if node['logstash']['instance'][instance]['user'] - delete_group = false if node['logstash']['instance'][instance]['group'] - end - - ur = user ls_user do - action :remove - only_if { delete_user } - end - new_resource.updated_by_last_action(ur.updated_by_last_action?) - - gr = group ls_group do - action :remove - only_if { delete_group } - end - new_resource.updated_by_last_action(gr.updated_by_last_action?) end action :create do + ls = ls_vars - ls_homedir = @useropts[:homedir] - ls_uid = @useropts[:uid] - ls_gid = @useropts[:gid] - ls_source_url = @source_url - ls_version = @version - ls_checksum = @checksum - ls_basedir = @base_directory - ls_user = @user - ls_group = @group - ls_name = @name - ls_instance_dir = @instance_dir - ls_enable_logrotate = @enable_logrotate - - ur = user ls_user do - home ls_homedir + ur = user ls[:user] do + home ls[:homedir] system true action :create manage_home true - uid ls_uid + uid ls[:uid] end new_resource.updated_by_last_action(ur.updated_by_last_action?) - gr = group ls_group do - gid ls_gid - members ls_user + gr = group ls[:group] do + gid ls[:gid] + members ls[:user] append true system true end @@ -100,24 +72,24 @@ def load_current_resource case @install_type when 'tarball' @run_context.include_recipe 'ark::default' - arkit = ark ls_name do - url ls_source_url - checksum ls_checksum - owner ls_user - group ls_group + arkit = ark ls[:name] do + url ls[:source_url] + checksum ls[:checksum] + owner ls[:user] + group ls[:group] mode 0755 - version ls_version - path ls_basedir + version ls[:version] + path ls[:basedir] action :put end new_resource.updated_by_last_action(arkit.updated_by_last_action?) %w(bin etc lib log tmp etc/conf.d patterns).each do |ldir| - r = directory "#{ls_instance_dir}/#{ldir}" do + r = directory "#{ls[:instance_dir]}/#{ldir}" do action :create mode '0755' - owner ls_user - group ls_group + owner ls[:user] + group ls[:group] end new_resource.updated_by_last_action(r.updated_by_last_action?) end @@ -126,40 +98,40 @@ def load_current_resource bdr = directory @base_directory do action :create mode '0755' - owner ls_user - group ls_group + owner ls[:user] + group ls[:group] end new_resource.updated_by_last_action(bdr.updated_by_last_action?) - idr = directory ls_instance_dir do + idr = directory ls[:instance_dir] do action :create mode '0755' - owner ls_user - group ls_group + owner ls[:user] + group ls[:group] end new_resource.updated_by_last_action(idr.updated_by_last_action?) %w(bin etc lib log tmp etc/conf.d patterns).each do |ldir| - r = directory "#{ls_instance_dir}/#{ldir}" do + r = directory "#{ls[:instance_dir]}/#{ldir}" do action :create mode '0755' - owner ls_user - group ls_group + owner ls[:user] + group ls[:group] end new_resource.updated_by_last_action(r.updated_by_last_action?) end - rfr = remote_file "#{ls_instance_dir}/lib/logstash-#{ls_version}.jar" do + rfr = remote_file "#{ls[:instance_dir]}/lib/logstash-#{ls[:version]}.jar" do owner 'root' group 'root' mode '0755' - source ls_source_url - checksum ls_checksum + source ls[:source_url] + checksum ls[:checksum] end new_resource.updated_by_last_action(rfr.updated_by_last_action?) - lr = link "#{ls_instance_dir}/lib/logstash.jar" do - to "#{ls_instance_dir}/lib/logstash-#{ls_version}.jar" + lr = link "#{ls[:instance_dir]}/lib/logstash.jar" do + to "#{ls[:instance_dir]}/lib/logstash-#{ls[:version]}.jar" only_if { new_resource.auto_symlink } end new_resource.updated_by_last_action(lr.updated_by_last_action?) @@ -168,79 +140,106 @@ def load_current_resource bdr = directory @base_directory do action :create mode '0755' - owner ls_user - group ls_group + owner ls[:user] + group ls[:group] end new_resource.updated_by_last_action(bdr.updated_by_last_action?) - idr = directory ls_instance_dir do + idr = directory ls[:instance_dir] do action :create mode '0755' - owner ls_user - group ls_group + owner ls[:user] + group ls[:group] end new_resource.updated_by_last_action(idr.updated_by_last_action?) %w(bin etc lib log tmp etc/conf.d patterns).each do |ldir| - r = directory "#{ls_instance_dir}/#{ldir}" do + r = directory "#{ls[:instance_dir]}/#{ldir}" do action :create mode '0755' - owner ls_user - group ls_group + owner ls[:user] + group ls[:group] end new_resource.updated_by_last_action(r.updated_by_last_action?) end - sd = directory "#{ls_instance_dir}/source" do + sd = directory "#{ls[:instance_dir]}/source" do action :create - owner ls_user - group ls_group + owner ls[:user] + group ls[:group] mode '0755' end new_resource.updated_by_last_action(sd.updated_by_last_action?) - gr = git "#{ls_instance_dir}/source" do + gr = git "#{ls[:instance_dir]}/source" do repository @repo reference @sha action :sync - user ls_user - group ls_group + user ls[:user] + group ls[:group] end new_resource.updated_by_last_action(gr.updated_by_last_action?) source_version = @sha || "v#{@version}" er = execute 'build-logstash' do - cwd "#{ls_instance_dir}/source" + cwd "#{ls[:instance_dir]}/source" environment(JAVA_HOME: @java_home) user ls_user # Changed from root cause building as root...WHA? command "make clean && make VERSION=#{source_version} jar" action :run - creates "#{ls_instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" - not_if "test -f #{ls_instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" + creates "#{ls[:instance_dir]}/source/build/logstash-#{source_version}--monolithic.jar" + not_if "test -f #{ls[:instance_dir]}/source/build/logstash-#{source_version}--monolithic.jar" end new_resource.updated_by_last_action(er.updated_by_last_action?) - lr = link "#{ls_instance_dir}/lib/logstash.jar" do - to "#{ls_instance_dir}/source/build/logstash-#{source_version}--monolithic.jar" + lr = link "#{ls[:instance_dir]}/lib/logstash.jar" do + to "#{ls[:instance_dir]}/source/build/logstash-#{source_version}--monolithic.jar" only_if { new_resource.auto_symlink } end new_resource.updated_by_last_action(lr.updated_by_last_action?) else Chef::Application.fatal!("Unknown install type: #{@install_type}") end - logrotate(ls_name) + logrotate(ls) if ls[:logrotate_enable] end private -def logrotate(name) +def logrotate(ls) + name = ls[:name] + @run_context.include_recipe 'logrotate::default' + logrotate_app "logstash_#{name}" do path "#{log_dir}/*.log" - size node['logstash']['instance'][name]['logging']['maxSize'] if node['logstash']['instance'][name]['logging']['useFileSize'] - frequency node['logstash']['instance'][name]['logging']['rotateFrequency'] - rotate node['logstash']['instance'][name]['logging']['maxBackup'] - options node['logstash']['instance'][name]['logrotate']['options'] - create "664 #{node['logstash']['instance'][name]['user']} #{node['logstash']['instance'][name]['group']}" + size ls[:logrotate_size] if ls[:logrotate_use_filesize] + frequency ls[:logrotate_rotate_frequency] + rotate ls[:logrotate_max_backup] + options ls[:logrotate_options] + create "664 #{ls[:user]} #{ls[:group]}" end end + +def ls_vars + ls = { + homedir: @useropts[:homedir], + uid: @useropts[:uid], + gid: @useropts[:gid], + source_url: @source_url, + version: @version, + checksum: @checksum, + basedir: @base_directory, + user: @user, + group: @group, + name: @name, + instance_dir: @instance_dir, + enable_logrotate: @enable_logrotate, + logrotate_size: @logrotate_size, + logrotate_use_filesize: @logrotate_use_filesize, + logrotate_rotate_frequency: @logrotate_rotate_frequency, + logrotate_max_backup: @logrotate_max_backup, + logrotate_options: @logrotate_options, + logrotate_enable: @logrotate_enable + } + ls +end diff --git a/providers/pattern.rb b/providers/pattern.rb index 56e17c5..88bfaf2 100644 --- a/providers/pattern.rb +++ b/providers/pattern.rb @@ -14,16 +14,18 @@ def load_current_resource @instance = new_resource.instance if node['logstash']['instance'].key?(@instance) attributes = node['logstash']['instance'][@instance] + defaults = node['logstash']['instance']['default'] else attributes = node['logstash']['instance']['default'] end - @templates = new_resource.templates || attributes['pattern_templates'] - @variables = new_resource.variables || attributes['pattern_templates_variables'] - @path = new_resource.path || "#{attributes['basedir']}/#{@instance}/patterns" - @owner = new_resource.owner || attributes['user'] - @group = new_resource.group || attributes['group'] - @mode = new_resource.mode || '0644' - @templates_cookbook = new_resource.templates_cookbook || attributes['pattern_templates_cookbook'] + @basedir = attributes['basedir'] || defaults['basedir'] + @templates = new_resource.templates || attributes['pattern_templates'] || defaults['pattern_templates'] + @variables = new_resource.variables || attributes['pattern_templates_variables'] || defaults['pattern_templates_variables'] + @owner = new_resource.owner || attributes['user'] || defaults['user'] + @group = new_resource.group || attributes['group'] || defaults['group'] + @templates_cookbook = new_resource.templates_cookbook || defaults['pattern_templates_cookbook'] || defaults['pattern_templates_cookbook'] + @mode = new_resource.mode || '0644' + @path = new_resource.path || "#{@basedir}/#{@instance}/patterns" end action :create do diff --git a/providers/plugins.rb b/providers/plugins.rb index 83f6d6a..c0e9e96 100644 --- a/providers/plugins.rb +++ b/providers/plugins.rb @@ -15,18 +15,19 @@ def load_current_resource @instance = new_resource.instance || 'default' if node['logstash']['instance'].key?(@instance) attributes = node['logstash']['instance'][@instance] + defaults = node['logstash']['instance']['default'] else attributes = node['logstash']['instance']['default'] end - @base_directory = new_resource.base_directory || attributes['basedir'] - @version = new_resource.version || attributes['plugins']['version'] - @checksum = new_resource.checksum || attributes['plugins']['checksum'] - @source_url = new_resource.source_url || attributes['plugins']['source_url'] - @user = new_resource.user || attributes['user'] - @group = new_resource.group || attributes['group'] + @base_directory = new_resource.base_directory || attributes['basedir'] || defaults['basedir'] + @version = new_resource.version || attributes['plugins_version'] || defaults['plugins_version'] + @checksum = new_resource.checksum || attributes['plugins_checksum'] || defaults['plugins_checksum'] + @source_url = new_resource.source_url || attributes['plugins_source_url'] || defaults['plugins_source_url'] + @user = new_resource.user || attributes['user'] || defaults['user'] + @group = new_resource.group || attributes['group'] || defaults['group'] @instance_dir = "#{@base_directory}/#{@instance}" - @install_type = new_resource.install_type || attributes['plugins']['install_type'] - @install_check = new_resource.install_check || attributes['plugins']['check_if_installed'] + @install_type = new_resource.install_type || attributes['plugins_install_type'] || defaults['plugins_install_type'] + @install_check = new_resource.install_check || attributes['plugins_check_if_installed'] || defaults['plugins_check_if_installed'] end action :create do @@ -73,4 +74,4 @@ def load_current_resource else Chef::Application.fatal!("Unknown install type: #{@install_type}") end -end \ No newline at end of file +end diff --git a/providers/service.rb b/providers/service.rb index 2b2d3f5..9acc548 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -13,19 +13,28 @@ def load_current_resource @instance = new_resource.instance if node['logstash']['instance'].key?(@instance) - @attributes = node['logstash']['instance'][@instance] + attributes = node['logstash']['instance'][@instance] + defaults = node['logstash']['instance']['default'] else - @attributes = node['logstash']['instance']['default'] + attributes = node['logstash']['instance']['default'] end + + @basedir = attributes['basedir'] || defaults['basedir'] @service_name = new_resource.service_name || "logstash_#{@instance}" - @home = "#{@attributes['basedir']}/#{@instance}" - @method = new_resource.method || @attributes['init_method'] + @home = "#{@basedir}/#{@instance}" + @method = new_resource.method || attributes['init_method'] || defaults['init_method'] @command = new_resource.command || "#{@home}/bin/logstash" - @args = new_resource.args || default_args + @user = new_resource.user || attributes['user'] || defaults['user'] + @group = new_resource.group || attributes['group'] || defaults['group'] + @log_file = attributes['log_file'] || defaults['log_file'] + @max_heap = attributes['xmx'] || defaults['xmx'] + @min_heap = attributes['xms'] || defaults['xms'] @description = new_resource.description || @service_name - @chdir = new_resource.chdir || @home - @user = new_resource.user || @attributes['user'] - @group = new_resource.group || @attributes['group'] + @chdir = @home + @workers = attributes['workers'] || defaults['workers'] + @debug = attributes['debug'] || defaults['debug'] + @install_type = attributes['install_type'] || defaults['install_type'] + @supervisor_gid = attributes['supervisor_gid'] || defaults['supervisor_gid'] end action :restart do @@ -85,7 +94,7 @@ def load_current_resource pr = pleaserun svc[:service_name] do name svc[:service_name] program svc[:command] - args svc[:args] + args default_args description svc[:description] chdir svc[:chdir] user svc[:user] @@ -101,17 +110,24 @@ def load_current_resource when 'native' if platform_family? 'debian' if node['platform_version'] >= '12.04' - if @attributes['install_type'] == 'tarball' + if svc[:install_type] == 'tarball' tp_source = 'init/binary_upstart.erb' else tp_source = 'init/java_upstart.erb' end + args = default_args tp = template "/etc/init/#{svc[:service_name]}.conf" do mode '0644' source tp_source - variables(home: svc[:home], - name: svc[:name], - command: svc[:command] + variables( + home: svc[:home], + name: svc[:name], + command: svc[:command], + args: args, + user: svc[:user], + group: svc[:group], + description: svc[:description], + supervisor_gid: svc[:supervisor_gid] ) end new_resource.updated_by_last_action(tp.updated_by_last_action?) @@ -153,12 +169,13 @@ def load_current_resource owner 'root' group 'root' mode '0774' - variables(config_file: @attributes['config_dir'], - home: svc[:home], - name: svc[:name], - log_file: @attributes['log_file'], - max_heap: @attributes['xmx'], - min_heap: @attributes['xms'] + variables( + config_file: "#{svc[:home]}/etc/conf.d", + home: svc[:home], + name: svc[:name], + log_file: svc[:log_file], + max_heap: svc[:max_heap], + min_heap: svc[:min_heap] ) end new_resource.updated_by_last_action(tp.updated_by_last_action?) @@ -179,10 +196,9 @@ def load_current_resource def default_args svc = svc_vars args = ['agent', '-f', "#{svc[:home]}/etc/conf.d/"] - args.concat ['--pluginpath', @attributes['pluginpath']] if @attributes['pluginpath'] - args.concat ['-vv'] if @attributes['debug'] - args.concat ['-l', "#{svc[:home]}/log/#{@attributes['log_file']}"] if @attributes['log_file'] - args.concat ['-w', @attributes['workers'].to_s] if @attributes['workers'] + args.concat ['-vv'] if svc[:debug] + args.concat ['-l', "#{svc[:home]}/log/#{svc[:log_file]}"] if svc[:log_file] + args.concat ['-w', svc[:workers].to_s] if svc[:workers] args end @@ -193,11 +209,17 @@ def svc_vars home: @home, method: @method, command: @command, - args: @args, description: @description, chdir: @chdir, user: @user, - group: @group + group: @group, + log_file: @log_file, + max_heap: @max_heap, + min_heap: @min_heap, + workers: @workers, + debug: @debug, + install_type: @install_type, + supervisor_gid: @supervisor_gid } svc end diff --git a/recipes/agent.rb b/recipes/agent.rb index 927be08..0157648 100644 --- a/recipes/agent.rb +++ b/recipes/agent.rb @@ -7,6 +7,8 @@ name = 'agent' +Chef::Application.fatal!("attribute hash node['logstash']['instance']['#{name}'] must exist.") if node['logstash']['instance'][name].nil? + # these should all default correctly. listing out for example. logstash_instance name do action :create diff --git a/recipes/server.rb b/recipes/server.rb index 9587dda..6a1f547 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -13,7 +13,8 @@ # install logstash 'server' name = 'server' -attributes = node['logstash']['instance'][name] + +Chef::Application.fatal!("attribute hash node['logstash']['instance']['#{name}'] must exist.") if node['logstash']['instance'][name].nil? # these should all default correctly. listing out for example. logstash_instance name do @@ -22,7 +23,7 @@ # services are hard! Let's go LWRP'ing. FIREBALL! FIREBALL! FIREBALL! logstash_service name do - action [:enable, :start] + action [:enable] end es_ip = service_ip(name, 'elasticsearch') @@ -30,16 +31,20 @@ logstash_config name do action [:create] variables( - elasticsearch_ip: es_ip, - elasticsearch_embedded: true + elasticsearch_ip: es_ip, + elasticsearch_embedded: true ) end logstash_plugins 'contrib' do - instance name + instance name action [:create] end logstash_pattern name do action [:create] end + +logstash_service name do + action [:start] +end diff --git a/resources/instance.rb b/resources/instance.rb index 7f06876..0ec9b35 100644 --- a/resources/instance.rb +++ b/resources/instance.rb @@ -21,7 +21,6 @@ attribute :sha, kind_of: String, default: 'HEAD' attribute :repo, kind_of: String, default: 'git://github.com/logstash/logstash.git' attribute :java_home, kind_of: String, default: '/usr/lib/jvm/java-6-openjdk' # openjdk6 on ubuntu -attribute :user, kind_of: String, default: 'logstash' -attribute :group, kind_of: String, default: 'logstash' -attribute :user_opts, kind_of: Hash, default: { homedir: '/var/lib/logstash', uid: nil, gid: nil } +attribute :user, kind_of: String +attribute :group, kind_of: String attribute :enable_logrotate, kind_of: [TrueClass, FalseClass] diff --git a/resources/plugins.rb b/resources/plugins.rb index 0caeaba..b6e9d3e 100644 --- a/resources/plugins.rb +++ b/resources/plugins.rb @@ -18,4 +18,4 @@ attribute :group, kind_of: String attribute :base_directory, kind_of: String attribute :install_type, kind_of: String, default: 'native' -attribute :install_check, kind_of: String \ No newline at end of file +attribute :install_check, kind_of: String diff --git a/resources/service.rb b/resources/service.rb index fc117c8..6d8d198 100644 --- a/resources/service.rb +++ b/resources/service.rb @@ -15,6 +15,5 @@ attribute :command, kind_of: String attribute :args, kind_of: Array attribute :description, kind_of: String -attribute :chdir, kind_of: String attribute :user, kind_of: String attribute :group, kind_of: String diff --git a/templates/default/init/binary_upstart.erb b/templates/default/init/binary_upstart.erb index 3d76d29..c69c6a7 100644 --- a/templates/default/init/binary_upstart.erb +++ b/templates/default/init/binary_upstart.erb @@ -10,33 +10,20 @@ limit nofile 65550 65550 chdir <%= @home %> -<% unless node['logstash']['instance'][@name]['upstart_with_sudo'] -%> -setuid <%= node['logstash']['instance'][@name]['user'] %> - <% unless node['logstash']['instance'][@name]['supervisor_gid'].to_s.empty? -%> -setgid <%= node['logstash']['instance'][@name]['supervisor_gid'] %> +<% unless @upstart_with_sudo -%> +setuid <%= @user %> + <% unless @supervisor_gid.to_s.empty? -%> +setgid <%= @supervisor_gid %> <% end -%> <% end -%> script export LOGSTASH_HOME="<%= @home %>" export HOME=$LOGSTASH_HOME - export LOGSTASH_OPTS="agent -f $LOGSTASH_HOME/etc/conf.d" - <% if node['logstash']['instance'][@name]['pluginpath'] -%> - export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['instance'][@name]['pluginpath'] %>" - <% end -%> - <% if node['logstash']['instance'][@name]['workers'] %> - export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['instance'][@name]['workers'] %>" - <% end %> - <% if node['logstash']['instance'][@name]['log_file'] -%> - export LOGSTASH_OPTS="$LOGSTASH_OPTS -l $LOGSTASH_HOME/log/<%= node['logstash']['instance'][@name]['log_file'] %>" - <% end -%> - <% if node['logstash']['instance'][@name]['debug'] -%> - export LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" - <% end -%> - export OPTS="$JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS" + export LOGSTASH_OPTS="<%= @args %>" - <% if node['logstash']['instance'][@name]['upstart_with_sudo'] -%> - exec sudo -u <%= node['logstash']['instance'][@name]['user'] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS + <% if @upstart_with_sudo -%> + exec sudo -u <%= @user %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS <% else -%> exec $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS <% end -%> diff --git a/test/unit/spec/agent_spec.rb b/test/unit/spec/agent_spec.rb index 3c82b00..e952b22 100644 --- a/test/unit/spec/agent_spec.rb +++ b/test/unit/spec/agent_spec.rb @@ -11,19 +11,19 @@ end include_context 'stubs-common' - it 'calls the logstash_instance LWPR' do + it 'calls the logstash_instance LWRP' do expect(chef_run).to create_logstash_instance('agent') end - it 'calls the logstash_config LWPR' do + it 'calls the logstash_config LWRP' do expect(chef_run).to create_logstash_config('agent') end - it 'calls the logstash_pattern LWPR' do + it 'calls the logstash_pattern LWRP' do expect(chef_run).to create_logstash_pattern('agent') end - it 'calls the logstash_instance LWPR' do + it 'calls the logstash_instance LWRP' do expect(chef_run).to enable_logstash_service('agent') expect(chef_run).to start_logstash_service('agent') end diff --git a/test/unit/spec/beaver_spec.rb b/test/unit/spec/beaver_spec.rb index 1088fe1..e700750 100644 --- a/test/unit/spec/beaver_spec.rb +++ b/test/unit/spec/beaver_spec.rb @@ -2,13 +2,18 @@ require_relative 'spec_helper' describe 'logstash::beaver' do - before { logstash_stubs } describe 'ubuntu' do - let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) } + let(:runner) { ChefSpec::Runner.new(::UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + # runner.node.set['logstash'] ... + runner.converge(described_recipe) + end + include_context 'stubs-common' it 'writes some chefspec code' do pending 'todo' end end -end +end \ No newline at end of file diff --git a/test/unit/spec/default_spec.rb b/test/unit/spec/default_spec.rb index b995568..455c73f 100644 --- a/test/unit/spec/default_spec.rb +++ b/test/unit/spec/default_spec.rb @@ -2,13 +2,18 @@ require_relative 'spec_helper' describe 'logstash::default' do - before { logstash_stubs } describe 'ubuntu' do - let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) } + let(:runner) { ChefSpec::Runner.new(::UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + # runner.node.set['logstash'] ... + runner.converge(described_recipe) + end + include_context 'stubs-common' it 'writes some chefspec code' do pending 'todo' end end -end +end \ No newline at end of file diff --git a/test/unit/spec/haproxy_spec.rb b/test/unit/spec/haproxy_spec.rb index c9cfe80..fa70276 100644 --- a/test/unit/spec/haproxy_spec.rb +++ b/test/unit/spec/haproxy_spec.rb @@ -2,13 +2,18 @@ require_relative 'spec_helper' describe 'logstash::haproxy' do - before { logstash_stubs } describe 'ubuntu' do - let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) } + let(:runner) { ChefSpec::Runner.new(::UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + # runner.node.set['logstash'] ... + runner.converge(described_recipe) + end + include_context 'stubs-common' it 'writes some chefspec code' do pending 'todo' end end -end +end \ No newline at end of file diff --git a/test/unit/spec/index_cleaer_spec.rb b/test/unit/spec/index_cleaer_spec.rb deleted file mode 100644 index c4ae32e..0000000 --- a/test/unit/spec/index_cleaer_spec.rb +++ /dev/null @@ -1,14 +0,0 @@ -# Encoding: utf-8 -require_relative 'spec_helper' - -describe 'logstash::index_cleaner' do - before { logstash_stubs } - describe 'ubuntu' do - let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) } - - it 'writes some chefspec code' do - pending 'todo' - end - - end -end diff --git a/test/unit/spec/index_cleaner_spec.rb b/test/unit/spec/index_cleaner_spec.rb new file mode 100644 index 0000000..c693edf --- /dev/null +++ b/test/unit/spec/index_cleaner_spec.rb @@ -0,0 +1,19 @@ +# Encoding: utf-8 +require_relative 'spec_helper' + +describe 'logstash::index_cleaner' do + describe 'ubuntu' do + let(:runner) { ChefSpec::Runner.new(::UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + # runner.node.set['logstash'] ... + runner.converge(described_recipe) + end + include_context 'stubs-common' + + it 'writes some chefspec code' do + pending 'todo' + end + + end +end \ No newline at end of file diff --git a/test/unit/spec/lwrp_config_spec.rb b/test/unit/spec/lwrp_config_spec.rb index bae33b4..f234be7 100644 --- a/test/unit/spec/lwrp_config_spec.rb +++ b/test/unit/spec/lwrp_config_spec.rb @@ -14,14 +14,12 @@ runner.node.set['logstash']['instance']['server']['config_templates'] = { output_stdout: 'config/output_stdout.conf.erb' } - runner.node.set['logstash']['instance']['server']['config_templates_variables'] = { - elasticsearch_ip: '127.0.0.1', - elasticsearch_embedded: true - } runner.node.set['logstash']['instance']['server']['basedir'] = '/opt/logstash' runner.node.set['logstash']['instance']['server']['user'] = 'logstash' runner.node.set['logstash']['instance']['server']['group'] = 'logstash' runner.node.set['logstash']['instance']['server']['config_templates_cookbook'] = 'logstash' + runner.node.set['logstash']['instance']['server']['elasticsearch_ip'] = '127.0.0.1' + runner.node.set['logstash']['instance']['server']['elasticsearch_embedded'] = true runner.converge(described_recipe) end include_context 'stubs-common' diff --git a/test/unit/spec/pyshipper_spec.rb b/test/unit/spec/pyshipper_spec.rb index 68a9721..8a92f8c 100644 --- a/test/unit/spec/pyshipper_spec.rb +++ b/test/unit/spec/pyshipper_spec.rb @@ -2,16 +2,18 @@ require_relative 'spec_helper' describe 'logstash::pyshipper' do - before { logstash_stubs } describe 'ubuntu' do - before do - @chef_run = ::ChefSpec::Runner.new ::UBUNTU_OPTS - @chef_run.converge 'logstash::pyshipper' + let(:runner) { ChefSpec::Runner.new(::UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + # runner.node.set['logstash'] ... + runner.converge(described_recipe) end + include_context 'stubs-common' it 'writes some chefspec code' do pending 'todo' end end -end +end \ No newline at end of file diff --git a/test/unit/spec/server_spec.rb b/test/unit/spec/server_spec.rb index 2fba609..d19c2e9 100644 --- a/test/unit/spec/server_spec.rb +++ b/test/unit/spec/server_spec.rb @@ -11,22 +11,26 @@ end include_context 'stubs-common' - it 'calls the logstash_instance LWPR' do + it 'calls the logstash_instance LWRP' do expect(chef_run).to create_logstash_instance('server') end - it 'calls the logstash_config LWPR' do + it 'calls the logstash_config LWRP' do expect(chef_run).to create_logstash_config('server') end - it 'calls the logstash_pattern LWPR' do + it 'calls the logstash_pattern LWRP' do expect(chef_run).to create_logstash_pattern('server') end - it 'calls the logstash_instance LWPR' do + it 'calls the logstash_instance 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 + expect(chef_run).to create_logstash_plugins('contrib') + end + end end diff --git a/test/unit/spec/source_spec.rb b/test/unit/spec/source_spec.rb index e520082..30bf3bf 100644 --- a/test/unit/spec/source_spec.rb +++ b/test/unit/spec/source_spec.rb @@ -2,13 +2,18 @@ require_relative 'spec_helper' describe 'logstash::source' do - before { logstash_stubs } describe 'ubuntu' do - let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) } + let(:runner) { ChefSpec::Runner.new(::UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + # runner.node.set['logstash'] ... + runner.converge(described_recipe) + end + include_context 'stubs-common' it 'writes some chefspec code' do pending 'todo' end end -end +end \ No newline at end of file diff --git a/test/unit/spec/zero_mq_repo.rb b/test/unit/spec/zero_mq_repo.rb index 2b72d78..f51c2ab 100644 --- a/test/unit/spec/zero_mq_repo.rb +++ b/test/unit/spec/zero_mq_repo.rb @@ -2,13 +2,18 @@ require_relative 'spec_helper' describe 'logstash::zero_mq_repo' do - before { logstash_stubs } describe 'ubuntu' do - let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) } + let(:runner) { ChefSpec::Runner.new(::UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + # runner.node.set['logstash'] ... + runner.converge(described_recipe) + end + include_context 'stubs-common' it 'writes some chefspec code' do pending 'todo' end end -end +end \ No newline at end of file From 52abe96e6db6c6e9b5991e0c17658ee662159dd9 Mon Sep 17 00:00:00 2001 From: Peter Burkholder Date: Mon, 14 Apr 2014 22:31:57 -0400 Subject: [PATCH 15/55] Seems to work with wrapper --- Gemfile | 1 + Gemfile.lock | 5 +++++ attributes/default.rb | 2 ++ providers/config.rb | 2 +- providers/service.rb | 7 ++++++- resources/service.rb | 2 ++ 6 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 1533eca..006d6ef 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,7 @@ gem 'test-kitchen', '~> 1.1' gem 'kitchen-vagrant' gem 'serverspec', '~> 0.14' gem 'rake' +gem 'strainer', '~> 3.3' group :chef_gems do gem 'pleaserun' diff --git a/Gemfile.lock b/Gemfile.lock index fc7a881..e0883fe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -30,6 +30,7 @@ GEM varia_model (~> 0.1) buff-extensions (0.5.0) buff-ignore (1.1.1) + buff-platform (0.1.0) buff-ruby_engine (0.1.0) buff-shell_out (0.1.1) buff-ruby_engine (~> 0.1.0) @@ -251,6 +252,9 @@ GEM slop (3.5.0) solve (0.8.2) specinfra (0.7.1) + strainer (3.3.0) + berkshelf (~> 2.0) + buff-platform (~> 0.1) stud (0.0.17) ffi metriks @@ -301,4 +305,5 @@ DEPENDENCIES rake rubocop (~> 0.18) serverspec (~> 0.14) + strainer (~> 3.3) test-kitchen (~> 1.1) diff --git a/attributes/default.rb b/attributes/default.rb index 491d73c..706b1f2 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -58,6 +58,8 @@ default['logstash']['instance']['default']['upstart_with_sudo'] = false default['logstash']['instance']['default']['init_method'] = 'native' # pleaserun or native or runit +default['logstash']['instance']['default']['service_templates_cookbook'] = 'logstash' + # roles/flags for various autoconfig/discovery components default['logstash']['instance']['default']['enable_embedded_es'] = false diff --git a/providers/config.rb b/providers/config.rb index 421624d..dc8bae6 100644 --- a/providers/config.rb +++ b/providers/config.rb @@ -30,7 +30,7 @@ def load_current_resource action :create do conf = conf_vars - # Chef::Log.info("config vars: #{conf.inspect}") + Chef::Log.info("config vars: #{conf.inspect}") conf[:templates].each do |template, file| tp = template"#{conf[:path]}/#{::File.basename(file).chomp(::File.extname(file))}" do source file diff --git a/providers/service.rb b/providers/service.rb index 9acc548..f96b34c 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -20,6 +20,7 @@ def load_current_resource end @basedir = attributes['basedir'] || defaults['basedir'] + @templates_cookbook = new_resource.templates_cookbook || attributes['service_templates_cookbook'] || defaults['service_templates_cookbook'] @service_name = new_resource.service_name || "logstash_#{@instance}" @home = "#{@basedir}/#{@instance}" @method = new_resource.method || attributes['init_method'] || defaults['init_method'] @@ -119,6 +120,7 @@ def load_current_resource tp = template "/etc/init/#{svc[:service_name]}.conf" do mode '0644' source tp_source + cookbook svc[:templates_cookbook] variables( home: svc[:home], name: svc[:name], @@ -148,6 +150,7 @@ def load_current_resource new_resource.updated_by_last_action(ex.updated_by_last_action?) template '/etc/systemd/system/logstash_server.service' do tp = source 'logstash_server.service.erb' + cookbook svc[:templates_cookbook] owner 'root' group 'root' mode '0755' @@ -166,6 +169,7 @@ def load_current_resource elsif platform_family? 'rhel', 'fedora' tp = template "/etc/init.d/#{svc[:service_name]}" do source "init.#{svc[:service_name]}.erb" + cookbook svc[:templates_cookbook] owner 'root' group 'root' mode '0774' @@ -219,7 +223,8 @@ def svc_vars workers: @workers, debug: @debug, install_type: @install_type, - supervisor_gid: @supervisor_gid + supervisor_gid: @supervisor_gid, + templates_cookbook: @templates_cookbook } svc end diff --git a/resources/service.rb b/resources/service.rb index 6d8d198..53cc414 100644 --- a/resources/service.rb +++ b/resources/service.rb @@ -17,3 +17,5 @@ attribute :description, kind_of: String attribute :user, kind_of: String attribute :group, kind_of: String +attribute :templates_cookbook, kind_of: String + From 9b7a832b92ff723c6c036d16624aef47db346d70 Mon Sep 17 00:00:00 2001 From: Peter Burkholder Date: Tue, 15 Apr 2014 10:39:29 -0400 Subject: [PATCH 16/55] Back to original logging --- providers/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/config.rb b/providers/config.rb index dc8bae6..421624d 100644 --- a/providers/config.rb +++ b/providers/config.rb @@ -30,7 +30,7 @@ def load_current_resource action :create do conf = conf_vars - Chef::Log.info("config vars: #{conf.inspect}") + # Chef::Log.info("config vars: #{conf.inspect}") conf[:templates].each do |template, file| tp = template"#{conf[:path]}/#{::File.basename(file).chomp(::File.extname(file))}" do source file From 1e5dc8d1abd7eb1b5c9276180bfee90b82254a59 Mon Sep 17 00:00:00 2001 From: V Aminev Date: Thu, 17 Apr 2014 18:11:57 +0400 Subject: [PATCH 17/55] Properly notify service to restart --- providers/config.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/providers/config.rb b/providers/config.rb index 421624d..ddefc1d 100644 --- a/providers/config.rb +++ b/providers/config.rb @@ -26,6 +26,7 @@ def load_current_resource @group = new_resource.group || attributes['group'] || defaults['group'] @mode = new_resource.mode || '0644' @path = new_resource.path || "#{@basedir}/#{@instance}/etc/conf.d" + @service_name = new_resource.service_name || "logstash_#{@instance}" end action :create do @@ -39,7 +40,7 @@ def load_current_resource group conf[:group] mode conf[:mode] variables conf[:variables] - notifies :restart, "logstash_service[#{conf[:instance]}]" + notifies :restart, "logstash_service[#{conf[:service_name]}]" action :create end new_resource.updated_by_last_action(tp.updated_by_last_action?) From 2b647899ac673307cfdb96fe0c30f476683e9802 Mon Sep 17 00:00:00 2001 From: V Aminev Date: Thu, 17 Apr 2014 18:25:07 +0400 Subject: [PATCH 18/55] add service name to resources --- resources/config.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/config.rb b/resources/config.rb index 971fd6c..78b77f0 100644 --- a/resources/config.rb +++ b/resources/config.rb @@ -10,6 +10,7 @@ default_action :create if defined?(default_action) attribute :instance, kind_of: String, name_attribute: true +attribute :service_name, kind_of: String attribute :templates, kind_of: Hash attribute :variables, kind_of: Hash attribute :owner, kind_of: String From 28a45be8abe18840f16057b5b559a13ef79e85fc Mon Sep 17 00:00:00 2001 From: V Aminev Date: Thu, 17 Apr 2014 18:34:02 +0400 Subject: [PATCH 19/55] Update config.rb --- providers/config.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/providers/config.rb b/providers/config.rb index ddefc1d..e9e88a5 100644 --- a/providers/config.rb +++ b/providers/config.rb @@ -58,6 +58,7 @@ def conf_vars owner: @owner, group: @group, mode: @mode, + service_name: @service_name, templates_cookbook: @templates_cookbook } conf From b8e56501f8dac21adda95a103978b7f04e0264cb Mon Sep 17 00:00:00 2001 From: Peter Burkholder Date: Thu, 17 Apr 2014 14:30:42 -0400 Subject: [PATCH 20/55] Fix pattern templates is Hash to match attributes --- resources/pattern.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/pattern.rb b/resources/pattern.rb index dae946d..4d3361f 100644 --- a/resources/pattern.rb +++ b/resources/pattern.rb @@ -10,7 +10,7 @@ default_action :create if defined?(default_action) attribute :instance, kind_of: String, name_attribute: true -attribute :templates, kind_of: Array +attribute :templates, kind_of: Hash attribute :variables, kind_of: Hash attribute :path, kind_of: String attribute :owner, kind_of: String From 3cabe22749d3160c481a73c99682a4cc609ff82c Mon Sep 17 00:00:00 2001 From: Peter Burkholder Date: Thu, 17 Apr 2014 19:45:28 -0400 Subject: [PATCH 21/55] Join them args together --- templates/default/init/binary_upstart.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/default/init/binary_upstart.erb b/templates/default/init/binary_upstart.erb index c69c6a7..7ab27ea 100644 --- a/templates/default/init/binary_upstart.erb +++ b/templates/default/init/binary_upstart.erb @@ -20,7 +20,7 @@ setgid <%= @supervisor_gid %> script export LOGSTASH_HOME="<%= @home %>" export HOME=$LOGSTASH_HOME - export LOGSTASH_OPTS="<%= @args %>" + export LOGSTASH_OPTS="<%= @args.join(' ') %>" <% if @upstart_with_sudo -%> exec sudo -u <%= @user %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS From eb191146bdde3abd29039d311620b41750da5564 Mon Sep 17 00:00:00 2001 From: Peter Burkholder Date: Thu, 24 Apr 2014 13:24:59 -0400 Subject: [PATCH 22/55] Fix #273; Add 1.4 LS_HEAP_SIZE support --- attributes/default.rb | 2 +- providers/config.rb | 2 +- providers/pattern.rb | 2 +- providers/service.rb | 1 + resources/service.rb | 1 - templates/default/init/binary_upstart.erb | 1 + test/unit/spec/agent_spec.rb | 1 + test/unit/spec/default_spec.rb | 2 +- test/unit/spec/lwrp_config_spec.rb | 1 + test/unit/spec/lwrp_pattern_spec.rb | 1 + test/unit/spec/server_spec.rb | 3 ++- 11 files changed, 11 insertions(+), 6 deletions(-) diff --git a/attributes/default.rb b/attributes/default.rb index 706b1f2..4cb18b5 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -35,7 +35,7 @@ default['logstash']['instance']['default']['log_file'] = 'logstash.log' default['logstash']['instance']['default']['xms'] = '1024M' -default['logstash']['instance']['default']['xmx'] = '1024M' +default['logstash']['instance']['default']['xmx'] = "#{(node.memory.total.to_i * 0.6).floor / 1024}m" default['logstash']['instance']['default']['java_opts'] = '' default['logstash']['instance']['default']['gc_opts'] = '-XX:+UseParallelOldGC' default['logstash']['instance']['default']['ipv4_only'] = false diff --git a/providers/config.rb b/providers/config.rb index e9e88a5..2a83d1c 100644 --- a/providers/config.rb +++ b/providers/config.rb @@ -26,7 +26,7 @@ def load_current_resource @group = new_resource.group || attributes['group'] || defaults['group'] @mode = new_resource.mode || '0644' @path = new_resource.path || "#{@basedir}/#{@instance}/etc/conf.d" - @service_name = new_resource.service_name || "logstash_#{@instance}" + @service_name = new_resource.service_name || @instance end action :create do diff --git a/providers/pattern.rb b/providers/pattern.rb index 88bfaf2..d9d2ae5 100644 --- a/providers/pattern.rb +++ b/providers/pattern.rb @@ -30,7 +30,7 @@ def load_current_resource action :create do pattern = pattern_vars - # Chef::Log.info("config vars: #{conf.inspect}") + # Chef::Log.info("config vars: #{pattern.inspect}") pattern[:templates].each do |template, file| tp = template "#{pattern[:path]}/#{::File.basename(file).chomp(::File.extname(file))}" do source file diff --git a/providers/service.rb b/providers/service.rb index f96b34c..95c157d 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -125,6 +125,7 @@ def load_current_resource home: svc[:home], name: svc[:name], command: svc[:command], + max_heap: svc[:max_heap], args: args, user: svc[:user], group: svc[:group], diff --git a/resources/service.rb b/resources/service.rb index 53cc414..b335a32 100644 --- a/resources/service.rb +++ b/resources/service.rb @@ -18,4 +18,3 @@ attribute :user, kind_of: String attribute :group, kind_of: String attribute :templates_cookbook, kind_of: String - diff --git a/templates/default/init/binary_upstart.erb b/templates/default/init/binary_upstart.erb index 7ab27ea..6a95057 100644 --- a/templates/default/init/binary_upstart.erb +++ b/templates/default/init/binary_upstart.erb @@ -18,6 +18,7 @@ setgid <%= @supervisor_gid %> <% end -%> script + export LS_HEAP_SIZE="<%= @max_heap %>" export LOGSTASH_HOME="<%= @home %>" export HOME=$LOGSTASH_HOME export LOGSTASH_OPTS="<%= @args.join(' ') %>" diff --git a/test/unit/spec/agent_spec.rb b/test/unit/spec/agent_spec.rb index e952b22..e759e42 100644 --- a/test/unit/spec/agent_spec.rb +++ b/test/unit/spec/agent_spec.rb @@ -7,6 +7,7 @@ let(:node) { runner.node } let(:chef_run) do # runner.node.set['logstash'] ... + runner.node.set['memory']['total'] = '1024000kb' runner.converge(described_recipe) end include_context 'stubs-common' diff --git a/test/unit/spec/default_spec.rb b/test/unit/spec/default_spec.rb index 455c73f..acc0b1e 100644 --- a/test/unit/spec/default_spec.rb +++ b/test/unit/spec/default_spec.rb @@ -6,7 +6,7 @@ let(:runner) { ChefSpec::Runner.new(::UBUNTU_OPTS) } let(:node) { runner.node } let(:chef_run) do - # runner.node.set['logstash'] ... + runner.node.set['memory']['total'] = '1024000kb' runner.converge(described_recipe) end include_context 'stubs-common' diff --git a/test/unit/spec/lwrp_config_spec.rb b/test/unit/spec/lwrp_config_spec.rb index f234be7..3aa2ed3 100644 --- a/test/unit/spec/lwrp_config_spec.rb +++ b/test/unit/spec/lwrp_config_spec.rb @@ -11,6 +11,7 @@ let(:runner) { ChefSpec::Runner.new(::LWRP) } let(:node) { runner.node } let(:chef_run) do + runner.node.set['memory']['total'] = '1024000kb' runner.node.set['logstash']['instance']['server']['config_templates'] = { output_stdout: 'config/output_stdout.conf.erb' } diff --git a/test/unit/spec/lwrp_pattern_spec.rb b/test/unit/spec/lwrp_pattern_spec.rb index 2238d22..e1e06ca 100644 --- a/test/unit/spec/lwrp_pattern_spec.rb +++ b/test/unit/spec/lwrp_pattern_spec.rb @@ -8,6 +8,7 @@ let(:node) { runner.node } let(:chef_run) do runner.node.merge(::UBUNTU_OPTS) + runner.node.set['memory']['total'] = '1024000kb' runner.node.set['logstash']['instance']['server']['pattern_templates'] = { 'default' => 'patterns/custom_patterns.erb' } diff --git a/test/unit/spec/server_spec.rb b/test/unit/spec/server_spec.rb index d19c2e9..e5f3bf4 100644 --- a/test/unit/spec/server_spec.rb +++ b/test/unit/spec/server_spec.rb @@ -7,6 +7,7 @@ let(:node) { runner.node } let(:chef_run) do # runner.node.set['logstash'] ... + runner.node.set['memory']['total'] = '1024000kb' runner.converge(described_recipe) end include_context 'stubs-common' @@ -23,7 +24,7 @@ expect(chef_run).to create_logstash_pattern('server') end - it 'calls the logstash_instance LWRP' do + it 'calls the logstash_service LWRP' do expect(chef_run).to enable_logstash_service('server') expect(chef_run).to start_logstash_service('server') end From beb0db45c33a2f7c5ba0ad4796a8cd88f1b72ed1 Mon Sep 17 00:00:00 2001 From: Martin Forssen Date: Tue, 29 Apr 2014 10:59:01 +0200 Subject: [PATCH 23/55] Switch to elasticsearch curator. Rewrote index_cleaner recipe to use the new elasticsearch curator. Also added code to the recipe which figures out the address of the es cluster to talk to. This code was extracted into a library from server.rb --- libraries/logstash_util.rb | 30 ++++++++++++++++++++++++++++++ recipes/index_cleaner.rb | 27 ++------------------------- recipes/server.rb | 24 ++---------------------- 3 files changed, 34 insertions(+), 47 deletions(-) create mode 100644 libraries/logstash_util.rb diff --git a/libraries/logstash_util.rb b/libraries/logstash_util.rb new file mode 100644 index 0000000..6e55326 --- /dev/null +++ b/libraries/logstash_util.rb @@ -0,0 +1,30 @@ +module Logstash + def self.es_server_ip(node) + if Chef::Config[:solo] + node['logstash']['elasticsearch_ip'] + else + es_results = [] + Chef::Search::Query.new.search(:node, node['logstash']['elasticsearch_query']) { |o| es_results << o } + + if !es_results.empty? + return es_results[0]['ipaddress'] + else + es_server_ip = node['logstash']['elasticsearch_ip'] + end + end + end + + def self.graphite_server_ip(node) + if Chef::Config[:solo] + node['logstash']['graphite_ip'] + else + graphite_results = [] + Chef::Search::Query.new.search(:node, node['logstash']['graphite_query']) { |o| graphite_results << o } + if !graphite_results.empty? + graphite_results[0]['ipaddress'] + else + node['logstash']['graphite_ip'] + end + end + end +end diff --git a/recipes/index_cleaner.rb b/recipes/index_cleaner.rb index afdba6b..eadf84c 100644 --- a/recipes/index_cleaner.rb +++ b/recipes/index_cleaner.rb @@ -1,38 +1,15 @@ # Encoding: utf-8 include_recipe 'python::pip' -base_dir = File.join(node['logstash']['basedir'], 'index_cleaner') -index_cleaner_bin = File.join(base_dir, 'logstash_index_cleaner.py') days_to_keep = node['logstash']['index_cleaner']['days_to_keep'] log_file = node['logstash']['index_cleaner']['cron']['log_file'] -python_pip 'pyes' do +python_pip 'elasticsearch-curator' do action :install end -directory base_dir do - action :create - mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] -end - -cookbook_file index_cleaner_bin do - source 'logstash_index_cleaner.py' - owner node['logstash']['user'] - group node['logstash']['group'] - mode '0774' -end - -# FIXME: http://tickets.opscode.com/browse/CHEF-3547 -file index_cleaner_bin do - mode '0744' - action :touch - only_if { Chef::VERSION == '10.16.0' } -end - cron 'logstash_index_cleaner' do - command "#{index_cleaner_bin} -d #{days_to_keep} &> #{log_file}" + command "curator --host #{::Logstash.es_server_ip(node)} -d #{days_to_keep} &> #{log_file}" minute node['logstash']['index_cleaner']['cron']['minute'] hour node['logstash']['index_cleaner']['cron']['hour'] end diff --git a/recipes/server.rb b/recipes/server.rb index f6310f3..651db52 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -33,26 +33,6 @@ patterns_dir = node['logstash']['server']['home'] + '/' + node['logstash']['server']['patterns_dir'] end -if Chef::Config[:solo] - es_server_ip = node['logstash']['elasticsearch_ip'] - graphite_server_ip = node['logstash']['graphite_ip'] -else - es_results = search(:node, node['logstash']['elasticsearch_query']) - graphite_results = search(:node, node['logstash']['graphite_query']) - - if !es_results.empty? - es_server_ip = es_results[0]['ipaddress'] - else - es_server_ip = node['logstash']['elasticsearch_ip'] - end - - if !graphite_results.empty? - graphite_server_ip = graphite_results[0]['ipaddress'] - else - graphite_server_ip = node['logstash']['graphite_ip'] - end -end - # Create directory for logstash directory node['logstash']['server']['home'] do action :create @@ -137,8 +117,8 @@ group node['logstash']['group'] mode '0644' variables( - :graphite_server_ip => graphite_server_ip, - :es_server_ip => es_server_ip, + :graphite_server_ip => ::Logstash.graphite_server_ip(node), + :es_server_ip => ::Logstash.es_server_ip(node), :enable_embedded_es => node['logstash']['server']['enable_embedded_es'], :es_cluster => node['logstash']['elasticsearch_cluster'], :patterns_dir => patterns_dir From 1aa9d8eec48bfbb2e98abdee6c0086b3d1dc013e Mon Sep 17 00:00:00 2001 From: Daniel Givens Date: Thu, 1 May 2014 13:57:18 -0500 Subject: [PATCH 24/55] Updated some default attributes to fit the new schema --- attributes/default.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/attributes/default.rb b/attributes/default.rb index 4cb18b5..4aedf5f 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -2,9 +2,9 @@ # roles/flags for various search/discovery default['logstash']['instance']['default']['graphite_role'] = 'graphite_server' -default['logstash']['instance']['default']['graphite_query'] = "roles:#{node['logstash']['graphite_role']} AND chef_environment:#{node.chef_environment}" +default['logstash']['instance']['default']['graphite_query'] = "roles:#{node['logstash']['instance']['default']['graphite_role']} AND chef_environment:#{node.chef_environment}" default['logstash']['instance']['default']['elasticsearch_role'] = 'elasticsearch_server' -default['logstash']['instance']['default']['elasticsearch_query'] = "roles:#{node['logstash']['elasticsearch_role']} AND chef_environment:#{node.chef_environment}" +default['logstash']['instance']['default']['elasticsearch_query'] = "roles:#{node['logstash']['instance']['default']['elasticsearch_role']} AND chef_environment:#{node.chef_environment}" default['logstash']['instance']['default']['elasticsearch_cluster'] = 'logstash' default['logstash']['instance']['default']['elasticsearch_ip'] = '' default['logstash']['instance']['default']['elasticsearch_port'] = '' @@ -15,7 +15,7 @@ default['logstash']['instance']['default']['user'] = 'logstash' default['logstash']['instance']['default']['group'] = 'logstash' default['logstash']['instance']['default']['user_opts'] = { homedir: '/var/lib/logstash', uid: nil, gid: nil } -default['logstash']['instance']['default']['supervisor_gid'] = node['logstash']['group'] +default['logstash']['instance']['default']['supervisor_gid'] = node['logstash']['instance']['default']['group'] default['logstash']['instance']['default']['pid_dir'] = '/var/run/logstash' default['logstash']['instance']['default']['create_account'] = true default['logstash']['instance']['default']['join_groups'] = [] From 1dd4d2ee2fff4777b84078955d78dd0aa21375a8 Mon Sep 17 00:00:00 2001 From: Daniel Givens Date: Thu, 1 May 2014 13:58:49 -0500 Subject: [PATCH 25/55] service_query was in the wrong place --- libraries/search.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/search.rb b/libraries/search.rb index 96d4ab0..1cc185c 100644 --- a/libraries/search.rb +++ b/libraries/search.rb @@ -14,8 +14,8 @@ def service_ip(instance = 'default', service = 'elasticsearch') end if Chef::Config[:solo] service_ip = attributes["#{service}_ip"] || defaults["#{service}_ip"] - service_query = attributes["#{service}_query"] || defaults["#{service}_query"] else + service_query = attributes["#{service}_query"] || defaults["#{service}_query"] results = search(:node, service_query) if !results.empty? service_ip = results[0]['ipaddress'] From 5e2e095f26fa1ba85640fa00007d15330b6b662d Mon Sep 17 00:00:00 2001 From: Daniel Givens Date: Thu, 1 May 2014 14:02:00 -0500 Subject: [PATCH 26/55] Replacing an orphaned variable in the logrotate path log_dir seems to have been used at one time, but is no longer defined anywhere. --- providers/instance.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/instance.rb b/providers/instance.rb index 262e729..d57f964 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -211,7 +211,7 @@ def logrotate(ls) @run_context.include_recipe 'logrotate::default' logrotate_app "logstash_#{name}" do - path "#{log_dir}/*.log" + path "#{ls[:homedir]}/log/*.log" size ls[:logrotate_size] if ls[:logrotate_use_filesize] frequency ls[:logrotate_rotate_frequency] rotate ls[:logrotate_max_backup] From 17e064802b98bcca4af2a928e785bf7e7b9fb725 Mon Sep 17 00:00:00 2001 From: Daniel Givens Date: Thu, 1 May 2014 14:04:43 -0500 Subject: [PATCH 27/55] Updated attributes to match new schema --- recipes/index_cleaner.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/recipes/index_cleaner.rb b/recipes/index_cleaner.rb index afdba6b..5a8dc73 100644 --- a/recipes/index_cleaner.rb +++ b/recipes/index_cleaner.rb @@ -1,7 +1,7 @@ # Encoding: utf-8 include_recipe 'python::pip' -base_dir = File.join(node['logstash']['basedir'], 'index_cleaner') +base_dir = File.join(node['logstash']['instance']['default']['basedir'], 'index_cleaner') index_cleaner_bin = File.join(base_dir, 'logstash_index_cleaner.py') days_to_keep = node['logstash']['index_cleaner']['days_to_keep'] log_file = node['logstash']['index_cleaner']['cron']['log_file'] @@ -13,14 +13,14 @@ directory base_dir do action :create mode '0755' - owner node['logstash']['user'] - group node['logstash']['group'] + owner node['logstash']['instance']['default']['user'] + group node['logstash']['instance']['default']['group'] end cookbook_file index_cleaner_bin do source 'logstash_index_cleaner.py' - owner node['logstash']['user'] - group node['logstash']['group'] + owner node['logstash']['instance']['default']['user'] + group node['logstash']['instance']['default']['group'] mode '0774' end From cb7477bb28d9b48e6db9e0c10256de9e0d9df5c7 Mon Sep 17 00:00:00 2001 From: Daniel Givens Date: Thu, 1 May 2014 14:19:51 -0500 Subject: [PATCH 28/55] Added logic to honor the enable_embedded_es attribute for servers --- recipes/server.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/recipes/server.rb b/recipes/server.rb index 6a1f547..f0b9d72 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -28,11 +28,17 @@ es_ip = service_ip(name, 'elasticsearch') +if node[:logstash][:instance][name].key?('enable_embedded_es') + embedded_es = node[:logstash][:instance][name][:enable_embedded_es] +else + embedded_es = node[:logstash][:instance][:default][:enable_embedded_es] +end + logstash_config name do action [:create] variables( elasticsearch_ip: es_ip, - elasticsearch_embedded: true + elasticsearch_embedded: embedded_es ) end From 7dbfd5c227b9e54d4883151feb3683e8a9b25d53 Mon Sep 17 00:00:00 2001 From: Daniel Givens Date: Thu, 1 May 2014 14:31:55 -0500 Subject: [PATCH 29/55] Updated runit and upstart init methods Made changes for the new attribute structure and consistency. --- providers/service.rb | 32 +++++++++++++++-- templates/default/init/java_upstart.erb | 26 ++++++-------- templates/default/init/upstart.erb | 29 +++++++-------- templates/default/sv-logstash_agent-run.erb | 26 ++++++-------- templates/default/sv-logstash_server-run.erb | 38 +++++++++----------- templates/default/sv-logstash_web-run.erb | 24 ++++++------- 6 files changed, 91 insertions(+), 84 deletions(-) diff --git a/providers/service.rb b/providers/service.rb index 95c157d..ee9779f 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -30,6 +30,9 @@ def load_current_resource @log_file = attributes['log_file'] || defaults['log_file'] @max_heap = attributes['xmx'] || defaults['xmx'] @min_heap = attributes['xms'] || defaults['xms'] + @gc_opts = attributes['gc_opts'] || defaults['gc_opts'] + @ipv4_only = attributes['ipv4_only'] || defaults['ipv4_only'] + @java_opts = attributes['java_opts'] || defaults['java_opts'] @description = new_resource.description || @service_name @chdir = @home @workers = attributes['workers'] || defaults['workers'] @@ -106,7 +109,25 @@ def load_current_resource new_resource.updated_by_last_action(pr.updated_by_last_action?) when 'runit' @run_context.include_recipe 'runit::default' - ri = runit_service(svc[:service_name]) + ri = runit_service svc[:service_name] do + options({ + :name => svc[:name], + :home => svc[:home], + :max_heap => svc[:max_heap], + :min_heap => svc[:min_heap], + :gc_opts => svc[:gc_opts], + :java_opts => svc[:java_opts], + :ipv4_only => svc[:ipv4_only], + :debug => svc[:debug], + :log_file => svc[:log_file], + :workers => svc[:workers], + :install_type => svc[:install_type], + :supervisor_gid => svc[:supervisor_gid], + :user => svc[:user], + :web_address => svc[:web_address], + :web_port => svc[:web_port] + }) + end new_resource.updated_by_last_action(ri.updated_by_last_action?) when 'native' if platform_family? 'debian' @@ -125,11 +146,18 @@ def load_current_resource home: svc[:home], name: svc[:name], command: svc[:command], - max_heap: svc[:max_heap], args: args, user: svc[:user], group: svc[:group], description: svc[:description], + max_heap: svc[:max_heap], + min_heap: svc[:min_heap], + gc_opts: svc[:gc_opts], + java_opts: svc[:java_opts], + ipv4_only: svc[:ipv4_only], + debug: svc[:debug], + log_file: svc[:log_file], + workers: svc[:workers], supervisor_gid: svc[:supervisor_gid] ) end diff --git a/templates/default/init/java_upstart.erb b/templates/default/init/java_upstart.erb index 8027232..cdfdbfe 100644 --- a/templates/default/init/java_upstart.erb +++ b/templates/default/init/java_upstart.erb @@ -11,9 +11,9 @@ limit nofile 65550 65550 chdir <%= @home %> <% unless node['logstash']['instance'][@name]['upstart_with_sudo'] -%> -setuid <%= node['logstash']['instance'][@name]['user'] %> - <% unless node['logstash']['instance'][@name]['supervisor_gid'].to_s.empty? -%> -setgid <%= node['logstash']['instance'][@name]['supervisor_gid'] %> +setuid <%= @user %> + <% unless @supervisor_gid.to_s.empty? -%> +setgid <%= @supervisor_gid %> <% end -%> <% end -%> @@ -21,25 +21,19 @@ script export LOGSTASH_HOME="<%= @home %>" export HOME=$LOGSTASH_HOME - export GC_OPTS="<%= node['logstash']['instance'][@name]['gc_opts'] %>" - export JAVA_OPTS="-server -Xms<%= node['logstash']['instance'][@name]['xms'] %> -Xmx<%= node['logstash']['instance'][@name]['xmx'] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= node['logstash']['instance'][@name]['java_opts'] %> <%= '-Djava.net.preferIPv4Stack=true' if node['logstash']['instance'][@name]['ipv4_only'] %>" + export GC_OPTS="<%= @gc_opts %>" + export JAVA_OPTS="-server -Xms<%= @min_heap %> -Xmx<%= @max_heap %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= @java_opts %> <%= '-Djava.net.preferIPv4Stack=true' if @ipv4_only %>" export LOGSTASH_OPTS="agent -f $LOGSTASH_HOME/etc/conf.d" - <% if node['logstash']['instance'][@name]['pluginpath'] -%> - export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['instance'][@name]['pluginpath'] %>" - <% end -%> - <% if node['logstash']['instance'][@name]['workers'] %> - export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['instance'][@name]['workers'] %>" - <% end %> - <% if node['logstash']['instance'][@name]['log_file'] -%> - export LOGSTASH_OPTS="$LOGSTASH_OPTS -l $LOGSTASH_HOME/log/<%= node['logstash']['instance'][@name]['log_file'] %>" - <% end -%> - <% if node['logstash']['instance'][@name]['debug'] -%> + export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath $LOGSTASH_HOME/lib" + export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= @workers %>" + export LOGSTASH_OPTS="$LOGSTASH_OPTS -l $LOGSTASH_HOME/log/<%= @log_file %>" + <% if @debug -%> export LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" <% end -%> export OPTS="$JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS" <% if node['logstash']['instance'][@name]['upstart_with_sudo'] -%> - exec sudo -u <%= node['logstash']['instance'][@name]['user'] %> /usr/bin/java $OPTS + exec sudo -u <%= @user %> /usr/bin/java $OPTS <% else -%> exec /usr/bin/java $OPTS <% end -%> diff --git a/templates/default/init/upstart.erb b/templates/default/init/upstart.erb index 2cf704b..cdfdbfe 100644 --- a/templates/default/init/upstart.erb +++ b/templates/default/init/upstart.erb @@ -11,34 +11,29 @@ limit nofile 65550 65550 chdir <%= @home %> <% unless node['logstash']['instance'][@name]['upstart_with_sudo'] -%> -setuid <%= node['logstash']['instance'][@name]['user'] %> - <% unless node['logstash']['instance'][@name]['supervisor_gid'].to_s.empty? -%> -setgid <%= node['logstash']['instance'][@name]['supervisor_gid'] %> +setuid <%= @user %> + <% unless @supervisor_gid.to_s.empty? -%> +setgid <%= @supervisor_gid %> <% end -%> <% end -%> script export LOGSTASH_HOME="<%= @home %>" export HOME=$LOGSTASH_HOME - export GC_OPTS="<%= node['logstash']['instance'][@name]['gc_opts'] %>" - export JAVA_OPTS="-server -Xms<%= node['logstash']['instance'][@name]['xms'] %> -Xmx<%= node['logstash']['instance'][@name]['xmx'] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= node['logstash']['instance'][@name]['java_opts'] %> <%= '-Djava.net.preferIPv4Stack=true' if node['logstash']['instance'][@name]['ipv4_only'] %>" - export LOGSTASH_OPTS="agent -f <%= node['logstash']['instance'][@name]['home'] %>/etc/conf.d" - <% if node['logstash']['instance'][@name]['pluginpath'] -%> - export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['instance'][@name]['pluginpath'] %>" - <% end -%> - <% if node['logstash']['instance'][@name]['workers'] %> - export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['instance'][@name]['workers'] %>" - <% end %> - <% if node['logstash']['instance'][@name]['log_file'] -%> - export LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= @home %>/log/<%= node['logstash']['instance'][@name]['log_file'] %>" - <% end -%> - <% if node['logstash']['instance'][@name]['debug'] -%> + + export GC_OPTS="<%= @gc_opts %>" + export JAVA_OPTS="-server -Xms<%= @min_heap %> -Xmx<%= @max_heap %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= @java_opts %> <%= '-Djava.net.preferIPv4Stack=true' if @ipv4_only %>" + export LOGSTASH_OPTS="agent -f $LOGSTASH_HOME/etc/conf.d" + export LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath $LOGSTASH_HOME/lib" + export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= @workers %>" + export LOGSTASH_OPTS="$LOGSTASH_OPTS -l $LOGSTASH_HOME/log/<%= @log_file %>" + <% if @debug -%> export LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" <% end -%> export OPTS="$JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS" <% if node['logstash']['instance'][@name]['upstart_with_sudo'] -%> - exec sudo -u <%= node['logstash']['instance'][@name]['user'] %> /usr/bin/java $OPTS + exec sudo -u <%= @user %> /usr/bin/java $OPTS <% else -%> exec /usr/bin/java $OPTS <% end -%> diff --git a/templates/default/sv-logstash_agent-run.erb b/templates/default/sv-logstash_agent-run.erb index 770f435..a624c0d 100644 --- a/templates/default/sv-logstash_agent-run.erb +++ b/templates/default/sv-logstash_agent-run.erb @@ -1,23 +1,19 @@ #!/bin/sh -cd /<%= node['logstash']['agent']['home'] %> +cd /<%= @options[:home] %> exec 2>&1 # Need to set LOGSTASH_HOME and HOME so sincedb will work -LOGSTASH_HOME="<%= node['logstash']['agent']['home'] %>" +LOGSTASH_HOME="<%= @options[:home] %>" GC_OPTS="-XX:+UseParallelOldGC" -JAVA_OPTS="-server -Xms<%= node['logstash']['agent']['xms'] %> -Xmx<%= node['logstash']['agent']['xmx'] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/" -LOGSTASH_OPTS="agent -f <%= node['logstash']['agent']['config_dir'] %>" -<% if node['logstash']['agent']['debug'] -%> +JAVA_OPTS="-server -Xms<%= @options[:min_heap] %> -Xmx<%= @options[:max_heap] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/" +LOGSTASH_OPTS="agent -f $LOGSTASH_HOME/etc/conf.d" +<% if @options[:debug] -%> LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" <% end -%> -<% if node['logstash']['agent']['log_file'] -%> -LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= node['logstash']['agent']['log_file'] %>" -<% end -%> -<% if node['logstash']['agent']['workers'] %> -export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['agent']['workers'] %>" -<% end %> -<% if node['logstash']['supervisor_gid'] -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node["logstash"]["user"] %>:<%= node["logstash"]["supervisor_gid"] %> java $JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS +LOGSTASH_OPTS="$LOGSTASH_OPTS -l $LOGSTASH_HOME/log/<%= @options[:log_file] %>" +export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= @options[:workers] %>" +<% if @options[:supervisor_gid] -%> +HOME=$LOGSTASH_HOME exec chpst -u <%= @options[:user] %>:<%= @options[:supervisor_gid] %> java $JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS <% else -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node["logstash"]["user"] %> java $JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS -<% end -%> \ No newline at end of file +HOME=$LOGSTASH_HOME exec chpst -u <%= @options[:user] %> java $JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS +<% end -%> diff --git a/templates/default/sv-logstash_server-run.erb b/templates/default/sv-logstash_server-run.erb index 628e04e..0fc077a 100644 --- a/templates/default/sv-logstash_server-run.erb +++ b/templates/default/sv-logstash_server-run.erb @@ -3,35 +3,29 @@ ulimit -Hn 65550 ulimit -Sn 65550 -cd /<%= node['logstash']['server']['home'] %> +cd /<%= @options[:home] %> exec 2>&1 # Need to set LOGSTASH_HOME and HOME so sincedb will work -LOGSTASH_HOME="<%= node['logstash']['server']['home'] %>" -GC_OPTS="<%= node['logstash']['server']['gc_opts'] %>" -JAVA_OPTS="-server -Xms<%= node['logstash']['server']['xms'] %> -Xmx<%= node['logstash']['server']['xmx'] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= node['logstash']['server']['java_opts'] %> <%= '-Djava.net.preferIPv4Stack=true' if node['logstash']['agent']['ipv4_only'] %>" -LOGSTASH_OPTS="agent -f <%= node['logstash']['server']['home'] %>/etc/conf.d" -<% if node['logstash']['server']['pluginpath'] -%> -LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath <%= node['logstash']['server']['pluginpath'] %>" -<% end -%> -<% if node['logstash']['server']['debug'] -%> +LOGSTASH_HOME="<%= @options[:home] %>" +GC_OPTS="<%= @options[:gc_opts] %>" +JAVA_OPTS="-server -Xms<%= @options[:min_heap] %> -Xmx<%= @options[:max_heap] %> -Djava.io.tmpdir=$LOGSTASH_HOME/tmp/ <%= @options[:java_opts] %> <%= '-Djava.net.preferIPv4Stack=true' if @options[:ipv4_only] %>" +LOGSTASH_OPTS="agent -f $LOGSTASH_HOME/etc/conf.d" +LOGSTASH_OPTS="$LOGSTASH_OPTS --pluginpath $LOGSTASH_HOME/lib" +<% if @options[:debug] -%> LOGSTASH_OPTS="$LOGSTASH_OPTS -vv" <% end -%> -<% if node['logstash']['server']['log_file'] -%> -LOGSTASH_OPTS="$LOGSTASH_OPTS -l <%= node['logstash']['server']['log_file'] %>" -<% end -%> -<% if node['logstash']['server']['workers'] -%> -export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= node['logstash']['server']['workers'] %>" -<% end -%> -<% if node['logstash']['server']['install_method'] == 'tarball' -%> -<% if node['logstash']['supervisor_gid'] -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node["logstash"]["user"] %>:<%= node["logstash"]["supervisor_gid"] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS +LOGSTASH_OPTS="$LOGSTASH_OPTS -l $LOGSTASH_HOME/log/<%= @options[:log_file] %>" +export LOGSTASH_OPTS="$LOGSTASH_OPTS -w <%= @options[:workers] %>" +<% if @options[:install_type] == 'tarball' -%> +<% if @options[:supervisor_gid] -%> +HOME=$LOGSTASH_HOME exec chpst -u <%= @options[:user] %>:<%= @options[:supervisor_gid] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS <% else -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node["logstash"]["user"] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS +HOME=$LOGSTASH_HOME exec chpst -u <%= @options[:user] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS <% end -%> <% else -%> -<% if node['logstash']['supervisor_gid'] -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node["logstash"]["user"] %>:<%= node["logstash"]["supervisor_gid"] %> java $JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS +<% if @options[:supervisor_gid] -%> +HOME=$LOGSTASH_HOME exec chpst -u <%= @options[:user] %>:<%= @options[:supervisor_gid] %> java $JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS <% else -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node["logstash"]["user"] %> java $JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS +HOME=$LOGSTASH_HOME exec chpst -u <%= @options[:user] %> java $JAVA_OPTS $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS <% end -%> <% end -%> diff --git a/templates/default/sv-logstash_web-run.erb b/templates/default/sv-logstash_web-run.erb index 98bba71..bd8d05c 100644 --- a/templates/default/sv-logstash_web-run.erb +++ b/templates/default/sv-logstash_web-run.erb @@ -3,24 +3,24 @@ ulimit -Hn 65550 ulimit -Sn 65550 -cd /<%= node['logstash']['server']['home'] %> +cd /<%= @options[:home] %> exec 2>&1 -LOGSTASH_HOME="<%= node['logstash']['server']['home'] %>" -GC_OPTS="<%= node['logstash']['server']['gc_opts'] %>" +LOGSTASH_HOME="<%= @options[:home] %>" +GC_OPTS="<%= @options[:gc_opts] %>" -LOGSTASH_OPTS="web -a <%= node['logstash']['server']['web']['address'] %> -p <%= node['logstash']['server']['web']['port'] %>" +LOGSTASH_OPTS="web -a <%= node[:logstash][:instance][@name][:web][:address] %> -p <%= node[:logstash][:instance][@name][:web][:port] %>" -<% if node['logstash']['server']['install_method'] == 'tarball' -%> -<% if ! node['logstash']['supervisor_gid'].empty? -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node['logstash']['user'] %>:<%= node['logstash']['supervisor_gid'] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS +<% if @options[:install_type] == 'tarball' -%> +<% if ! @options[:supervisor_gid].empty? -%> +HOME=$LOGSTASH_HOME exec chpst -u <%= @options[:user] %>:<%= @options[:supervisor_gid] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS <% else -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node['logstash']['user'] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS +HOME=$LOGSTASH_HOME exec chpst -u <%= @options[:user] %> $LOGSTASH_HOME/bin/logstash $LOGSTASH_OPTS <% end -%> <% else -%> -<% if ! node['logstash']['supervisor_gid'].empty? -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node['logstash']['user'] %>:<%= node['logstash']['supervisor_gid'] %> java $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS +<% if ! @options[:supervisor_gid].empty? -%> +HOME=$LOGSTASH_HOME exec chpst -u <%= @options[:user] %>:<%= @options[:supervisor_gid] %> java $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS <% else -%> -HOME=$LOGSTASH_HOME exec chpst -u <%= node['logstash']['user'] %> java $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS +HOME=$LOGSTASH_HOME exec chpst -u <%= @options[:user] %> java $GC_OPTS -jar $LOGSTASH_HOME/lib/logstash.jar $LOGSTASH_OPTS <% end -%> -<% end -%> \ No newline at end of file +<% end -%> From b05d7775c46e9b07c8f0e757587518bfd80cfc7e Mon Sep 17 00:00:00 2001 From: Paul Czarkowski Date: Fri, 2 May 2014 09:39:47 -0500 Subject: [PATCH 30/55] config templates for server recipe berkshelf 3 / Chefdk compatibility --- Berksfile | 7 ++++--- recipes/server.rb | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Berksfile b/Berksfile index 16965fd..97d7c1f 100644 --- a/Berksfile +++ b/Berksfile @@ -1,15 +1,16 @@ # Encoding: utf-8 +source 'https://api.berkshelf.com' + metadata -cookbook 'rabbitmq', git: 'git://github.com/opscode-cookbooks/rabbitmq.git' cookbook 'java' cookbook 'pleaserun', git: 'https://github.com/paulczar/chef-pleaserun.git' +cookbook 'curl' +cookbook 'ark' group :test do cookbook 'minitest-handler', git: 'git://github.com/btm/minitest-handler-cookbook.git' cookbook 'elasticsearch', git: 'git://github.com/elasticsearch/cookbook-elasticsearch.git' cookbook 'kibana', git: 'git://github.com/lusis/chef-kibana.git' - cookbook 'curl' - cookbook 'ark' end diff --git a/recipes/server.rb b/recipes/server.rb index f0b9d72..ee35aec 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -34,7 +34,14 @@ embedded_es = node[:logstash][:instance][:default][:enable_embedded_es] end +my_templates = { + 'input_syslog' => 'config/input_syslog.conf.erb', + 'output_stdout' => 'config/output_stdout.conf.erb', + 'output_elasticsearch' => 'config/output_elasticsearch.conf.erb' +} + logstash_config name do + templates my_templates action [:create] variables( elasticsearch_ip: es_ip, From e9e3100df1acbc7e9c8f653f1ff62ee0c501c5d5 Mon Sep 17 00:00:00 2001 From: paulczar Date: Sat, 3 May 2014 15:06:46 -0500 Subject: [PATCH 31/55] support for redhat family native services --- .kitchen.yml | 3 + providers/instance.rb | 4 +- providers/service.rb | 40 +++-- templates/default/init/java_init.logstash.erb | 170 ++++++++++++++++++ .../default/init/tarball_init.logstash.erb | 164 +++++++++++++++++ ...binary_upstart.erb => tarball_upstart.erb} | 0 6 files changed, 363 insertions(+), 18 deletions(-) create mode 100755 templates/default/init/java_init.logstash.erb create mode 100644 templates/default/init/tarball_init.logstash.erb rename templates/default/init/{binary_upstart.erb => tarball_upstart.erb} (100%) diff --git a/.kitchen.yml b/.kitchen.yml index 15c8c13..78af441 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -13,6 +13,7 @@ provisioner: platforms: - name: ubuntu-12.04 + - name: centos-6.5 suites: - name: server @@ -24,6 +25,8 @@ suites: logstash: instance: server: + xms: '256M' + xmx: '256M' config_templates: input_syslog: 'config/input_syslog.conf.erb' output_stdout: 'config/output_stdout.conf.erb' diff --git a/providers/instance.rb b/providers/instance.rb index d57f964..ca434e1 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -122,8 +122,8 @@ def load_current_resource end rfr = remote_file "#{ls[:instance_dir]}/lib/logstash-#{ls[:version]}.jar" do - owner 'root' - group 'root' + owner ls[:user] + group ls[:group] mode '0755' source ls[:source_url] checksum ls[:checksum] diff --git a/providers/service.rb b/providers/service.rb index d44abc3..4aac389 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -46,7 +46,7 @@ def load_current_resource case svc[:method] when 'native' sv = service svc[:service_name] do - provider Chef::Provider::Service::Upstart + # provider Chef::Provider::Service::Upstart action [:restart] end new_resource.updated_by_last_action(sv.updated_by_last_action?) @@ -58,7 +58,7 @@ def load_current_resource case svc[:method] when 'native' sv = service svc[:service_name] do - provider Chef::Provider::Service::Upstart + # provider Chef::Provider::Service::Upstart action [:start] end new_resource.updated_by_last_action(sv.updated_by_last_action?) @@ -70,7 +70,7 @@ def load_current_resource case svc[:method] when 'native' sv = service svc[:service_name] do - provider Chef::Provider::Service::Upstart + # provider Chef::Provider::Service::Upstart action [:stop] end new_resource.updated_by_last_action(sv.updated_by_last_action?) @@ -82,7 +82,7 @@ def load_current_resource case svc[:method] when 'native' sv = service svc[:service_name] do - provider Chef::Provider::Service::Upstart + # provider Chef::Provider::Service::Upstart action [:reload] end new_resource.updated_by_last_action(sv.updated_by_last_action?) @@ -132,15 +132,10 @@ def load_current_resource when 'native' if platform_family? 'debian' if node['platform_version'] >= '12.04' - if svc[:install_type] == 'tarball' - tp_source = 'init/binary_upstart.erb' - else - tp_source = 'init/java_upstart.erb' - end args = default_args tp = template "/etc/init/#{svc[:service_name]}.conf" do mode '0644' - source tp_source + source "init/#{svc[:install_type]}_upstart.erb" cookbook svc[:templates_cookbook] variables( home: svc[:home], @@ -196,19 +191,32 @@ def load_current_resource new_resource.updated_by_last_action(sv.updated_by_last_action?) elsif platform_family? 'rhel', 'fedora' + args = default_args tp = template "/etc/init.d/#{svc[:service_name]}" do - source "init.#{svc[:service_name]}.erb" + source "init/#{svc[:install_type]}_init.logstash.erb" cookbook svc[:templates_cookbook] owner 'root' group 'root' mode '0774' variables( - config_file: "#{svc[:home]}/etc/conf.d", - home: svc[:home], - name: svc[:name], - log_file: svc[:log_file], + home: svc[:home], + name: svc[:name], + command: svc[:command], + args: args, + user: svc[:user], + group: svc[:group], + description: svc[:description], max_heap: svc[:max_heap], - min_heap: svc[:min_heap] + min_heap: svc[:min_heap], + gc_opts: svc[:gc_opts], + java_opts: svc[:java_opts], + ipv4_only: svc[:ipv4_only], + debug: svc[:debug], + log_file: svc[:log_file], + workers: svc[:workers], + supervisor_gid: svc[:supervisor_gid], + config_file: "#{svc[:home]}/etc/conf.d", + group: svc[:group] ) end new_resource.updated_by_last_action(tp.updated_by_last_action?) diff --git a/templates/default/init/java_init.logstash.erb b/templates/default/init/java_init.logstash.erb new file mode 100755 index 0000000..f227d48 --- /dev/null +++ b/templates/default/init/java_init.logstash.erb @@ -0,0 +1,170 @@ +#!/usr/bin/env bash +# +# logstash +# +# chkconfig: - 57 47 +# description: logstash +# processname: logstash + + +PIDDIR="/var/run" +export PIDFILE="/var/run/logstash-<%= @name %>.pid" +export LS_HOME="<%= @home %>" +export LS_CONFIG="<%= @config_file %>" +LS_USER="<%= @user %>" +LS_LOG="<%= @log_file %>" +LOGDIR="<%= ::File.dirname @log_file %>" +export JAVA_OPTS="-server -Xms<%= @min_heap %> -Xmx<%= @max_heap %> -Djava.io.tmpdir=$LS_HOME/tmp/" +BIN_SCRIPT="/usr/bin/env java $JAVA_OPTS -jar $LS_HOME/lib/logstash.jar agent -f $LS_CONFIG > $LS_LOG 2>&1 & echo \$! > $PIDFILE" + +if [ -f /etc/init.d/functions ] ; then + . /etc/init.d/functions +fi + +start() { + + if [ ! -d "$PIDDIR" ] ; then + mkdir "$PIDDIR" + chown -R $LS_USER:$LS_USER $PIDDIR + fi + + if [ ! -d "$LOGDIR" ] ; then + mkdir "$LOGDIR" + fi + + chown -R $LS_USER:$LS_USER $LOGDIR $PIDDIR + + + if [ -f $PIDFILE ]; then + echo -e "\033[31;1mPID file found in $PIDFILE, already running?\033[0m" + ls_pid="$(cat $PIDFILE)" + pid_running="$( ps ax | grep 'java' | grep $ls_pid )" + + if [ ! -z "$pid_running" ] ; then + echo -e "\033[31;1mPID $ls_pid still alive, logstash is already running. Doing nothing\033[0m" + return 1 + fi + fi + + echo -e "\033[1mStarting logstash...\033[0m" + pushd $LS_HOME > /dev/null 2>&1 + su $LS_USER -c "$BIN_SCRIPT" > /dev/null 2>&1 + ls_pid=$! + result=$? + popd > /dev/null 2>&1 + + if [ $result -ne 0 ] ; then + failure + echo -e "Logstash did not start successfully" + exit 1 + else + success + echo -e "Logstash started successfully" + fi + +} + + + +function stop() { + echo -n -e "\033[1mStopping logstash...\033[0m" + + if [ -z "$SHUTDOWN_WAIT" ]; then + SHUTDOWN_WAIT=5 + fi + + if [ ! -z "$PIDFILE" ]; then + if [ -f "$PIDFILE" ]; then + kill -0 `cat $PIDFILE` >/dev/null 2>&1 + if [ $? -gt 0 ]; then + echo "PID file ($PIDFILE) found but no matching process was found. Nothing to do." + return 0 + fi + else + echo "\$PIDFILE was set ($PIDFILE) but the specified file does not exist. Is Logstash running? Assuming it has stopped and pro\ + ceeding." + return 0 + fi + fi + + kill `cat $PIDFILE` >/dev/null 2>&1 + + if [ ! -z "$PIDFILE" ]; then + if [ -f "$PIDFILE" ]; then + while [ $SHUTDOWN_WAIT -ge 0 ]; do + kill -0 `cat $PIDFILE` >/dev/null 2>&1 + if [ $? -gt 0 ]; then + rm $PIDFILE + break + fi + if [ $SHUTDOWN_WAIT -gt 0 ]; then + sleep 1 + fi + SHUTDOWN_WAIT=`expr $SHUTDOWN_WAIT - 1 ` + done + # still not dead, we may need to resort to drastic measures + if [ -f "$PIDFILE" ]; then + kill -0 `cat $PIDFILE` >/dev/null 2>&1 + if [ $? -eq 0 ]; then + echo "Application still alive, sleeping for 20 seconds before sending SIGKILL" + sleep 20 + kill -0 `cat $PIDFILE` >/dev/null 2>&1 + if [ $? -eq 0 ]; then + kill -9 `cat $PIDFILE` >/dev/null 2>&1 + echo "Killed with extreme prejudice" + else + echo "Application stopped, no need to use SIGKILL" + fi + rm $PIDFILE + fi + fi + fi + fi +} + +restart() { + stop + start +} + +status() { + # GOT PIDFILE? + [ -f $PIDFILE ] && pid=$(cat $PIDFILE) + + # RUNNING + if [[ $pid && -d "/proc/$pid" ]]; then + success + echo -e "Logstash is running with pid $pid" + fi + + # NOT RUNNING + if [[ ! $pid || ! -d "/proc/$pid" ]]; then + echo "Logstash not running" + fi + + # STALE PID FOUND + if [[ ! -d "/proc/$pid" && -f $PIDFILE ]]; then + echo -e "\033[1;31;40m[!] Stale PID found in $PIDFILE\033[0m" + fi +} + + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + restart + ;; + status) + status $2 + ;; + *) + echo $"Usage: $0 {start|stop|restart|status [-v]|}" + exit 1 +esac + +exit $? diff --git a/templates/default/init/tarball_init.logstash.erb b/templates/default/init/tarball_init.logstash.erb new file mode 100644 index 0000000..28b1db4 --- /dev/null +++ b/templates/default/init/tarball_init.logstash.erb @@ -0,0 +1,164 @@ +#!/usr/bin/env bash +# +# logstash +# +# chkconfig: - 57 47 +# description: logstash +# processname: logstash + + +PIDDIR="/var/run/logstash" +export PIDFILE="/var/run/logstash-<%= @name %>.pid" +export LS_HOME="<%= @home %>" +export LS_HEAP_SIZE="<%= @max_heap %>" +export LOGSTASH_OPTS="<%= @args.join(' ') %>" +LS_USER="<%= @user %>" +LS_LOG="<%= @log_file %>" +LOGDIR="<%= ::File.dirname @log_file %>" +export JAVA_OPTS="-server -Xms<%= @min_heap %> -Xmx<%= @max_heap %> -Djava.io.tmpdir=$LS_HOME/tmp/" +BIN_SCRIPT="/usr/bin/env $LS_HOME/bin/logstash $LOGSTASH_OPTS > $LS_LOG 2>&1 & echo \$! > $PIDFILE" + +if [ -f /etc/init.d/functions ] ; then + . /etc/init.d/functions +fi + +start() { + + if [ ! -d "$PIDDIR" ] ; then + mkdir "$PIDDIR" + chown $LS_USER:$LS_USER $PIDDIR + fi + + if [ -f $PIDFILE ]; then + echo -e "\033[31;1mPID file found in $PIDFILE, already running?\033[0m" + ls_pid="$(cat $PIDFILE)" + pid_running="$( ps ax | grep 'java' | grep $ls_pid )" + + if [ ! -z "$pid_running" ] ; then + echo -e "\033[31;1mPID $ls_pid still alive, logstash is already running. Doing nothing\033[0m" + return 1 + fi + fi + + echo -e "\033[1mStarting logstash...\033[0m" + pushd $LS_HOME > /dev/null 2>&1 + su $LS_USER -c "$BIN_SCRIPT" > /dev/null 2>&1 + ls_pid=$! + result=$? + popd > /dev/null 2>&1 + + if [ $result -ne 0 ] ; then + failure + echo -e "Logstash did not start successfully" + exit 1 + else + success + echo -e "Logstash started successfully" + fi + +} + + + +function stop() { + echo -n -e "\033[1mStopping logstash...\033[0m" + + if [ -z "$SHUTDOWN_WAIT" ]; then + SHUTDOWN_WAIT=5 + fi + + if [ ! -z "$PIDFILE" ]; then + if [ -f "$PIDFILE" ]; then + kill -0 `cat $PIDFILE` >/dev/null 2>&1 + if [ $? -gt 0 ]; then + echo "PID file ($PIDFILE) found but no matching process was found. Nothing to do." + return 0 + fi + else + echo "\$PIDFILE was set ($PIDFILE) but the specified file does not exist. Is Logstash running? Assuming it has stopped and pro\ + ceeding." + return 0 + fi + fi + + kill `cat $PIDFILE` >/dev/null 2>&1 + + if [ ! -z "$PIDFILE" ]; then + if [ -f "$PIDFILE" ]; then + while [ $SHUTDOWN_WAIT -ge 0 ]; do + kill -0 `cat $PIDFILE` >/dev/null 2>&1 + if [ $? -gt 0 ]; then + rm $PIDFILE + break + fi + if [ $SHUTDOWN_WAIT -gt 0 ]; then + sleep 1 + fi + SHUTDOWN_WAIT=`expr $SHUTDOWN_WAIT - 1 ` + done + # still not dead, we may need to resort to drastic measures + if [ -f "$PIDFILE" ]; then + kill -0 `cat $PIDFILE` >/dev/null 2>&1 + if [ $? -eq 0 ]; then + echo "Application still alive, sleeping for 20 seconds before sending SIGKILL" + sleep 20 + kill -0 `cat $PIDFILE` >/dev/null 2>&1 + if [ $? -eq 0 ]; then + kill -9 `cat $PIDFILE` >/dev/null 2>&1 + echo "Killed with extreme prejudice" + else + echo "Application stopped, no need to use SIGKILL" + fi + rm $PIDFILE + fi + fi + fi + fi +} + +restart() { + stop + start +} + +status() { + # GOT PIDFILE? + [ -f $PIDFILE ] && pid=$(cat $PIDFILE) + + # RUNNING + if [[ $pid && -d "/proc/$pid" ]]; then + success + echo -e "Logstash is running with pid $pid" + fi + + # NOT RUNNING + if [[ ! $pid || ! -d "/proc/$pid" ]]; then + echo "Logstash not running" + fi + + # STALE PID FOUND + if [[ ! -d "/proc/$pid" && -f $PIDFILE ]]; then + echo -e "\033[1;31;40m[!] Stale PID found in $PIDFILE\033[0m" + fi +} + + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + restart + ;; + status) + status $2 + ;; + *) + echo $"Usage: $0 {start|stop|restart|status [-v]|}" + exit 1 +esac + +exit $? diff --git a/templates/default/init/binary_upstart.erb b/templates/default/init/tarball_upstart.erb similarity index 100% rename from templates/default/init/binary_upstart.erb rename to templates/default/init/tarball_upstart.erb From 23306b35e51c875a006142ca131ae3c49df4ce64 Mon Sep 17 00:00:00 2001 From: Daniel Givens Date: Thu, 8 May 2014 07:22:25 -0500 Subject: [PATCH 32/55] service_ip was missing service_query --- libraries/logstash_util.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/logstash_util.rb b/libraries/logstash_util.rb index 9b023e1..79ea36d 100644 --- a/libraries/logstash_util.rb +++ b/libraries/logstash_util.rb @@ -13,6 +13,7 @@ def self.service_ip(node, instance = 'default', service = 'elasticsearch') service_ip = attributes["#{service}_ip"] || defaults["#{service}_ip"] else results = [] + service_query = attributes["#{service}_query"] || defaults["#{service}_query"] Chef::Search::Query.new.search(:node, service_query) { |o| results << o } if !results.empty? service_ip = results[0]['ipaddress'] From 0d3bd8b37254180c95bcaee6a816406784c73e80 Mon Sep 17 00:00:00 2001 From: Daniel Givens Date: Thu, 8 May 2014 16:06:23 -0500 Subject: [PATCH 33/55] Updated default logstash version to 1.4.1 --- attributes/default.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/attributes/default.rb b/attributes/default.rb index 904ce6e..71ba8bf 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -21,15 +21,15 @@ default['logstash']['instance']['default']['join_groups'] = [] default['logstash']['instance']['default']['homedir'] = '/var/lib/logstash' -default['logstash']['instance']['default']['version'] = '1.4.0' -default['logstash']['instance']['default']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.0.tar.gz' -default['logstash']['instance']['default']['checksum'] = 'ab62394bb56da10cb20ee106badf22734402b21435977ec4f9aa65257627c629' +default['logstash']['instance']['default']['version'] = '1.4.1' +default['logstash']['instance']['default']['source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-1.4.1.tar.gz' +default['logstash']['instance']['default']['checksum'] = 'a1db8eda3d8bf441430066c384578386601ae308ccabf5d723df33cee27304b4' default['logstash']['instance']['default']['install_type'] = 'tarball' default['logstash']['instance']['default']['plugins_install_type'] = 'native' # native|tarball -default['logstash']['instance']['default']['plugins_version'] = '1.4.0' -default['logstash']['instance']['default']['plugins_source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-contrib-1.4.0.tar.gz' -default['logstash']['instance']['default']['plugins_checksum'] = '23aa397a7832d6e3553eba8360ff5d4ccfff98aac1e4a1b8201c21755e8d77c9' +default['logstash']['instance']['default']['plugins_version'] = '1.4.1' +default['logstash']['instance']['default']['plugins_source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-contrib-1.4.1.tar.gz' +default['logstash']['instance']['default']['plugins_checksum'] = 'beb0927351a3c298cd346225950c8d4fbda984ba54252d8a2f244329207c31e2' default['logstash']['instance']['default']['plugins_install_type'] = 'tarball' default['logstash']['instance']['default']['plugins_check_if_installed'] = 'lib/logstash/filters/translate.rb' From 8d24f74baaa04580b9f61bf616d3cdf15243dc35 Mon Sep 17 00:00:00 2001 From: Mihai Petracovici Date: Fri, 9 May 2014 11:07:49 -0500 Subject: [PATCH 34/55] Fix not respecting the instance pattern cookbook --- providers/pattern.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/pattern.rb b/providers/pattern.rb index d9d2ae5..43e3bd2 100644 --- a/providers/pattern.rb +++ b/providers/pattern.rb @@ -23,7 +23,7 @@ def load_current_resource @variables = new_resource.variables || attributes['pattern_templates_variables'] || defaults['pattern_templates_variables'] @owner = new_resource.owner || attributes['user'] || defaults['user'] @group = new_resource.group || attributes['group'] || defaults['group'] - @templates_cookbook = new_resource.templates_cookbook || defaults['pattern_templates_cookbook'] || defaults['pattern_templates_cookbook'] + @templates_cookbook = new_resource.templates_cookbook || attributes['pattern_templates_cookbook'] || defaults['pattern_templates_cookbook'] @mode = new_resource.mode || '0644' @path = new_resource.path || "#{@basedir}/#{@instance}/patterns" end From fdf46701ce62e64c5d22ba0f58585014015a7694 Mon Sep 17 00:00:00 2001 From: Mihai Petracovici Date: Fri, 9 May 2014 12:48:34 -0500 Subject: [PATCH 35/55] Sync instance with actual attributes used --- providers/instance.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/providers/instance.rb b/providers/instance.rb index ca434e1..a001825 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -23,7 +23,6 @@ def load_current_resource @version = new_resource.version || attributes['version'] || defaults['version'] @checksum = new_resource.checksum || attributes['checksum'] || defaults['checksum'] @source_url = new_resource.source_url || attributes['source_url'] || defaults['source_url'] - @enable_logrotate = new_resource.enable_logrotate || attributes['enable_logrotate'] || defaults['enable_logrotate'] @repo = new_resource.repo @sha = new_resource.sha @java_home = new_resource.java_home @@ -33,7 +32,7 @@ def load_current_resource @instance_dir = "#{@base_directory}/#{new_resource.name}".clone @logrotate_size = attributes['logrotate_max_size'] || defaults['logrotate_max_size'] @logrotate_use_filesize = attributes['logrotate_use_filesize'] || defaults['logrotate_use_filesize'] - @logrotate_rotate_frequency = attributes['logrotate_rotate_frequency'] || defaults['logrotate_rotate_frequency'] + @logrotate_frequency = attributes['logrotate_frequency'] || defaults['logrotate_frequency'] @logrotate_max_backup = attributes['logrotate_max_backup'] || defaults['logrotate_max_backup'] @logrotate_options = attributes['logrotate_options'] || defaults['logrotate_options'] @logrotate_enable = attributes['logrotate_enable'] || defaults['logrotate_enable'] @@ -213,7 +212,7 @@ def logrotate(ls) logrotate_app "logstash_#{name}" do path "#{ls[:homedir]}/log/*.log" size ls[:logrotate_size] if ls[:logrotate_use_filesize] - frequency ls[:logrotate_rotate_frequency] + frequency ls[:logrotate_frequency] rotate ls[:logrotate_max_backup] options ls[:logrotate_options] create "664 #{ls[:user]} #{ls[:group]}" @@ -236,7 +235,7 @@ def ls_vars enable_logrotate: @enable_logrotate, logrotate_size: @logrotate_size, logrotate_use_filesize: @logrotate_use_filesize, - logrotate_rotate_frequency: @logrotate_rotate_frequency, + logrotate_frequency: @logrotate_frequency, logrotate_max_backup: @logrotate_max_backup, logrotate_options: @logrotate_options, logrotate_enable: @logrotate_enable From 12503c562d59a8bb1bd0cad078e05e9526d06b2e Mon Sep 17 00:00:00 2001 From: paulczar Date: Sat, 10 May 2014 14:52:46 -0500 Subject: [PATCH 36/55] added curator as LWRP --- .kitchen.yml | 1 + .rubocop.yml | 4 +-- Berksfile | 2 +- CHANGELOG.md | 7 +++- attributes/default.rb | 8 ++++- attributes/index_cleaner.rb | 7 ---- libraries/matchers.rb | 4 +++ metadata.rb | 2 +- providers/curator.rb | 54 ++++++++++++++++++++++++++++ recipes/index_cleaner.rb | 18 ---------- recipes/server.rb | 4 +++ resources/curator.rb | 17 +++++++++ test/unit/spec/agent_spec.rb | 6 ++++ test/unit/spec/index_cleaner_spec.rb | 24 ------------- test/unit/spec/lwrp_config_spec.rb | 18 +++++++++- test/unit/spec/server_spec.rb | 13 +++++-- 16 files changed, 131 insertions(+), 58 deletions(-) delete mode 100644 attributes/index_cleaner.rb create mode 100644 providers/curator.rb delete mode 100644 recipes/index_cleaner.rb create mode 100644 resources/curator.rb delete mode 100644 test/unit/spec/index_cleaner_spec.rb diff --git a/.kitchen.yml b/.kitchen.yml index 78af441..417ca4f 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -21,6 +21,7 @@ suites: - recipe[apt::default] - recipe[java::default] - recipe[logstash::server] + - recipe[logstash::index_cleaner] attributes: logstash: instance: diff --git a/.rubocop.yml b/.rubocop.yml index 6878a68..65f6e11 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,8 +1,8 @@ AllCops: - Excludes: + Exclude: - vendor - test/support/** - Includes: + Include: - Rakefile - Gemfile - Vagrantfile diff --git a/Berksfile b/Berksfile index 854457a..dff6d58 100644 --- a/Berksfile +++ b/Berksfile @@ -1,6 +1,6 @@ # Encoding: utf-8 -# source 'https://api.berkshelf.com' +#source 'https://api.berkshelf.com' if Gem::Version.new(Berkshelf::VERSION) > Gem::Version.new('3') metadata diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e9f9f0..686801e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,12 @@ This file is used to list changes made in each version of chef-logstash. -## 0.8.0: + +## 0.9.1: + +* curator LWRP + +## 0.9.0: _this will almost certainly break backwards compatibility_ diff --git a/attributes/default.rb b/attributes/default.rb index 71ba8bf..1541e54 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -35,7 +35,7 @@ default['logstash']['instance']['default']['log_file'] = 'logstash.log' default['logstash']['instance']['default']['xms'] = '1024M' -default['logstash']['instance']['default']['xmx'] = "#{(node.memory.total.to_i * 0.6).floor / 1024}M" +default['logstash']['instance']['default']['xmx'] = "#{(node['memory']['total'].to_i * 0.6).floor / 1024}M" default['logstash']['instance']['default']['java_opts'] = '' default['logstash']['instance']['default']['gc_opts'] = '-XX:+UseParallelOldGC' default['logstash']['instance']['default']['ipv4_only'] = false @@ -78,3 +78,9 @@ default['logstash']['instance']['default']['logrotate_max_backup'] = 10 default['logstash']['instance']['default']['logrotate_max_size'] = '10M' default['logstash']['instance']['default']['logrotate_use_filesize'] = false + +# Curator +default['logstash']['instance']['default']['curator_days_to_keep'] = 31 +default['logstash']['instance']['default']['curator_cron_minute'] = '0' +default['logstash']['instance']['default']['curator_cron_hour'] = '*' +default['logstash']['instance']['default']['curator_cron_log_file'] = '/dev/null' diff --git a/attributes/index_cleaner.rb b/attributes/index_cleaner.rb deleted file mode 100644 index e2dc103..0000000 --- a/attributes/index_cleaner.rb +++ /dev/null @@ -1,7 +0,0 @@ -# Encoding: utf-8 -default['logstash']['instance']['default']['index_cleaner']['days_to_keep'] = 31 -default['logstash']['instance']['default']['index_cleaner']['cron'] = { - 'minute' => '0', - 'hour' => '*', - 'log_file' => '/dev/null' -} diff --git a/libraries/matchers.rb b/libraries/matchers.rb index 009cd76..02951f0 100644 --- a/libraries/matchers.rb +++ b/libraries/matchers.rb @@ -47,4 +47,8 @@ def stop_logstash_service(name) ChefSpec::Matchers::ResourceMatcher.new(:logstash_service, :stop, name) end + def create_logstash_curator(name) + ChefSpec::Matchers::ResourceMatcher.new(:logstash_curator, :create, name) + end + end diff --git a/metadata.rb b/metadata.rb index 5a45ee0..77eb707 100644 --- a/metadata.rb +++ b/metadata.rb @@ -5,7 +5,7 @@ license 'Apache 2.0' description 'Installs/Configures logstash' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '0.9.0' +version '0.9.1' %w(ubuntu debian redhat centos scientific amazon fedora).each do |os| supports os diff --git a/providers/curator.rb b/providers/curator.rb new file mode 100644 index 0000000..dfb1c54 --- /dev/null +++ b/providers/curator.rb @@ -0,0 +1,54 @@ +# Encoding: utf-8 +# Cookbook Name:: logstash +# Provider:: curator +# Author:: John E. Vincent +# License:: Apache 2.0 +# +# Copyright 2014, John E. Vincent + +require 'chef/mixin/shell_out' +require 'chef/mixin/language' +include Chef::Mixin::ShellOut + +def load_current_resource + @instance = new_resource.instance || 'default' + if node['logstash']['instance'].key?(@instance) + attributes = node['logstash']['instance'][@instance] + defaults = node['logstash']['instance']['default'] + else + attributes = node['logstash']['instance']['default'] + end + @days_to_keep = new_resource.days_to_keep || attributes['curator_days_to_keep'] || defaults['curator_days_to_keep'] + @minute = new_resource.minute || attributes['curator_cron_minute'] || defaults['curator_cron_minute'] + @hour = new_resource.hour || attributes['curator_cron_hour'] || defaults['curator_cron_hour'] + @log_file = new_resource.log_file || attributes['curator_cron_log_file'] || defaults['curator_cron_log_file'] + @user = new_resource.user || attributes['user'] || defaults['user'] +end + + +action :create do + ls_instance = @instance + ls_days_to_keep = @days_to_keep + ls_log_file = @log_file + ls_hour = @hour + ls_minute = @minute + ls_user = @user + + @run_context.include_recipe 'python::pip' + + pi = python_pip 'elasticsearch-curator' do + action :install + end + new_resource.updated_by_last_action(pi.updated_by_last_action?) + + cr = cron 'logstash_index_cleaner' do + command "curator --host #{::Logstash.service_ip(node, ls_instance, 'elasticsearch')} -d #{ls_days_to_keep} &> #{ls_log_file}" + user ls_user + minute ls_minute + hour ls_hour + end + new_resource.updated_by_last_action(cr.updated_by_last_action?) +end + + + diff --git a/recipes/index_cleaner.rb b/recipes/index_cleaner.rb deleted file mode 100644 index 0d35e82..0000000 --- a/recipes/index_cleaner.rb +++ /dev/null @@ -1,18 +0,0 @@ -# Encoding: utf-8 - -# TURN This into a LWRP please!!!!!! - -include_recipe 'python::pip' - -days_to_keep = node['logstash']['instance']['default']['index_cleaner']['days_to_keep'] -log_file = node['logstash']['instance']['default']['index_cleaner']['cron']['log_file'] - -python_pip 'elasticsearch-curator' do - action :install -end - -cron 'logstash_index_cleaner' do - command "curator --host #{::Logstash.service_ip(node)} -d #{days_to_keep} &> #{log_file}" - minute node['logstash']['instance']['default']['index_cleaner']['cron']['minute'] - hour node['logstash']['instance']['default']['index_cleaner']['cron']['hour'] -end diff --git a/recipes/server.rb b/recipes/server.rb index 6738744..4c821de 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -56,3 +56,7 @@ logstash_service name do action [:start] end + +logstash_curator 'server' do + action [:create] +end \ No newline at end of file diff --git a/resources/curator.rb b/resources/curator.rb new file mode 100644 index 0000000..0e05a8e --- /dev/null +++ b/resources/curator.rb @@ -0,0 +1,17 @@ +# Encoding: utf-8 +# Cookbook Name:: logstash +# Resource:: config +# Author:: John E. Vincent +# Copyright 2014, John E. Vincent +# License:: Apache 2.0 + +actions :create + +default_action :create if defined?(default_action) + +attribute :instance, kind_of: String, name_attribute: true +attribute :days_to_keep, kind_of: String +attribute :minute, kind_of: String +attribute :hour, kind_of: String +attribute :log_file, kind_of: String +attribute :user, kind_of: String \ No newline at end of file diff --git a/test/unit/spec/agent_spec.rb b/test/unit/spec/agent_spec.rb index b340c63..f80b163 100644 --- a/test/unit/spec/agent_spec.rb +++ b/test/unit/spec/agent_spec.rb @@ -8,6 +8,12 @@ let(:chef_run) do # runner.node.set['logstash'] ... runner.node.automatic['memory']['total'] = '1024kB' + runner.node.set['logstash']['instance']['agent']['basedir'] = '/opt/logstash' + runner.node.set['logstash']['instance']['agent']['user'] = 'logstash' + runner.node.set['logstash']['instance']['agent']['group'] = 'logstash' + runner.node.set['logstash']['instance']['agent']['config_templates_cookbook'] = 'logstash' + runner.node.set['logstash']['instance']['agent']['elasticsearch_ip'] = '127.0.0.1' + runner.node.set['logstash']['instance']['agent']['enable_embedded_es'] = false runner.converge(described_recipe) end include_context 'stubs-common' diff --git a/test/unit/spec/index_cleaner_spec.rb b/test/unit/spec/index_cleaner_spec.rb deleted file mode 100644 index fb35151..0000000 --- a/test/unit/spec/index_cleaner_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -# Encoding: utf-8 -require_relative 'spec_helper' - -describe 'logstash::index_cleaner' do - describe 'ubuntu' do - let(:runner) { ChefSpec::Runner.new(::UBUNTU_OPTS) } - let(:node) { runner.node } - let(:chef_run) do - # runner.node.set['logstash'] ... - runner.node.automatic['memory']['total'] = '1024kB' - runner.converge(described_recipe) - end - include_context 'stubs-common' - - it 'installs elasticsearch_curator' do - expect(chef_run).to install_python_pip('elasticsearch-curator') - end - - it 'creates a cronjob' do - expect(chef_run).to create_cron('logstash_index_cleaner') - end - - end -end diff --git a/test/unit/spec/lwrp_config_spec.rb b/test/unit/spec/lwrp_config_spec.rb index 20df915..42e1dad 100644 --- a/test/unit/spec/lwrp_config_spec.rb +++ b/test/unit/spec/lwrp_config_spec.rb @@ -25,7 +25,7 @@ end include_context 'stubs-common' - it 'installs the config template' do + it 'installs the output_stdout template' do expect(chef_run).to create_template('/opt/logstash/server/etc/conf.d/output_stdout.conf').with( source: 'config/output_stdout.conf.erb', cookbook: 'logstash', @@ -40,5 +40,21 @@ ) end + it 'installs the input_syslog template' do + expect(chef_run).to create_template('/opt/logstash/server/etc/conf.d/input_syslog.conf').with( + source: 'config/input_syslog.conf.erb', + cookbook: 'logstash', + action: [:create] + ) + end + + it 'installs the output_elasticsearch template' do + expect(chef_run).to create_template('/opt/logstash/server/etc/conf.d/output_elasticsearch.conf').with( + source: 'config/output_elasticsearch.conf.erb', + cookbook: 'logstash', + action: [:create] + ) + end + end end diff --git a/test/unit/spec/server_spec.rb b/test/unit/spec/server_spec.rb index 9f666b6..8849b7f 100644 --- a/test/unit/spec/server_spec.rb +++ b/test/unit/spec/server_spec.rb @@ -7,8 +7,13 @@ let(:node) { runner.node } let(:chef_run) do # runner.node.set['logstash'] ... - runner.node.set['logstash']['instance']['server']['enable_embedded_es'] = true runner.node.automatic['memory']['total'] = '1024kB' + runner.node.set['logstash']['instance']['server']['basedir'] = '/opt/logstash' + runner.node.set['logstash']['instance']['server']['user'] = 'logstash' + runner.node.set['logstash']['instance']['server']['group'] = 'logstash' + runner.node.set['logstash']['instance']['server']['config_templates_cookbook'] = 'logstash' + runner.node.set['logstash']['instance']['server']['elasticsearch_ip'] = '127.0.0.1' + runner.node.set['logstash']['instance']['server']['enable_embedded_es'] = true runner.converge(described_recipe) end include_context 'stubs-common' @@ -34,5 +39,9 @@ expect(chef_run).to create_logstash_plugins('contrib') end + it 'calls the logstash_curator LWRP' do + expect(chef_run).to create_logstash_curator('server') + end + end -end +end \ No newline at end of file From d00708ca0ed39c5a804eb6effcaf19093b22b51d Mon Sep 17 00:00:00 2001 From: paulczar Date: Sat, 10 May 2014 16:51:56 -0500 Subject: [PATCH 37/55] fixing tests, and passing tests. --- .gitignore | 1 + .kitchen.yml | 1 - .rubocop.yml | 16 ++-- .travis.yml | 7 +- Berksfile | 2 +- Gemfile.lock | 82 +++++++++---------- Rakefile | 14 ++-- libraries/logstash_conf.rb | 14 ++-- providers/config.rb | 4 +- providers/curator.rb | 46 ++++++++--- providers/pattern.rb | 2 +- providers/plugins.rb | 4 +- providers/service.rb | 2 +- recipes/beaver.rb | 4 +- recipes/server.rb | 2 +- resources/curator.rb | 4 +- .../server/serverspec/server_spec.rb | 37 ++++++++- test/unit/spec/agent_spec.rb | 2 +- test/unit/spec/server_spec.rb | 4 +- 19 files changed, 150 insertions(+), 98 deletions(-) diff --git a/.gitignore b/.gitignore index 6362a7b..aa73350 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ vendor/ .kitchen/ .kitchen.local.yml .bundle/ +bin/ \ No newline at end of file diff --git a/.kitchen.yml b/.kitchen.yml index 417ca4f..78af441 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -21,7 +21,6 @@ suites: - recipe[apt::default] - recipe[java::default] - recipe[logstash::server] - - recipe[logstash::index_cleaner] attributes: logstash: instance: diff --git a/.rubocop.yml b/.rubocop.yml index 65f6e11..874ad9b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,12 +1,13 @@ AllCops: Exclude: - - vendor - - test/support/** + - 'vendor/**/*' + - 'test/support/**' + - 'bin/**/*' Include: - - Rakefile - - Gemfile - - Vagrantfile - - Berksfile + - '**/Rakefile' + - '**/Gemfile' + - '**/Vagrantfile' + - '**/Berksfile' CyclomaticComplexity: Enabled: false @@ -35,3 +36,6 @@ Documentation: MethodLength: Max: 50 + +SingleSpaceBeforeFirstArg: + Enabled: false \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 6d9c270..bad871f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: ruby -gemfile: rvm: - 1.9.3 -install: bundle install -script: bundle exec berks install -script: bundle exec rake style spec \ No newline at end of file +install: bundle install --path vendor --binstubs +script: bin/berks install +script: bin/rake style spec \ No newline at end of file diff --git a/Berksfile b/Berksfile index dff6d58..c56fd81 100644 --- a/Berksfile +++ b/Berksfile @@ -1,6 +1,6 @@ # Encoding: utf-8 -#source 'https://api.berkshelf.com' if Gem::Version.new(Berkshelf::VERSION) > Gem::Version.new('3') +source 'https://api.berkshelf.com' if Gem::Version.new(Berkshelf::VERSION) > Gem::Version.new('3') metadata diff --git a/Gemfile.lock b/Gemfile.lock index e0883fe..f710d0e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,17 +1,17 @@ GEM remote: https://rubygems.org/ specs: - activesupport (3.2.17) + activesupport (3.2.18) i18n (~> 0.6, >= 0.6.4) multi_json (~> 1.0) - addressable (2.3.5) - akami (1.2.1) + addressable (2.3.6) + akami (1.2.2) gyoku (>= 0.4.0) nokogiri - ast (1.1.0) - atomic (1.1.15) + ast (2.0.0) + atomic (1.1.16) avl_tree (1.1.3) - berkshelf (2.0.14) + berkshelf (2.0.16) activesupport (~> 3.2.0) addressable (~> 2.3.4) buff-shell_out (~> 0.1) @@ -22,8 +22,8 @@ GEM minitar (~> 0.5.4) rbzip2 (~> 0.2.0) retryable (~> 1.3.3) - ridley (~> 1.5.0) - solve (>= 0.5.0) + ridley (~> 1.7.0) + solve (~> 0.8.2) thor (~> 0.18.0) buff-config (0.4.0) buff-extensions (~> 0.3) @@ -36,11 +36,11 @@ GEM buff-ruby_engine (~> 0.1.0) builder (3.2.2) cabin (0.6.1) - celluloid (0.14.1) - timers (>= 1.0.0) - celluloid-io (0.14.1) - celluloid (>= 0.14.1) - nio4r (>= 0.4.5) + celluloid (0.15.2) + timers (~> 1.1.0) + celluloid-io (0.15.0) + celluloid (>= 0.15.0) + nio4r (>= 0.5.0) chef (11.10.4) chef-zero (~> 1.7, >= 1.7.2) diff-lcs (~> 1.2, >= 1.2.4) @@ -80,7 +80,7 @@ GEM erubis (2.7.0) faraday (0.8.9) multipart-post (~> 1.2.0) - fauxhai (2.1.0) + fauxhai (2.1.2) net-ssh ohai ffi (1.9.3) @@ -110,7 +110,7 @@ GEM rubocop (~> 0.10) gyoku (1.1.1) builder (>= 2.1.2) - hashie (2.0.5) + hashie (2.1.1) highline (1.6.21) hitimes (1.2.1) httpclient (2.3.4.1) @@ -120,7 +120,7 @@ GEM insist (1.0.0) ipaddress (0.8.0) json (1.8.1) - kitchen-vagrant (0.14.0) + kitchen-vagrant (0.15.0) test-kitchen (~> 1.0) listen (1.3.1) rb-fsevent (>= 0.9.3) @@ -130,7 +130,7 @@ GEM logging (1.8.2) little-plugger (>= 1.1.3) multi_json (>= 1.8.4) - lumberjack (1.0.4) + lumberjack (1.0.5) method_source (0.8.2) metriks (0.9.9.6) atomic (~> 1.0) @@ -140,18 +140,18 @@ GEM minitar (0.5.4) mixlib-authentication (1.3.0) mixlib-log - mixlib-cli (1.4.0) + mixlib-cli (1.5.0) mixlib-config (2.1.0) mixlib-log (1.6.0) - mixlib-shellout (1.3.0) + mixlib-shellout (1.4.0) moneta (0.6.0) - multi_json (1.9.0) + multi_json (1.10.0) multipart-post (1.2.0) mustache (0.99.5) net-http-persistent (2.9.4) - net-scp (1.1.2) + net-scp (1.2.1) net-ssh (>= 2.6.5) - net-ssh (2.8.0) + net-ssh (2.9.0) net-ssh-gateway (1.2.0) net-ssh (>= 2.6.5) net-ssh-multi (1.2.0) @@ -160,7 +160,7 @@ GEM nio4r (1.0.0) nokogiri (1.5.11) nori (1.1.5) - ohai (6.20.0) + ohai (6.22.0) ipaddress mixlib-cli mixlib-config @@ -168,10 +168,10 @@ GEM mixlib-shellout systemu (~> 2.5.2) yajl-ruby - parser (2.1.7) - ast (~> 1.1) + parser (2.1.9) + ast (>= 1.1, < 3.0) slop (~> 3.4, >= 3.4.5) - pleaserun (0.0.4) + pleaserun (0.0.7) cabin (> 0) cabin clamp @@ -189,9 +189,9 @@ GEM rack (~> 1.2) rack (1.5.2) rainbow (2.0.0) - rake (10.1.1) + rake (10.3.1) rb-fsevent (0.9.4) - rb-inotify (0.9.3) + rb-inotify (0.9.4) ffi (>= 0.5.0) rb-kqueue (0.2.2) ffi (>= 0.5.0) @@ -199,14 +199,14 @@ GEM rest-client (1.6.7) mime-types (>= 1.16) retryable (1.3.5) - ridley (1.5.3) + ridley (1.7.1) addressable buff-config (~> 0.2) buff-extensions (~> 0.3) buff-ignore (~> 1.1) buff-shell_out (~> 0.1) - celluloid (~> 0.14.0) - celluloid-io (~> 0.14.0) + celluloid (~> 0.15) + celluloid-io (~> 0.15) erubis faraday (>= 0.8.4) hashie (>= 2.0.2) @@ -214,7 +214,6 @@ GEM mixlib-authentication (>= 1.3.0) net-http-persistent (>= 2.8) net-ssh - nio4r (>= 0.5.0) retryable solve (>= 0.4.4) varia_model (~> 0.1) @@ -227,15 +226,15 @@ GEM rspec-expectations (2.14.5) diff-lcs (>= 1.1.3, < 2.0) rspec-mocks (2.14.6) - rubocop (0.19.0) + rubocop (0.21.0) json (>= 1.7.7, < 2) - parser (~> 2.1.7) + parser (~> 2.1.9) powerpack (~> 0.0.6) rainbow (>= 1.99.1, < 3.0) ruby-progressbar (~> 1.4) ruby-progressbar (1.4.2) rubyntlm (0.1.1) - safe_yaml (1.0.1) + safe_yaml (1.0.3) savon (0.9.5) akami (~> 1.0) builder (>= 2.1.2) @@ -244,16 +243,16 @@ GEM nokogiri (>= 1.4.0) nori (~> 1.0) wasabi (~> 1.0) - serverspec (0.15.4) + serverspec (0.16.0) highline net-ssh - rspec (>= 2.13.0) + rspec (~> 2.13) specinfra (>= 0.7.1) slop (3.5.0) solve (0.8.2) - specinfra (0.7.1) - strainer (3.3.0) - berkshelf (~> 2.0) + specinfra (1.12.0) + strainer (3.4.0) + berkshelf (>= 2.0, < 4.0) buff-platform (~> 0.1) stud (0.0.17) ffi @@ -266,8 +265,7 @@ GEM safe_yaml (~> 1.0) thor (~> 0.18) thor (0.18.1) - timers (2.0.0) - hitimes + timers (1.1.0) treetop (1.4.15) polyglot polyglot (>= 0.3.1) diff --git a/Rakefile b/Rakefile index 7201d99..8d53648 100644 --- a/Rakefile +++ b/Rakefile @@ -25,16 +25,16 @@ end require 'rspec/core/rake_task' desc 'Run ChefSpec unit tests' -RSpec::Core::RakeTask.new(:spec) do |t, args| +RSpec::Core::RakeTask.new(:spec) do |t, _args| t.rspec_opts = 'test/unit/spec' end # The default rake task should just run it all task default: %w(style spec integration) -begin - require 'kitchen/rake_tasks' - Kitchen::RakeTasks.new -rescue LoadError - puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI'] -end +# begin + # require 'kitchen/rake_tasks' + # Kitchen::RakeTasks.new +# rescue LoadError + # puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI'] +# end diff --git a/libraries/logstash_conf.rb b/libraries/logstash_conf.rb index e74e8f5..3951530 100644 --- a/libraries/logstash_conf.rb +++ b/libraries/logstash_conf.rb @@ -4,8 +4,6 @@ # Evaluate objects for logstash config file. class Erubis::RubyEvaluator::LogstashConf - private - def self.key_to_str(k) case k.class.to_s when 'String' @@ -39,7 +37,7 @@ def self.value_to_str(v, indent = 0) when String, Symbol, Fixnum, Float "\"#{v}\"" when Array - "[#{v.map { |e| value_to_str(e, indent) }.join(", ")}]" + "[#{v.map { |e| value_to_str(e, indent) }.join(', ')}]" when Hash, Mash hash_to_str(v, indent) + "\n" + indent(indent + 4) + '}' when TrueClass, FalseClass @@ -50,11 +48,11 @@ def self.value_to_str(v, indent = 0) end def self.key_value_to_str(k, v, indent = 0) - if !v.nil? + if v.nil? + key_to_str(k) + else # k.inspect + " => " + v.inspect key_to_str(k) + ' => ' + value_to_str(v, indent) - else - key_to_str(k) end end @@ -65,7 +63,7 @@ def self.plugin_to_arr(plugin, patterns_dir_plugins = nil, patterns_dir = nil, i # result << " if [type] == \"#{hash['type']}\" {" if hash.has_key?('type') and type_to_condition indent += 4 result << indent(indent) + name.to_s + ' {' - result << indent(indent) + key_value_to_str('patterns_dir', patterns_dir, indent) if patterns_dir_plugins.include?(name.to_s) && !patterns_dir.nil? && !hash.key?('patterns_dir') + result << indent(indent) + key_value_to_str('patterns_dir', patterns_dir, indent) if patterns_dir_plugins.include?(name.to_s) && patterns_dir && !hash.key?('patterns_dir') hash.sort.each do |k, v| # next if k == 'type' and type_to_condition indent += 4 @@ -79,8 +77,6 @@ def self.plugin_to_arr(plugin, patterns_dir_plugins = nil, patterns_dir = nil, i return result.join("\n") end - public - def self.section_to_str(section, version = nil, patterns_dir = nil, indent = 0) result = [] patterns_dir_plugins = ['grok'] diff --git a/providers/config.rb b/providers/config.rb index 2a83d1c..9f81d31 100644 --- a/providers/config.rb +++ b/providers/config.rb @@ -32,8 +32,8 @@ def load_current_resource action :create do conf = conf_vars # Chef::Log.info("config vars: #{conf.inspect}") - conf[:templates].each do |template, file| - tp = template"#{conf[:path]}/#{::File.basename(file).chomp(::File.extname(file))}" do + conf[:templates].each do |_template, file| + tp = template "#{conf[:path]}/#{::File.basename(file).chomp(::File.extname(file))}" do source file cookbook conf[:templates_cookbook] owner conf[:owner] diff --git a/providers/curator.rb b/providers/curator.rb index dfb1c54..2ea5e2c 100644 --- a/providers/curator.rb +++ b/providers/curator.rb @@ -25,14 +25,13 @@ def load_current_resource @user = new_resource.user || attributes['user'] || defaults['user'] end - action :create do - ls_instance = @instance - ls_days_to_keep = @days_to_keep - ls_log_file = @log_file - ls_hour = @hour - ls_minute = @minute - ls_user = @user + cur_instance = @instance + cur_days_to_keep = @days_to_keep + cur_log_file = @log_file + cur_hour = @hour + cur_minute = @minute + cur_user = @user @run_context.include_recipe 'python::pip' @@ -41,14 +40,37 @@ def load_current_resource end new_resource.updated_by_last_action(pi.updated_by_last_action?) - cr = cron 'logstash_index_cleaner' do - command "curator --host #{::Logstash.service_ip(node, ls_instance, 'elasticsearch')} -d #{ls_days_to_keep} &> #{ls_log_file}" - user ls_user - minute ls_minute - hour ls_hour + cr = cron "curator-#{cur_instance}" do + command "curator --host #{::Logstash.service_ip(node, cur_instance, 'elasticsearch')} -d #{cur_days_to_keep} &> #{cur_log_file}" + user cur_user + minute cur_minute + hour cur_hour + action [:create] end new_resource.updated_by_last_action(cr.updated_by_last_action?) end +action :delete do + cur_instance = @instance + cur_days_to_keep = @days_to_keep + cur_log_file = @log_file + cur_hour = @hour + cur_minute = @minute + cur_user = @user + + @run_context.include_recipe 'python::pip' + pi = python_pip 'elasticsearch-curator' do + action :install + end + new_resource.updated_by_last_action(pi.updated_by_last_action?) + cr = cron "curator-#{cur_instance}" do + command "curator --host #{::Logstash.service_ip(node, cur_instance, 'elasticsearch')} -d #{cur_days_to_keep} &> #{cur_log_file}" + user cur_user + minute cur_minute + hour cur_hour + action [:delete] + end + new_resource.updated_by_last_action(cr.updated_by_last_action?) +end diff --git a/providers/pattern.rb b/providers/pattern.rb index 43e3bd2..829d306 100644 --- a/providers/pattern.rb +++ b/providers/pattern.rb @@ -31,7 +31,7 @@ def load_current_resource action :create do pattern = pattern_vars # Chef::Log.info("config vars: #{pattern.inspect}") - pattern[:templates].each do |template, file| + pattern[:templates].each do |_template, file| tp = template "#{pattern[:path]}/#{::File.basename(file).chomp(::File.extname(file))}" do source file cookbook pattern[:templates_cookbook] diff --git a/providers/plugins.rb b/providers/plugins.rb index c0e9e96..167cc09 100644 --- a/providers/plugins.rb +++ b/providers/plugins.rb @@ -51,7 +51,7 @@ def load_current_resource cwd ls_instance_dir notifies :restart, "logstash_service[#{ls_instance}]" # this is a temp workaround to make the plugin command idempotent. - not_if { ::File.exists?("#{ls_instance_dir}/#{ls_install_check}") } + not_if { ::File.exist?("#{ls_instance_dir}/#{ls_install_check}") } end new_resource.updated_by_last_action(ex.updated_by_last_action?) when 'tarball' @@ -68,7 +68,7 @@ def load_current_resource action [:put] notifies :restart, "logstash_service[#{ls_instance}]" # this is a temp workaround to ensure idempotent. - not_if { ::File.exists?("#{ls_instance_dir}/#{ls_install_check}") } + not_if { ::File.exist?("#{ls_instance_dir}/#{ls_install_check}") } end new_resource.updated_by_last_action(arkit.updated_by_last_action?) else diff --git a/providers/service.rb b/providers/service.rb index 4aac389..7099ccf 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -104,7 +104,7 @@ def load_current_resource user svc[:user] group svc[:group] action :create - not_if { ::File.exists?("/etc/init.d/#{svc[:service_name]}") } + not_if { ::File.exist?("/etc/init.d/#{svc[:service_name]}") } end new_resource.updated_by_last_action(pr.updated_by_last_action?) when 'runit' diff --git a/recipes/beaver.rb b/recipes/beaver.rb index ffbc548..4b1a5bb 100644 --- a/recipes/beaver.rb +++ b/recipes/beaver.rb @@ -28,7 +28,7 @@ logstash_server_ip = nil if Chef::Config[:solo] logstash_server_ip = node['logstash']['beaver']['server_ipaddress'] if node['logstash']['beaver']['server_ipaddress'] -elsif !node['logstash']['beaver']['server_ipaddress'].nil? +elsif node['logstash']['beaver']['server_ipaddress'] logstash_server_ip = node['logstash']['beaver']['server_ipaddress'] elsif node['logstash']['beaver']['server_role'] logstash_server_results = search(:node, "roles:#{node['logstash']['beaver']['server_role']}") @@ -51,7 +51,7 @@ owner node['logstash']['user'] group node['logstash']['group'] recursive true - not_if { ::File.exists?(dir) } + not_if { ::File.exist?(dir) } end end diff --git a/recipes/server.rb b/recipes/server.rb index 4c821de..b6725c3 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -59,4 +59,4 @@ logstash_curator 'server' do action [:create] -end \ No newline at end of file +end diff --git a/resources/curator.rb b/resources/curator.rb index 0e05a8e..9d3bf70 100644 --- a/resources/curator.rb +++ b/resources/curator.rb @@ -5,7 +5,7 @@ # Copyright 2014, John E. Vincent # License:: Apache 2.0 -actions :create +actions :create, :delete default_action :create if defined?(default_action) @@ -14,4 +14,4 @@ attribute :minute, kind_of: String attribute :hour, kind_of: String attribute :log_file, kind_of: String -attribute :user, kind_of: String \ No newline at end of file +attribute :user, kind_of: String diff --git a/test/integration/server/serverspec/server_spec.rb b/test/integration/server/serverspec/server_spec.rb index c4c74e4..3e407a2 100644 --- a/test/integration/server/serverspec/server_spec.rb +++ b/test/integration/server/serverspec/server_spec.rb @@ -1,11 +1,44 @@ # Encoding: utf-8 require 'spec_helper' -describe 'logstash server' do - it { pending 'it writes the tests for the code or else it gets the hose again' } +# Java 1.6 +describe package('openjdk-6-jre') do + it { should be_installed } end +# Logstash Instance describe service('logstash_server') do it { should be_enabled } it { should be_running } end + +describe user('logstash') do + it { should exist } +end + +# Logstash Config +describe file('/opt/logstash/server/etc/conf.d/input_syslog.conf') do + it { should be_file } +end + +describe file('/opt/logstash/server/etc/conf.d/output_elasticsearch.conf') do + it { should be_file } +end + +describe file('/opt/logstash/server/etc/conf.d/output_stdout.conf') do + it { should be_file } +end + +describe port(9200) do + it { should be_listening } +end + +describe port(5959) do + it { should be_listening } +end + + +# Logstash Curator +describe cron do + it { should have_entry('0 * * * * curator --host 127.0.0.1 -d 31 &> /dev/null').with_user('logstash') } +end \ No newline at end of file diff --git a/test/unit/spec/agent_spec.rb b/test/unit/spec/agent_spec.rb index f80b163..4648138 100644 --- a/test/unit/spec/agent_spec.rb +++ b/test/unit/spec/agent_spec.rb @@ -13,7 +13,7 @@ runner.node.set['logstash']['instance']['agent']['group'] = 'logstash' runner.node.set['logstash']['instance']['agent']['config_templates_cookbook'] = 'logstash' runner.node.set['logstash']['instance']['agent']['elasticsearch_ip'] = '127.0.0.1' - runner.node.set['logstash']['instance']['agent']['enable_embedded_es'] = false + runner.node.set['logstash']['instance']['agent']['enable_embedded_es'] = false runner.converge(described_recipe) end include_context 'stubs-common' diff --git a/test/unit/spec/server_spec.rb b/test/unit/spec/server_spec.rb index 8849b7f..75ca8bf 100644 --- a/test/unit/spec/server_spec.rb +++ b/test/unit/spec/server_spec.rb @@ -13,7 +13,7 @@ runner.node.set['logstash']['instance']['server']['group'] = 'logstash' runner.node.set['logstash']['instance']['server']['config_templates_cookbook'] = 'logstash' runner.node.set['logstash']['instance']['server']['elasticsearch_ip'] = '127.0.0.1' - runner.node.set['logstash']['instance']['server']['enable_embedded_es'] = true + runner.node.set['logstash']['instance']['server']['enable_embedded_es'] = true runner.converge(described_recipe) end include_context 'stubs-common' @@ -44,4 +44,4 @@ end end -end \ No newline at end of file +end From ac6a871730dbf4f338027d5df762f33375d51544 Mon Sep 17 00:00:00 2001 From: Daniel Givens Date: Wed, 14 May 2014 15:21:08 -0500 Subject: [PATCH 38/55] "cluster" needed for elasticsearch output --- recipes/server.rb | 2 ++ templates/default/config/output_elasticsearch.conf.erb | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/recipes/server.rb b/recipes/server.rb index b6725c3..bf1ef07 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -27,6 +27,7 @@ end embedded_es = node['logstash']['instance'][name]['enable_embedded_es'] || node['logstash']['instance']['default']['enable_embedded_es'] +es_cluster = node['logstash']['instance'][name]['elasticsearch_cluster'] || node['logstash']['instance']['default']['elasticsearch_cluster'] my_templates = { 'input_syslog' => 'config/input_syslog.conf.erb', @@ -39,6 +40,7 @@ action [:create] variables( elasticsearch_ip: ::Logstash.service_ip(node, name, 'elasticsearch'), + elasticsearch_cluster: es_cluster, elasticsearch_embedded: embedded_es ) end diff --git a/templates/default/config/output_elasticsearch.conf.erb b/templates/default/config/output_elasticsearch.conf.erb index 1a137a4..826594c 100644 --- a/templates/default/config/output_elasticsearch.conf.erb +++ b/templates/default/config/output_elasticsearch.conf.erb @@ -2,9 +2,9 @@ output { elasticsearch { <% if @elasticsearch_ip -%> host => "<%= @elasticsearch_ip %>" -<% end -%> -<% if @elasticsearch_embedded -%> + cluster => "<%= @elasticsearch_cluster %>" +<% elsif @elasticsearch_embedded -%> embedded => true <% end -%> } -} \ No newline at end of file +} From 48c3ec06e7dfe389167de575ecf67d2383dfd14c Mon Sep 17 00:00:00 2001 From: Paul Czarkowski Date: Thu, 22 May 2014 17:26:25 -0500 Subject: [PATCH 39/55] default tests with java 7 remove pleaserun dependency add logrotate resources to instance lwrp --- .kitchen.yml | 5 +++++ Berksfile | 3 ++- metadata.rb | 2 +- providers/instance.rb | 14 +++++++------- resources/instance.rb | 8 +++++++- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/.kitchen.yml b/.kitchen.yml index 78af441..0085af5 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -22,6 +22,11 @@ suites: - recipe[java::default] - recipe[logstash::server] attributes: + java: + install_flavor: "oracle" + jdk_version: "7" + oracle: + accept_oracle_download_terms: true logstash: instance: server: diff --git a/Berksfile b/Berksfile index c56fd81..c6fa66f 100644 --- a/Berksfile +++ b/Berksfile @@ -5,10 +5,11 @@ source 'https://api.berkshelf.com' if Gem::Version.new(Berkshelf::VERSION) > Gem metadata cookbook 'java' -cookbook 'pleaserun', git: 'https://github.com/paulczar/chef-pleaserun.git' cookbook 'curl' cookbook 'ark' +cookbook 'pleaserun', git: 'https://github.com/paulczar/chef-pleaserun.git' + group :test do cookbook 'minitest-handler', git: 'git://github.com/btm/minitest-handler-cookbook.git' cookbook 'elasticsearch', git: 'git://github.com/elasticsearch/cookbook-elasticsearch.git' diff --git a/metadata.rb b/metadata.rb index 77eb707..4b327f6 100644 --- a/metadata.rb +++ b/metadata.rb @@ -11,7 +11,7 @@ supports os end -%w(build-essential runit git ant java logrotate yum python ark pleaserun).each do |ckbk| +%w(build-essential runit git ant java logrotate yum python ark).each do |ckbk| depends ckbk end diff --git a/providers/instance.rb b/providers/instance.rb index a001825..09ec0de 100644 --- a/providers/instance.rb +++ b/providers/instance.rb @@ -28,14 +28,14 @@ def load_current_resource @java_home = new_resource.java_home @user = new_resource.user || attributes['user'] || defaults['user'] @group = new_resource.group || attributes['group'] || defaults['group'] - @useropts = attributes['user_opts'] || defaults['user_opts'] + @useropts = new_resource.user_opts || attributes['user_opts'] || defaults['user_opts'] @instance_dir = "#{@base_directory}/#{new_resource.name}".clone - @logrotate_size = attributes['logrotate_max_size'] || defaults['logrotate_max_size'] - @logrotate_use_filesize = attributes['logrotate_use_filesize'] || defaults['logrotate_use_filesize'] - @logrotate_frequency = attributes['logrotate_frequency'] || defaults['logrotate_frequency'] - @logrotate_max_backup = attributes['logrotate_max_backup'] || defaults['logrotate_max_backup'] - @logrotate_options = attributes['logrotate_options'] || defaults['logrotate_options'] - @logrotate_enable = attributes['logrotate_enable'] || defaults['logrotate_enable'] + @logrotate_size = new_resource.user_opts || attributes['logrotate_max_size'] || defaults['logrotate_max_size'] + @logrotate_use_filesize = new_resource.logrotate_use_filesize || attributes['logrotate_use_filesize'] || defaults['logrotate_use_filesize'] + @logrotate_frequency = new_resource.logrotate_frequency || attributes['logrotate_frequency'] || defaults['logrotate_frequency'] + @logrotate_max_backup = new_resource.logrotate_max_backup || attributes['logrotate_max_backup'] || defaults['logrotate_max_backup'] + @logrotate_options = new_resource.logrotate_options || attributes['logrotate_options'] || defaults['logrotate_options'] + @logrotate_enable = new_resource.logrotate_enable || attributes['logrotate_enable'] || defaults['logrotate_enable'] end action :delete do diff --git a/resources/instance.rb b/resources/instance.rb index 0ec9b35..5405330 100644 --- a/resources/instance.rb +++ b/resources/instance.rb @@ -23,4 +23,10 @@ attribute :java_home, kind_of: String, default: '/usr/lib/jvm/java-6-openjdk' # openjdk6 on ubuntu attribute :user, kind_of: String attribute :group, kind_of: String -attribute :enable_logrotate, kind_of: [TrueClass, FalseClass] +attribute :logrotate_enable, kind_of: [TrueClass, FalseClass] +attribute :user_opts, kind_of: [Hash] +attribute :logrotate_size, kind_of: [String] +attribute :logrotate_use_filesize, kind_of: [TrueClass, FalseClass] +attribute :logrotate_frequency, kind_of: [String] +attribute :logrotate_max_backup, kind_of: [Integer] +attribute :logrotate_options, kind_of: [String] From 0b928525bcdcd7cfe76a5f316e3e18df69e240e3 Mon Sep 17 00:00:00 2001 From: Niko Kurtti Date: Sat, 24 May 2014 11:02:45 +0300 Subject: [PATCH 40/55] Use the PIDDIR variable for pidfile location, not hard coded (and wrong) value --- templates/default/init/tarball_init.logstash.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/default/init/tarball_init.logstash.erb b/templates/default/init/tarball_init.logstash.erb index 28b1db4..a6a29cf 100644 --- a/templates/default/init/tarball_init.logstash.erb +++ b/templates/default/init/tarball_init.logstash.erb @@ -8,7 +8,7 @@ PIDDIR="/var/run/logstash" -export PIDFILE="/var/run/logstash-<%= @name %>.pid" +export PIDFILE="$PIDDIR/logstash-<%= @name %>.pid" export LS_HOME="<%= @home %>" export LS_HEAP_SIZE="<%= @max_heap %>" export LOGSTASH_OPTS="<%= @args.join(' ') %>" From 6ce8b4aca995ee0835791a0afa28cf896929ec8c Mon Sep 17 00:00:00 2001 From: Niko Kurtti Date: Sun, 25 May 2014 13:28:39 +0300 Subject: [PATCH 41/55] bind host support + searching local nodes ip library method --- attributes/default.rb | 1 + libraries/logstash_util.rb | 16 +++++++++++++--- recipes/server.rb | 8 ++++++++ .../default/config/output_elasticsearch.conf.erb | 3 +++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/attributes/default.rb b/attributes/default.rb index 1541e54..26f0200 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -62,6 +62,7 @@ # roles/flags for various autoconfig/discovery components default['logstash']['instance']['default']['enable_embedded_es'] = false +default['logstash']['instance']['default']['bind_host_interface'] = '' default['logstash']['instance']['default']['inputs'] = [] default['logstash']['instance']['default']['filters'] = [] diff --git a/libraries/logstash_util.rb b/libraries/logstash_util.rb index 79ea36d..ab10933 100644 --- a/libraries/logstash_util.rb +++ b/libraries/logstash_util.rb @@ -2,7 +2,7 @@ # rubocop:disable RedundantReturn module Logstash - def self.service_ip(node, instance = 'default', service = 'elasticsearch') + def self.service_ip(node, instance = 'default', service = 'elasticsearch', interface=nil) if node['logstash']['instance'].key?(instance) attributes = node['logstash']['instance'][instance] defaults = node['logstash']['instance']['default'] @@ -16,10 +16,20 @@ def self.service_ip(node, instance = 'default', service = 'elasticsearch') service_query = attributes["#{service}_query"] || defaults["#{service}_query"] Chef::Search::Query.new.search(:node, service_query) { |o| results << o } if !results.empty? - service_ip = results[0]['ipaddress'] + service_ip = get_ip_from_results(results[0],interface) else - service_ip = attributes["#{service}_ip"] || defaults["#{service}_ip"] + service_ip = attributes["#{service}_ip"] || defaults["#{service}_ip"] end end end + + def self.get_ip_for_node(node, interface) + if interface.nil? + service_ip = node['ipaddress'] + else + service_ip = node['network']['interfaces'][interface]['addresses'].to_hash.find { + |addr, addr_info| addr_info['family'] == 'inet'}.first + end + end + end diff --git a/recipes/server.rb b/recipes/server.rb index bf1ef07..8a41646 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -29,6 +29,13 @@ embedded_es = node['logstash']['instance'][name]['enable_embedded_es'] || node['logstash']['instance']['default']['enable_embedded_es'] es_cluster = node['logstash']['instance'][name]['elasticsearch_cluster'] || node['logstash']['instance']['default']['elasticsearch_cluster'] +bind_host_if = node['logstash']['instance'][name]['bind_host_interface'] || node['logstash']['instance']['default']['bind_host_interface'] +if (!bind_host_if.empty? ) + bind_host = ::Logstash.get_ip_for_node(node, bind_host_if) +else + bind_host = nil +end + my_templates = { 'input_syslog' => 'config/input_syslog.conf.erb', 'output_stdout' => 'config/output_stdout.conf.erb', @@ -40,6 +47,7 @@ action [:create] variables( elasticsearch_ip: ::Logstash.service_ip(node, name, 'elasticsearch'), + bind_host: bind_host, elasticsearch_cluster: es_cluster, elasticsearch_embedded: embedded_es ) diff --git a/templates/default/config/output_elasticsearch.conf.erb b/templates/default/config/output_elasticsearch.conf.erb index 826594c..59cb750 100644 --- a/templates/default/config/output_elasticsearch.conf.erb +++ b/templates/default/config/output_elasticsearch.conf.erb @@ -5,6 +5,9 @@ output { cluster => "<%= @elasticsearch_cluster %>" <% elsif @elasticsearch_embedded -%> embedded => true +<% end -%> +<% if @bind_host -%> + bind_host => "<%= @bind_host %>" <% end -%> } } From 8bd4e9e2158319952ee4fc5e2e7f137b72d725f3 Mon Sep 17 00:00:00 2001 From: Niko Kurtti Date: Sun, 25 May 2014 15:09:18 +0300 Subject: [PATCH 42/55] custom index support --- attributes/default.rb | 1 + recipes/server.rb | 5 ++++- templates/default/config/output_elasticsearch.conf.erb | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/attributes/default.rb b/attributes/default.rb index 26f0200..1baee2e 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -63,6 +63,7 @@ # roles/flags for various autoconfig/discovery components default['logstash']['instance']['default']['enable_embedded_es'] = false default['logstash']['instance']['default']['bind_host_interface'] = '' +default['logstash']['instance']['default']['index'] = nil default['logstash']['instance']['default']['inputs'] = [] default['logstash']['instance']['default']['filters'] = [] diff --git a/recipes/server.rb b/recipes/server.rb index 8a41646..dbfa247 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -28,6 +28,8 @@ embedded_es = node['logstash']['instance'][name]['enable_embedded_es'] || node['logstash']['instance']['default']['enable_embedded_es'] es_cluster = node['logstash']['instance'][name]['elasticsearch_cluster'] || node['logstash']['instance']['default']['elasticsearch_cluster'] +index = node['logstash']['instance'][name]['index'] || node['logstash']['instance']['default']['index'] + bind_host_if = node['logstash']['instance'][name]['bind_host_interface'] || node['logstash']['instance']['default']['bind_host_interface'] if (!bind_host_if.empty? ) @@ -49,7 +51,8 @@ elasticsearch_ip: ::Logstash.service_ip(node, name, 'elasticsearch'), bind_host: bind_host, elasticsearch_cluster: es_cluster, - elasticsearch_embedded: embedded_es + elasticsearch_embedded: embedded_es, + index: index ) end # ^ see `.kitchen.yml` for example attributes to configure templates. diff --git a/templates/default/config/output_elasticsearch.conf.erb b/templates/default/config/output_elasticsearch.conf.erb index 59cb750..df1825d 100644 --- a/templates/default/config/output_elasticsearch.conf.erb +++ b/templates/default/config/output_elasticsearch.conf.erb @@ -8,6 +8,9 @@ output { <% end -%> <% if @bind_host -%> bind_host => "<%= @bind_host %>" +<% end -%> +<% if @index -%> + index => "<%= @index %>" <% end -%> } } From bd46deda3f2ee598b461db6e031f0f8d930ef4de Mon Sep 17 00:00:00 2001 From: Niko Kurtti Date: Thu, 29 May 2014 06:57:39 +0300 Subject: [PATCH 43/55] change index variable name to es_index --- attributes/default.rb | 2 +- recipes/server.rb | 4 ++-- templates/default/config/output_elasticsearch.conf.erb | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/attributes/default.rb b/attributes/default.rb index 1baee2e..8f0f358 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -63,7 +63,7 @@ # roles/flags for various autoconfig/discovery components default['logstash']['instance']['default']['enable_embedded_es'] = false default['logstash']['instance']['default']['bind_host_interface'] = '' -default['logstash']['instance']['default']['index'] = nil +default['logstash']['instance']['default']['es_index'] = nil default['logstash']['instance']['default']['inputs'] = [] default['logstash']['instance']['default']['filters'] = [] diff --git a/recipes/server.rb b/recipes/server.rb index dbfa247..67e2d8a 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -28,7 +28,7 @@ embedded_es = node['logstash']['instance'][name]['enable_embedded_es'] || node['logstash']['instance']['default']['enable_embedded_es'] es_cluster = node['logstash']['instance'][name]['elasticsearch_cluster'] || node['logstash']['instance']['default']['elasticsearch_cluster'] -index = node['logstash']['instance'][name]['index'] || node['logstash']['instance']['default']['index'] +es_index = node['logstash']['instance'][name]['es_index'] || node['logstash']['instance']['default']['es_index'] bind_host_if = node['logstash']['instance'][name]['bind_host_interface'] || node['logstash']['instance']['default']['bind_host_interface'] @@ -52,7 +52,7 @@ bind_host: bind_host, elasticsearch_cluster: es_cluster, elasticsearch_embedded: embedded_es, - index: index + es_index: es_index ) end # ^ see `.kitchen.yml` for example attributes to configure templates. diff --git a/templates/default/config/output_elasticsearch.conf.erb b/templates/default/config/output_elasticsearch.conf.erb index df1825d..86bfa32 100644 --- a/templates/default/config/output_elasticsearch.conf.erb +++ b/templates/default/config/output_elasticsearch.conf.erb @@ -9,8 +9,8 @@ output { <% if @bind_host -%> bind_host => "<%= @bind_host %>" <% end -%> -<% if @index -%> - index => "<%= @index %>" +<% if @es_index -%> + index => "<%= @es_index %>" <% end -%> } } From ba5d293f9a876019c75abd123c397add6827a957 Mon Sep 17 00:00:00 2001 From: Vitaly Gorodetsky Date: Tue, 3 Jun 2014 12:27:52 +0300 Subject: [PATCH 44/55] fixes Vagrantfile --- Vagrantfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Vagrantfile b/Vagrantfile index 2bbbaaa..6ce58ed 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -21,6 +21,9 @@ chef_json = { webserver_listen: '0.0.0.0', webserver: 'nginx', install_type: 'file' + }, + logstash: { + instance: { server: {} } } } From e88bdb98ce4c7f34743fb8c2410136023bdba608 Mon Sep 17 00:00:00 2001 From: Vitaly Gorodetsky Date: Tue, 3 Jun 2014 21:55:28 +0300 Subject: [PATCH 45/55] fixes tests, makes tests pass --- libraries/logstash_util.rb | 12 +++++----- recipes/server.rb | 3 +-- .../server/serverspec/server_spec.rb | 5 ++-- test/unit/spec/lwrp_config_spec.rb | 23 +++++++++++++------ 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/libraries/logstash_util.rb b/libraries/logstash_util.rb index ab10933..91258ee 100644 --- a/libraries/logstash_util.rb +++ b/libraries/logstash_util.rb @@ -2,7 +2,7 @@ # rubocop:disable RedundantReturn module Logstash - def self.service_ip(node, instance = 'default', service = 'elasticsearch', interface=nil) + def self.service_ip(node, instance = 'default', service = 'elasticsearch', interface = nil) if node['logstash']['instance'].key?(instance) attributes = node['logstash']['instance'][instance] defaults = node['logstash']['instance']['default'] @@ -16,9 +16,9 @@ def self.service_ip(node, instance = 'default', service = 'elasticsearch', inter service_query = attributes["#{service}_query"] || defaults["#{service}_query"] Chef::Search::Query.new.search(:node, service_query) { |o| results << o } if !results.empty? - service_ip = get_ip_from_results(results[0],interface) + service_ip = get_ip_from_results(results[0], interface) else - service_ip = attributes["#{service}_ip"] || defaults["#{service}_ip"] + service_ip = attributes["#{service}_ip"] || defaults["#{service}_ip"] end end end @@ -27,9 +27,9 @@ def self.get_ip_for_node(node, interface) if interface.nil? service_ip = node['ipaddress'] else - service_ip = node['network']['interfaces'][interface]['addresses'].to_hash.find { - |addr, addr_info| addr_info['family'] == 'inet'}.first + service_ip = node['network']['interfaces'][interface]['addresses'].to_hash.find do + |_, addr_info| addr_info['family'] == 'inet' + end.first end end - end diff --git a/recipes/server.rb b/recipes/server.rb index 67e2d8a..8f8a6af 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -30,9 +30,8 @@ es_cluster = node['logstash']['instance'][name]['elasticsearch_cluster'] || node['logstash']['instance']['default']['elasticsearch_cluster'] es_index = node['logstash']['instance'][name]['es_index'] || node['logstash']['instance']['default']['es_index'] - bind_host_if = node['logstash']['instance'][name]['bind_host_interface'] || node['logstash']['instance']['default']['bind_host_interface'] -if (!bind_host_if.empty? ) +if !bind_host_if.empty? bind_host = ::Logstash.get_ip_for_node(node, bind_host_if) else bind_host = nil diff --git a/test/integration/server/serverspec/server_spec.rb b/test/integration/server/serverspec/server_spec.rb index 3e407a2..ff3a6eb 100644 --- a/test/integration/server/serverspec/server_spec.rb +++ b/test/integration/server/serverspec/server_spec.rb @@ -1,7 +1,7 @@ # Encoding: utf-8 require 'spec_helper' -# Java 1.6 +# Java 1.6 describe package('openjdk-6-jre') do it { should be_installed } end @@ -37,8 +37,7 @@ it { should be_listening } end - # Logstash Curator describe cron do it { should have_entry('0 * * * * curator --host 127.0.0.1 -d 31 &> /dev/null').with_user('logstash') } -end \ No newline at end of file +end diff --git a/test/unit/spec/lwrp_config_spec.rb b/test/unit/spec/lwrp_config_spec.rb index 42e1dad..d6ae02f 100644 --- a/test/unit/spec/lwrp_config_spec.rb +++ b/test/unit/spec/lwrp_config_spec.rb @@ -32,11 +32,7 @@ owner: 'logstash', group: 'logstash', mode: '0644', - variables: { - elasticsearch_ip: '127.0.0.1', - elasticsearch_embedded: true - }, - action: [:create] + action: [:create] ) end @@ -44,7 +40,10 @@ expect(chef_run).to create_template('/opt/logstash/server/etc/conf.d/input_syslog.conf').with( source: 'config/input_syslog.conf.erb', cookbook: 'logstash', - action: [:create] + owner: 'logstash', + group: 'logstash', + mode: '0644', + action: [:create] ) end @@ -52,7 +51,17 @@ expect(chef_run).to create_template('/opt/logstash/server/etc/conf.d/output_elasticsearch.conf').with( source: 'config/output_elasticsearch.conf.erb', cookbook: 'logstash', - action: [:create] + owner: 'logstash', + group: 'logstash', + mode: '0644', + variables: { + elasticsearch_ip: '127.0.0.1', + bind_host: nil, + elasticsearch_cluster: 'logstash', + elasticsearch_embedded: true, + es_index: nil + }, + action: [:create] ) end From d906cc86951bf5a17adaf9aba570fa76ac17d724 Mon Sep 17 00:00:00 2001 From: "Bernhard K. Weisshuhn" Date: Thu, 12 Jun 2014 11:59:46 +0200 Subject: [PATCH 46/55] use correct name for get_ip_for_node() --- libraries/logstash_util.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/logstash_util.rb b/libraries/logstash_util.rb index 91258ee..42c5809 100644 --- a/libraries/logstash_util.rb +++ b/libraries/logstash_util.rb @@ -16,7 +16,7 @@ def self.service_ip(node, instance = 'default', service = 'elasticsearch', inter service_query = attributes["#{service}_query"] || defaults["#{service}_query"] Chef::Search::Query.new.search(:node, service_query) { |o| results << o } if !results.empty? - service_ip = get_ip_from_results(results[0], interface) + service_ip = get_ip_for_node(results[0], interface) else service_ip = attributes["#{service}_ip"] || defaults["#{service}_ip"] end From b6926594a4a177d1e689c9e5ba55b12a6031c37b Mon Sep 17 00:00:00 2001 From: "Bernhard K. Weisshuhn" Date: Fri, 13 Jun 2014 18:42:20 +0200 Subject: [PATCH 47/55] test not for openjdk package version but for java -version we switched to using oracle java 1.7 in 48c3ec06 --- test/integration/server/serverspec/server_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/server/serverspec/server_spec.rb b/test/integration/server/serverspec/server_spec.rb index ff3a6eb..eed8f90 100644 --- a/test/integration/server/serverspec/server_spec.rb +++ b/test/integration/server/serverspec/server_spec.rb @@ -2,8 +2,8 @@ require 'spec_helper' # Java 1.6 -describe package('openjdk-6-jre') do - it { should be_installed } +describe command('java -version') do + it { should return_stdout /java version "1.7.\d+_\d+"/ } end # Logstash Instance From 2e9ed14d1b29a89b4ba92de402339345594a9fe3 Mon Sep 17 00:00:00 2001 From: Matt Chun-Lum Date: Sun, 15 Jun 2014 13:15:27 -0700 Subject: [PATCH 48/55] remove duplicate plugin_install_type attribute definition, move comment --- attributes/default.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/attributes/default.rb b/attributes/default.rb index 8f0f358..ee54563 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -26,11 +26,10 @@ default['logstash']['instance']['default']['checksum'] = 'a1db8eda3d8bf441430066c384578386601ae308ccabf5d723df33cee27304b4' default['logstash']['instance']['default']['install_type'] = 'tarball' -default['logstash']['instance']['default']['plugins_install_type'] = 'native' # native|tarball default['logstash']['instance']['default']['plugins_version'] = '1.4.1' default['logstash']['instance']['default']['plugins_source_url'] = 'https://download.elasticsearch.org/logstash/logstash/logstash-contrib-1.4.1.tar.gz' default['logstash']['instance']['default']['plugins_checksum'] = 'beb0927351a3c298cd346225950c8d4fbda984ba54252d8a2f244329207c31e2' -default['logstash']['instance']['default']['plugins_install_type'] = 'tarball' +default['logstash']['instance']['default']['plugins_install_type'] = 'tarball' # native|tarball default['logstash']['instance']['default']['plugins_check_if_installed'] = 'lib/logstash/filters/translate.rb' default['logstash']['instance']['default']['log_file'] = 'logstash.log' From 8ca495dab7f18df88ad1180e5b7a3c13643cf488 Mon Sep 17 00:00:00 2001 From: "Bernhard K. Weisshuhn" Date: Wed, 18 Jun 2014 15:21:05 +0200 Subject: [PATCH 49/55] add cookbook parameter to runit resource --- providers/service.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/providers/service.rb b/providers/service.rb index 7099ccf..e88092b 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -127,6 +127,7 @@ def load_current_resource web_address: svc[:web_address], web_port: svc[:web_port] ) + cookbook svc[:templates_cookbook] end new_resource.updated_by_last_action(ri.updated_by_last_action?) when 'native' From 9934b47ba950006d89d3dbbe19091bcc55035297 Mon Sep 17 00:00:00 2001 From: Niko Kurtti Date: Tue, 24 Jun 2014 10:43:43 +0300 Subject: [PATCH 50/55] add java_opts and ipv4_only to tarball+java init scripts --- providers/service.rb | 2 ++ templates/default/init/java_init.logstash.erb | 2 +- templates/default/init/tarball_init.logstash.erb | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/providers/service.rb b/providers/service.rb index e88092b..759d879 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -258,6 +258,8 @@ def svc_vars log_file: @log_file, max_heap: @max_heap, min_heap: @min_heap, + java_opts: @java_opts, + ipv4_only: @ipv4_only, workers: @workers, debug: @debug, install_type: @install_type, diff --git a/templates/default/init/java_init.logstash.erb b/templates/default/init/java_init.logstash.erb index f227d48..b0ed39b 100755 --- a/templates/default/init/java_init.logstash.erb +++ b/templates/default/init/java_init.logstash.erb @@ -14,7 +14,7 @@ export LS_CONFIG="<%= @config_file %>" LS_USER="<%= @user %>" LS_LOG="<%= @log_file %>" LOGDIR="<%= ::File.dirname @log_file %>" -export JAVA_OPTS="-server -Xms<%= @min_heap %> -Xmx<%= @max_heap %> -Djava.io.tmpdir=$LS_HOME/tmp/" +export JAVA_OPTS="-server -Xms<%= @min_heap %> -Xmx<%= @max_heap %> -Djava.io.tmpdir=$LS_HOME/tmp/ <%= @java_opts %> <%= '-Djava.net.preferIPv4Stack=true' if @ipv4_only %>" BIN_SCRIPT="/usr/bin/env java $JAVA_OPTS -jar $LS_HOME/lib/logstash.jar agent -f $LS_CONFIG > $LS_LOG 2>&1 & echo \$! > $PIDFILE" if [ -f /etc/init.d/functions ] ; then diff --git a/templates/default/init/tarball_init.logstash.erb b/templates/default/init/tarball_init.logstash.erb index a6a29cf..7991ec5 100644 --- a/templates/default/init/tarball_init.logstash.erb +++ b/templates/default/init/tarball_init.logstash.erb @@ -15,7 +15,7 @@ export LOGSTASH_OPTS="<%= @args.join(' ') %>" LS_USER="<%= @user %>" LS_LOG="<%= @log_file %>" LOGDIR="<%= ::File.dirname @log_file %>" -export JAVA_OPTS="-server -Xms<%= @min_heap %> -Xmx<%= @max_heap %> -Djava.io.tmpdir=$LS_HOME/tmp/" +export JAVA_OPTS="-server -Xms<%= @min_heap %> -Xmx<%= @max_heap %> -Djava.io.tmpdir=$LS_HOME/tmp/ <%= @java_opts %> <%= '-Djava.net.preferIPv4Stack=true' if @ipv4_only %>" BIN_SCRIPT="/usr/bin/env $LS_HOME/bin/logstash $LOGSTASH_OPTS > $LS_LOG 2>&1 & echo \$! > $PIDFILE" if [ -f /etc/init.d/functions ] ; then From 9b1b1fbc23a83fd1ed7b78c093950f2aa30fce35 Mon Sep 17 00:00:00 2001 From: Paul Czarkowski Date: Tue, 8 Jul 2014 10:48:24 -0500 Subject: [PATCH 51/55] fixing tests and small pieces before the big merge --- Berksfile | 6 +- Gemfile | 48 ++- Gemfile.lock | 289 +++++++----------- Rakefile | 14 +- libraries/logstash_conf.rb | 5 - libraries/matchers.rb | 2 +- providers/service.rb | 8 +- recipes/server.rb | 27 +- .../config/output_elasticsearch.conf.erb | 5 +- .../server/serverspec/server_spec.rb | 2 +- test/unit/spec/beaver_spec.rb | 2 +- test/unit/spec/default_spec.rb | 2 +- test/unit/spec/haproxy_spec.rb | 2 +- test/unit/spec/pyshipper_spec.rb | 2 +- test/unit/spec/source_spec.rb | 2 +- test/unit/spec/spec_helper.rb | 9 +- test/unit/spec/zero_mq_repo.rb | 2 +- 17 files changed, 177 insertions(+), 250 deletions(-) diff --git a/Berksfile b/Berksfile index c6fa66f..305b85c 100644 --- a/Berksfile +++ b/Berksfile @@ -11,7 +11,7 @@ cookbook 'ark' cookbook 'pleaserun', git: 'https://github.com/paulczar/chef-pleaserun.git' group :test do - cookbook 'minitest-handler', git: 'git://github.com/btm/minitest-handler-cookbook.git' - cookbook 'elasticsearch', git: 'git://github.com/elasticsearch/cookbook-elasticsearch.git' - cookbook 'kibana', git: 'git://github.com/lusis/chef-kibana.git' + cookbook 'minitest-handler', git: 'https://github.com/btm/minitest-handler-cookbook.git' + cookbook 'elasticsearch', git: 'https://github.com/elasticsearch/cookbook-elasticsearch.git' + cookbook 'kibana', git: 'https://github.com/lusis/chef-kibana.git' end diff --git a/Gemfile b/Gemfile index 006d6ef..b88a705 100644 --- a/Gemfile +++ b/Gemfile @@ -1,29 +1,27 @@ -# Encoding: utf-8 - source 'https://rubygems.org' -gem 'chefspec', '~> 3.2' -gem 'foodcritic', '~> 3.0' -gem 'rubocop', '~> 0.18' -gem 'chef', '~> 11.8' -gem 'berkshelf', '~> 2.0' -gem 'test-kitchen', '~> 1.1' -gem 'kitchen-vagrant' -gem 'serverspec', '~> 0.14' -gem 'rake' -gem 'strainer', '~> 3.3' - -group :chef_gems do - gem 'pleaserun' -end +gem 'berkshelf', '> 3' -group :development do - gem 'guard', '~> 1.8' - gem 'guard-rubocop', '~> 0.2' - gem 'guard-foodcritic' -end +# Uncomment these lines if you want to live on the Edge: +# +# group :development do +# gem "berkshelf", github: "berkshelf/berkshelf" +# gem "vagrant", github: "mitchellh/vagrant", tag: "v1.5.2" +# end +# +# group :plugins do +# gem "vagrant-berkshelf", github: "berkshelf/vagrant-berkshelf" +# gem "vagrant-omnibus", github: "schisamo/vagrant-omnibus" +# end -group :infinite_loop_solver do - gem 'celluloid', '~> 0.14' - gem 'celluloid-io', '~> 0.14' -end +gem 'chef', '>= 11.8' +gem 'rake', '>= 10.2' +gem 'rubocop', '= 0.23' +gem 'foodcritic', '< 4.0' +gem 'chefspec', '>= 3.4' +gem 'serverspec', '>= 1.6' +gem 'test-kitchen' +gem 'kitchen-vagrant' +gem 'guard', '>= 2.6' +gem 'guard-rubocop', '>= 1.1' +gem 'guard-foodcritic', '>= 1.0.2' diff --git a/Gemfile.lock b/Gemfile.lock index f710d0e..f6c303a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,48 +1,40 @@ GEM remote: https://rubygems.org/ specs: - activesupport (3.2.18) - i18n (~> 0.6, >= 0.6.4) - multi_json (~> 1.0) addressable (2.3.6) - akami (1.2.2) - gyoku (>= 0.4.0) - nokogiri ast (2.0.0) - atomic (1.1.16) - avl_tree (1.1.3) - berkshelf (2.0.16) - activesupport (~> 3.2.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) - chozo (>= 0.6.1) - faraday (~> 0.8.0) - faraday (~> 0.8.5) - hashie (>= 2.0.2) + celluloid (~> 0.16.0.pre) + celluloid-io (~> 0.16.0.pre) + faraday (~> 0.9.0) minitar (~> 0.5.4) - rbzip2 (~> 0.2.0) + octokit (~> 3.0) retryable (~> 1.3.3) - ridley (~> 1.7.0) - solve (~> 0.8.2) - thor (~> 0.18.0) - buff-config (0.4.0) - buff-extensions (~> 0.3) - varia_model (~> 0.1) - buff-extensions (0.5.0) + 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-platform (0.1.0) buff-ruby_engine (0.1.0) buff-shell_out (0.1.1) buff-ruby_engine (~> 0.1.0) - builder (3.2.2) - cabin (0.6.1) - celluloid (0.15.2) - timers (~> 1.1.0) - celluloid-io (0.15.0) - celluloid (>= 0.15.0) - nio4r (>= 0.5.0) - chef (11.10.4) - chef-zero (~> 1.7, >= 1.7.2) + 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) @@ -52,34 +44,31 @@ GEM mixlib-cli (~> 1.4) mixlib-config (~> 2.0) mixlib-log (~> 1.3) - mixlib-shellout (~> 1.3) + mixlib-shellout (~> 1.4) net-ssh (~> 2.6) net-ssh-multi (~> 1.1) - ohai (~> 6.0) + ohai (~> 7.0.4) pry (~> 0.9) - puma (~> 1.6) rest-client (>= 1.0.4, < 1.7.0) yajl-ruby (~> 1.1) - chef-zero (1.7.3) + chef-zero (2.0.2) hashie (~> 2.0) json mixlib-log (~> 1.3) - moneta (< 0.7.0) rack chefspec (3.4.0) chef (~> 11.0) fauxhai (~> 2.0) rspec (~> 2.14) - chozo (0.6.1) - activesupport (>= 3.2.0) - hashie (>= 2.0.2) - multi_json (>= 1.3.0) - clamp (0.6.3) 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.8.9) - multipart-post (~> 1.2.0) + faraday (0.9.0) + multipart-post (>= 1.2, < 3) fauxhai (2.1.2) net-ssh ohai @@ -91,51 +80,34 @@ GEM rake treetop (~> 1.4.10) yajl-ruby (~> 1.1.0) - formatador (0.2.4) + formatador (0.2.5) gherkin (2.11.8) multi_json (~> 1.3) - gssapi (1.0.3) - ffi (>= 1.0.1) - guard (1.8.3) + guard (2.6.1) formatador (>= 0.2.4) - listen (~> 1.3) - lumberjack (>= 1.0.2) - pry (>= 0.9.10) - thor (>= 0.14.6) + 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 (0.2.2) - guard (~> 1.8) - rubocop (~> 0.10) - gyoku (1.1.1) - builder (>= 2.1.2) - hashie (2.1.1) + guard-rubocop (1.1.0) + guard (~> 2.0) + rubocop (~> 0.20) + hashie (2.1.2) highline (1.6.21) hitimes (1.2.1) - httpclient (2.3.4.1) - httpi (0.9.7) - rack - i18n (0.6.9) - insist (1.0.0) ipaddress (0.8.0) json (1.8.1) kitchen-vagrant (0.15.0) test-kitchen (~> 1.0) - listen (1.3.1) + listen (2.7.9) + celluloid (>= 0.15.2) rb-fsevent (>= 0.9.3) rb-inotify (>= 0.9) - rb-kqueue (>= 0.2) - little-plugger (1.1.3) - logging (1.8.2) - little-plugger (>= 1.1.3) - multi_json (>= 1.8.4) - lumberjack (1.0.5) + lumberjack (1.0.9) method_source (0.8.2) - metriks (0.9.9.6) - atomic (~> 1.0) - avl_tree (~> 1.1.2) - hitimes (~> 1.1) mime-types (1.25.1) minitar (0.5.4) mixlib-authentication (1.3.0) @@ -144,14 +116,12 @@ GEM mixlib-config (2.1.0) mixlib-log (1.6.0) mixlib-shellout (1.4.0) - moneta (0.6.0) - multi_json (1.10.0) - multipart-post (1.2.0) - mustache (0.99.5) + 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.0) + net-ssh (2.9.1) net-ssh-gateway (1.2.0) net-ssh (>= 2.6.5) net-ssh-multi (1.2.0) @@ -159,104 +129,89 @@ GEM net-ssh-gateway (>= 1.2.0) nio4r (1.0.0) nokogiri (1.5.11) - nori (1.1.5) - ohai (6.22.0) + octokit (3.2.0) + sawyer (~> 0.5.3) + ohai (7.0.4) ipaddress + mime-types (~> 1.16) mixlib-cli - mixlib-config + mixlib-config (~> 2.0) mixlib-log - mixlib-shellout + 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) - pleaserun (0.0.7) - cabin (> 0) - cabin - clamp - insist - mustache - ohai (~> 6.20) - stud - polyglot (0.3.4) + polyglot (0.3.5) powerpack (0.0.9) - pry (0.9.12.6) - coderay (~> 1.0) - method_source (~> 0.8) + pry (0.10.0) + coderay (~> 1.1.0) + method_source (~> 0.8.1) slop (~> 3.4) - puma (1.6.3) - rack (~> 1.2) rack (1.5.2) rainbow (2.0.0) - rake (10.3.1) + rake (10.3.2) rb-fsevent (0.9.4) - rb-inotify (0.9.4) - ffi (>= 0.5.0) - rb-kqueue (0.2.2) + rb-inotify (0.9.5) ffi (>= 0.5.0) - rbzip2 (0.2.0) - rest-client (1.6.7) - mime-types (>= 1.16) + 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 (1.7.1) + ridley (4.0.0) addressable - buff-config (~> 0.2) - buff-extensions (~> 0.3) + buff-config (~> 1.0) + buff-extensions (~> 1.0) buff-ignore (~> 1.1) buff-shell_out (~> 0.1) - celluloid (~> 0.15) - celluloid-io (~> 0.15) + celluloid (~> 0.16.0.pre) + celluloid-io (~> 0.16.0.pre) erubis - faraday (>= 0.8.4) - hashie (>= 2.0.2) + 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) - net-ssh retryable - solve (>= 0.4.4) - varia_model (~> 0.1) - winrm (~> 1.1.0) - rspec (2.14.1) - rspec-core (~> 2.14.0) - rspec-expectations (~> 2.14.0) - rspec-mocks (~> 2.14.0) - rspec-core (2.14.8) - rspec-expectations (2.14.5) + 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-mocks (2.14.6) - rubocop (0.21.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.4.2) - rubyntlm (0.1.1) + ruby-progressbar (1.5.1) safe_yaml (1.0.3) - savon (0.9.5) - akami (~> 1.0) - builder (>= 2.1.2) - gyoku (>= 0.4.0) - httpi (~> 0.9) - nokogiri (>= 1.4.0) - nori (~> 1.0) - wasabi (~> 1.0) - serverspec (0.16.0) + 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) - specinfra (>= 0.7.1) + rspec-its + specinfra (~> 1.18) slop (3.5.0) - solve (0.8.2) - specinfra (1.12.0) - strainer (3.4.0) - berkshelf (>= 2.0, < 4.0) - buff-platform (~> 0.1) - stud (0.0.17) - ffi - metriks + 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) @@ -264,44 +219,30 @@ GEM net-ssh (~> 2.7) safe_yaml (~> 1.0) thor (~> 0.18) - thor (0.18.1) - timers (1.1.0) + thor (0.19.1) + timers (3.0.1) + hitimes treetop (1.4.15) polyglot polyglot (>= 0.3.1) - uuidtools (2.1.4) - varia_model (0.3.2) - buff-extensions (~> 0.2) - hashie (>= 2.0.2) - wasabi (1.0.0) - nokogiri (>= 1.4.0) - winrm (1.1.3) - gssapi (~> 1.0.0) - httpclient (~> 2.2, >= 2.2.0.2) - logging (~> 1.6, >= 1.6.1) - nokogiri (~> 1.5) - rubyntlm (~> 0.1.1) - savon (= 0.9.5) - uuidtools (~> 2.1.2) + 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 (~> 2.0) - celluloid (~> 0.14) - celluloid-io (~> 0.14) - chef (~> 11.8) - chefspec (~> 3.2) - foodcritic (~> 3.0) - guard (~> 1.8) - guard-foodcritic - guard-rubocop (~> 0.2) + 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 - pleaserun - rake - rubocop (~> 0.18) - serverspec (~> 0.14) - strainer (~> 3.3) - test-kitchen (~> 1.1) + rake (>= 10.2) + rubocop (= 0.23) + serverspec (>= 1.6) + test-kitchen diff --git a/Rakefile b/Rakefile index 8d53648..2cecebd 100644 --- a/Rakefile +++ b/Rakefile @@ -4,7 +4,7 @@ require 'bundler/setup' namespace :style do require 'rubocop/rake_task' desc 'Run Ruby style checks' - Rubocop::RakeTask.new(:ruby) + RuboCop::RakeTask.new(:ruby) require 'foodcritic' desc 'Run Chef style checks' @@ -32,9 +32,9 @@ end # The default rake task should just run it all task default: %w(style spec integration) -# begin - # require 'kitchen/rake_tasks' - # Kitchen::RakeTasks.new -# rescue LoadError - # puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI'] -# end +begin + require 'kitchen/rake_tasks' + Kitchen::RakeTasks.new +rescue LoadError + puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI'] +end diff --git a/libraries/logstash_conf.rb b/libraries/logstash_conf.rb index 3951530..d35d402 100644 --- a/libraries/logstash_conf.rb +++ b/libraries/logstash_conf.rb @@ -59,20 +59,16 @@ def self.key_value_to_str(k, v, indent = 0) def self.plugin_to_arr(plugin, patterns_dir_plugins = nil, patterns_dir = nil, indent = 0) # , type_to_condition) result = [] plugin.each do |name, hash| -# result << '' -# result << " if [type] == \"#{hash['type']}\" {" if hash.has_key?('type') and type_to_condition indent += 4 result << indent(indent) + name.to_s + ' {' result << indent(indent) + key_value_to_str('patterns_dir', patterns_dir, indent) if patterns_dir_plugins.include?(name.to_s) && patterns_dir && !hash.key?('patterns_dir') hash.sort.each do |k, v| -# next if k == 'type' and type_to_condition indent += 4 result << indent(indent) + key_value_to_str(k, v, indent) indent -= 4 end result << indent(indent) + '}' indent -= 4 -# result << ' }' if hash.has_key?('type') and type_to_condition end return result.join("\n") end @@ -81,7 +77,6 @@ def self.section_to_str(section, version = nil, patterns_dir = nil, indent = 0) result = [] patterns_dir_plugins = ['grok'] patterns_dir_plugins << 'multiline' if Gem::Version.new(version) >= Gem::Version.new('1.1.2') unless version.nil? -# type_to_condition = Gem::Version.new(version) >= Gem::Version.new('1.2.0') section.each do |segment| result << '' if segment.key?('condition') || segment.key?('block') diff --git a/libraries/matchers.rb b/libraries/matchers.rb index 02951f0..4f4a82a 100644 --- a/libraries/matchers.rb +++ b/libraries/matchers.rb @@ -21,7 +21,7 @@ def create_logstash_pattern(name) ChefSpec::Matchers::ResourceMatcher.new(:logstash_pattern, :create, name) end - # LWRP - Plugins + # LWRP - Plugins def create_logstash_plugins(name) ChefSpec::Matchers::ResourceMatcher.new(:logstash_plugins, :create, name) end diff --git a/providers/service.rb b/providers/service.rb index 759d879..825924c 100644 --- a/providers/service.rb +++ b/providers/service.rb @@ -46,7 +46,7 @@ def load_current_resource case svc[:method] when 'native' sv = service svc[:service_name] do - # provider Chef::Provider::Service::Upstart + # provider Chef::Provider::Service::Upstart action [:restart] end new_resource.updated_by_last_action(sv.updated_by_last_action?) @@ -58,7 +58,7 @@ def load_current_resource case svc[:method] when 'native' sv = service svc[:service_name] do - # provider Chef::Provider::Service::Upstart + # provider Chef::Provider::Service::Upstart action [:start] end new_resource.updated_by_last_action(sv.updated_by_last_action?) @@ -70,7 +70,7 @@ def load_current_resource case svc[:method] when 'native' sv = service svc[:service_name] do - # provider Chef::Provider::Service::Upstart + # provider Chef::Provider::Service::Upstart action [:stop] end new_resource.updated_by_last_action(sv.updated_by_last_action?) @@ -82,7 +82,7 @@ def load_current_resource case svc[:method] when 'native' sv = service svc[:service_name] do - # provider Chef::Provider::Service::Upstart + # provider Chef::Provider::Service::Upstart action [:reload] end new_resource.updated_by_last_action(sv.updated_by_last_action?) diff --git a/recipes/server.rb b/recipes/server.rb index 8f8a6af..b3cd0d8 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -26,32 +26,21 @@ action [:enable] end -embedded_es = node['logstash']['instance'][name]['enable_embedded_es'] || node['logstash']['instance']['default']['enable_embedded_es'] -es_cluster = node['logstash']['instance'][name]['elasticsearch_cluster'] || node['logstash']['instance']['default']['elasticsearch_cluster'] -es_index = node['logstash']['instance'][name]['es_index'] || node['logstash']['instance']['default']['es_index'] +my_templates = node['logstash']['instance']['default']['config_templates'] -bind_host_if = node['logstash']['instance'][name]['bind_host_interface'] || node['logstash']['instance']['default']['bind_host_interface'] -if !bind_host_if.empty? - bind_host = ::Logstash.get_ip_for_node(node, bind_host_if) -else - bind_host = nil +if my_templates.empty? + my_templates = { + 'input_syslog' => 'config/input_syslog.conf.erb', + 'output_stdout' => 'config/output_stdout.conf.erb', + 'output_elasticsearch' => 'config/output_elasticsearch.conf.erb' + } end -my_templates = { - 'input_syslog' => 'config/input_syslog.conf.erb', - 'output_stdout' => 'config/output_stdout.conf.erb', - 'output_elasticsearch' => 'config/output_elasticsearch.conf.erb' -} - logstash_config name do templates my_templates action [:create] variables( - elasticsearch_ip: ::Logstash.service_ip(node, name, 'elasticsearch'), - bind_host: bind_host, - elasticsearch_cluster: es_cluster, - elasticsearch_embedded: embedded_es, - es_index: es_index + elasticsearch_embedded: true ) end # ^ see `.kitchen.yml` for example attributes to configure templates. diff --git a/templates/default/config/output_elasticsearch.conf.erb b/templates/default/config/output_elasticsearch.conf.erb index 86bfa32..e72c6b6 100644 --- a/templates/default/config/output_elasticsearch.conf.erb +++ b/templates/default/config/output_elasticsearch.conf.erb @@ -2,8 +2,11 @@ output { elasticsearch { <% if @elasticsearch_ip -%> host => "<%= @elasticsearch_ip %>" +<% end -%> +<% if @elasticsearch_cluster -%> cluster => "<%= @elasticsearch_cluster %>" -<% elsif @elasticsearch_embedded -%> +<% end -%> +<% if @elasticsearch_embedded -%> embedded => true <% end -%> <% if @bind_host -%> diff --git a/test/integration/server/serverspec/server_spec.rb b/test/integration/server/serverspec/server_spec.rb index eed8f90..4168d4c 100644 --- a/test/integration/server/serverspec/server_spec.rb +++ b/test/integration/server/serverspec/server_spec.rb @@ -3,7 +3,7 @@ # Java 1.6 describe command('java -version') do - it { should return_stdout /java version "1.7.\d+_\d+"/ } + it { should return_stdout(/java version "1.7.\d+_\d+"/) } end # Logstash Instance diff --git a/test/unit/spec/beaver_spec.rb b/test/unit/spec/beaver_spec.rb index 6d3cac7..c234d53 100644 --- a/test/unit/spec/beaver_spec.rb +++ b/test/unit/spec/beaver_spec.rb @@ -12,7 +12,7 @@ include_context 'stubs-common' it 'writes some chefspec code' do - pending 'todo' + skip 'todo' end end diff --git a/test/unit/spec/default_spec.rb b/test/unit/spec/default_spec.rb index dee2f2a..b226dcf 100644 --- a/test/unit/spec/default_spec.rb +++ b/test/unit/spec/default_spec.rb @@ -12,7 +12,7 @@ include_context 'stubs-common' it 'writes some chefspec code' do - pending 'todo' + skip 'todo' end end diff --git a/test/unit/spec/haproxy_spec.rb b/test/unit/spec/haproxy_spec.rb index 8584e56..51d1d53 100644 --- a/test/unit/spec/haproxy_spec.rb +++ b/test/unit/spec/haproxy_spec.rb @@ -12,7 +12,7 @@ include_context 'stubs-common' it 'writes some chefspec code' do - pending 'todo' + skip 'todo' end end diff --git a/test/unit/spec/pyshipper_spec.rb b/test/unit/spec/pyshipper_spec.rb index 5b1455a..34091a1 100644 --- a/test/unit/spec/pyshipper_spec.rb +++ b/test/unit/spec/pyshipper_spec.rb @@ -12,7 +12,7 @@ include_context 'stubs-common' it 'writes some chefspec code' do - pending 'todo' + skip 'todo' end end diff --git a/test/unit/spec/source_spec.rb b/test/unit/spec/source_spec.rb index 539e0a3..e9086bc 100644 --- a/test/unit/spec/source_spec.rb +++ b/test/unit/spec/source_spec.rb @@ -12,7 +12,7 @@ include_context 'stubs-common' it 'writes some chefspec code' do - pending 'todo' + skip 'todo' end end diff --git a/test/unit/spec/spec_helper.rb b/test/unit/spec/spec_helper.rb index ac8a69f..9b95abc 100644 --- a/test/unit/spec/spec_helper.rb +++ b/test/unit/spec/spec_helper.rb @@ -1,7 +1,8 @@ # Encoding: utf-8 +require 'rspec/expectations' require 'chefspec' require 'chefspec/berkshelf' -# require 'chefspec/server' +require 'chefspec/server' require 'chef/application' ::LOG_LEVEL = :fatal @@ -28,9 +29,9 @@ end shared_examples 'example' do -# it 'does not include example recipe by default' do -# expect(chef_run).not_to include_recipe('example::default') -# end + # it 'does not include example recipe by default' do + # expect(chef_run).not_to include_recipe('example::default') + # end end at_exit { ChefSpec::Coverage.report! } diff --git a/test/unit/spec/zero_mq_repo.rb b/test/unit/spec/zero_mq_repo.rb index 47ef0aa..ac75be2 100644 --- a/test/unit/spec/zero_mq_repo.rb +++ b/test/unit/spec/zero_mq_repo.rb @@ -12,7 +12,7 @@ include_context 'stubs-common' it 'writes some chefspec code' do - pending 'todo' + skip 'todo' end end From 32ba3d12c7af1d143778a8b66e7c226747cee636 Mon Sep 17 00:00:00 2001 From: Paul Czarkowski Date: Tue, 8 Jul 2014 10:58:50 -0500 Subject: [PATCH 52/55] use system gecode for fastererer testing --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index bad871f..d1a68fd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ language: ruby rvm: - 1.9.3 +env: + - USE_SYSTEM_GECODE=1 install: bundle install --path vendor --binstubs script: bin/berks install -script: bin/rake style spec \ No newline at end of file +script: bin/rake style spec From 7260a40fce431a3832e1e427fab0088279cdc1b2 Mon Sep 17 00:00:00 2001 From: Paul Czarkowski Date: Tue, 8 Jul 2014 11:17:51 -0500 Subject: [PATCH 53/55] travis changes --- .travis.yml | 11 ++++++++--- Rakefile | 8 +++++--- test/unit/spec/lwrp_config_spec.rb | 6 +----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index d1a68fd..0bb6bdf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,11 @@ rvm: - 1.9.3 env: - USE_SYSTEM_GECODE=1 -install: bundle install --path vendor --binstubs -script: bin/berks install -script: bin/rake style spec +before_install: + - sudo apt-get update + - sudo apt-get install libgecode-dev +install: + - bundle install --path vendor --binstubs +script: + - bin/berks install + - bin/rake style spec diff --git a/Rakefile b/Rakefile index 2cecebd..c1e3742 100644 --- a/Rakefile +++ b/Rakefile @@ -17,9 +17,11 @@ task style: ['style:chef', 'style:ruby'] require 'kitchen' desc 'Run Test Kitchen integration tests' task :integration do - Kitchen.logger = Kitchen.default_file_logger - Kitchen::Config.new.instances.each do |instance| - instance.test(:always) + unless ENV['CI'] + Kitchen.logger = Kitchen.default_file_logger + Kitchen::Config.new.instances.each do |instance| + instance.test(:always) + end end end diff --git a/test/unit/spec/lwrp_config_spec.rb b/test/unit/spec/lwrp_config_spec.rb index d6ae02f..29979e4 100644 --- a/test/unit/spec/lwrp_config_spec.rb +++ b/test/unit/spec/lwrp_config_spec.rb @@ -55,11 +55,7 @@ group: 'logstash', mode: '0644', variables: { - elasticsearch_ip: '127.0.0.1', - bind_host: nil, - elasticsearch_cluster: 'logstash', - elasticsearch_embedded: true, - es_index: nil + elasticsearch_embedded: true }, action: [:create] ) From 6ee155ac3b64b4958d0396f21815e1a0a9cea3a7 Mon Sep 17 00:00:00 2001 From: Paul Czarkowski Date: Tue, 8 Jul 2014 11:28:12 -0500 Subject: [PATCH 54/55] more chefspec fixes --- Rakefile | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Rakefile b/Rakefile index c1e3742..c649ea3 100644 --- a/Rakefile +++ b/Rakefile @@ -34,9 +34,11 @@ end # The default rake task should just run it all task default: %w(style spec integration) -begin - require 'kitchen/rake_tasks' - Kitchen::RakeTasks.new -rescue LoadError - puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI'] +unless ENV['CI'] + begin + require 'kitchen/rake_tasks' + Kitchen::RakeTasks.new + rescue LoadError + puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI'] + end end From c8ca82b4ca2d46e5ed91e76c41a2c7188b04c0ae Mon Sep 17 00:00:00 2001 From: Paul Czarkowski Date: Tue, 8 Jul 2014 11:44:08 -0500 Subject: [PATCH 55/55] Gemfile style fix --- Gemfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Gemfile b/Gemfile index b88a705..e0def1b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# Encoding: utf-8 + source 'https://rubygems.org' gem 'berkshelf', '> 3'