Created
December 1, 2010 14:48
-
-
Save glarizza/723574 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# MC Agent for facts.txt | |
# | |
# This is my test at pushing/popping/querying data from facts.txt | |
# | |
# Initial file contains: | |
# | |
# huron_class=staff,shel | |
# environment=production | |
hash = {} | |
searchvar = "shel" | |
pushvar = "crankd" | |
def open_file() | |
h = {} | |
# Open /etc/facts.txt if it exists, fail if it does not. | |
if File.exists?("/etc/facts.txt") | |
File.open("/etc/facts.txt") do |fp| | |
fp.each do |line| | |
key, value = line.chomp.split("=") | |
h[key] = value | |
end | |
end | |
return h | |
else | |
puts "File does not exist" | |
exit(1) | |
end | |
end # End of open_file def | |
if ARGV[0] == "-r" | |
hash = open_file() | |
if hash.size == 0 | |
puts "/etc/facts.txt contains no key/value pairs" | |
exit(0) | |
end | |
puts "Before Removing Variable:\n" | |
hash.each {|key, value| puts "#{key} is #{value}"} | |
# Attempt at removing a searched class from the list of classes | |
# This is only proof-of-concept, no file writes yet | |
if hash["huron_class"] =~ /,#{searchvar},/ or hash["huron_class"] =~ /,#{searchvar}/ | |
hash["huron_class"][",#{searchvar}"]= "" | |
elsif hash["huron_class"] =~ /#{searchvar},/ | |
hash["huron_class"]["#{searchvar},"]= "" | |
elsif hash["huron_class"] == "#{searchvar}" | |
hash["huron_class"] = "" | |
else | |
puts "\nSearch Variable does not exist." | |
end | |
puts "\nAfter Removing Variable:\n" | |
hash.each {|key, value| puts "#{key} is #{value}"} | |
end | |
if ARGV[0] == "-a" | |
hash = open_file() | |
if hash.size == 0 | |
puts "/etc/facts.txt contains no key/value pairs" | |
exit(0) | |
end | |
# Attempt at Adding a passed variable onto the list of classes | |
# This is only proof-of-concept, no file writes yet | |
puts "\nBefore Adding Variable to List:\n" | |
hash.each {|key, value| puts "#{key} is #{value}"} | |
hash["huron_class"] += ",#{pushvar}" | |
puts "\nAfter Adding Variable:\n" | |
hash.each {|key, value| puts "#{key} is #{value}"} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment