Revision 895a7680
Added by Ohad Levy about 14 years ago
- ID 895a7680b4765a509637d886af640ae44af91fee
app/controllers/fact_values_controller.rb | ||
---|---|---|
if Host.importHostAndFacts params.delete("facts")
|
||
render :text => "Imported facts", :status => 200 and return
|
||
else
|
||
render :text => "Failed to import facts", :status => 500
|
||
render :text => "Failed to import facts", :status => 400
|
||
end
|
||
}
|
||
end
|
app/models/host.rb | ||
---|---|---|
|
||
def self.importHostAndFacts yaml
|
||
facts = YAML::load yaml
|
||
raise "invalid Fact" unless facts.is_a?(Puppet::Node::Facts)
|
||
return false unless facts.is_a?(Puppet::Node::Facts)
|
||
|
||
h=Host.find_or_create_by_name facts.name
|
||
return h.importFacts(facts)
|
||
rescue Exception => e
|
||
logger.error e.message
|
||
return false
|
||
h=find_or_create_by_name(facts.name)
|
||
h.save(false) if h.new_record?
|
||
h.importFacts(facts)
|
||
end
|
||
|
||
# import host facts, required when running without storeconfigs.
|
||
... | ... | |
raise "Host is pending for Build" if build
|
||
time = facts.values[:_timestamp]
|
||
time = time.to_time if time.is_a?(String)
|
||
if last_compile.nil? or (last_compile + 1.minute < time)
|
||
self.last_compile = time
|
||
begin
|
||
# save all other facts
|
||
if self.respond_to?("merge_facts")
|
||
self.merge_facts(facts.values)
|
||
# pre 0.25 it was called setfacts
|
||
else
|
||
self.setfacts(facts.values)
|
||
end
|
||
# we are saving here with no validations, as we want this process to be as fast
|
||
# as possible, assuming we already have all the right settings in Foreman.
|
||
# If we don't (e.g. we never install the server via Foreman, we populate the fields from facts
|
||
# TODO: if it was installed by Foreman and there is a mismatch,
|
||
# we should probably send out an alert.
|
||
self.save(false)
|
||
|
||
# we want to import other information only if this host was never installed via Foreman
|
||
installed_at.nil? ? self.populateFieldsFromFacts : true
|
||
rescue
|
||
logger.warn "Failed to save #{name}: #{errors.full_messages.join(", ")}"
|
||
$stdout.puts $!
|
||
end
|
||
end
|
||
|
||
# we are not doing anything we already processed this fact (or a newer one)
|
||
return true unless last_compile.nil? or (last_compile + 1.minute < time)
|
||
|
||
self.last_compile = time
|
||
# save all other facts - pre 0.25 it was called setfacts
|
||
self.respond_to?("merge_facts") ? merge_facts(facts.values) : self.setfacts(facts.values)
|
||
|
||
# we want to import other information only if this host was never installed via Foreman
|
||
populateFieldsFromFacts if installed_at.nil?
|
||
|
||
# we are saving here with no validations, as we want this process to be as fast
|
||
# as possible, assuming we already have all the right settings in Foreman.
|
||
# If we don't (e.g. we never install the server via Foreman, we populate the fields from facts
|
||
# TODO: if it was installed by Foreman and there is a mismatch,
|
||
# we should probably send out an alert.
|
||
return self.save(false)
|
||
|
||
rescue Exception => e
|
||
logger.warn "Failed to save #{facts.name}: #{e}"
|
||
end
|
||
|
||
def fv name
|
||
... | ... | |
def populateFieldsFromFacts
|
||
self.mac = fv(:macaddress)
|
||
self.ip = fv(:ipaddress) if ip.nil?
|
||
self.domain = Domain.find_or_create_by_name fv(:domain)
|
||
self.domain = Domain.find_or_create_by_name fv(:domain) unless fv(:domain).empty?
|
||
# On solaris architecture fact is harwareisa
|
||
if myarch=fv(:architecture) || fv(:hardwareisa)
|
||
self.arch=Architecture.find_or_create_by_name myarch
|
||
self.arch=Architecture.find_or_create_by_name myarch unless myarch.empty?
|
||
end
|
||
# by default, puppet doesnt store an env name in the database
|
||
env=fv(:environment) || "production"
|
test/functional/fact_names_controller_test.rb | ||
---|---|---|
require 'test_helper'
|
||
|
||
class FactNamesControllerTest < ActionController::TestCase
|
||
# FactNamesController is empty, so there's nothing to test.
|
||
end
|
test/functional/fact_values_controller_test.rb | ||
---|---|---|
|
||
def test_create_invalid
|
||
post :create, {:facts => fact_fixture[1..-1]}, set_session_user
|
||
assert_response :error
|
||
assert_response :bad_request
|
||
end
|
||
|
||
def test_create_valid
|
||
post :create, {:facts => fact_fixture}, set_session_user
|
||
assert_response :success
|
||
end
|
||
|
||
|
||
end
|
test/unit/helpers/fact_names_helper_test.rb | ||
---|---|---|
require 'test_helper'
|
||
|
||
class FactNamesHelperTest < ActionView::TestCase
|
||
end
|
test/unit/host_test.rb | ||
---|---|---|
test "should import facts from yaml stream" do
|
||
h=Host.new(:name => "sinn1636.lan")
|
||
h.disk = "!" # workaround for now
|
||
h.importFacts YAML::load(File.read(File.expand_path(File.dirname(__FILE__) + "/facts.yml")))
|
||
assert h.valid?
|
||
assert h.importFacts YAML::load(File.read(File.expand_path(File.dirname(__FILE__) + "/facts.yml")))
|
||
end
|
||
|
||
test "should import facts from yaml of a new host" do
|
Also available in: Unified diff
fixes #295 - This YAML generates a 500 error when trying to send over http