Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Accumulator Resources With Definitions (rebased onto develop) #230

Closed
Closed
Show file tree
Hide file tree
Changes from all 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 @@
~FC015
~FC017
14 changes: 13 additions & 1 deletion .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ platforms:
- recipe[yum-epel]

suites:
- name: default
- name: single-node
run_list:
- recipe[runit]
- recipe[graphite_example::single_node]
Expand All @@ -24,3 +24,15 @@ suites:
uwsgi:
workers: 4
listen_http: true
- name: carbon-aggregator
run_list:
- recipe[runit]
- recipe[graphite_example::carbon_aggregator]
- name: carbon-cache
run_list:
- recipe[runit]
- recipe[graphite_example::carbon_cache]
- name: carbon-relay
run_list:
- recipe[runit]
- recipe[graphite_example::carbon_relay]
2 changes: 1 addition & 1 deletion Cheffile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ site 'http://community.opscode.com/api/v1'
cookbook 'apt'
cookbook 'ubuntu'
cookbook 'yum'
cookbook 'runit', git: 'https://github.com/hw-cookbooks/runit.git'
cookbook 'runit', '1.6.0' # version 1.7.0 requires ChefSpec 4
cookbook 'graphite', path: '.'
cookbook 'graphite_example', path: './example/graphite_example'
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
source 'https://rubygems.org'

gem 'rake'
gem 'ohai', '~> 7.4' if RUBY_VERSION < '2' # Fix Ruby 1.9.3 support
gem 'librarian-chef'
gem 'emeril', :group => :release

Expand Down
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ the all begin with an underscore `_`.

It's like a treasure hunt.

## Custom Resources
## Custom Resources and Definitions

### Carbon daemons
Management for the various
Expand All @@ -98,9 +98,9 @@ receive your metrics and write them to disk.
first with one of the `graphite_carbon_*` resources. Multiple
daemons can be run by using multiple resources with names such as
`cache:a`, `cache:b`, etc..
* `graphite_carbon_aggregator`: data driven resource for carbon-aggregator configuration
* `graphite_carbon_cache`: data driven resource for carbon-cache configuration
* `graphite_carbon_relay`: data driven resource for carbon-cache configuration
* `graphite_carbon_aggregator`: data driven definition for carbon-aggregator configuration
* `graphite_carbon_cache`: data driven definition for carbon-cache configuration
* `graphite_carbon_relay`: data driven definition for carbon-cache configuration

