From 0b3d363ac0980a99d969f014db1453ca20ad81c6 Mon Sep 17 00:00:00 2001 From: Thomas Goirand Date: Wed, 7 Apr 2021 21:53:08 +0200 Subject: [PATCH] Add support for neutron_api_uwsgi_config in Debian This patch is adding the configuration of the number of workers, threads, and the size of the listen queue in Debian, which uses uwsgi to run Neutron API. Therefore, this patch adds a new neutron_api_uwsgi_config provider as well as a new neutron::wsgi::uwsgi class. Change-Id: Iaa4171c05586ddca9fd3af1b4a78adfbe4105bab --- .../neutron_api_uwsgi_config/ini_setting.rb | 8 +++ lib/puppet/type/neutron_api_uwsgi_config.rb | 27 ++++++++ manifests/deps.pp | 2 + manifests/wsgi/uwsgi.pp | 41 ++++++++++++ .../notes/uwsgi-f1ee807e4c902c65.yaml | 7 ++ spec/classes/neutron_wsgi_uwsgi_spec.rb | 31 +++++++++ .../ini_setting_spec.rb | 57 +++++++++++++++++ .../type/neutron_api_uwsgi_config_spec.rb | 64 +++++++++++++++++++ 8 files changed, 237 insertions(+) create mode 100644 lib/puppet/provider/neutron_api_uwsgi_config/ini_setting.rb create mode 100644 lib/puppet/type/neutron_api_uwsgi_config.rb create mode 100644 manifests/wsgi/uwsgi.pp create mode 100644 releasenotes/notes/uwsgi-f1ee807e4c902c65.yaml create mode 100644 spec/classes/neutron_wsgi_uwsgi_spec.rb create mode 100644 spec/unit/provider/neutron_api_uwsgi_config/ini_setting_spec.rb create mode 100644 spec/unit/type/neutron_api_uwsgi_config_spec.rb diff --git a/lib/puppet/provider/neutron_api_uwsgi_config/ini_setting.rb b/lib/puppet/provider/neutron_api_uwsgi_config/ini_setting.rb new file mode 100644 index 000000000..ffdbcc074 --- /dev/null +++ b/lib/puppet/provider/neutron_api_uwsgi_config/ini_setting.rb @@ -0,0 +1,8 @@ +Puppet::Type.type(:neutron_api_uwsgi_config).provide( + :ini_setting, + :parent => Puppet::Type.type(:openstack_config).provider(:ini_setting) +) do + def self.file_path + '/etc/neutron/neutron-api-uwsgi.ini' + end +end diff --git a/lib/puppet/type/neutron_api_uwsgi_config.rb b/lib/puppet/type/neutron_api_uwsgi_config.rb new file mode 100644 index 000000000..88fffa76d --- /dev/null +++ b/lib/puppet/type/neutron_api_uwsgi_config.rb @@ -0,0 +1,27 @@ +Puppet::Type.newtype(:neutron_api_uwsgi_config) do + + ensurable + + newparam(:name, :namevar => true) do + desc 'Section/setting name to manage from /etc/neutron/neutron-api-uwsgi.ini' + newvalues(/\S+\/\S+/) + 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 + ['neutron::install::end'] + end + + newproperty(:value) do + desc 'The value of the setting to be defined.' + munge do |value| + value = value.to_s.strip + value.capitalize! if value =~ /^(true|false)$/i + value + end + end +end diff --git a/manifests/deps.pp b/manifests/deps.pp index 5f282d34c..6453e28bc 100644 --- a/manifests/deps.pp +++ b/manifests/deps.pp @@ -67,6 +67,8 @@ class neutron::deps { Anchor['neutron::config::begin'] -> Neutron_rootwrap_config<||> ~> Anchor['neutron::config::end'] Anchor['neutron::config::begin'] -> Ovn_metadata_agent_config<||> ~> Anchor['neutron::config::end'] Anchor['neutron::config::begin'] -> Ironic_neutron_agent_config<||> ~> Anchor['neutron::config::end'] + Anchor['neutron::config::begin'] -> Neutron_api_uwsgi_config<||> ~> Anchor['neutron::config::end'] + # Support packages need to be installed in the install phase, but we don't # put them in the chain above because we don't want any false dependencies diff --git a/manifests/wsgi/uwsgi.pp b/manifests/wsgi/uwsgi.pp new file mode 100644 index 000000000..718eb4124 --- /dev/null +++ b/manifests/wsgi/uwsgi.pp @@ -0,0 +1,41 @@ +# +# Copyright 2021 Thomas Goirand +# +# Author: Thomas Goirand +# +# == Class: neutron::wsgi::uwsgi +# +# Configure the UWSGI service for Neutron API. +# +# == Parameters +# +# [*processes*] +# (Optional) Number of processes. +# Defaults to $::os_workers. +# +# [*threads*] +# (Optional) Number of threads. +# Defaults to 32. +# +# [*listen_queue_size*] +# (Optional) Socket listen queue size. +# Defaults to 100 +# +class neutron::wsgi::uwsgi ( + $processes = $::os_workers, + $threads = 32, + $listen_queue_size = 100, +){ + + include neutron::deps + + if $::os_package_type != 'debian'{ + warning('This class is only valid for Debian, as other operating systems are not using uwsgi by default.') + } + + neutron_api_uwsgi_config { + 'uwsgi/processes': value => $processes; + 'uwsgi/threads': value => $threads; + 'uwsgi/listen': value => $listen_queue_size; + } +} diff --git a/releasenotes/notes/uwsgi-f1ee807e4c902c65.yaml b/releasenotes/notes/uwsgi-f1ee807e4c902c65.yaml new file mode 100644 index 000000000..829b1112c --- /dev/null +++ b/releasenotes/notes/uwsgi-f1ee807e4c902c65.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + A new class neutron::wsgi::uwsgi exist to allow configuring uwsgi in + operating systems that support this (ie: currently Debian). This helps + configuring the number of processes, threads and listen socket. + Also, a new neutron_api_wsgi_config provider now exist. diff --git a/spec/classes/neutron_wsgi_uwsgi_spec.rb b/spec/classes/neutron_wsgi_uwsgi_spec.rb new file mode 100644 index 000000000..b8028cfc2 --- /dev/null +++ b/spec/classes/neutron_wsgi_uwsgi_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe 'neutron::wsgi::uwsgi' do + + shared_examples 'neutron::wsgi::uwsgi' do + context 'with default parameters' do + it { + should contain_class('neutron::deps') + } + + it { + is_expected.to contain_neutron_api_uwsgi_config('uwsgi/processes').with_value(facts[:os_workers]) + is_expected.to contain_neutron_api_uwsgi_config('uwsgi/threads').with_value('32') + is_expected.to contain_neutron_api_uwsgi_config('uwsgi/listen').with_value('100') + } + 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({ + :os_workers => 8, + })) + end + it_behaves_like 'neutron::wsgi::uwsgi' + end + end +end diff --git a/spec/unit/provider/neutron_api_uwsgi_config/ini_setting_spec.rb b/spec/unit/provider/neutron_api_uwsgi_config/ini_setting_spec.rb new file mode 100644 index 000000000..5445e3622 --- /dev/null +++ b/spec/unit/provider/neutron_api_uwsgi_config/ini_setting_spec.rb @@ -0,0 +1,57 @@ +# +# 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(:neutron_api_uwsgi_config).provider(:ini_setting) +describe provider_class do + + it 'should default to the default setting when no other one is specified' do + resource = Puppet::Type::Neutron_api_uwsgi_config.new( + {:name => 'DEFAULT/foo', :value => 'bar'} + ) + provider = provider_class.new(resource) + expect(provider.section).to eq('DEFAULT') + expect(provider.setting).to eq('foo') + end + + it 'should allow setting to be set explicitly' do + resource = Puppet::Type::Neutron_api_uwsgi_config.new( + {:name => 'dude/foo', :value => 'bar'} + ) + provider = provider_class.new(resource) + expect(provider.section).to eq('dude') + expect(provider.setting).to eq('foo') + end + + it 'should ensure absent when is specified as a value' do + resource = Puppet::Type::Neutron_api_uwsgi_config.new( + {:name => 'dude/foo', :value => ''} + ) + provider = provider_class.new(resource) + provider.exists? + expect(resource[:ensure]).to eq :absent + end + + it 'should ensure absent when value matches ensure_absent_val' do + resource = Puppet::Type::Neutron_api_uwsgi_config.new( + {:name => 'dude/foo', :value => 'foo', :ensure_absent_val => 'foo' } + ) + provider = provider_class.new(resource) + provider.exists? + expect(resource[:ensure]).to eq :absent + end + +end diff --git a/spec/unit/type/neutron_api_uwsgi_config_spec.rb b/spec/unit/type/neutron_api_uwsgi_config_spec.rb new file mode 100644 index 000000000..b6d27cde8 --- /dev/null +++ b/spec/unit/type/neutron_api_uwsgi_config_spec.rb @@ -0,0 +1,64 @@ +require 'puppet' +require 'puppet/type/neutron_api_uwsgi_config' + +describe 'Puppet::Type.type(:neutron_api_uwsgi_config)' do + before :each do + @neutron_api_uwsgi_config = Puppet::Type.type(:neutron_api_uwsgi_config).new(:name => 'DEFAULT/foo', :value => 'bar') + end + + it 'should require a name' do + expect { + Puppet::Type.type(:neutron_api_uwsgi_config).new({}) + }.to raise_error(Puppet::Error, 'Title or name must be provided') + end + + it 'should not expect a name with whitespace' do + expect { + Puppet::Type.type(:neutron_api_uwsgi_config).new(:name => 'f oo') + }.to raise_error(Puppet::Error, /Parameter name failed/) + end + + it 'should fail when there is no section' do + expect { + Puppet::Type.type(:neutron_api_uwsgi_config).new(:name => 'foo') + }.to raise_error(Puppet::Error, /Parameter name failed/) + end + + it 'should not require a value when ensure is absent' do + Puppet::Type.type(:neutron_api_uwsgi_config).new(:name => 'DEFAULT/foo', :ensure => :absent) + end + + it 'should accept a valid value' do + @neutron_api_uwsgi_config[:value] = 'bar' + expect(@neutron_api_uwsgi_config[:value]).to eq('bar') + end + + it 'should not accept a value with whitespace' do + @neutron_api_uwsgi_config[:value] = 'b ar' + expect(@neutron_api_uwsgi_config[:value]).to eq('b ar') + end + + it 'should accept valid ensure values' do + @neutron_api_uwsgi_config[:ensure] = :present + expect(@neutron_api_uwsgi_config[:ensure]).to eq(:present) + @neutron_api_uwsgi_config[:ensure] = :absent + expect(@neutron_api_uwsgi_config[:ensure]).to eq(:absent) + end + + it 'should not accept invalid ensure values' do + expect { + @neutron_api_uwsgi_config[:ensure] = :latest + }.to raise_error(Puppet::Error, /Invalid value/) + end + + it 'should autorequire the package that install the file' do + catalog = Puppet::Resource::Catalog.new + anchor = Puppet::Type.type(:anchor).new(:name => 'neutron::install::end') + catalog.add_resource anchor, @neutron_api_uwsgi_config + dependency = @neutron_api_uwsgi_config.autorequire + expect(dependency.size).to eq(1) + expect(dependency[0].target).to eq(@neutron_api_uwsgi_config) + expect(dependency[0].source).to eq(anchor) + end + +end