Skip to content
This repository has been archived by the owner on Mar 20, 2021. It is now read-only.

Big cookbook refactoring #101

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .foodcritic
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
~FC057
~FC007
26 changes: 9 additions & 17 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,21 @@ platforms:
- name: centos-7.2

suites:
- name: kibana3_apache
- name: default
run_list:
- 'recipe[netstat]'
- 'recipe[apache2]'
- 'recipe[kibana]'
- 'recipe[kibana::apache]'
attributes: {}
- name: kibana3_nginx
run_list:
- 'recipe[netstat]'
- 'recipe[chef_nginx]'
- 'recipe[kibana]'
- 'recipe[kibana::nginx]'
attributes: {}
- name: kibana3_source
attributes:
kibana:
version: '4.6.3'
- name: tarball_default
run_list:
- 'recipe[netstat]'
- 'recipe[apache2]'
- 'recipe[kibana]'
- 'recipe[kibana::apache]'
attributes:
kibana:
install_method: 'source'
version: '4.6.3'
install_method: 'tarball'
service:
bin_path: 'kibana/bin'
- name: kibana4_apache
run_list:
- 'recipe[netstat]'
Expand Down
28 changes: 26 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,35 @@ AllCops:

BlockComments:
Enabled: false
BlockLength:
Enabled: false

Style/RescueModifier:
Exclude:
- 'Rakefile'
- 'libraries/helpers.rb'

LeadingCommentSpace:
Enabled: false

LineLength:
Enabled: false

# Longer classes are okay.
Metrics/MethodLength:
Max: 30
Metrics/ModuleLength:
Max: 150

# We're not against complexity.
Metrics/AbcSize:
Max: 55
Metrics/CyclomaticComplexity:
Max: 20
Metrics/PerceivedComplexity:
Max: 20

# Mixlib::ShellOut is nice to have as an older hash syntax
Style/HashSyntax:
Enabled: false

Style/ClassAndModuleChildren:
Enabled: false
14 changes: 0 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ env:
- TESTS="unit"
- TESTS="rubocop"
- TESTS="foodcritic"
- TESTS="integration:docker[kibana3-apache-ubuntu-1404]"
- TESTS="integration:docker[kibana3-nginx-ubuntu-1404]"
- TESTS="integration:docker[kibana4-apache-ubuntu-1404]"
- TESTS="integration:docker[kibana4-nginx-ubuntu-1404]"
- TESTS="integration:docker[kibana4-apache-package-ubuntu-1404]"
- TESTS="integration:docker[kibana4-apache-ubuntu-1604]"
- TESTS="integration:docker[kibana4-nginx-ubuntu-1604]"
- TESTS="integration:docker[kibana4-apache-package-ubuntu-1604]"
- TESTS="integration:docker[kibana3-apache-centos-64]"
- TESTS="integration:docker[kibana3-nginx-centos-64]"
- TESTS="integration:docker[kibana4-apache-centos-64]"
- TESTS="integration:docker[kibana4-nginx-centos-64]"
- TESTS="integration:docker[kibana4-apache-centos-72]"
- TESTS="integration:docker[kibana4-nginx-centos-72]"

