Skip to content

Commit

Permalink
Update common to 165e2adc3ab559597f461e6eae3eb004967070f9
Browse files Browse the repository at this point in the history
165e2adc3ab559597f461e6eae3eb004967070f9 Add an example for the simple silent counter.
2e150420d7cd0730298aeb6d02ed81b5f40c4fa5 Add default integer value for when file is missing.
9f9222fdc9b059818ea69810d7cd4eb2330576b6 Add missing os import.
4c4b2afa75e06d56fb1591c17409e7bad53ec746 Don't increment if --noop is used.
94a5db1cb8cb6746d27d44badfc54c3065a78d9b Add missing word.
78fba387c0530042dfe783458346684928692e1b Clean up of some small string changes.
66da5d10e123777227e41acbb340e771b2fd6079 Add a counter variable that increments silently on each puppet run.
586994094055e6e41904b39d746dab5bbf042304 Extremely small fixes.
  • Loading branch information
xbezdick committed Mar 3, 2015
1 parent d6cf2bb commit 1f9aa29
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Puppetfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod 'cinder',
:git => 'https://github.com/stackforge/puppet-cinder.git'

mod 'common',
:commit => '2c0ed2844c606fd806bde0c02e47e79c88fab4a9',
:commit => '165e2adc3ab559597f461e6eae3eb004967070f9',
:git => 'https://github.com/purpleidea/puppet-common.git'

mod 'concat',
Expand Down
10 changes: 10 additions & 0 deletions common/examples/counter-example.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# example use of silent counter

include ::common::counter # that's it!

# NOTE: we only see the notify message. no other exec/change is shown!
notify { 'counter':
message => "Value is: ${::common_counter_simple}",
}

# vim: ts=8
70 changes: 70 additions & 0 deletions common/lib/facter/counter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Increment an ID so that it is unique to each puppet run.
# Copyright (C) 2012-2013+ James Shubin
# Written by James Shubin <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require 'facter'

# find the module_vardir
dir = Facter.value('puppet_vardirtmp') # nil if missing
if dir.nil? # let puppet decide if present!
dir = Facter.value('puppet_vardir')
if dir.nil?
var = nil
else
var = dir.gsub(/\/$/, '')+'/'+'tmp/' # ensure trailing slash
end
else
var = dir.gsub(/\/$/, '')+'/'
end

if var.nil?
# if we can't get a valid vardirtmp, then we can't continue
module_vardir = nil
counterdir = nil
counter_simple = nil
else
module_vardir = var+'common/'
counterdir = module_vardir+'counter/'
counter_simple = counterdir+'simple'
end

# NOTE: module specific mkdirs, needed to ensure there is no blocking/deadlock!
if not(var.nil?) and not File.directory?(var)
Dir::mkdir(var)
end

if not(module_vardir.nil?) and not File.directory?(module_vardir)
Dir::mkdir(module_vardir)
end

if not(counterdir.nil?) and not File.directory?(counterdir)
Dir::mkdir(counterdir)
end

value = 0 # default value to use if file doesn't exist
# create the fact if the counter file contains a valid int, or if it's empty: 0
if not(counter_simple.nil?) and File.exist?(counter_simple)
# an empty file will output a value of 0 with this idiomatic line...
value = File.open(counter_simple, 'r').read.strip.to_i # read into int
end
Facter.add('common_counter_simple') do
#confine :operatingsystem => %w{CentOS, RedHat, Fedora}
setcode {
value
}
end

# vim: ts=8
1 change: 1 addition & 0 deletions common/manifests/again/delta.pp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
default => '',
}

# TODO: add --name '${name}' as a uid to prevent running of duplicates!
$arglist = ["--delta ${valid_delta}", "${valid_start_timer_now}"]
$args = join(delete($arglist, ''), ' ')

Expand Down
75 changes: 75 additions & 0 deletions common/manifests/counter.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Increment an ID so that it is unique to each puppet run.
# Copyright (C) 2012-2013+ James Shubin
# Written by James Shubin <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

