Add support for pcmk resource op defaults

This adds support for setting op defaults via pcs.
Tested via:
include ::pacemaker::resource_op_defaults

A) No defaults set to start with:
[root@controller-0 pacemaker]# pcs resource op defaults
No defaults set

B) Hiera key to pass a new default:
[root@controller-0 pacemaker]# hiera -c /etc/puppet/hiera.yaml pacemaker::resource_op_defaults::defaults
{"timeout_test"=>{"name"=>"timeout", "value"=>"60s"}}

C) Puppet apply:
[root@controller-0 pacemaker]# puppet apply /tmp/test.pp
Notice: Compiled catalog for controller-0.redhat.local in environment production in 0.06 seconds
Notice: /Stage[main]/Pacemaker::Resource_op_defaults/Pcmk_resource_op_default[timeout_test]/ensure: created
Notice: Applied catalog in 2.89 seconds

D) Correct results
[root@controller-0 pacemaker]# pcs resource op defaults
timeout: 60s

E) Change value:
[root@controller-0 pacemaker]# hiera -c /etc/puppet/hiera.yaml pacemaker::resource_op_defaults::defaults
{"timeout_test"=>{"name"=>"timeout", "value"=>"65s"}}

[root@controller-0 pacemaker]# puppet apply /tmp/test.pp

[root@controller-0 pacemaker]# pcs resource op defaults
timeout: 65s

F) Idempotency:
[root@controller-0 pacemaker]# puppet apply /tmp/test.pp
Notice: Compiled catalog for controller-0.redhat.local in environment production in 0.05 seconds
Notice: Applied catalog in 0.97 seconds

Change-Id: Id72941260c264081cec253382d8e4c975c4a6104
This commit is contained in:
Michele Baldessari 2019-06-11 16:42:34 +02:00
parent 447cef0ad8
commit 9beb316c73
4 changed files with 247 additions and 0 deletions

View File

@ -0,0 +1,40 @@
require_relative '../pcmk_common'
# Currently the implementation is somewhat naive (will not work great
# with ensure => absent, unless the correct current value is also
# specified). For more proper handling, prefetching should be
# implemented and `value` should be switched from a param to a
# property. This should be possible to do without breaking the
# interface of the resource type.
Puppet::Type.type(:pcmk_resource_op_default).provide(:pcs) do
desc 'Manages default values for pacemaker resource operations via pcs'
def create
name = @resource[:name]
value = @resource[:value]
cmd = "resource op defaults #{name}='#{value}'"
pcs('create', name, cmd, @resource[:tries], @resource[:try_sleep],
@resource[:verify_on_create], @resource[:post_success_sleep])
end
def destroy
name = @resource[:name]
cmd = "resource defaults #{name}="
pcs('create', name, cmd, @resource[:tries], @resource[:try_sleep],
@resource[:verify_on_create], @resource[:post_success_sleep])
end
def exists?
name = @resource[:name]
value = @resource[:value]
cmd = "resource op defaults | grep '^#{name}: #{value}\$'"
Puppet.debug("defaults exists #{cmd}")
status = pcs('show', name, cmd, @resource[:tries], @resource[:try_sleep],
@resource[:verify_on_create], @resource[:post_success_sleep])
return status == false ? false : true
end
end

View File

@ -0,0 +1,81 @@
require 'puppet/parameter/boolean'
Puppet::Type.newtype(:pcmk_resource_op_default) do
@doc = "A default value for pacemaker resource op options"
ensurable
newparam(:name) do
desc "A unique name of the option"
end
newparam(:value) do
desc "A default value for the option"
end
newparam(:post_success_sleep) do
desc "The time to sleep after successful pcs action. The reason to set
this is to avoid immediate back-to-back 'pcs resource create' calls
when creating multiple resources. Defaults to '0'."
munge do |value|
if value.is_a?(String)
unless value =~ /^[-\d.]+$/
raise ArgumentError, "post_success_sleep must be a number"
end
value = Float(value)
end
raise ArgumentError, "post_success_sleep cannot be a negative number" if value < 0
value
end
defaultto 0
end
## borrowed from exec.rb
newparam(:tries) do
desc "The number of times to attempt to create a pcs resource.
Defaults to '1'."
munge do |value|
if value.is_a?(String)
unless value =~ /^[\d]+$/
raise ArgumentError, "Tries must be an integer"
end
value = Integer(value)
end
raise ArgumentError, "Tries must be an integer >= 1" if value < 1
value
end
defaultto 1
end
newparam(:try_sleep) do
desc "The time to sleep in seconds between 'tries'."
munge do |value|
if value.is_a?(String)
unless value =~ /^[-\d.]+$/
raise ArgumentError, "try_sleep must be a number"
end
value = Float(value)
end
raise ArgumentError, "try_sleep cannot be a negative number" if value < 0
value
end
defaultto 0
end
newparam(:verify_on_create, :boolean => true, :parent => Puppet::Parameter::Boolean) do
desc "Whether to verify pcs resource creation with an additional
call to 'pcs resource show' rather than just relying on the exit
status of 'pcs resource create'. When true, $try_sleep
determines how long to wait to verify and $post_success_sleep is
ignored. Defaults to `false`."
defaultto false
end
end

View File

@ -0,0 +1,74 @@
# == Class: pacemaker::resource_defaults
#
# Defaults to set for pcs resources
#
# === Parameters:
#
# [*defaults*]
# (required) Comma separated string of key=value pairs specifying defaults.
#
# [*post_success_sleep*]
# (optional) How long to wait acfter successful action
# Defaults to 0
#
# [*tries*]
# (optional) How many times to attempt to create the constraint
# Defaults to 1
#
# [*try_sleep*]
# (optional) How long to wait between tries, in seconds
# Defaults to 0
#
# [*verify_on_create*]
# (optional) Whether to verify creation of resource
# Defaults to false
#
# [*ensure*]
# (optional) Whether to create or remove the defaults
# Defaults to present
#
# === Dependencies
#
# None
#
# === Authors
#
# Crag Wolfe <cwolfe@redhat.com>
# Jason Guiditta <jguiditt@redhat.com>
#
# === Copyright
#
# Copyright (C) 2016 Red Hat Inc.
#
# 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 pacemaker::resource_op_defaults(
$defaults,
$post_success_sleep = 0,
$tries = 1,
$try_sleep = 0,
$verify_on_create = false,
$ensure = 'present',
) {
create_resources(
'pcmk_resource_op_default',
$defaults,
{
ensure => $ensure,
post_success_sleep => $post_success_sleep,
tries => $tries,
try_sleep => $try_sleep,
verify_on_create => $verify_on_create,
}
)
}

View File

@ -0,0 +1,52 @@
#
# Copyright (C) 2017 Red Hat, Inc.
#
# 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.
#
require 'spec_helper'
describe 'pacemaker::resource_op_defaults', type: :class do
let(:title) { 'foo' }
shared_examples_for 'pacemaker::resource_op_defaults' do
context 'with params' do
let(:params) { {
:defaults => { 'timeout_test' => { 'name' => 'timeout', 'value' => '60s', }, },
:tries => 10,
:try_sleep => 20,
} }
it { is_expected.to compile.with_all_deps }
it {
is_expected.to contain_pcmk_resource_op_default('timeout_test').with(
:name => 'timeout',
:value => '60s',
:tries => 10,
:try_sleep => 20,
)
}
end
end
on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) do
facts.merge({ :hostname => 'node.example.com' })
end
it_behaves_like 'pacemaker::resource_op_defaults'
end
end
end