Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
573 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
280 changes: 133 additions & 147 deletions
280
pacemaker/lib/puppet/provider/pcmk_resource/default.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,156 +1,142 @@ | ||
Puppet::Type.type(:pcmk_resource).provide(:default) do | ||
desc 'A base resource definition for a pacemaker resource' | ||
|
||
### overloaded methods | ||
def create | ||
cmd = 'resource create ' + @resource[:name] + ' ' + @resource[:resource_type] + ' ' + @resource[:resource_params] + ' op monitor interval=' + @resource[:interval] | ||
if @resource[:monitor_params] and not @resource[:monitor_params].empty? | ||
cmd += hash_to_params(@resource[:monitor_params]) | ||
end | ||
# group defaults to empty | ||
if not @resource[:group].empty? | ||
cmd += ' --group ' + @resource[:group] | ||
end | ||
# clone defaults to false | ||
if @resource[:clone] | ||
cmd += ' --clone' | ||
end | ||
# do pcs create | ||
pcs('create', cmd) | ||
end | ||
|
||
def destroy | ||
cmd = 'resource delete ' + @resource[:name] | ||
pcs('delete', cmd) | ||
end | ||
|
||
def exists? | ||
cmd = 'resource show ' + @resource[:name] + ' > /dev/null 2>&1' | ||
pcs('show', cmd) | ||
end | ||
|
||
|
||
### property methods | ||
def resource_params | ||
cmd = 'resource show ' + @resource[:name] | ||
get_attrs = pcs('get interval', cmd) | ||
|
||
# find the Attributes | ||
for line in get_attrs.lines.each do | ||
return (line.scan /Attributes: (.+?)$/m)[0][0].strip if line.include? 'Attributes:' | ||
end | ||
# return empty string if Attributes not found | ||
'' | ||
end | ||
|
||
def resource_params=(value) | ||
cmd = 'resource update ' + @resource[:name] + ' ' + value | ||
pcs('update attributes', cmd) | ||
end | ||
|
||
def group | ||
# get the list of groups and their resources | ||
cmd = 'resource --groups' | ||
resource_groups = pcs('group list', cmd) | ||
|
||
# find the group that has the resource in it | ||
for group in resource_groups.lines.each do | ||
return group[0, /:/ =~ group] if group.include? @resource[:name] | ||
end | ||
# return empty string if a group wasn't found | ||
# that includes the resource in it. | ||
'' | ||
end | ||
|
||
def group=(value) | ||
if value.empty? | ||
cmd = 'resource ungroup ' + group + ' ' + @resource[:name] | ||
pcs('ungroup', cmd) | ||
else | ||
cmd = 'resource group add ' + value + ' ' + @resource[:name] | ||
pcs('group add', cmd) | ||
end | ||
end | ||
|
||
def clone | ||
cmd = 'resource show ' + @resource[:name] + '-clone > /dev/null 2>&1' | ||
pcs('show clone', cmd) == false ? false : true | ||
end | ||
|
||
def clone=(value) | ||
if not value | ||
cmd = 'resource unclone ' + @resource[:name] | ||
pcs('unclone', cmd) | ||
else | ||
cmd = 'resource clone ' + @resource[:name] | ||
pcs('clone', cmd) | ||
end | ||
end | ||
|
||
def interval | ||
cmd = 'resource show ' + @resource[:name] | ||
get_interval = pcs('get interval', cmd) | ||
|
||
# find the interval value | ||
for line in get_interval.lines.each do | ||
return (line.scan /interval=(.+?) /m)[0][0] if line.include? 'interval=' | ||
end | ||
# return empty string if an interval value wasn't found | ||
'' | ||
end | ||
desc 'A base resource definition for a pacemaker resource' | ||
|
||
def interval=(value) | ||
cmd = 'resource update ' + @resource[:name] + ' op monitor interval=' + value | ||
pcs('update interval', cmd) | ||
end | ||
### overloaded methods | ||
def create | ||
resource_params = @resource[:resource_params] | ||
meta_params = @resource[:meta_params] | ||
op_params = @resource[:op_params] | ||
clone_params = @resource[:clone_params] | ||
group_params = @resource[:group_params] | ||
master_params = @resource[:master_params] | ||
|
||
def monitor_params | ||
cmd = 'resource show ' + @resource[:name] | ||
pcs_output = pcs('get monitor params', cmd) | ||
|
||
pcs_output.each_line do |line| | ||
line.strip.match(/(Operations: )?monitor ([^(]+)/) do |match| | ||
Puppet.debug(match.inspect) | ||
return params_to_hash(match[2]) | ||
end | ||
end | ||
# return empty string if monitor params not found | ||
'' | ||
suffixes = 0 | ||
if clone_params then suffixes +=1 end | ||
if master_params then suffixes +=1 end | ||
if group_params then suffixes +=1 end | ||
if suffixes > 1 | ||
raise(Puppet::Error, "May only define one of clone_params, "+ | ||
"master_params and group_params") | ||
end | ||
|
||
def monitor_params=(value) | ||
cmd = 'resource update ' + @resource[:name] + ' op monitor ' + hash_to_params(value) | ||
pcs('update interval', cmd) | ||
end | ||
# Build the 'pcs resource create' command. Check out the pcs man page :-) | ||
cmd = 'resource create ' + @resource[:name]+' ' +@resource[:resource_type] | ||
if not_empty_string(resource_params) | ||
cmd += ' ' + resource_params | ||
end | ||
if not_empty_string(meta_params) | ||
cmd += ' meta ' + meta_params | ||
end | ||
if not_empty_string(op_params) | ||
cmd += ' op ' + op_params | ||
end | ||
if clone_params | ||
cmd += ' --clone' | ||
if not_empty_string(clone_params) | ||
cmd += ' ' + clone_params | ||
end | ||
end | ||
if not_empty_string(group_params) | ||
cmd += ' --group ' + group_params | ||
end | ||
if master_params | ||
cmd += ' --master' | ||
if not_empty_string(master_params) | ||
cmd += ' ' + master_params | ||
end | ||
end | ||
|
||
# do pcs create | ||
pcs('create', cmd) | ||
end | ||
|
||
def destroy | ||
cmd = 'resource delete ' + @resource[:name] | ||
pcs('delete', cmd) | ||
end | ||
|
||
def exists? | ||
cmd = 'resource show ' + @resource[:name] + ' > /dev/null 2>&1' | ||
pcs('show', cmd) | ||
end | ||
|
||
|
||
### property methods | ||
|
||
# It isn't an easy road if you want to make these true | ||
# puppet-like resource properties. Here is a start if you are feeling brave: | ||
# https://github.com/cwolferh/puppet-pacemaker/blob/pcmk_resource_improvements_try0/lib/puppet/provider/pcmk_resource/default.rb#L64 | ||
def resource_params | ||
@resource[:resource_params] | ||
end | ||
|
||
def resource_params=(value) | ||
end | ||
|
||
def op_params | ||
@resource[:op_params] | ||
end | ||
|
||
def op_params=(value) | ||
end | ||
|
||
def meta_params | ||
@resource[:meta_params] | ||
end | ||
|
||
def meta_params=(value) | ||
end | ||
|
||
def group_params | ||
@resource[:group_params] | ||
end | ||
|
||
def group_params=(value) | ||
end | ||
|
||
def master_params | ||
@resource[:master_params] | ||
end | ||
|
||
def master_params=(value) | ||
end | ||
|
||
def clone_params | ||
@resource[:clone_params] | ||
end | ||
|
||
def clone_params=(value) | ||
end | ||
|
||
def not_empty_string(p) | ||
p && p.kind_of?(String) && ! p.empty? | ||
end | ||
|
||
private | ||
|
||
def pcs(name, cmd) | ||
Puppet.debug("/usr/sbin/pcs #{cmd}") | ||
pcs_out = `/usr/sbin/pcs #{cmd}` | ||
#puts name | ||
#puts $?.exitstatus | ||
if $?.exitstatus != 0 and pcs_out.lines.first and not name.include? 'show' | ||
Puppet.debug("Error: #{pcs_out}") | ||
raise Puppet::Error, "pcs #{name} failed: #{pcs_out.lines.first.chomp!}" if $?.exitstatus | ||
end | ||
def pcs(name, cmd) | ||
try_sleep = @resource[:try_sleep] | ||
max_tries = name.include?('show') ? 1 : @resource[:tries] | ||
max_tries.times do |try| | ||
try_text = max_tries > 1 ? "try #{try+1}/#{max_tries}: " : '' | ||
Puppet.debug("#{try_text}/usr/sbin/pcs #{cmd}") | ||
pcs_out = `/usr/sbin/pcs #{cmd} 2>&1` | ||
if name.include?('show') | ||
# return output for good exit or false for failure. | ||
$?.exitstatus == 0 ? pcs_out : false | ||
end | ||
return $?.exitstatus == 0 ? pcs_out : false | ||
end | ||
if $?.exitstatus == 0 | ||
sleep @resource[:post_success_sleep] | ||
return pcs_out | ||
end | ||
Puppet.debug("Error: #{pcs_out}") | ||
if try == max_tries-1 | ||
pcs_out_line = pcs_out.lines.first ? pcs_out.lines.first.chomp! : '' | ||
raise Puppet::Error, "pcs #{name} failed: #{pcs_out_line}" | ||
end | ||
if try_sleep > 0 | ||
Puppet.debug("Sleeping for #{try_sleep} seconds between tries") | ||
sleep try_sleep | ||
end | ||
end | ||
end | ||
|
||
def params_to_hash(str) | ||
str.split.reduce({}) do |hash, param| | ||
k,v = param.split '=' | ||
hash[k] = v | ||
hash | ||
end | ||
end | ||
|
||
def hash_to_params(hash) | ||
params = '' | ||
hash.each_pair do |k,v| | ||
params += " #{k}=#{v}" | ||
end | ||
params | ||
end | ||
end |
Oops, something went wrong.