class common::counter {

include common::vardir

#$vardir = $::common::vardir::module_vardir # with trailing slash
$vardir = regsubst($::common::vardir::module_vardir, '\/$', '')

# store 'counter' in a separate directory
file { "${vardir}/counter/":
ensure => directory, # make sure this is a directory
recurse => true, # don't recurse into directory
purge => true, # don't purge unmanaged files
force => true, # don't purge subdirs and links
require => File["${vardir}/"],
}

file { "${vardir}/counter/increment.py":
# NOTE: this is actually templated, but no templating
# is actually being used. This gives us the option to
# pass in some variables if we decide we would like a
# way to get values in other than via command line...
# we could pass in some environ data or other data...
content => template('common/counter/increment.py.erb'),
owner => root,
group => root,
mode => 755,
ensure => present,
require => File["${vardir}/counter/"],
}

# NOTE: this is a simple counter. it is 'simple' because it is probably
# possible to build more complex counters that can ensure that only one
# increment happens per puppet run if multiple puppets run in parallel!
# in other words: this simple counter doesn't do any file locking stuff
file { "${vardir}/counter/simple":
owner => root,
group => root,
mode => 644,
ensure => present,
require => File["${vardir}/counter/"],
}

# this command silently increments the counter without displaying logs!
exec { 'counter':
# echo an error message and be false, if the incrementing fails
command => '/bin/echo "common::counter failed" && /bin/false',
# NOTE: this 'unless' is actually _NOT_ idempotent as is normal
# it should always return true, unless there's an error running
unless => "${vardir}/counter/increment.py '${vardir}/counter/simple'",
logoutput => on_failure,
require => [
File["${vardir}/counter/increment.py"],
File["${vardir}/counter/simple"],
],
}
}

# vim: ts=8
8 changes: 4 additions & 4 deletions common/manifests/frag/frag.pp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}

# this is the parent (basename) dir of $name which is special if i frag
$frag_d = sprintf("%s/", regsubst($name, '((\/[\w.-]+)*)(\/)([\w.-]+)', '\1'))
$frag_d = sprintf("%s/", regsubst("${name}", '((\/[\w.-]+)*)(\/)([\w.-]+)', '\1'))

# the file (used to set perms and as a placeholder so it's not deleted)
file { "${name}":
Expand Down Expand Up @@ -96,10 +96,10 @@
$source = ''
# TODO: add more file object features if someone needs them or if bored
) {
# finds the file name in a complete path; eg: /tmp/dir/file => file
#$x = regsubst($name, '(\/[\w.]+)*(\/)([\w.]+)', '\3')
# finds the file basename in a complete path; eg: /tmp/dir/file => file
#$x = regsubst("${name}", '(\/[\w.]+)*(\/)([\w.]+)', '\3')
# finds the basepath in a complete path; eg: /tmp/dir/file => /tmp/dir/
$d = sprintf("%s/", regsubst($name, '((\/[\w.-]+)*)(\/)([\w.-]+)', '\1'))
$d = sprintf("%s/", regsubst("${name}", '((\/[\w.-]+)*)(\/)([\w.-]+)', '\1'))

# the file fragment
file { "${name}":
Expand Down
2 changes: 1 addition & 1 deletion common/manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@

class common {


}
# vim: ts=8
2 changes: 1 addition & 1 deletion common/templates/again/again.py.erb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ if fork == 0: # child
os.execvpe(argv[0], argv, env) # this command does not return!

else: # parent
print "pid: %d will try to puppet again..." % fork
print "pid: %d will try to run puppet again..." % fork
sys.exit(0) # let puppet exit successfully!

# vim: ts=8
61 changes: 61 additions & 0 deletions common/templates/counter/increment.py.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Read in a single integer from a text file, increment it, and write it back.
This script is usually only executed by a puppet exec type.
"""
# Increment an ID so that it is unique to each puppet run.
# Copyright (C) 2012-2013+ James Shubin
# Written by James Shubin <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import sys
if len(sys.argv) != 2: sys.exit(1)

# parse the commandline, and don't run the increment if --noop is used :)
pid = os.getpid() # my pid
ppid = os.getppid() # parent pid

# parse parent cmdline
with open("/proc/%d/cmdline" % ppid, 'r') as f:
cmdline = f.read()
argv = cmdline.split("\0") # separated by nulls (the: ^@ character in vim)
if argv[-1] == '': argv.pop() # remove empty element at end of list if exists

# TODO: does the noop detection work when we run as a service ? (probably not!)
if '--noop' in argv:
sys.exit(0)

# now do the actual incremental work...
f = open(sys.argv[1], 'r')
a = [l.strip() for l in f.readlines() if l.strip() != '']
f.close()
if len(a) == 0:
i = 0
elif len(a) != 1: sys.exit(2)
else:
try:
i = int(a[0])
except ValueError, e:
sys.exit(3)

#print(i)
f = open(sys.argv[1], 'w')
f.write("%d\n" % (i+1))
f.close()
sys.exit(0)

0 comments on commit 1f9aa29

Please sign in to comment.