### Storage
[Whisper](https://github.com/graphite-project/whisper) is
Expand All @@ -110,7 +110,7 @@ your own wrapper if you live on the edge and prefer [Ceres](https://github.com/g

* `graphite_storage`: makes a directory intended for graphite storage,
installs whisper
* `graphite_storage_schema`: data driven resource for storage schema
* `graphite_storage_schema`: data driven definition for storage schema

### Graphite Web
Write the configuration file for [Graphite Web](https://github.com/graphite-project/graphite-web)
Expand All @@ -127,15 +127,15 @@ Yes it's [writing python via ruby](https://github.com/hw-cookbooks/graphite/blob
A runit service definition is provided to [start a uwsgi process](https://github.com/hw-cookbooks/graphite/blob/master/example/graphite_example/recipes/single_node.rb#L105), but note that choice of web server for proxying to the application server is left up to you. No more hard Apache dependency!

### Accumulators
Due to the graphite config file format, the data driven resources use
an accumulator pattern to find the appropriate resources in the run
context and extract provided configuration data. You should never need
to use these directly, but you're welcome to go crazy.

* `graphite_carbon_conf_accumulator`: lookup named carbon resources in
run context and gather config
* `graphite_storage_conf_accumulator`: lookup named storage schema
resources in run context and gather config
Due to the graphite config file format, the data driven definitions
use an accumulator pattern to accumulate configuration data and inject
it into the final configuration file. You should never need to use
these directly, but you're welcome to go crazy.

* `graphite_carbon_conf_accumulator`: creates *carbon.conf*
configuration file using accumulator pattern.
* `graphite_storage_conf_accumulator`: creates
*storage-schemas.conf* configuration file using accumulator pattern.

If you look at the
[example cookbook recipe](https://github.com/hw-cookbooks/graphite/blob/master/example/graphite_example/recipes/single_node.rb#L6)
Expand Down
12 changes: 12 additions & 0 deletions definitions/graphite_carbon_aggregator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
define :graphite_carbon_aggregator do
carbon_backend = params[:backend]
carbon_config = params[:config]
carbon_action = params[:action]

graphite_carbon_conf_accumulator params[:name] do
type :aggregator
backend carbon_backend
config carbon_config
action carbon_action
end
end
12 changes: 12 additions & 0 deletions definitions/graphite_carbon_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
define :graphite_carbon_cache do
carbon_backend = params[:backend]
carbon_config = params[:config]
carbon_action = params[:action]

graphite_carbon_conf_accumulator params[:name] do
type :cache
backend carbon_backend
config carbon_config
action carbon_action
end
end
86 changes: 86 additions & 0 deletions definitions/graphite_carbon_conf_accumulator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
define :graphite_carbon_conf_accumulator, :action => :create, :file => 'carbon.conf' do
# The backend can never be nil
backend = params[:backend].nil? ? 'whisper' : params[:backend]

# Get the backend package name and its attributes
backend_name, backend_attributes =
if backend.is_a?(Hash)
attrs = backend.dup
[attrs.delete('name'), attrs]
else
[backend, {}]
end

# Install the backend, whisper
backend_pkg_resource =
begin
resources(python_pip: backend_name)
rescue Chef::Exceptions::ResourceNotFound
python_pip backend_name do
Chef::Log.info "Installing storage backend: #{package_name}"
only_if { params[:action] == :create }
action :install
end
end
# Pass the attributes to the package resource
backend_attributes.each do |attr, value|
backend_pkg_resource.send(attr, value)
end

# Create the carbon.conf configuration file
carbon_conf_resource =
begin
resources(template: params[:file])
rescue Chef::Exceptions::ResourceNotFound
template params[:file] do
path "#{node['graphite']['base_dir']}/conf/#{params[:file]}"
cookbook 'graphite'
source 'carbon.conf.erb'
owner node['graphite']['user']
group node['graphite']['group']
mode 0644
variables config: []
action params[:action]
end
end

# Accumulate the configuration data in Chef::Resource::Template#variables
if params.key?(:type) && params[:config].is_a?(Hash)
config_variable = carbon_conf_resource.variables[:config]
conf_obj = config_variable.find { |i| i[:name] == params[:name] }

if conf_obj.nil?
config_variable << {
type: params[:type].to_s,
name: params[:name],
config: params[:config]
}
else
index = config_variable.index(conf_obj)
config_variable[index][:config].merge!(params[:config])
end
end

# For notifications support (only works in Chef >= 12).
# @example
# my_definition = graphite_carbon_conf_accumulator 'default' do
# enable_logrotation: true,
# user: 'graphite',
# max_cache_size: 'inf',
# max_updates_per_second: 500,
# max_creates_per_minute: 50,
# line_receiver_interface: '0.0.0.0',
# line_receiver_port: 2003,
# udp_receiver_port: 2003,
# pickle_receiver_port: 2004,
# enable_udp_listener: true,
# cache_query_port: '7002',
# cache_write_strategy: 'sorted',
# use_flow_control: true,
# log_updates: false,
# log_cache_hits: false,
# whisper_autoflush: false
# end
# my_definition.notifies :run, 'execute[thing]'
carbon_conf_resource
end
12 changes: 12 additions & 0 deletions definitions/graphite_carbon_relay.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
define :graphite_carbon_relay do
carbon_backend = params[:backend]
carbon_config = params[:config]
carbon_action = params[:action]

graphite_carbon_conf_accumulator params[:name] do
type :relay
backend carbon_backend
config carbon_config
action carbon_action
end
end
46 changes: 46 additions & 0 deletions definitions/graphite_storage_conf_accumulator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
define :graphite_storage_conf_accumulator, :action => :create, :file => 'storage-schemas.conf' do

# Create the storage-schemas.conf configuration file
storage_conf_resource =
begin
resources(template: params[:file])
rescue Chef::Exceptions::ResourceNotFound
template params[:file] do
path "#{node['graphite']['base_dir']}/conf/#{params[:file]}"
cookbook 'graphite'
source 'carbon.conf.erb'
owner node['graphite']['user']
group node['graphite']['group']
mode 0644
variables config: []
action params[:action]
end
end

# Accumulate the configuration data in Chef::Resource::Template#variables
if params[:config].is_a?(Hash)
config_variable = storage_conf_resource.variables[:config]
conf_obj = config_variable.find { |i| i[:name] == params[:name] }

if conf_obj.nil?
config_variable << {
name: params[:name],
config: params[:config]
}
else
index = config_variable.index(conf_obj)
config_variable[index][:config].merge!(params[:config])
end
end

# For notifications support (only works in Chef >= 12).
# @example
# my_definition = graphite_storage_conf_accumulator 'carbon' do
# config(
# pattern: '^carbon\.',
# retentions: '60:90d'
# )
# end
# my_definition.notifies :run, 'execute[thing]'
storage_conf_resource
end
9 changes: 9 additions & 0 deletions definitions/graphite_storage_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define :graphite_storage_schema do
carbon_config = params[:config]
carbon_action = params[:action]

graphite_storage_conf_accumulator params[:name] do
config carbon_config
action carbon_action
end
end
2 changes: 2 additions & 0 deletions example/graphite_example/recipes/carbon_aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
]
})
end

graphite_service "aggregator"
2 changes: 2 additions & 0 deletions example/graphite_example/recipes/carbon_relay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
]
})
end

graphite_service "relay"
4 changes: 2 additions & 2 deletions libraries/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def delete_graphite_storage(name)
ChefSpec::Matchers::ResourceMatcher.new(:graphite_storage, :delete, name)
end

def create_graphite_carbon_conf_accumulator(name)
ChefSpec::Matchers::ResourceMatcher.new(:graphite_carbon_conf_accumulator, :create, name)
def create_graphite_carbon_conf_accumulator(_name)
ChefSpec::Matchers::ResourceMatcher.new(:template, :create, 'carbon.conf')
end

def enable_runit_service(resource_name)
Expand Down
41 changes: 0 additions & 41 deletions libraries/mixins.rb

This file was deleted.

Loading