Skip to content

Commit

Permalink
implementing a better way to handle aws cloudWatch logs configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
amalucelli committed Jan 31, 2017
1 parent 34a350e commit c2a40f0
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ driver:
name: vagrant

provisioner:
name: chef_zero
name: chef_solo

platforms:
- name: ubuntu-14.04
Expand Down
67 changes: 0 additions & 67 deletions .rubocop_todo.yml

This file was deleted.

7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
Changes
=======

# 1.1.5 / 2017-01-31
* Implementing a better way to handle AWS CloudWatch Logs configurations,
now each log has a configuration file that is provisioned via Chef Resources.

# 1.1.4 / 2016-11-07
* Implementing `logging_config_file` configuration, that overrides the default logging configuration to a WARNING value.
* Implementing `logging_config_file` configuration, that overrides the default
logging configuration to a WARNING value.

# 1.1.3 / 2016-10-31
* Fixing `aws_access_key_id` and `aws_secret_access_key` attributes values.
Expand Down
35 changes: 25 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ Agent and deploy it's configurations automatically.

Add this cookbook to your base recipe:
```ruby
cookbook 'aws-cloudwatchlogs', '~> 1.1.4'
cookbook 'aws-cloudwatchlogs', '~> 1.1.5'
```

You need to configure the following node attributes via an `environment` or `role`:
```ruby
default_attributes(
'aws-cwlogs' => {
'aws_cwlogs' => {
'region' => 'your_aws_region',
'aws_access_key_id' => 'your_aws_access_key',
'aws_secret_access_key' => 'your_aws_secret_key',
'log_files' => {
'/var/log/syslog' => {
'log' => {
'syslog' => {
'datetime_format' => '%b %d %H:%M:%S',
'file' => '/var/log/syslog',
'buffer_duration' => '5000',
Expand All @@ -35,10 +35,10 @@ default_attributes(

Or you can also configure by declaring it in another cookbook at a higher precedence level:
```ruby
default['aws-cwlogs']['region'] = 'your_aws_region'
default['aws-cwlogs']['aws_access_key_id'] = 'your_aws_access_key'
default['aws-cwlogs']['aws_secret_access_key'] = 'your_aws_secret_key'
default['aws-cwlogs']['log_files']['/var/log/syslog'] = {
default['aws_cwlogs']['region'] = 'your_aws_region'
default['aws_cwlogs']['aws_access_key_id'] = 'your_aws_access_key'
default['aws_cwlogs']['aws_secret_access_key'] = 'your_aws_secret_key'
default['aws_cwlogs']['log']['syslog'] = {
'datetime_format' => '%b %d %H:%M:%S',
'file' => '/var/log/syslog',
'buffer_duration' => '5000',
Expand All @@ -48,7 +48,22 @@ default['aws-cwlogs']['log_files']['/var/log/syslog'] = {
}
```

**Remember**: You can configure as many logs as you need with `log_files` attribute.
Once you defined the attributes, you will need to reference `aws_cwlogs` resource in your recipe:
```ruby
include_recipe 'aws-cloudwatchlogs'

aws_cwlogs 'syslog' do
log node['aws_cwlogs']['log']['syslog']
end

aws_cwlogs 'messages' do
log node['aws_cwlogs']['log']['messages']
end
```

This will create a unique configuration file in AWS CloudWatch Logs that will be stored in `etc/config` directory.

**Remember**: You can configure as many logs as you need with `log` attribute.

**Note**: We are not making use of `data_bags` for AWS Credentials in this recipe at this time.

Expand All @@ -57,7 +72,7 @@ default['aws-cwlogs']['log_files']['/var/log/syslog'] = {
Those attributes used before will generate the AWS CloudWatch Logs configuration below:

```ini
[/var/log/syslog]
[syslog]
datetime_format = %b %d %H:%M:%S
file = /var/log/syslog
buffer_duration = 5000
Expand Down
8 changes: 4 additions & 4 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#

# AWS Credentials
default['aws-cwlogs']['region'] = nil
default['aws-cwlogs']['aws_access_key_id'] = nil
default['aws-cwlogs']['aws_secret_access_key'] = nil
default['aws_cwlogs']['region'] = nil
default['aws_cwlogs']['aws_access_key_id'] = nil
default['aws_cwlogs']['aws_secret_access_key'] = nil

# AWS CloudWatch Logs
default['aws-cwlogs']['path'] = '/var/awslogs'
default['aws_cwlogs']['path'] = '/var/awslogs'
28 changes: 28 additions & 0 deletions libraries/matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# Cookbook Name:: aws-cloudwatchlogs
# Libraries:: matchers
#
# Copyright 2016, Alexandre Malucelli, All Rights Reserved.
#
# 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 defined?(ChefSpec)
def add_aws_cwlogs(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:aws_cwlogs, :add, resource_name)
end

def remove_aws_cwlogs(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:aws_cwlogs, :remove, resource_name)
end
end
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
license 'Apache 2.0'
description 'Installs/Configures AWS CloudWatch Logs Agent'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '1.1.4'
version '1.1.5'
source_url 'https://github.com/amalucelli/chef-cloudwatchlogs' if respond_to? :source_url
issues_url 'https://github.com/amalucelli/chef-cloudwatchlogs/issues' if respond_to? :issues_url
45 changes: 45 additions & 0 deletions providers/aws_cwlogs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# Cookbook Name:: aws-cloudwatchlogs
# Providers:: aws_cwlogs
#
# Copyright 2016, Alexandre Malucelli, All Rights Reserved.
#
# 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.
#

provides :aws_cwlogs if respond_to?(:provides)
use_inline_resources if defined?(use_inline_resources)

action :add do
Chef::Log.debug "Adding configuration for #{new_resource.name}"
template ::File.join(node['aws_cwlogs']['path'], 'etc/config', "#{new_resource.name}.conf") do
owner 'root'
mode 00600
source 'template.conf.erb'
variables ({
logName: new_resource.name,
logConfig: new_resource.log
})
cookbook new_resource.cookbook
notifies :restart, 'service[awslogs]', :delayed
end
end

action :remove do
conf_path = ::File.join(node['aws_cwlogs']['path'], 'etc/config')
Chef::Log.debug "Removing #{new_resource.name} from #{conf_path}"
file ::File.join(node['aws_cwlogs']['path'], 'etc/config', "#{new_resource.name}.conf") do
action :delete
notifies :restart, 'service[awslogs]', :delayed
end
end
19 changes: 9 additions & 10 deletions recipes/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
#

# always keep aws.conf updated
template "#{node['aws-cwlogs']['path']}/etc/aws.conf" do
template "#{node['aws_cwlogs']['path']}/etc/aws.conf" do
source 'aws.conf.erb'
owner 'root'
group 'root'
mode 0600
variables ({
:awsRegion => node['aws-cwlogs']['region'],
:awsAccessKey => node['aws-cwlogs']['aws_access_key_id'],
:awsSecretKey => node['aws-cwlogs']['aws_secret_access_key']
:awsRegion => node['aws_cwlogs']['region'],
:awsAccessKey => node['aws_cwlogs']['aws_access_key_id'],
:awsSecretKey => node['aws_cwlogs']['aws_secret_access_key']
})
end

# always keep logging.conf updated
template "#{node['aws-cwlogs']['path']}/etc/logging.conf" do
template "#{node['aws_cwlogs']['path']}/etc/logging.conf" do
source 'logging.conf.erb'
owner 'root'
group 'root'
Expand All @@ -40,18 +40,17 @@

# always generate awslogs.conf based on default
# attributes related to log files
template "#{node['aws-cwlogs']['path']}/etc/awslogs.conf" do
template "#{node['aws_cwlogs']['path']}/etc/awslogs.conf" do
source 'awslogs.conf.erb'
owner 'root'
group 'root'
mode 0644
variables ({
:logFiles => node['aws-cwlogs']['log_files']
})
end

# always restart aws cloudwatch logs agent
# after the configuration files were updated
service 'awslogs' do
action [:restart]
action [:enable, :restart]
supports :restart => true, :status => true, :start => true, :stop => true
subscribes :restart, "template [#{node['aws_cwlogs']['path']}/etc/awslogs.conf]", :delayed
end
8 changes: 4 additions & 4 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
# limitations under the License.
#

if node['aws-cwlogs']['region'].nil?
if node['aws_cwlogs']['region'].nil?
log('AWS Region is necessary for this cookbook.') { level :error }
return
end

if node['aws-cwlogs']['aws_access_key_id'].nil?
if node['aws_cwlogs']['aws_access_key_id'].nil?
log('AWS Access Key is necessary for this cookbook.') { level :error }
return
end

if node['aws-cwlogs']['aws_secret_access_key'].nil?
if node['aws_cwlogs']['aws_secret_access_key'].nil?
log('AWS Secret Access Key is necessary for this cookbook.') { level :error }
return
end

# only install if it isn't installed
include_recipe 'aws-cloudwatchlogs::install' unless ::File.exist?(node['aws-cwlogs']['path'])
include_recipe 'aws-cloudwatchlogs::install' unless ::File.exist?(node['aws_cwlogs']['path'])
# always reconfigure aws cloudwatch logs configuration files
include_recipe 'aws-cloudwatchlogs::config'
20 changes: 9 additions & 11 deletions recipes/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
#

# create base directory of agent even if it isn't installed yet
directory "#{node['aws-cwlogs']['path']}/etc" do
directory "#{node['aws_cwlogs']['path']}/etc" do
recursive true
end

template "#{node['aws-cwlogs']['path']}/etc/aws.conf" do
template "#{node['aws_cwlogs']['path']}/etc/aws.conf" do
source 'aws.conf.erb'
owner 'root'
group 'root'
mode 0600
variables ({
:awsRegion => node['aws-cwlogs']['region'],
:awsAccessKey => node['aws-cwlogs']['aws_access_key_id'],
:awsSecretKey => node['aws-cwlogs']['aws_secret_access_key']
:awsRegion => node['aws_cwlogs']['region'],
:awsAccessKey => node['aws_cwlogs']['aws_access_key_id'],
:awsSecretKey => node['aws_cwlogs']['aws_secret_access_key']
})
end

template "#{node['aws-cwlogs']['path']}/etc/logging.conf" do
template "#{node['aws_cwlogs']['path']}/etc/logging.conf" do
source 'logging.conf.erb'
owner 'root'
group 'root'
Expand All @@ -46,13 +46,10 @@
owner 'root'
group 'root'
mode 0644
variables ({
:logFiles => node['aws-cwlogs']['log_files']
})
end

# download setup script that will install aws cloudwatch logs agent
remote_file "#{node['aws-cwlogs']['path']}/awslogs-agent-setup.py" do
remote_file "#{node['aws_cwlogs']['path']}/awslogs-agent-setup.py" do
source 'https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py'
owner 'root'
group 'root'
Expand All @@ -61,12 +58,13 @@

# install aws cloudwatch logs agent
execute 'Install CloudWatch Logs Agent' do
command "#{node['aws-cwlogs']['path']}/awslogs-agent-setup.py -n -r #{node['aws-cwlogs']['region']} -c /tmp/awslogs.cfg"
command "#{node['aws_cwlogs']['path']}/awslogs-agent-setup.py -n -r #{node['aws_cwlogs']['region']} -c /tmp/awslogs.cfg"
not_if { system 'pgrep -f awslogs' }
end

# restart the agent service in the end to ensure that
# the agent will run with the custom configurations
service 'awslogs' do
action [:enable, :restart]
supports :restart => true, :status => true, :start => true, :stop => true
end
Loading

0 comments on commit c2a40f0

Please sign in to comment.