1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
require 'puppet/rails/resource'
class Puppet::Rails::Host < ActiveRecord::Base
has_many :fact_values, :through => :fact_names
has_many :fact_names
belongs_to :puppet_classes
has_many :source_files
has_many :resources, :include => [ :param_names, :param_values ]
acts_as_taggable
def facts(name)
if fv = self.fact_values.find(:first, :conditions => "fact_names.name = '#{name}'")
return fv.value
else
return nil
end
end
# If the host already exists, get rid of its objects
def self.clean(host)
if obj = self.find_by_name(host)
obj.rails_objects.clear
return obj
else
return nil
end
end
# Store our host in the database.
def self.store(hash)
unless hash[:name]
raise ArgumentError, "You must specify the hostname for storage"
end
create = true
args = {}
if hash[:facts].include?("ipaddress")
args[:ip] = hash[:facts]["ipaddress"]
end
host = nil
Puppet::Util.benchmark(:info, "Found/created host") do
host = self.find_or_create_by_name(hash[:facts]["hostname"], args)
end
Puppet::Util.benchmark(:info, "Converted facts") do
hash[:facts].each do |name, value|
if create
fn = host.fact_names.find_or_create_by_name(name)
fv = fn.fact_values.find_or_create_by_value(value)
else
fn = host.fact_names.find_by_name(name) || host.fact_names.new(:name => name)
unless fv = fn.fact_values.find_by_value(value)
fn.fact_values << fn.fact_values.new(:value => value)
end
end
host.fact_names << fn
end
end
unless hash[:resources]
raise ArgumentError, "You must pass resources"
end
Puppet::Util.benchmark(:info, "Converted resources") do
hash[:resources].each do |resource|
resargs = resource.to_hash.stringify_keys
if create
res = host.resources.find_or_create_by_restype_and_title(resource[:type], resource[:title])
else
unless res = host.resources.find_by_restype_and_title(resource[:type], resource[:title])
res = host.resources.new(:restype => resource[:type], :title => resource[:title])
host.resources << res
end
end
resargs.each do |param, value|
if create
pn = res.param_names.find_or_create_by_name(param)
pv = pn.param_values.find_or_create_by_value(value)
else
unless pn = res.param_names.find_by_name(param)
pn = res.param_names.new(:name => param)
end
unless pn.param_values.find_by_value(value)
pn.param_values << pn.param_values.new(:value => value)
end
end
res.param_names << pn
end
end
end
Puppet::Util.benchmark(:info, "Saved host to database") do
host.save
end
return host
end
# Add all of our RailsObjects
def addobjects(objects)
objects.each do |tobj|
params = {}
tobj.each do |p,v| params[p] = v end
args = {:ptype => tobj.type, :name => tobj.name}
[:tags, :file, :line].each do |param|
if val = tobj.send(param)
args[param] = val
end
end
robj = rails_objects.build(args)
robj.addparams(params)
if tobj.collectable
robj.toggle(:collectable)
end
end
end
end
# $Id$
|