From ed21a7e62c626429d3a1fa08e65760a2067a996e Mon Sep 17 00:00:00 2001 From: Rianol Jou Date: Fri, 2 Dec 2016 16:50:00 +0800 Subject: [PATCH] Update params --- manifests/init.pp | 175 +++++++++++++++++++++++++------------- spec/classes/init_spec.rb | 114 ++++++++++++++++--------- 2 files changed, 190 insertions(+), 99 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index 2a39f5c..bd95e69 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -4,8 +4,20 @@ # # === Parameters # -# [*version*] -# Specify version of nomad binary to download. +# [*arch*] +# Architecture of nomad binary to download. +# +# [*archive_path*] +# Path used when installing nomad via the url. +# +# [*bin_dir*] +# Directory to create the symlink to the nomad binary in. +# +# [*config_defaults*] +# Configuration defaults hash. Gets merged with config_hash. +# +# [*config_dir*] +# Directory to place nomad configuration files in. # # [*config_hash*] # Use this to populate the JSON config file for nomad. @@ -13,79 +25,127 @@ # [*config_mode*] # Use this to set the JSON config file mode for nomad. # -# [*pretty_config*] -# Generates a human readable JSON config file. Defaults to `false`. +# [*download_extension*] +# The extension of the archive file containing the nomad binary to download. # -# [*pretty_config_indent*] -# Toggle indentation for human readable JSON file. Defaults to `4`. +# [*download_url*] +# Fully qualified url to the location of the archive file containing the nomad binary. +# +# [*download_url_base*] +# Base url to the location of the archive file containing the nomad binary. +# +# [*extra_groups*] +# Extra groups to add the nomad system user to. +# +# [*extra_options*] +# Extra arguments to be passed to the nomad agent +# +# [*group*] +# Name of the group that should own the nomad configuration files. +# +# [*init_style*] +# What style of init system your system uses. Set to 'unmanaged' to disable +# managing init system files for the nomad service entirely. # # [*install_method*] # Valid strings: `package` - install via system package # `url` - download and extract from a url. Defaults to `url`. # `none` - disable install. # -# [*package_name*] -# Only valid when the install_method == package. Defaults to `nomad`. +# [*log_file*] +# Path used when writing log to. +# +# [*manage_group*] +# Whether to create/manage the group that should own the nomad configuration files. +# +# [*manage_service*] +# Whether to manage the nomad service. +# +# [*manage_user*] +# Whether to create/manage the user that should own nomad's configuration files. +# +# [*os*] +# OS component in the name of the archive file containing the nomad binary. # # [*package_ensure*] # Only valid when the install_method == package. Defaults to `latest`. # +# [*package_name*] +# Only valid when the install_method == package. Defaults to `nomad`. +# +# [*pretty_config*] +# Generates a human readable JSON config file. Defaults to `false`. +# +# [*pretty_config_indent*] +# Toggle indentation for human readable JSON file. Defaults to `4`. +# +# [*purge_config_dir*] +# Purge config files no longer generated by Puppet # # [*restart_on_change*] # Determines whether to restart nomad agent on $config_hash changes. -# This will not affect reloads when service, check or watch configs change. -# Defaults to `true`. +# Defaults to `true`. # -# [*extra_options*] -# Extra arguments to be passed to the nomad agent +# [*service_enable*] +# Whether to enable the nomad service to start at boot. # -# [*init_style*] -# What style of init system your system uses. +# [*service_ensure*] +# Whether the nomad service should be running or not. +# +# [*user*] +# Name of the user that should own the nomad configuration files. +# +# [*version*] +# Specify version of nomad binary to download. # -# [*purge_config_dir*] -# Purge config files no longer generated by Puppet class nomad ( - $manage_user = true, - $user = 'nomad', - $manage_group = true, - $extra_groups = [], - $purge_config_dir = true, - $group = 'nomad', - $bin_dir = '/usr/local/bin', - $arch = $nomad::params::arch, - $version = $nomad::params::version, - $install_method = $nomad::params::install_method, - $os = $nomad::params::os, - $download_url = undef, - $download_url_base = $nomad::params::download_url_base, - $download_extension = $nomad::params::download_extension, - $package_name = $nomad::params::package_name, - $package_ensure = $nomad::params::package_ensure, - $config_dir = '/etc/nomad', - $extra_options = '', - $config_hash = {}, - $config_defaults = {}, - $config_mode = $nomad::params::config_mode, - $pretty_config = false, - $pretty_config_indent = 4, - $service_enable = true, - $service_ensure = 'running', - $manage_service = true, - $restart_on_change = true, - $init_style = $nomad::params::init_style, + $arch = $::nomad::params::arch, + $archive_path = $::nomad::params::archive_path, + $bin_dir = $::nomad::params::bin_dir, + $config_defaults = $::nomad::params::config_defaults, + $config_dir = $::nomad::params::config_dir, + $config_hash = $::nomad::params::config_hash, + $config_mode = $::nomad::params::config_mode, + $download_extension = $::nomad::params::download_extension, + $download_url = $::nomad::params::download_url, + $download_url_base = $::nomad::params::download_url_base, + $extra_groups = $::nomad::params::extra_groups, + $extra_options = $::nomad::params::extra_options, + $group = $::nomad::params::group, + $init_style = $::nomad::params::init_style, + $install_method = $::nomad::params::install_method, + $log_file = $::nomad::params::log_file, + $manage_group = $::nomad::params::manage_group, + $manage_service = $::nomad::params::manage_service, + $manage_user = $::nomad::params::manage_user, + $os = $::nomad::params::os, + $package_ensure = $::nomad::params::package_ensure, + $package_name = $::nomad::params::package_name, + $pretty_config = $::nomad::params::pretty_config, + $pretty_config_indent = $::nomad::params::pretty_config_indent, + $purge_config_dir = $::nomad::params::purge_config_dir, + $restart_on_change = $::nomad::params::restart_on_change, + $service_enable = $::nomad::params::service_enable, + $service_ensure = $::nomad::params::service_ensure, + $user = $::nomad::params::user, + $version = $::nomad::params::version, ) inherits nomad::params { + # lint:ignore:140chars $real_download_url = pick($download_url, "${download_url_base}${version}/${package_name}_${version}_${os}_${arch}.${download_extension}") + # lint:endignore - validate_bool($purge_config_dir) - validate_bool($manage_user) - validate_array($extra_groups) - validate_bool($manage_service) - validate_bool($restart_on_change) validate_hash($config_hash) validate_hash($config_defaults) + validate_array($extra_groups) + validate_bool($manage_group) + validate_bool($manage_service) + validate_bool($manage_user) validate_bool($pretty_config) validate_integer($pretty_config_indent) + validate_bool($purge_config_dir) + validate_bool($restart_on_change) + validate_bool($service_enable) $config_hash_real = deep_merge($config_defaults, $config_hash) validate_hash($config_hash_real) @@ -96,19 +156,18 @@ $data_dir = undef } - - if ($config_hash_real['ports'] and $config_hash_real['ports']['rpc']) { - $rpc_port = $config_hash_real['ports']['rpc'] + if ($config_hash_real['ports'] and $config_hash_real['ports']['http']) { + $http_port = $config_hash_real['ports']['http'] } else { - $rpc_port = 8400 + $http_port = 4646 } - if ($config_hash_real['addresses'] and $config_hash_real['addresses']['rpc']) { - $rpc_addr = $config_hash_real['addresses']['rpc'] - } elsif ($config_hash_real['client_addr']) { - $rpc_addr = $config_hash_real['client_addr'] + if ($config_hash_real['addresses'] and $config_hash_real['addresses']['http']) { + $http_addr = $config_hash_real['addresses']['http'] + } elsif ($config_hash_real['bind_addr']) { + $http_addr = $config_hash_real['bind_addr'] } else { - $rpc_addr = $::ipaddress_lo + $http_addr = $::ipaddress_lo } $notify_service = $restart_on_change ? { diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index 0daf3ac..1a5b64c 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -15,44 +15,79 @@ end # Installation Stuff - context 'On an unsupported arch' do - let(:facts) {{ :architecture => 'bogus' }} + context 'On an unsupported architecture' do + let(:facts) {{ :architecture => '__ARCHITECTURE__' }} let(:params) {{ :install_method => 'package' }} it { expect { should compile }.to raise_error(/Unsupported kernel architecture:/) } end - context 'When not specifying whether to purge config' do - it { should contain_file('/etc/nomad').with(:purge => true,:recurse => true) } + context 'When passing a non-bool as manage_group' do + let(:params) {{ + :manage_group => '__STRING__' + }} + it { expect { should compile }.to raise_error(/is not a boolean/) } end - context 'When passing a non-bool as purge_config_dir' do + context 'When passing a non-bool as manage_service' do let(:params) {{ - :purge_config_dir => 'hello' + :manage_service => '__STRING__' }} it { expect { should compile }.to raise_error(/is not a boolean/) } end - context 'When passing a non-bool as manage_service' do + context 'When passing a non-bool as manage_user' do + let(:params) {{ + :manage_user => '__STRING__' + }} + it { expect { should compile }.to raise_error(/is not a boolean/) } + end + + context 'When passing a non-integer as pretty_config_indent' do + let(:params) {{ + :pretty_config_indent => '__STRING__' + }} + it { expect { should compile }.to raise_error(/Expected first argument to be an Integer/) } + end + + context 'When passing a non-bool as pretty_config' do + let(:params) {{ + :pretty_config => '__STRING__' + }} + it { expect { should compile }.to raise_error(/is not a boolean/) } + end + + context 'When not specifying purge_config_dir' do + it { should contain_file('/etc/nomad').with(:purge => true, :recurse => true) } + end + + context 'When passing a non-bool as purge_config_dir' do let(:params) {{ - :manage_service => 'hello' + :purge_config_dir => '__STRING__' }} it { expect { should compile }.to raise_error(/is not a boolean/) } end - context 'When disable config purging' do + context 'When purge_config_dir disabled' do let(:params) {{ :purge_config_dir => false }} it { should contain_class('nomad::config').with(:purge => false) } end - context 'nomad::config should notify nomad::run_service' do + context 'When not specifying restart_on_change' do it { should contain_class('nomad::config').that_notifies(['Class[nomad::run_service]']) } end - context 'nomad::config should not notify nomad::run_service on config change' do + context 'When passing a non-bool as restart_on_change' do + let(:params) {{ + :restart_on_change => '__STRING__' + }} + it { expect { should compile }.to raise_error(/is not a boolean/) } + end + + context 'When restart_on_change disabled' do let(:params) {{ :restart_on_change => false }} @@ -63,22 +98,22 @@ let(:params) {{ :install_method => 'package' }} - it { should contain_package('nomad').with(:ensure => 'latest') } + it { should contain_package('nomad').with(:ensure => :latest) } end context 'When requesting to install via a custom package and version' do let(:params) {{ :install_method => 'package', - :package_ensure => 'specific_release', - :package_name => 'custom_nomad_package' + :package_ensure => '__PACKAGE_ENSURE__', + :package_name => '__PACKAGE_NAME__' }} - it { should contain_package('custom_nomad_package').with(:ensure => 'specific_release') } + it { should contain_package('__PACKAGE_NAME__').with(:ensure => '__PACKAGE_ENSURE__') } end context "When installing via URL by default" do it { should contain_archive('/opt/puppet-archive/nomad-0.5.0.zip').with(:source => 'https://releases.hashicorp.com/nomad/0.5.0/nomad_0.5.0_linux_amd64.zip') } - it { should contain_file('/opt/puppet-archive').with(:ensure => 'directory') } - it { should contain_file('/opt/puppet-archive/nomad-0.5.0').with(:ensure => 'directory') } + it { should contain_file('/opt/puppet-archive').with(:ensure => :directory) } + it { should contain_file('/opt/puppet-archive/nomad-0.5.0').with(:ensure => :directory) } it { should contain_file('/usr/local/bin/nomad').that_notifies('Class[nomad::run_service]') } end @@ -87,8 +122,8 @@ :archive_path => '/usr/share/puppet-archive', }} it { should contain_archive('/usr/share/puppet-archive/nomad-0.5.0.zip').with(:source => 'https://releases.hashicorp.com/nomad/0.5.0/nomad_0.5.0_linux_amd64.zip') } - it { should contain_file('/usr/share/puppet-archive').with(:ensure => 'directory') } - it { should contain_file('/usr/share/puppet-archive/nomad-0.5.0').with(:ensure => 'directory') } + it { should contain_file('/usr/share/puppet-archive').with(:ensure => :directory) } + it { should contain_file('/usr/share/puppet-archive/nomad-0.5.0').with(:ensure => :directory) } it { should contain_file('/usr/local/bin/nomad').that_notifies('Class[nomad::run_service]') } end @@ -109,9 +144,9 @@ context "When installing via URL by with a custom url" do let(:params) {{ - :download_url => 'http://myurl', + :download_url => '__DOWNLOAD_URL__', }} - it { should contain_archive('/opt/puppet-archive/nomad-0.5.0.zip').with(:source => 'http://myurl') } + it { should contain_archive('/opt/puppet-archive/nomad-0.5.0.zip').with(:source => '__DOWNLOAD_URL__') } it { should contain_file('/usr/local/bin/nomad').that_notifies('Class[nomad::run_service]') } end @@ -119,7 +154,7 @@ let(:params) {{ :install_method => 'package' }} - it { should contain_package('nomad').with(:ensure => 'latest') } + it { should contain_package('nomad').with(:ensure => :latest) } end context 'When requesting to not to install' do @@ -150,8 +185,9 @@ context 'The bootstrap_expect in config_hash is an int' do let(:params) {{ - :config_hash => - { 'bootstrap_expect' => '5' } + :config_hash => { + 'server' => { 'bootstrap_expect' => '5' } + } }} it { should contain_file('nomad config.json').with_content(/"bootstrap_expect":5/) } it { should_not contain_file('nomad config.json').with_content(/"bootstrap_expect":"5"/) } @@ -163,7 +199,7 @@ 'data_dir' => '/dir1', }, :config_hash => { - 'bootstrap_expect' => '5', + 'server' => { 'bootstrap_expect' => '5' } } }} it { should contain_file('nomad config.json').with_content(/"bootstrap_expect":5/) } @@ -174,46 +210,42 @@ let(:params) {{ :config_defaults => { 'data_dir' => '/dir1', - 'server' => false, 'ports' => { - 'http' => 1, - 'rpc' => '8300', + 'http' => 4646, + 'rpc' => '4647', }, }, :config_hash => { - 'bootstrap_expect' => '5', - 'server' => true, + 'server' => { 'bootstrap_expect' => '5' }, 'ports' => { 'http' => -1, - 'https' => 8500, + 'serf' => 4648, }, } }} it { should contain_file('nomad config.json').with_content(/"bootstrap_expect":5/) } it { should contain_file('nomad config.json').with_content(/"data_dir":"\/dir1"/) } - it { should contain_file('nomad config.json').with_content(/"server":true/) } it { should contain_file('nomad config.json').with_content(/"http":-1/) } - it { should contain_file('nomad config.json').with_content(/"https":8500/) } - it { should contain_file('nomad config.json').with_content(/"rpc":8300/) } + it { should contain_file('nomad config.json').with_content(/"rpc":4647/) } + it { should contain_file('nomad config.json').with_content(/"serf":4648/) } end context 'When pretty config is true' do let(:params) {{ :pretty_config => true, :config_hash => { - 'bootstrap_expect' => '5', - 'server' => true, + 'server' => { 'bootstrap_expect' => '5' }, 'ports' => { 'http' => -1, - 'https' => 8500, + 'rpc' => 4647, }, } }} - it { should contain_file('nomad config.json').with_content(/"bootstrap_expect": 5,/) } - it { should contain_file('nomad config.json').with_content(/"server": true/) } - it { should contain_file('nomad config.json').with_content(/"http": -1,/) } - it { should contain_file('nomad config.json').with_content(/"https": 8500/) } + it { should contain_file('nomad config.json').with_content(/"server": \{/) } + it { should contain_file('nomad config.json').with_content(/"bootstrap_expect": 5/) } it { should contain_file('nomad config.json').with_content(/"ports": \{/) } + it { should contain_file('nomad config.json').with_content(/"http": -1,/) } + it { should contain_file('nomad config.json').with_content(/"rpc": 4647/) } end context "When asked not to manage the user" do