From 7c30bd791fd5a9a5ee00003709752b057e3d86c8 Mon Sep 17 00:00:00 2001 From: Luca Miccini Date: Tue, 12 Nov 2019 08:01:11 +0100 Subject: [PATCH] Improve stonith leves idempotency. This commit improves the way stonith levels are set up and their resiliency against redeployments by introducing a stonith_levels custom fact that collects the current stonith levels defined for the specific server, so we can compare against the desired number of levels defined in hiera. If these do not match (for example if there are additional levels that are no longer necessary), the clean up step also introduced by this commit takes care of deleting the ones no longer necessary. Change-Id: Ifae73ac2bf4481d0a11e89c0ea0916e85dd2db1d --- lib/facter/stonith_levels.rb | 25 +++++++++++++++++++++++++ manifests/fencing.pp | 22 +++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 lib/facter/stonith_levels.rb diff --git a/lib/facter/stonith_levels.rb b/lib/facter/stonith_levels.rb new file mode 100644 index 000000000..8d8089b90 --- /dev/null +++ b/lib/facter/stonith_levels.rb @@ -0,0 +1,25 @@ +# Copyright 2016 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + +Facter.add('stonith_levels') do + setcode do + + hostname = Facter::Core::Execution.execute("crm_node -n 2> /dev/null", {}) + + stonith_levels = Facter::Core::Execution.execute("pcs stonith level | sed -n \"/^Target: #{hostname}$/,/^Target:/{/^Target: #{hostname}$/b;/^Target:/b;p}\" |tail -1 | awk '{print $2}' 2> /dev/null", {}) + + stonith_levels + end +end diff --git a/manifests/fencing.pp b/manifests/fencing.pp index d64cb05cf..4888cecdd 100644 --- a/manifests/fencing.pp +++ b/manifests/fencing.pp @@ -84,7 +84,7 @@ class tripleo::fencing( $content = $config['devices'] # check if the devices: section in fence.yaml contains levels. - # if it doesn't, assume level=1 an build a hash with the content. + # if it doesn't, assume level=1 and build a hash with the content. if is_array($content) { $all_levels = {'level1' => $content} } @@ -92,6 +92,26 @@ class tripleo::fencing( $all_levels = $content } + # collect the number of stonith levels currently defined for this system + # and convert it to integer. + $local_levels = 0 + $facts['stonith_levels'] + + # if the number of levels defined on this system is greather than the number in hiera + # we need to delete the delta. + if $local_levels > $all_levels.length { + $begin = $all_levels.length + 1 + range("${begin}", "${local_levels}").each |$level|{ + pacemaker::stonith::level{ "stonith-${level}": + ensure => 'absent', + level => $level, + target => '$(/usr/sbin/crm_node -n)', + stonith_resources => [''], + tries => $tries, + try_sleep => $try_sleep, + } + } + } + $all_levels.each |$index, $levelx_devices |{ $level = regsubst($index, 'level', '', 'G')