From 5242d3a08be2dbd53d45825acf38683aebb1e07a Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Tue, 19 Apr 2022 08:31:45 +0900 Subject: [PATCH] Support virtlockd configurations ... so that this module supports configuration of all libvirt daemons. Change-Id: I93a02935df3a23e15b4a37081dc2a6ea646f6c79 --- .../provider/virtlockd_config/ini_setting.rb | 37 ++++++++++ lib/puppet/type/virtlockd_config.rb | 57 +++++++++++++++ manifests/compute/libvirt/config.pp | 7 ++ manifests/compute/libvirt/virtlockd.pp | 65 +++++++++++++++++ .../notes/virtlockd-d412855bb1536dea.yaml | 4 ++ .../nova_compute_libvirt_config_spec.rb | 11 +++ .../nova_compute_libvirt_virtlockd_spec.rb | 67 ++++++++++++++++++ .../virtlockd_config/ini_setting_spec.rb | 69 +++++++++++++++++++ spec/unit/type/virtlockd_config_spec.rb | 24 +++++++ 9 files changed, 341 insertions(+) create mode 100644 lib/puppet/provider/virtlockd_config/ini_setting.rb create mode 100644 lib/puppet/type/virtlockd_config.rb create mode 100644 manifests/compute/libvirt/virtlockd.pp create mode 100644 releasenotes/notes/virtlockd-d412855bb1536dea.yaml create mode 100644 spec/classes/nova_compute_libvirt_virtlockd_spec.rb create mode 100644 spec/unit/provider/virtlockd_config/ini_setting_spec.rb create mode 100644 spec/unit/type/virtlockd_config_spec.rb diff --git a/lib/puppet/provider/virtlockd_config/ini_setting.rb b/lib/puppet/provider/virtlockd_config/ini_setting.rb new file mode 100644 index 000000000..c536d7973 --- /dev/null +++ b/lib/puppet/provider/virtlockd_config/ini_setting.rb @@ -0,0 +1,37 @@ +Puppet::Type.type(:virtlockd_config).provide( + :ini_setting, + :parent => Puppet::Type.type(:ini_setting).provider(:ruby) +) do + + def exists? + if resource[:value] == ensure_absent_val + resource[:ensure] = :absent + elsif resource[:quote] + unless resource[:value].start_with?('"') + resource[:value] = '"' + resource[:value] + '"' + end + end + super + end + + def section + '' + end + + def setting + resource[:name] + end + + def separator + '=' + end + + def ensure_absent_val + resource[:ensure_absent_val] + end + + def self.file_path + '/etc/libvirt/virtlockd.conf' + end + +end diff --git a/lib/puppet/type/virtlockd_config.rb b/lib/puppet/type/virtlockd_config.rb new file mode 100644 index 000000000..b7f08ade9 --- /dev/null +++ b/lib/puppet/type/virtlockd_config.rb @@ -0,0 +1,57 @@ +Puppet::Type.newtype(:virtlockd_config) do + + ensurable + + newparam(:name, :namevar => true) do + desc 'setting name to manage from virtlockd.conf' + newvalues(/\S+/) + end + + newproperty(:value) do + desc 'The value of the setting to be defined.' + munge do |value| + value = value.to_s.strip + value + end + + def is_to_s( currentvalue ) + if resource.secret? + return '[old secret redacted]' + else + return currentvalue + end + end + + def should_to_s( newvalue ) + if resource.secret? + return '[new secret redacted]' + else + return newvalue + end + end + end + + newparam(:secret, :boolean => true) do + desc 'Whether to hide the value from Puppet logs. Defaults to `false`.' + + newvalues(:true, :false) + + defaultto false + end + + newparam(:quote, :boolean => true) do + desc 'Whether to quote the value. Defauls to `false`.' + newvalues(:true, :false) + defaultto false + end + + newparam(:ensure_absent_val) do + desc 'A value that is specified as the value property will behave as if ensure => absent was specified' + defaultto('') + end + + autorequire(:anchor) do + ['nova::install::end'] + end + +end diff --git a/manifests/compute/libvirt/config.pp b/manifests/compute/libvirt/config.pp index 78c784b7c..e1db3bf28 100644 --- a/manifests/compute/libvirt/config.pp +++ b/manifests/compute/libvirt/config.pp @@ -21,6 +21,10 @@ # (optional) Allow configuration of arbitrary virtlogd configurations. # The value is an hash of virtlogd_config resources. # +# [*virtlockd_config*] +# (optional) Allow configuration of arbitrary virtlockd configurations. +# The value is an hash of virtlockd_config resources. +# # [*virtnodedevd_config*] # (optional) Allow configuration of arbitrary virtnodedevd configurations. # The value is an hash of virtnodedevd_config resources. @@ -47,6 +51,7 @@ class nova::compute::libvirt::config ( $libvirtd_config = {}, $virtlogd_config = {}, + $virtlockd_config = {}, $virtnodedevd_config = {}, $virtproxyd_config = {}, $virtqemud_config = {}, @@ -58,6 +63,7 @@ class nova::compute::libvirt::config ( validate_legacy(Hash, 'validate_hash', $libvirtd_config) validate_legacy(Hash, 'validate_hash', $virtlogd_config) + validate_legacy(Hash, 'validate_hash', $virtlockd_config) validate_legacy(Hash, 'validate_hash', $virtnodedevd_config) validate_legacy(Hash, 'validate_hash', $virtproxyd_config) validate_legacy(Hash, 'validate_hash', $virtqemud_config) @@ -66,6 +72,7 @@ class nova::compute::libvirt::config ( create_resources('libvirtd_config', $libvirtd_config) create_resources('virtlogd_config', $virtlogd_config) + create_resources('virtlockd_config', $virtlockd_config) create_resources('virtnodedevd_config', $virtnodedevd_config) create_resources('virtproxyd_config', $virtproxyd_config) create_resources('virtqemud_config', $virtqemud_config) diff --git a/manifests/compute/libvirt/virtlockd.pp b/manifests/compute/libvirt/virtlockd.pp new file mode 100644 index 000000000..8b80f1ecd --- /dev/null +++ b/manifests/compute/libvirt/virtlockd.pp @@ -0,0 +1,65 @@ +# == Class: nova::compute::libvirt::virtlockd +# +# virtlockd configuration +# +# === Parameters: +# +# [*log_level*] +# Defines a log level to filter log outputs. +# Defaults to $::os_service_default +# +# [*log_filters*] +# Defines a log filter to select a different logging level for +# for a given category log outputs. +# Defaults to $::os_service_default +# +# [*log_outputs*] +# (optional) Defines log outputs, as specified in +# https://libvirt.org/logging.html +# Defaults to $::os_service_default +# +# [*max_clients*] +# The maximum number of concurrent client connections to allow +# on primary socket. +# Defaults to $::os_service_default +# +# [*admin_max_clients*] +# The maximum number of concurrent client connections to allow +# on administrative socket. +# Defaults to $::os_service_default +# +# [*max_size*] +# Maximum file size before rolling over. +# Defaults to $::os_service_default +# +# [*max_backups*] +# Maximum nuber of backup files to keep. +# Defaults to $::os_service_default +# +class nova::compute::libvirt::virtlockd ( + $log_level = $::os_service_default, + $log_filters = $::os_service_default, + $log_outputs = $::os_service_default, + $max_clients = $::os_service_default, + $admin_max_clients = $::os_service_default, + $max_size = $::os_service_default, + $max_backups = $::os_service_default, +) { + + include nova::deps + require nova::compute::libvirt + + virtlockd_config { + 'log_level': value => pick($log_level, $::os_service_default); + 'log_filters': value => pick($log_filters, $::os_service_default), quote => true; + 'log_outputs': value => pick($log_outputs, $::os_service_default), quote => true; + 'max_clients': value => pick($max_clients, $::os_service_default); + 'admin_max_clients': value => pick($admin_max_clients, $::os_service_default); + 'max_size': value => pick($max_size, $::os_service_default); + 'max_backups': value => pick($max_backups, $::os_service_default); + } + + Anchor['nova::config::begin'] + -> Virtlockd_config<||> + -> Anchor['nova::config::end'] +} diff --git a/releasenotes/notes/virtlockd-d412855bb1536dea.yaml b/releasenotes/notes/virtlockd-d412855bb1536dea.yaml new file mode 100644 index 000000000..187aae494 --- /dev/null +++ b/releasenotes/notes/virtlockd-d412855bb1536dea.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Now this module supports configuration of the ``virtlockd`` service. diff --git a/spec/classes/nova_compute_libvirt_config_spec.rb b/spec/classes/nova_compute_libvirt_config_spec.rb index 73095b5d3..e6bdd2cf1 100644 --- a/spec/classes/nova_compute_libvirt_config_spec.rb +++ b/spec/classes/nova_compute_libvirt_config_spec.rb @@ -13,6 +13,11 @@ describe 'nova::compute::libvirt::config' do 'foo2' => { 'value' => 'fooValue' }, 'bar2' => { 'value' => 'barValue' }, 'baz2' => { 'ensure' => 'absent' } + }, + :virtlockd_config => { + 'foo3' => { 'value' => 'fooValue' }, + 'bar3' => { 'value' => 'barValue' }, + 'baz3' => { 'ensure' => 'absent' } } } end @@ -31,6 +36,12 @@ describe 'nova::compute::libvirt::config' do should contain_virtlogd_config('bar2').with_value('barValue') should contain_virtlogd_config('baz2').with_ensure('absent') } + + it { + should contain_virtlockd_config('foo3').with_value('fooValue') + should contain_virtlockd_config('bar3').with_value('barValue') + should contain_virtlockd_config('baz3').with_ensure('absent') + } end end diff --git a/spec/classes/nova_compute_libvirt_virtlockd_spec.rb b/spec/classes/nova_compute_libvirt_virtlockd_spec.rb new file mode 100644 index 000000000..91b848411 --- /dev/null +++ b/spec/classes/nova_compute_libvirt_virtlockd_spec.rb @@ -0,0 +1,67 @@ +# Unit tests for nova::compute::libvirt::virtlockd class +# +require 'spec_helper' + +describe 'nova::compute::libvirt::virtlockd' do + + let :pre_condition do + <<-eos + include nova + include nova::compute + include nova::compute::libvirt +eos + end + + shared_examples_for 'nova-compute-libvirt-virtlockd' do + + context 'with default parameters' do + let :params do + {} + end + + it { is_expected.to contain_class('nova::deps')} + it { is_expected.to contain_class('nova::compute::libvirt::virtlockd')} + + it { is_expected.to contain_virtlockd_config('log_level').with_value('')} + it { is_expected.to contain_virtlockd_config('log_outputs').with_value('').with_quote(true)} + it { is_expected.to contain_virtlockd_config('log_filters').with_value('').with_quote(true)} + it { is_expected.to contain_virtlockd_config('max_clients').with_value('')} + it { is_expected.to contain_virtlockd_config('admin_max_clients').with_value('')} + end + + context 'with specified parameters' do + let :params do + { :log_level => 3, + :log_outputs => '3:syslog', + :log_filters => '1:logging 4:object 4:json 4:event 1:util', + :max_clients => 1024, + :admin_max_clients => 5, + :max_size => 2097152, + :max_backups => 3, + } + end + + it { is_expected.to contain_class('nova::deps')} + it { is_expected.to contain_class('nova::compute::libvirt::virtlockd')} + + it { is_expected.to contain_virtlockd_config('log_level').with_value(params[:log_level])} + it { is_expected.to contain_virtlockd_config('log_outputs').with_value(params[:log_outputs]).with_quote(true)} + it { is_expected.to contain_virtlockd_config('log_filters').with_value(params[:log_filters]).with_quote(true)} + it { is_expected.to contain_virtlockd_config('max_clients').with_value(params[:max_clients])} + it { is_expected.to contain_virtlockd_config('admin_max_clients').with_value(params[:admin_max_clients])} + end + end + + on_supported_os({ + :supported_os => OSDefaults.get_supported_os + }).each do |os,facts| + context "on #{os}" do + let (:facts) do + facts.merge!(OSDefaults.get_facts()) + end + + it_configures 'nova-compute-libvirt-virtlockd' + end + end + +end diff --git a/spec/unit/provider/virtlockd_config/ini_setting_spec.rb b/spec/unit/provider/virtlockd_config/ini_setting_spec.rb new file mode 100644 index 000000000..d37747e8f --- /dev/null +++ b/spec/unit/provider/virtlockd_config/ini_setting_spec.rb @@ -0,0 +1,69 @@ +# +# these tests are a little concerning b/c they are hacking around the +# modulepath, so these tests will not catch issues that may eventually arise +# related to loading these plugins. +# I could not, for the life of me, figure out how to programatcally set the modulepath +$LOAD_PATH.push( + File.join( + File.dirname(__FILE__), + '..', + '..', + '..', + 'fixtures', + 'modules', + 'inifile', + 'lib') +) +require 'spec_helper' +provider_class = Puppet::Type.type(:virtlockd_config).provider(:ini_setting) +describe provider_class do + + it 'should allow setting to be set explicitly' do + resource = Puppet::Type::Virtlockd_config.new( + {:name => 'foo', :value => 'bar'} + ) + provider = provider_class.new(resource) + expect(provider.section).to eq('') + expect(provider.setting).to eq('foo') + end + + it 'should quote the value when quote is true' do + resource = Puppet::Type::Virtlockd_config.new( + {:name => 'foo', :value => 'baa', :quote => true } + ) + provider = provider_class.new(resource) + provider.exists? + expect(provider.section).to eq('') + expect(provider.setting).to eq('foo') + expect(resource[:value]).to eq('"baa"') + end + + it 'should not quote the value when quote is true but the value is quoted' do + resource = Puppet::Type::Virtlockd_config.new( + {:name => 'foo', :value => '"baa"', :quote => true } + ) + provider = provider_class.new(resource) + provider.exists? + expect(provider.section).to eq('') + expect(provider.setting).to eq('foo') + expect(resource[:value]).to eq('"baa"') + end + + it 'should ensure absent when is specified as a value' do + resource = Puppet::Type::Virtlockd_config.new( + {:name => 'foo', :value => ''} + ) + provider = provider_class.new(resource) + provider.exists? + expect(resource[:ensure]).to eq :absent + end + + it 'should ensure absent when is specified as a value and quote is true' do + resource = Puppet::Type::Virtlockd_config.new( + {:name => 'foo', :value => '', :quote => true} + ) + provider = provider_class.new(resource) + provider.exists? + expect(resource[:ensure]).to eq :absent + end +end diff --git a/spec/unit/type/virtlockd_config_spec.rb b/spec/unit/type/virtlockd_config_spec.rb new file mode 100644 index 000000000..a76744533 --- /dev/null +++ b/spec/unit/type/virtlockd_config_spec.rb @@ -0,0 +1,24 @@ +require 'puppet' +require 'puppet/type/virtlockd_config' + +describe 'Puppet::Type.type(:virtlockd_config)' do + before :each do + @virtlockd_config = Puppet::Type.type(:virtlockd_config).new(:name => 'DEFAULT/foo', :value => 'bar') + end + + it 'should accept a valid value' do + @virtlockd_config[:value] = 'bar' + expect(@virtlockd_config[:value]).to eq('bar') + end + + it 'should autorequire the package that install the file' do + catalog = Puppet::Resource::Catalog.new + anchor = Puppet::Type.type(:anchor).new(:name => 'nova::install::end') + catalog.add_resource anchor, @virtlockd_config + dependency = @virtlockd_config.autorequire + expect(dependency.size).to eq(1) + expect(dependency[0].target).to eq(@virtlockd_config) + expect(dependency[0].source).to eq(anchor) + end + +end