-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update common to 165e2adc3ab559597f461e6eae3eb004967070f9
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
Showing
9 changed files
with
224 additions
and
7 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
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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 |
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
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 |
---|---|---|
@@ -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 |
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
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 |
---|---|---|
|
@@ -19,5 +19,5 @@ | |
|
||
class common { | ||
|
||
|
||
} | ||
# vim: ts=8 |
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
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 |
---|---|---|
@@ -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) | ||
|