From 0a6ac4addcd586f398c1b2dd09b5fa6ef8aacfd2 Mon Sep 17 00:00:00 2001 From: Rocky Date: Fri, 24 Apr 2020 00:04:30 +1000 Subject: [PATCH] Support to configure memcache.conf Change-Id: Iab2520403edd47004c3edbeda66b4eb16eed587a --- .../swift_memcache_config/ini_setting.rb | 10 ++ lib/puppet/type/swift_memcache_config.rb | 53 +++++++++++ manifests/memcache.pp | 92 +++++++++++++++++++ ...-swift-memcache-conf-f119f4e1027bcdda.yaml | 4 + spec/classes/swift_memcache_spec.rb | 76 +++++++++++++++ 5 files changed, 235 insertions(+) create mode 100644 lib/puppet/provider/swift_memcache_config/ini_setting.rb create mode 100644 lib/puppet/type/swift_memcache_config.rb create mode 100644 manifests/memcache.pp create mode 100644 releasenotes/notes/add-swift-memcache-conf-f119f4e1027bcdda.yaml create mode 100644 spec/classes/swift_memcache_spec.rb diff --git a/lib/puppet/provider/swift_memcache_config/ini_setting.rb b/lib/puppet/provider/swift_memcache_config/ini_setting.rb new file mode 100644 index 00000000..05cbb348 --- /dev/null +++ b/lib/puppet/provider/swift_memcache_config/ini_setting.rb @@ -0,0 +1,10 @@ +Puppet::Type.type(:swift_memcache_config).provide( + :ini_setting, + :parent => Puppet::Type.type(:openstack_config).provider(:ini_setting) +) do + + def self.file_path + '/etc/swift/memcache.conf' + end + +end diff --git a/lib/puppet/type/swift_memcache_config.rb b/lib/puppet/type/swift_memcache_config.rb new file mode 100644 index 00000000..4c4f7c43 --- /dev/null +++ b/lib/puppet/type/swift_memcache_config.rb @@ -0,0 +1,53 @@ +Puppet::Type.newtype(:swift_memcache_config) do + + ensurable + + newparam(:name, :namevar => true) do + desc 'Section/setting name to manage from /etc/swift/memcache.conf' + newvalues(/\S+\/\S+/) + 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 + newvalues(/^[\S ]*$/) + + 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(: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(:package) do + 'swift' + end + +end diff --git a/manifests/memcache.pp b/manifests/memcache.pp new file mode 100644 index 00000000..a0f6a02b --- /dev/null +++ b/manifests/memcache.pp @@ -0,0 +1,92 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# == Class: swift::memcache +# +# This class creates a memcache configuration file +# +# +# === Parameters +# +# [*memcache_servers*] +# You can use this single conf file instead of having memcache_servers set in +# several other conf files under [filter:cache] for example. You can specify +# multiple servers separated with commas, as in: 10.1.2.3:11211,10.1.2.4:11211 +# Default to ['127.0.0.1:11211'] +# +# [*memcache_serialization_support*] +# Sets how memcache values are serialized and deserialized: +# 0 = older, insecure pickle serialization +# 1 = json serialization but pickles can still be read (still insecure) +# 2 = json serialization only (secure and the default) +# To avoid an instant full cache flush, existing installations should +# upgrade with 0, then set to 1 and reload, then after some time (24 hours) +# set to 2 and reload. +# In the future, the ability to use pickle serialization will be removed. +# Default to $::os_service_default +# +# [*memcache_max_connections*] +# Sets the maximum number of connections to each memcached server per worker +# Default to $::os_service_default +# +# [*connect_timeout*] +# Timeout for connection +# Default to $::os_service_default +# +# [*pool_timeout*] +# Timeout for pooled connection +# Default to $::os_service_default +# +# [*tries*] +# number of servers to retry on failures getting a pooled connection +# Default to $::os_service_default +# +# [*io_timeout*] +# Timeout for read and writes +# Default to $::os_service_default +# +# +# === Authors +# +# shi.yan@ardc.edu.au +# +class swift::memcache ( + $memcache_servers = ['127.0.0.1:11211'], + $memcache_serialization_support = $::os_service_default, + $memcache_max_connections = $::os_service_default, + $connect_timeout = $::os_service_default, + $pool_timeout = $::os_service_default, + $tries = $::os_service_default, + $io_timeout = $::os_service_default, +) { + + include swift::deps + include swift::params + + + file { '/etc/swift/memcache.conf': + ensure => file, + owner => 'swift', + group => 'swift', + mode => '0640', + } + + swift_memcache_config { + 'memcache/memcache_servers': value => join(any2array($memcache_servers), ','); + 'memcache/memcache_serialization_support': value => $memcache_serialization_support; + 'memcache/memcache_max_connections': value => $memcache_max_connections; + 'memcache/connect_timeout': value => $connect_timeout; + 'memcache/pool_timeout': value => $pool_timeout; + 'memcache/tries': value => $tries; + 'memcache/io_timeout': value => $io_timeout; + } +} diff --git a/releasenotes/notes/add-swift-memcache-conf-f119f4e1027bcdda.yaml b/releasenotes/notes/add-swift-memcache-conf-f119f4e1027bcdda.yaml new file mode 100644 index 00000000..82001bdf --- /dev/null +++ b/releasenotes/notes/add-swift-memcache-conf-f119f4e1027bcdda.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Support to configure memcache.conf file. diff --git a/spec/classes/swift_memcache_spec.rb b/spec/classes/swift_memcache_spec.rb new file mode 100644 index 00000000..8ba29a8e --- /dev/null +++ b/spec/classes/swift_memcache_spec.rb @@ -0,0 +1,76 @@ +require 'spec_helper' + +describe 'swift::memcache' do + shared_examples 'swift::memcache' do + + describe 'when using default parameters' do + + let :file_defaults do + { + :owner => 'swift', + :group => 'swift', + :mode => '0640', + } + end + + it {is_expected.to contain_file('/etc/swift/memcache.conf').with( + {:ensure => 'file'}.merge(file_defaults) + )} + + it { is_expected.to contain_swift_memcache_config( + 'memcache/memcache_servers').with_value(['127.0.0.1:11211']) } + it { is_expected.to contain_swift_memcache_config( + 'memcache/memcache_serialization_support').with_value('') } + it { is_expected.to contain_swift_memcache_config( + 'memcache/memcache_max_connections').with_value('') } + it { is_expected.to contain_swift_memcache_config( + 'memcache/connect_timeout').with_value('') } + it { is_expected.to contain_swift_memcache_config( + 'memcache/pool_timeout').with_value('') } + it { is_expected.to contain_swift_memcache_config( + 'memcache/tries').with_value('') } + it { is_expected.to contain_swift_memcache_config( + 'memcache/io_timeout').with_value('') } + end + + describe 'when overridding parameters' do + let :params do + { + :memcache_servers => ['1.1.1.1:11211', '2.2.2.2:11211'], + :memcache_serialization_support => 1, + :memcache_max_connections => 3, + :connect_timeout => '0.5', + :pool_timeout => '2.0', + :tries => 5, + :io_timeout => '1.0' + } + end + it { is_expected.to contain_swift_memcache_config( + 'memcache/memcache_servers').with_value('1.1.1.1:11211,2.2.2.2:11211') } + it { is_expected.to contain_swift_memcache_config( + 'memcache/memcache_serialization_support').with_value('1') } + it { is_expected.to contain_swift_memcache_config( + 'memcache/memcache_max_connections').with_value('3') } + it { is_expected.to contain_swift_memcache_config( + 'memcache/connect_timeout').with_value('0.5') } + it { is_expected.to contain_swift_memcache_config( + 'memcache/pool_timeout').with_value('2.0') } + it { is_expected.to contain_swift_memcache_config( + 'memcache/tries').with_value('5') } + it { is_expected.to contain_swift_memcache_config( + 'memcache/io_timeout').with_value('1.0') } + 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 'swift::memcache' + end + end +end