before_install: curl -L https://www.getchef.com/chef/install.sh | sudo bash -s -- -P chefdk
install: chef exec bundle install --jobs=3 --retry=3 --without='vagrant'
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ A cookbook that installs [Kibana](https://github.com/elastic/kibana).

Kibana requires ElasticSearch index to be configured to work as per logstash requirements.

[Kibana](https://www.elastic.co/products/kibana) - This cookbook is being written and tested to support Kibana 5.x and greater. If you must have a cookbook that works with older versions of Kibana, please test and then pin to a specific, older major.minor version of this cookbook and only leave the patch release to float. Older versions can be found via Git tags or on Chef Supermarket.

# Requirements

* Chef 11+
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ end

desc 'Foodcritic linter'
task :foodcritic do
sh 'foodcritic -f any -t ~FC007 .'
sh 'foodcritic -f any .'
end

desc 'Run Test Kitchen integration tests'
Expand Down Expand Up @@ -44,7 +44,7 @@ desc 'Run chefspec unit tests'
RSpec::Core::RakeTask.new(:unit) do |t|
t.rspec_opts = [].tap do |a|
a.push('--color')
a.push('--format progress')
a.push('--format documentation')
end.join(' ')
t.pattern = 'test/unit/spec/*_spec.rb'
end
Expand Down
70 changes: 8 additions & 62 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,74 +1,20 @@
# Encoding: utf-8

#<> Kibana major version
default['kibana']['version'] = '3'
#<> Kibana3 exact version
default['kibana']['kibana3_version'] = '3.0.0'
#<> Kibana4 exact version
default['kibana']['kibana4_version'] = '4.2.0'
#<> The base directory of kibana.
default['kibana']['base_dir'] = '/opt/kibana'
#<> The user under which Kibana is installed.
default['kibana']['user'] = 'kibana'
#<> The group under which Kibana is installed.
default['kibana']['group'] = 'kibana'
#<> Install method. Can be source, release or package
default['kibana']['install_method'] = 'release'
default['kibana']['version'] = '5.1.1'
#<> Install method. Can be tarball or package
default['kibana']['install_method'] = 'package'
#<> Kibana repository url for package method install
default['kibana']['repository_url'] = 'http://packages.elastic.co/kibana/4.5/debian'
#<> Kibana repository Public Signing Key
default['kibana']['repository_key'] = 'https://packages.elastic.co/GPG-KEY-elasticsearch'

url_version = node['kibana']["kibana#{node['kibana']['version']}_version"] || node['kibana']['version']
#<> Url of tarball
default['kibana']['url'] = Kibana::Url.new(node, url_version).get
#<> Checksum of the tarball
default['kibana']['checksum'] = 'df25bc0cc02385edcac446ef8cbd83b896cdc910a0fa1b0a7bd2a958164593a8'
# default['kibana']['repository_url'] = 'http://packages.elastic.co/kibana/4.5/debian'
default['kibana']['repository_url'] = 'https://artifacts.elastic.co/packages/5.x/apt'
#<> Checksum of the tarball (for Kibana4)
default['kibana']['kibana4_checksum'] = '67d586e43a35652adeb6780eaa785d3d785ce60cc74fbf3b6a9a53b753c8f985'

#<> The URL to Kibana repository.
default['kibana']['git']['url'] = 'https://github.com/elasticsearch/kibana.git'

#<> The git reference in the Kibana repository.
default['kibana']['git']['reference'] = 'v' + node['kibana']['kibana3_version']

#<> The version of Ruby and Gems to use for Kibana.
case node['platform_family']
when 'debian'
default['kibana']['rubyversion'] = '1.9.1'
when 'rhel'
default['kibana']['rubyversion'] = nil
end
default['kibana']['checksum']['4.6.3']['tar'] = '483d49d7d03052f4885c88d905b602f3fa432cb12e2c2cbdab82bb0d259d00c7'

#<> The interface on which to bind.
default['kibana']['interface'] = '127.0.0.1'

#<> The port on which to bind.
default['kibana']['port'] = 5601
#<> An Array of the elasticsearch service hosts.
default['kibana']['elasticsearch']['hosts'] = ['127.0.0.1']
#<> The port of the elasticsearch http service.
default['kibana']['elasticsearch']['port'] = 9200

default['kibana']['index'] = if node['kibana']['version'] > '3'
'.kibana'
else
'kibana-int'
end

#<> The which fields are shown by default.
default['kibana']['default_fields'] = '["@message"]'
#<> The operator used if no explicit operator is specified.
default['kibana']['default_operator'] = 'OR'
#<> The cookbook from which configuration template is taken
default['kibana']['config']['cookbook'] = nil
#<> The template from which configuration is generated from
default['kibana']['config']['source'] = nil
#<> Fields specifiers which default to @message (may need to be changed for newer logstash)
default['kibana']['highlighted_field'] = '@message'
default['kibana']['primary_field'] = '@message'
default['kibana']['default_index'] = '@message'

#<> The host to create apache vhost for.
default['kibana']['apache']['host'] = node['fqdn']
Expand Down Expand Up @@ -126,7 +72,7 @@
default['kibana']['nginx']['server_name'] = 'kibana'

#<> The nginx configuration source
default['kibana']['nginx']['source'] = node['kibana']['version'] == '4' ? 'nginx4.conf.erb' : 'nginx.conf.erb'
default['kibana']['nginx']['source'] = node['kibana']['version'] == '4' ? 'nginx4.conf.erb' : 'nginx5.conf.erb'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need to make this conditional test a regex (=~ /^4/) to match all 4.x versions

default['kibana']['nginx']['cookbook'] = 'kibana'

#<> Redirect requests to kibana service
Expand Down Expand Up @@ -160,7 +106,7 @@
end
end
default['kibana']['service']['cookbook'] = 'kibana'
default['kibana']['service']['bin_path'] = 'current/bin'
default['kibana']['service']['bin_path'] = 'bin'
default['kibana']['service']['options'] = ''

#<> The kibana 4 default application on load
Expand Down
1 change: 1 addition & 0 deletions chefignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ test/*
features/*
Guardfile
Procfile
test

# SCM #
#######
Expand Down
148 changes: 148 additions & 0 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
module KibanaCookbook
# Helper methods included by various providers and passed to the template engine
module Helpers
def determine_version(new_resource, node)
if new_resource.version
new_resource.version.to_s
elsif node['kibana'] && node['kibana']['version']
node['kibana']['version'].to_s
else
raise 'could not determine version of kibana to install'
end
end

def determine_install_type(new_resource, node)
if new_resource.type
new_resource.type.to_s
elsif node['kibana'] && node['kibana']['install_type']
node['kibana']['install_type'].to_s
else
raise 'could not determine how to install kibana (package? tarball?)'
end
end

def determine_repository_url(new_resource, node)
# TODO: add support version 5.x
# platform_family = node['platform_family']
# url_string = nil
if new_resource.repository_url
# url_string = new_resource.repository_url
new_resource.repository_url
else
# url_string = node['kibana']['repository_url']
node['kibana']['repository_url']
end
end

def determine_download_url(new_resource, node)
# platform_family = node['platform_family']
install_type = determine_install_type(new_resource, node)
version = determine_version(new_resource, node)
arch = determine_arch
ext = determine_ext

# url_string = nil
if new_resource.download_url
new_resource.download_url
elsif install_type.to_s == 'tar' || install_type.to_s == 'tarball'
prefix = 'https://download.elastic.co/kibana/kibana/kibana-'
suffix = "#{version}-#{node['os']}-#{arch}.#{ext}"
prefix + suffix
else
raise 'could not determine url for install kibana'
end

# return url_string
end

def determine_download_checksum(new_resource, node)
# platform_family = node['platform_family']
install_type = determine_install_type(new_resource, node)
version = determine_version(new_resource, node)

if new_resource.download_checksum
new_resource.download_checksum
elsif install_type.to_s == 'tar' || install_type.to_s == 'tarball'
node && version &&
node['kibana'] &&
node['kibana']['checksums'] &&
node['kibana']['checksums'][version] &&
node['kibana']['checksums'][version]['tar']
end
end

def find_kb_resource(run_context, resource_type, resource)
resource_name = resource.name

# if we are truly given a specific name to find
name_match = find_exact_resource(run_context, resource_type, resource_name) rescue nil
return name_match if name_match

# otherwise try the defaults
name_default = find_exact_resource(run_context, resource_type, 'default') rescue nil
name_kibana = find_exact_resource(run_context, resource_type, 'kibana') rescue nil

# if we found exactly one default name that matched
return name_default if name_default && !name_kibana
return name_kibana if name_kibana && !name_default

raise "Could not find exactly one #{resource_type} resource, and no specific resource or instance name was given"
end

# find exactly the resource name and type, but raise if there's multiple matches
# see https://github.com/chef/chef/blob/master/lib/chef/resource_collection/resource_set.rb#L80
def find_exact_resource(run_context, resource_type, resource_name)
rc = run_context.resource_collection
result = rc.find(resource_type => resource_name)

if result && result.is_a?(Array)
str = ''
str << "more than one #{resource_type} was found, "
str << 'you must specify a precise resource name'
raise str
end

result
end

# def find_instance_name_resource(run_context, resource_type, instance_name)
# results = []
# rc = run_context.resource_collection

# rc.each do |r|
# next unless r.resource_name == resource_type
# results << r
# end

# if !results.empty? && results.length > 1
# str = ''
# str << "more than one #{resource_type} was found, "
# str << 'you must specify a precise instance name'
# raise str
# elsif !results.empty?
# return results.first
# end

# return nil # falsey
# end

def determine_ext
case node['os']
when 'windows'
'zip'
else
'tar.gz'
end
end

def determine_arch
return '' if node['os'] == 'windows'
case node['kernel']['machine']
when 'x86_64'
'x86_64'
else
'-x86'
end
end
end
end
Loading