From 11dbde1b2418f2bd8ef94be7b73c5a13a3e6162a Mon Sep 17 00:00:00 2001 From: mh Date: Mon, 26 Jan 2015 22:09:41 +0100 Subject: [PATCH] make it a lot easier to define any kind of configuration values So far one would have had to add for each configuration setting to set a dedicated parameter and add an ini_setting. This change introduces a simple hash structure to set configuration values. This hash matches the influxdb toml file structure. Unfortunately puppet doesn't yet have an easy way (https://github.com/puppetlabs/puppetlabs-inifile/pull/129) to do things like that. Which means that I had to trick around a little bit with puppet data structures. This can be removed once create_ini_settings got merged into the inifile module, until then this is a workaround, that makes the configuration much more flexible. --- .fixtures.yml | 1 + Puppetfile | 1 + Puppetfile.lock | 2 + manifests/config.pp | 221 ++------------------- manifests/config_section.pp | 11 + manifests/config_section_values.pp | 25 +++ manifests/config_value.pp | 21 ++ manifests/init.pp | 43 +--- manifests/params.pp | 62 +----- metadata.json | 1 + spec/classes/influxdb_spec.rb | 50 ++++- spec/defines/config_section_spec.rb | 38 ++++ spec/defines/config_section_values_spec.rb | 43 ++++ spec/defines/config_value_spec.rb | 55 +++++ 14 files changed, 267 insertions(+), 307 deletions(-) create mode 100644 manifests/config_section.pp create mode 100644 manifests/config_section_values.pp create mode 100644 manifests/config_value.pp create mode 100644 spec/defines/config_section_spec.rb create mode 100644 spec/defines/config_section_values_spec.rb create mode 100644 spec/defines/config_value_spec.rb diff --git a/.fixtures.yml b/.fixtures.yml index 92436be..508e2f3 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -2,6 +2,7 @@ fixtures: repositories: inifile: https://github.com/puppetlabs/puppetlabs-inifile + stdlib: https://github.com/puppetlabs/puppetlabs-stdlib staging: https://github.com/nanliu/puppet-staging symlinks: influxdb: "#{source_dir}" diff --git a/Puppetfile b/Puppetfile index cb63748..242d901 100644 --- a/Puppetfile +++ b/Puppetfile @@ -2,3 +2,4 @@ forge 'http://forge.puppetlabs.com' mod 'nanliu/staging', '>= 0.4.0' mod 'puppetlabs/inifile', '>= 1.0.0' +mod 'puppetlabs/stdlib', '>= 4.5.1' diff --git a/Puppetfile.lock b/Puppetfile.lock index 26b5799..1874aea 100644 --- a/Puppetfile.lock +++ b/Puppetfile.lock @@ -3,8 +3,10 @@ FORGE specs: nanliu/staging (0.4.0) puppetlabs/inifile (1.0.3) + puppetlabs/stdlib (4.5.1) DEPENDENCIES nanliu/staging (>= 0.4.0) puppetlabs/inifile (>= 1.0.0) + puppetlabs/stdlib (>= 4.5.1) diff --git a/manifests/config.pp b/manifests/config.pp index ccc9147..c4e6a3e 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -1,213 +1,18 @@ # == Class: influxdb::config # More information on these settings available at: http://influxdb.org/docs/configuration.html +# create config options based on a hash or hierdata that looks like: +# +# influxdb::configuration: +# default: +# bind-address: '"0.0.0.0"' +# storage: +# dir: '/tmp/bla' class influxdb::config { - ini_setting { 'hostname': - section => '', - setting => 'hostname', - value => "\"${influxdb::hostname}\"", - } - - ini_setting { 'bind_address': - section => '', - setting => 'bind-address', - value => "\"${influxdb::bind_address}\"", - } - - # [logging] - ini_setting { 'logging_level': - section => 'logging', - setting => 'level', - value => "\"${influxdb::logging_level}\"", - } - - ini_setting { 'logging_file': - section => 'logging', - setting => 'file', - value => "\"${influxdb::logging_file}\"", - } - - # [admin] - ini_setting { 'admin_port': - section => 'admin', - setting => 'port', - value => $influxdb::admin_port, - } - - ini_setting { 'admin_assets': - section => 'admin', - setting => 'assets', - value => "\"${influxdb::admin_assets}\"", - } - - # [api] - ini_setting { 'api_port': - section => 'api', - setting => 'port', - value => $influxdb::api_port, - } - - ini_setting { 'api_read_timeout': - section => 'api', - setting => 'read-timeout', - value => "\"${influxdb::api_read_timeout}\"", - } - - # [raft] - ini_setting { 'raft_port': - section => 'raft', - setting => 'port', - value => $influxdb::raft_port, - } - - ini_setting { 'raft_dir': - section => 'raft', - setting => 'dir', - value => "\"${influxdb::raft_dir}\"", - } - - ini_setting { 'raft_election_timeout': - section => 'raft', - setting => 'election-timeout', - value => "\"${influxdb::raft_election_timeout}\"", - } - - # [storage] - ini_setting { 'storage_dir': - section => 'storage', - setting => 'dir', - value => "\"${influxdb::storage_dir}\"", - } - - ini_setting { 'storage_write_buffer_size': - section => 'storage', - setting => 'write-buffer-size', - value => $influxdb::storage_write_buffer_size, - } - - # [cluster] - ini_setting { 'cluster_seed_servers': - section => 'cluster', - setting => 'seed-servers', - value => $influxdb::cluster_seed_servers, - } - - ini_setting { 'cluster_protobuf_port': - section => 'cluster', - setting => 'protobuf_port', - value => $influxdb::cluster_protobuf_port, - } - - ini_setting { 'cluster_protobuf_timeout': - section => 'cluster', - setting => 'protobuf_timeout', - value => "\"${influxdb::cluster_protobuf_timeout}\"", - } - - ini_setting { 'cluster_protobuf_heartbeat': - section => 'cluster', - setting => 'protobuf_heartbeat', - value => "\"${influxdb::cluster_protobuf_heartbeat}\"", - } - - ini_setting { 'cluster_protobuf_min_backoff': - section => 'cluster', - setting => 'protobuf_min_backoff', - value => "\"${influxdb::cluster_protobuf_min_backoff}\"", - } - - ini_setting { 'cluster_protobuf_max_backoff': - section => 'cluster', - setting => 'protobuf_max_backoff', - value => "\"${influxdb::cluster_protobuf_max_backoff}\"", - } - - ini_setting { 'cluster_write_buffer_size': - section => 'cluster', - setting => 'write-buffer-size', - value => $influxdb::cluster_write_buffer_size, - } - - ini_setting { 'cluster_max_response_buffer_size': - section => 'cluster', - setting => 'max-response-buffer-size', - value => $influxdb::cluster_max_response_buffer_size, - } - - ini_setting { 'cluster_concurrent_shard_query_limit': - section => 'cluster', - setting => 'concurrent-shard-query-limit', - value => $influxdb::cluster_concurrent_shard_query_limit, - } - - # [leveldb] - ini_setting { 'leveldb_max_open_files': - section => 'leveldb', - setting => 'max-open-files', - value => $influxdb::leveldb_max_open_files, - } - - ini_setting { 'leveldb_lru_cache_size': - section => 'leveldb', - setting => 'lru-cache-size', - value => "\"${influxdb::leveldb_lru_cache_size}\"", - } - - ini_setting { 'leveldb_max_open_shards': - section => 'leveldb', - setting => 'max-open-shards', - value => $influxdb::leveldb_max_open_shards, - } - - ini_setting { 'leveldb_point_batch_size': - section => 'leveldb', - setting => 'point-batch-size', - value => $influxdb::leveldb_point_batch_size, - } - - ini_setting { 'leveldb_point_write_size': - section => 'leveldb', - setting => 'point-write-size', - value => $influxdb::leveldb_point_write_size, - } - - # [sharding] - - # [wal] - ini_setting { 'wal_dir': - section => 'wal', - setting => 'dir', - value => "\"${influxdb::wal_dir}\"", - } - - ini_setting { 'wal_flush_after': - section => 'wal', - setting => 'flush-after', - value => $influxdb::wal_flush_after, - } - - ini_setting { 'wal_bookmark_after': - section => 'wal', - setting => 'bookmark-after', - value => $influxdb::wal_bookmark_after, - } - - ini_setting { 'wal_index_after': - section => 'wal', - setting => 'index-after', - value => $influxdb::wal_index_after, - } - - ini_setting { 'wal_requests_per_logfile': - section => 'wal', - setting => 'requests-per-logfile', - value => $influxdb::wal_requests_per_logfile, - } - - # defaults for all settings - Ini_setting { - ensure => present, - path => $influxdb::config_path, - notify => Service['influxdb'], - require => Package['influxdb'], + if !empty($influxdb::configuration) { + $sections = keys($influxdb::configuration) + influxdb::config_section{ + $sections: + values => $influxdb::configuration + } } } diff --git a/manifests/config_section.pp b/manifests/config_section.pp new file mode 100644 index 0000000..5e2faa3 --- /dev/null +++ b/manifests/config_section.pp @@ -0,0 +1,11 @@ +# manage the different config sections +# this is just a wrapper to shuffle data around +define influxdb::config_section( + $values, +){ + $names = prefix(keys($values[$name]),"${name}@") + influxdb::config_section_values{ + $names: + values => $values[$name] + } +} diff --git a/manifests/config_section_values.pp b/manifests/config_section_values.pp new file mode 100644 index 0000000..b1d296c --- /dev/null +++ b/manifests/config_section_values.pp @@ -0,0 +1,25 @@ +# manage the values of a certain config section. +# this is just a wrapper to shuffle data around +define influxdb::config_section_values( + $values, +) { + if $name =~ /@/ and !($name =~ /^default@/) { + $tmp = split($name,'@') + $section = $tmp[0] + $setting = $tmp[1] + } else { + $section = '' + if $name =~ /^default@/ { + $tmp = split($name,'@') + $setting = $tmp[1] + } else { + $setting = $name + } + } + influxdb::config_value{ + $name: + section => $section, + setting => $setting, + value => $values[$setting] + } +} diff --git a/manifests/config_value.pp b/manifests/config_value.pp new file mode 100644 index 0000000..1f70873 --- /dev/null +++ b/manifests/config_value.pp @@ -0,0 +1,21 @@ +# set a certain config option +# looks like: +# influxdb::config_option{'bind_address': +# section => '', +# setting => 'bind-address', +# value => '"0.0.0.0"' +define influxdb::config_value( + $section, + $value, + $setting = $name, +) { + ini_setting { $name: + ensure => present, + section => $section, + setting => $setting, + value => $value, + path => $influxdb::config_path, + notify => Service['influxdb'], + require => Package['influxdb'], + } +} diff --git a/manifests/init.pp b/manifests/init.pp index 929c9b4..d7a6371 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,45 +1,12 @@ # == Class: influxdb class influxdb ( - $ensure = $influxdb::params::ensure, - $version = $influxdb::params::version, - $install_from_repository = $influxdb::params::install_from_repository, - $config_path = $influxdb::params::config_path, - $hostname = $influxdb::params::hostname, - $bind_address = $influxdb::params::bind_address, - $logging_level = $influxdb::params::logging_level, - $logging_file = $influxdb::params::logging_file, - $admin_port = $influxdb::params::admin_port, - $admin_assets = $influxdb::params::admin_assets, - $api_port = $influxdb::params::api_port, - $api_read_timeout = $influxdb::params::api_read_timeout, - $raft_port = $influxdb::params::raft_port, - $raft_dir = $influxdb::params::raft_dir, - $raft_election_timeout = $influxdb::params::raft_election_timeout, - $storage_dir = $influxdb::params::storage_dir, - $storage_write_buffer_size = $influxdb::params::storage_write_buffer_size, - $cluster_seed_servers = $influxdb::params::cluster_seed_servers, - $cluster_protobuf_port = $influxdb::params::cluster_protobuf_port, - $cluster_protobuf_timeout = $influxdb::params::cluster_protobuf_timeout, - $cluster_protobuf_heartbeat = $influxdb::params::cluster_protobuf_heartbeat, - $cluster_protobuf_min_backoff = $influxdb::params::cluster_protobuf_min_backoff, - $cluster_protobuf_max_backoff = $influxdb::params::cluster_protobuf_max_backoff, - $cluster_write_buffer_size = $influxdb::params::cluster_write_buffer_size, - $cluster_max_response_buffer_size = $influxdb::params::cluster_max_response_buffer_size, - $cluster_concurrent_shard_query_limit = $influxdb::params::cluster_concurrent_shard_query_limit, - $leveldb_max_open_files = $influxdb::params::leveldb_max_open_files, - $leveldb_lru_cache_size = $influxdb::params::leveldb_lru_cache_size, - $leveldb_max_open_shards = $influxdb::params::leveldb_max_open_shards, - $leveldb_point_batch_size = $influxdb::params::leveldb_point_batch_size, - $leveldb_write_batch_size = $influxdb::params::leveldb_write_batch_size, - $wal_dir = $influxdb::params::wal_dir, - $wal_flush_after = $influxdb::params::wal_flush_after, - $wal_bookmark_after = $influxdb::params::wal_bookmark_after, - $wal_index_after = $influxdb::params::wal_index_after, - $wal_requests_per_logfile = $influxdb::params::wal_requests_per_logfile, + $ensure = $influxdb::params::ensure, + $version = $influxdb::params::version, + $install_from_repository = $influxdb::params::install_from_repository, + $config_path = $influxdb::params::config_path, + $configuration = $influxdb::params::configuration, ) inherits influxdb::params { - class { 'influxdb::config': } class { 'influxdb::install': } class { 'influxdb::service': } - } diff --git a/manifests/params.pp b/manifests/params.pp index 003d5df..0bbd244 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -1,61 +1,9 @@ # == Class: influxdb::params # DO NOT CALL DIRECTLY class influxdb::params { - $ensure = 'installed' - $version = 'latest' - $install_from_repository = false - $config_path = '/opt/influxdb/shared/config.toml' - - $hostname = $::hostname - $bind_address = '0.0.0.0' - - # [logging] - $logging_level = 'info' - $logging_file = '/opt/influxdb/shared/influxdb.log' - - # [admin] - $admin_port = '8083' - $admin_assets = '/opt/influxdb/current/admin' - - # [api] - $api_port = '8086' - $api_read_timeout = '5s' - - # [input_plugins] - - # [raft] - $raft_port = '8090' - $raft_dir = '/opt/influxdb/shared/data/raft' - $raft_election_timeout = '1s' - - # [storage] - $storage_dir = '/opt/influxdb/shared/data/db' - $storage_write_buffer_size = '10000' - - # [cluster] - $cluster_seed_servers = '[]' - $cluster_protobuf_port = '8099' - $cluster_protobuf_timeout = '2s' - $cluster_protobuf_heartbeat = '200ms' - $cluster_protobuf_min_backoff = '1s' - $cluster_protobuf_max_backoff = '10s' - $cluster_write_buffer_size = '10000' - $cluster_max_response_buffer_size = '100' - $cluster_concurrent_shard_query_limit = '10' - - # [leveldb] - $leveldb_max_open_files = '40' - $leveldb_lru_cache_size = '200m' - $leveldb_max_open_shards = '0' - $leveldb_point_batch_size = '100' - $leveldb_point_write_size = '5000000' - - # [sharding] - - # [wal] - $wal_dir = '/opt/influxdb/shared/data/wal' - $wal_flush_after = '0' - $wal_bookmark_after = '0' - $wal_index_after = '1000' - $wal_requests_per_logfile = '10000' + $ensure = 'installed' + $version = 'latest' + $install_from_repository = false + $config_path = '/opt/influxdb/shared/config.toml' + $configuration = {} } diff --git a/metadata.json b/metadata.json index 32db868..60e1405 100644 --- a/metadata.json +++ b/metadata.json @@ -10,6 +10,7 @@ "dependencies": [ { "name": "nanliu/staging", "version_requirement": ">= 0.4.0" }, { "name": "puppetlabs/inifile", "version_requirement": ">= 1.0.0" } + { "name": "puppetlabs/stdlib", "version_requirement": ">= 4.5.1" } ], "operatingsystem_support": [ { diff --git a/spec/classes/influxdb_spec.rb b/spec/classes/influxdb_spec.rb index 87d293f..bb5f427 100644 --- a/spec/classes/influxdb_spec.rb +++ b/spec/classes/influxdb_spec.rb @@ -2,11 +2,53 @@ describe 'influxdb', :type => 'class' do let(:facts) { { :osfamily => 'Debian' } } + context 'default' do + it { should contain_class("influxdb::params") } - it { should contain_class("influxdb::params") } + it { should contain_influxdb__config } + it { should contain_influxdb__install } + it { should contain_influxdb__service } + end - it { should contain_influxdb__config } - it { should contain_influxdb__install } - it { should contain_influxdb__service } + context 'with configuration options' do + let(:params) { + { + :configuration => { + 'default' => { + 'bind-address' => '"0.0.0.0"', + }, + 'storage' => { + 'dir' => '/tmp/bla', + } + } + } + } + + it { should contain_class("influxdb::params") } + + it { should contain_influxdb__config } + it { should contain_influxdb__install } + it { should contain_influxdb__service } + + # lowlevel test to prove our configuration hash + it { should contain_ini_setting('default@bind-address').with( + :section => '', + :value => '"0.0.0.0"', + :setting => 'bind-address', + :ensure => 'present', + :path => '/opt/influxdb/shared/config.toml', + :notify => 'Service[influxdb]', + :require => 'Package[influxdb]', + )} + it { should contain_ini_setting('storage@dir').with( + :section => 'storage', + :value => '/tmp/bla', + :setting => 'dir', + :ensure => 'present', + :path => '/opt/influxdb/shared/config.toml', + :notify => 'Service[influxdb]', + :require => 'Package[influxdb]', + )} + end end diff --git a/spec/defines/config_section_spec.rb b/spec/defines/config_section_spec.rb new file mode 100644 index 0000000..552230d --- /dev/null +++ b/spec/defines/config_section_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' +describe 'influxdb::config_section' do + context 'by default' do + let(:title) { 'default' } + let(:params){ + { + :values => { 'default' => { 'bind-address' => '"0.0.0.0"' } } + } + } + it { should contain_influxdb__config_section_values('default@bind-address').with( + :values => { 'bind-address' => '"0.0.0.0"' } + )} + end + context 'with different params' do + let(:title) { 'storage' } + let(:params){ + { + :values => { 'storage' => { 'dir' => '"/tmp/dir"' } } + } + } + it { should contain_influxdb__config_section_values('storage@dir').with( + :values => { 'dir' => '"/tmp/dir"' }, + )} + end + context 'with different params 2' do + let(:title) { 'storage' } + let(:params){ + { + :values => { 'storage' => { 'dir' => '"/tmp/dir"' }, + 'default' => { 'bind-address' => '"0.0.0.0"' } } + } + } + it { should contain_influxdb__config_section_values('storage@dir').with( + :values => { 'dir' => '"/tmp/dir"' } + )} + end +end + diff --git a/spec/defines/config_section_values_spec.rb b/spec/defines/config_section_values_spec.rb new file mode 100644 index 0000000..f176118 --- /dev/null +++ b/spec/defines/config_section_values_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' +describe 'influxdb::config_section_values' do + context 'by default' do + let(:title) { 'bind-address' } + let(:params){ + { + :values => { 'bind-address' => '"0.0.0.0"' } + } + } + it { should contain_influxdb__config_value('bind-address').with( + :section => '', + :setting => 'bind-address', + :value => '"0.0.0.0"', + )} + end + context 'as default section' do + let(:title) { 'default@bind-address' } + let(:params){ + { + :values => { 'bind-address' => '"0.0.0.0"' } + } + } + it { should contain_influxdb__config_value('default@bind-address').with( + :section => '', + :setting => 'bind-address', + :value => '"0.0.0.0"', + )} + end + context 'with different params' do + let(:title) { 'storage@dir' } + let(:params){ + { + :values => { 'dir' => '"/tmp/dir"' } + } + } + it { should contain_influxdb__config_value('storage@dir').with( + :section => 'storage', + :setting => 'dir', + :value => '"/tmp/dir"', + )} + end +end + diff --git a/spec/defines/config_value_spec.rb b/spec/defines/config_value_spec.rb new file mode 100644 index 0000000..deba964 --- /dev/null +++ b/spec/defines/config_value_spec.rb @@ -0,0 +1,55 @@ +require 'spec_helper' +describe 'influxdb::config_value' do + let(:facts) { + { + :osfamily => 'RedHat', + :architecture => 'x86_64', + } + } + context 'by default' do + let(:title) { 'bind-address' } + let(:params){ + { + :section => '', + :value => '"0.0.0.0"' + } + } + let(:pre_condition){ + 'include influxdb' + } + it { should contain_ini_setting('bind-address').with( + :section => '', + :setting => 'bind-address', + :value => '"0.0.0.0"', + :ensure => 'present', + :path => '/opt/influxdb/shared/config.toml', + :notify => 'Service[influxdb]', + :require => 'Package[influxdb]', + )} + end + context 'with different params' do + let(:title) { 'storage_dir' } + let(:params){ + { + :setting => 'dir', + :section => 'storage', + :value => '"/tmp/dir"', + } + } + let(:pre_condition){ + 'class{"influxdb": + config_path => "/tmp/config.toml", + }' + } + it { should contain_ini_setting('storage_dir').with( + :section => 'storage', + :setting => 'dir', + :value => '"/tmp/dir"', + :ensure => 'present', + :path => '/tmp/config.toml', + :notify => 'Service[influxdb]', + :require => 'Package[influxdb]', + )} + end +end +