From c4971f52240169ead32176eb52e4b90d206e28fc Mon Sep 17 00:00:00 2001 From: Virender Khatri Date: Sun, 29 Nov 2015 00:25:07 +0530 Subject: [PATCH] #13, major changes to support repository package install --- README.md | 43 +++++++++++---------- attributes/default.rb | 15 +++++++- metadata.rb | 2 + recipes/default.rb | 9 ++++- recipes/install.rb | 62 ------------------------------- recipes/install_package.rb | 43 +++++++++++++++++++++ recipes/install_windows.rb | 39 +++++++++++++++++++ spec/unit/recipes/default_spec.rb | 34 ++++++++++++----- 8 files changed, 153 insertions(+), 94 deletions(-) delete mode 100755 recipes/install.rb create mode 100755 recipes/install_package.rb create mode 100755 recipes/install_windows.rb diff --git a/README.md b/README.md index eb0c549..46a3b05 100644 --- a/README.md +++ b/README.md @@ -41,12 +41,17 @@ This cookbook was tested on Windows, Amazon & Ubuntu Linux and expected to work - windows - powershell +- apt +- yum + ## Recipes - `filebeat::default` - default recipe (use it for run_list) -- `filebeat::install` - install filebeat +- `filebeat::install_windows` - install filebeat for windows platform + +- `filebeat::install_package` - install filebeat package for linux platform - `filebeat::config` - configure filebeat @@ -54,9 +59,9 @@ This cookbook was tested on Windows, Amazon & Ubuntu Linux and expected to work ## Core Attributes -* `default['filebeat']['version']` (default: `1.0.0-rc2`): filebeat version +* `default['filebeat']['version']` (default: `1.0.0`): filebeat version -* `default['filebeat']['package_url']` (default: `auto`): package url +* `default['filebeat']['package_url']` (default: `auto`): package url for windows installation * `default['filebeat']['conf_dir']` (default: `/etc/filebeat`): filebeat yaml configuration file directory @@ -79,40 +84,38 @@ This cookbook was tested on Windows, Amazon & Ubuntu Linux and expected to work * `default['filebeat']['config']['filebeat']['config_dir']` (default: `node['filebeat']['prospectors_dir']`): filebeat prospectors configuration files folder -* `default['filebeat']['config']['output']['elasticsearch']['enabled']` (default: `true`): enable elasticsearch output +* `default['filebeat']['config']['output']` (default: `{}`): configure elasticsearch. logstash, file etc. output -* `default['filebeat']['config']['output']['elasticsearch']['hosts']` (default: `[]`): elasticsearch hosts - -* `default['filebeat']['config']['output']['elasticsearch']['save_topology']` (default: `false`): +For more attribute info, visit below links: +https://github.com/elastic/filebeat/blob/master/etc/filebeat.yml -* `default['filebeat']['config']['output']['logstash']['enabled']` (default: `true`): enable logstash output -* `default['filebeat']['config']['output']['logstash']['hosts']` (default: `[]`): logstash hosts +# Elasticsearch YUM/APT Repository Attributes -* `default['filebeat']['config']['output']['logstash']['loadbalance']` (default: `true`): set true to load balance between logstash hosts +* `default['filebeat']['yum']['description']` (default: ``): beats yum reporitory attribute -* `default['filebeat']['config']['output']['logstash']['index']` (default: `filebeat`): logstash index name +* `default['filebeat']['yum']['gpgcheck']` (default: `true`): beats yum reporitory attribute -* `default['filebeat']['config']['output']['logstash']['tls']['enabled']` (default: `false`): +* `default['filebeat']['yum']['enabled']` (default: `true`): beats yum reporitory attribute -* `default['filebeat']['config']['output']['logstash']['save_topology']` (default: `false`): +* `default['filebeat']['yum']['baseurl']` (default: `https://packages.elastic.co/beats/yum/el/$basearch`): beatsyum reporitory attribute +* `default['filebeat']['yum']['gpgkey']` (default: `https://packages.elasticsearch.org/GPG-KEY-elasticsearch`): beats yum reporitory attribute -* `default['filebeat']['config']['output']['file']['enabled']` (default: `false`): +* `default['filebeat']['yum']['action']` (default: `:create`): beats yum reporitory attribute -* `default['filebeat']['config']['output']['file']['path']` (default: `/tmp/filebeat`): -* `default['filebeat']['config']['output']['file']['filename']` (default: `filebeat`): +* `default['filebeat']['apt']['description']` (default: `calculated`): beats apt reporitory attribute -* `default['filebeat']['config']['output']['file']['rotate_every_kb']` (default: `10240`): +* `default['filebeat']['apt']['components']` (default: `['stable', 'main']`): beats apt reporitory attribute -* `default['filebeat']['config']['output']['file']['number_of_files']` (default: `7`): +* `default['filebeat']['apt']['uri']` (default: `https://packages.elastic.co/beats/apt`): beats apt reporitory attribute +* `default['filebeat']['apt']['key']` (default: `http://packages.elasticsearch.org/GPG-KEY-elasticsearch`): beats apt reporitory attribute -For more attribute info, visit below links: +* `default['filebeat']['apt']['action']` (default: `:add`): filebeat apt reporitory attribute -https://github.com/elastic/filebeat/blob/master/etc/filebeat.yml ## How to Add Filebeat Prospectors via Node Attribute diff --git a/attributes/default.rb b/attributes/default.rb index 6221675..dfd8365 100755 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -1,4 +1,4 @@ -default['filebeat']['version'] = '1.0.0-rc2' +default['filebeat']['version'] = '1.0.0' default['filebeat']['disable_service'] = false default['filebeat']['package_url'] = 'auto' @@ -10,3 +10,16 @@ default['filebeat']['windows'] = { 'base_dir' => 'C:/opt/filebeat/' } + +default['filebeat']['yum']['baseurl'] = 'https://packages.elastic.co/beats/yum/el/$basearch' +default['filebeat']['yum']['description'] = 'Elastic Beats Repository' +default['filebeat']['yum']['gpgcheck'] = true +default['filebeat']['yum']['enabled'] = true +default['filebeat']['yum']['gpgkey'] = 'https://packages.elasticsearch.org/GPG-KEY-elasticsearch' +default['filebeat']['yum']['action'] = :create + +default['filebeat']['apt']['uri'] = 'https://packages.elastic.co/beats/apt' +default['filebeat']['apt']['description'] = 'Elastic Beats Repository' +default['filebeat']['apt']['components'] = %w(stable main) +default['filebeat']['apt']['action'] = :add +default['filebeat']['apt']['key'] = 'https://packages.elasticsearch.org/GPG-KEY-elasticsearch' diff --git a/metadata.rb b/metadata.rb index 640bcac..5953971 100644 --- a/metadata.rb +++ b/metadata.rb @@ -10,6 +10,8 @@ depends 'windows' depends 'powershell' +depends 'apt' +depends 'yum' %w(windows ubuntu centos amazon redhat fedora).each do |os| supports os diff --git a/recipes/default.rb b/recipes/default.rb index 218f662..690d16a 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -17,5 +17,12 @@ # limitations under the License. # -include_recipe 'filebeat::install' +# install filebeat +if node['platform'] == 'windows' + include_recipe 'filebeat::install_windows' +else + include_recipe 'filebeat::install_package' +end + +# configure filebeat include_recipe 'filebeat::config' diff --git a/recipes/install.rb b/recipes/install.rb deleted file mode 100755 index e042a57..0000000 --- a/recipes/install.rb +++ /dev/null @@ -1,62 +0,0 @@ -# -# Cookbook Name:: filebeat -# Recipe:: install -# -# Copyright 2015, Virender Khatri -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -if node['platform_family'] == 'debian' && node['kernel']['machine'] == 'x86_64' - arch_name = 'amd64' -else - arch_name = node['kernel']['machine'] -end - -if node['filebeat']['package_url'] == 'auto' - package_url = value_for_platform_family( - 'debian' => "https://download.elastic.co/beats/filebeat/filebeat_#{node['filebeat']['version']}_#{arch_name}.deb", - %w(rhel fedora) => "https://download.elastic.co/beats/filebeat/filebeat-#{node['filebeat']['version']}-#{arch_name}.rpm", - 'windows' => "https://download.elastic.co/beats/filebeat/filebeat-#{node['filebeat']['version']}-windows.zip" - ) -else - package_url = node['filebeat']['package_url'] -end - -package_file = ::File.join(Chef::Config[:file_cache_path], ::File.basename(package_url)) - -remote_file 'filebeat_package_file' do - path package_file - source package_url - not_if { ::File.exist?(package_file) } -end - -package 'filebeat' do - source package_file - options '--force-confdef --force-confold' if node['platform_family'] == 'debian' - provider Chef::Provider::Package::Dpkg if node['platform_family'] == 'debian' - not_if { node['platform'] == 'windows' } -end - -directory node['filebeat']['windows']['base_dir'] do - recursive true - action :create - only_if { node['platform'] == 'windows' } -end - -windows_zipfile node['filebeat']['windows']['base_dir'] do - source package_file - action :unzip - only_if { node['platform'] == 'windows' } - not_if { ::File.exist?(node['filebeat']['windows']['base_dir'] + "/filebeat-#{node['filebeat']['version']}-windows" + '/install-service-filebeat.ps1') } -end diff --git a/recipes/install_package.rb b/recipes/install_package.rb new file mode 100755 index 0000000..1c34cbf --- /dev/null +++ b/recipes/install_package.rb @@ -0,0 +1,43 @@ +# +# Cookbook Name:: filebeat +# Recipe:: package +# +# Copyright 2015, Virender Khatri +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +case node['platform_family'] +when 'debian' + # apt repository configuration + apt_repository 'beats' do + uri node['filebeat']['apt']['uri'] + components node['filebeat']['apt']['components'] + key node['filebeat']['apt']['key'] + action node['filebeat']['apt']['action'] + end +when 'rhel' + # yum repository configuration + yum_repository 'beats' do + description node['filebeat']['yum']['description'] + baseurl node['filebeat']['yum']['baseurl'] + gpgcheck node['filebeat']['yum']['gpgcheck'] + gpgkey node['filebeat']['yum']['gpgkey'] + enabled node['filebeat']['yum']['enabled'] + action node['filebeat']['yum']['action'] + end +end + +package 'filebeat' do + version node['platform_family'] == 'rhel' ? node['filebeat']['version'] + '-1' : node['filebeat']['version'] +end diff --git a/recipes/install_windows.rb b/recipes/install_windows.rb new file mode 100755 index 0000000..0529214 --- /dev/null +++ b/recipes/install_windows.rb @@ -0,0 +1,39 @@ +# +# Cookbook Name:: filebeat +# Recipe:: install_windows +# +# Copyright 2015, Virender Khatri +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package_url = node['filebeat']['package_url'] == 'auto' ? "https://download.elastic.co/beats/filebeat/filebeat-#{node['filebeat']['version']}-windows.zip" : node['filebeat']['package_url'] + +package_file = ::File.join(Chef::Config[:file_cache_path], ::File.basename(package_url)) + +remote_file 'filebeat_package_file' do + path package_file + source package_url + not_if { ::File.exist?(package_file) } +end + +directory node['filebeat']['windows']['base_dir'] do + recursive true + action :create +end + +windows_zipfile node['filebeat']['windows']['base_dir'] do + source package_file + action :unzip + not_if { ::File.exist?(node['filebeat']['windows']['base_dir'] + "/filebeat-#{node['filebeat']['version']}-windows" + '/install-service-filebeat.ps1') } +end diff --git a/spec/unit/recipes/default_spec.rb b/spec/unit/recipes/default_spec.rb index c599940..d226634 100644 --- a/spec/unit/recipes/default_spec.rb +++ b/spec/unit/recipes/default_spec.rb @@ -3,16 +3,6 @@ describe 'filebeat::default' do shared_examples_for 'filebeat' do context 'all_platforms' do - %w(install config).each do |r| - it "include recipe filebeat::#{r}" do - expect(chef_run).to include_recipe("filebeat::#{r}") - end - end - - it 'download filebeat package file' do - expect(chef_run).to create_remote_file('filebeat_package_file') - end - it 'create prospector directory /etc/filebeat/conf.d' do expect(chef_run).to create_directory('/etc/filebeat/conf.d') end @@ -37,6 +27,14 @@ include_examples 'filebeat' + it 'adds beats yum repository' do + expect(chef_run).to create_yum_repository('beats') + end + + it 'include recipe filebeat::install_package' do + expect(chef_run).to include_recipe('filebeat::install_package') + end + it 'install filebeat package' do expect(chef_run).to install_package('filebeat') end @@ -51,6 +49,14 @@ include_examples 'filebeat' + it 'adds beats apt beats' do + expect(chef_run).to add_apt_repository('beats') + end + + it 'include recipe filebeat::install_package' do + expect(chef_run).to include_recipe('filebeat::install_package') + end + it 'install filebeat package' do expect(chef_run).to install_package('filebeat') end @@ -65,6 +71,14 @@ include_examples 'filebeat' + it 'include recipe filebeat::install_windows' do + expect(chef_run).to include_recipe('filebeat::install_windows') + end + + it 'download filebeat package file' do + expect(chef_run).to create_remote_file('filebeat_package_file') + end + it 'create filebeat base dir C:/opt/filebeat/' do expect(chef_run).to create_directory('C:/opt/filebeat/') end