Use post in mcollective facter updater

This commit is contained in:
Mike Scherbakov 2012-10-03 16:52:26 +04:00
parent ead633567b
commit 5e5d0131e0
4 changed files with 79 additions and 116 deletions

View File

@ -22,22 +22,13 @@ action "get", :description => "fetches a value from a file" do
:display_as => "Value"
end
action "put", :description => "Value to add to file" do
action "post", :description => "Create new attributes file" do
display :failed
input :key,
:prompt => "Key",
:description => "Key you want to set in the file",
:type => :string,
:validation => '^[a-zA-Z0-9_]+$',
:optional => false,
:maxlength => 90
input :value,
:prompt => "Value",
:description => "Value you want to set in the file",
:type => :string,
:validation => '^[a-zA-Z0-9_]+$',
:type => :hash,
:optional => false,
:maxlength => 90
@ -45,19 +36,3 @@ action "put", :description => "Value to add to file" do
:description => "Status",
:display_as => "Status"
end
action "delete", :description => "Delete a key/value pair if it exists" do
display :failed
input :key,
:prompt => "Key",
:description => "Key you want to change in the file",
:type => :string,
:validation => '^[a-zA-Z0-9_]+$',
:optional => false,
:maxlength => 90
output :msg,
:description => "Status",
:display_as => "Status"
end

View File

@ -1,93 +1,75 @@
module MCollective
module Agent
class Nailyfact<RPC::Agent
metadata :name => "Naily Fact Agent",
:description => "Key/values in a text file",
:author => "Puppet Master Guy",
:license => "GPL",
:version => "Version 1",
:url => "www.naily.com",
:timeout => 10
nailyfile = "/etc/naily.facts"
def parse_facts(fname)
begin
if File.exist?(fname)
kv_map = {}
File.readlines(fname).each do |line|
if line =~ /^(.+)=(.+)$/
@key = $1.strip;
@val = $2.strip
kv_map.update({@key=>@val})
end
end
return kv_map
else
f = File.open(fname,'w')
f.close
return {}
end
rescue
logger.warn("Could not access naily facts file. There was an error in nailyfacts.rb:parse_facts")
return {}
end
end
module Agent
class Nailyfact<RPC::Agent
metadata :name => "Naily Fact Agent",
:description => "Key/values in a text file",
:author => "Puppet Master Guy",
:license => "GPL",
:version => "Version 1",
:url => "www.naily.com",
:timeout => 10
def write_facts(fname, facts)
nailyfile = "/etc/naily.facts"
if not File.exists?(File.dirname(fname))
Dir.mkdir(File.dirname(fname))
end
def parse_facts(fname)
begin
if File.exist?(fname)
kv_map = {}
File.readlines(fname).each do |line|
if line =~ /^(.+)=(.+)$/
@key = $1.strip;
@val = $2.strip
kv_map.update({@key=>@val})
end
end
return kv_map
else
f = File.open(fname,'w')
f.close
return {}
end
rescue
logger.warn("Could not access naily facts file. There was an error in nailyfacts.rb:parse_facts")
return {}
end
end
begin
f = File.open(fname,"w+")
facts.each do |k,v|
f.puts("#{k} = #{v}")
end
f.close
return true
rescue
return false
end
end
def write_facts(fname, facts)
if not File.exists?(File.dirname(fname))
Dir.mkdir(File.dirname(fname))
end
action "get" do
validate :key, String
kv_map = parse_facts(nailyfile)
if kv_map[request[:key]] != nil
reply[:value] = kv_map[request[:key]]
end
end
begin
f = File.open(fname,"w+")
facts.each do |k,v|
f.puts("#{k} = #{v}")
end
f.close
return true
rescue
return false
end
end
action "put" do
validate :key, String
validate :value, String
action "get" do
validate :key, String
kv_map = parse_facts(nailyfile)
if kv_map[request[:key]] != nil
reply[:value] = kv_map[request[:key]]
end
end
kv_map = parse_facts(nailyfile)
kv_map.update({request[:key] => request[:value]})
action "post" do
validate :value, Hash
if write_facts(nailyfile,kv_map)
reply[:msg] = "Settings Updated!"
else
reply.fail! "Could not write file!"
end
kv_map = request[:value]
end
action "delete" do
validate :key, String
kv_map = parse_facts(nailyfile)
kv_map.delete(request[:key])
if write_facts(nailyfile,kv_map)
reply[:msg] = "Setting deleted!"
else
reply.fail! "Could not write file!"
end
end
end
end
if write_facts(nailyfile, kv_map)
reply[:msg] = "Settings Updated!"
else
reply.fail! "Could not write file!"
end
end
end
end
end

View File

@ -9,6 +9,9 @@ if File.exist?("/etc/naily.facts")
Facter.add(var) do
setcode { val }
end
Facter.add("myrole") do
setcode {'a' => {'b' => {'c' => 'd'}}}
end
end
end
end

View File

@ -16,17 +16,20 @@ describe "MCollective" do
end
it "it should update facts file with new key-value and could get it back" do
value_to_send = rand(2**30).to_s
data_to_send = {"anykey" => rand(2**30).to_s, "other" => "static"}
node = "admin"
mc = rpcclient("nailyfact")
mc.progress = false
mc.discover(:nodes => [node])
stats = mc.put(:key => "role", :value => value_to_send)
stats.should have(1).items
stats[0].results[:statuscode].should eql(0)
stats = mc.get(:key => "role")
stats = mc.post(:value => data_to_send)
check_mcollective_result(stats)
stats[0].results[:data][:value].should eql(value_to_send)
stats = mc.get(:key => "anykey")
check_mcollective_result(stats)
stats[0].results[:data][:value].should eql(data_to_send['anykey'])
stats = mc.get(:key => "other")
check_mcollective_result(stats)
stats[0].results[:data][:value].should eql(data_to_send['other'])
end
end
end