Ensure that conflict errors are bound to the rails.errors[:conflict] key.
This is so that we can detect the "all errors are conflict errors" condition.
Signed-off-by: Paul Kelly <paul.ian.kelly@googlemail.com>
···
---
app/models/orchestration.rb | 6 +++---
app/models/orchestration/dns.rb | 4 ++--
lib/net/dhcp/record.rb | 2 +-
lib/net/dns.rb | 2 +-
lib/net/dns/a_record.rb | 2 +-
lib/net/dns/ptr_record.rb | 2 +-
6 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/app/models/orchestration.rb b/app/models/orchestration.rb
index 5d4d0b3…ad921fc 100644
— a/app/models/orchestration.rb
+++ b/app/models/orchestration.rb
@@ -40,9 +40,9 @@ module Orchestration
end
# log and add to errors
- def failure msg, backtrace=nil
- def failure msg, backtrace=nil, dest = :base
logger.warn(backtrace ? msg + backtrace.join("\n") : msg)
@@ -88,7 +88,7 @@ module Orchestration
rescue Net::Conflict => e
task.status = “conflict”
@record_conflicts << e
diff --git a/app/models/orchestration/dns.rb b/app/models/orchestration/dns.rb
index 1c91d26…4e21836 100644
— a/app/models/orchestration/dns.rb
+++ b/app/models/orchestration/dns.rb
@@ -44,8 +44,8 @@ module Orchestration::DNS
def validate_dns
return unless dns?
return unless new_record?
-
failure("- #{hostname} A record is already in DNS", nil, :conflict) if dns_a_record.conflicting?
-
failure("- #{ip} PTR is already in DNS", nil, :conflict) if dns_ptr_record.conflicting?
end
def dns_record_attrs
diff --git a/lib/net/dhcp/record.rb b/lib/net/dhcp/record.rb
index d6d1df0…1abbecc 100644
— a/lib/net/dhcp/record.rb
+++ b/lib/net/dhcp/record.rb
@@ -31,7 +31,7 @@ module Net::DHCP
e.type = "dhcp"
e.expected = to_s
e.actual = conflicts
diff --git a/lib/net/dns/a_record.rb b/lib/net/dns/a_record.rb
index bb61119…f0d7960 100644
— a/lib/net/dns/a_record.rb
+++ b/lib/net/dns/a_record.rb
@@ -24,7 +24,7 @@ module Net
@conflicts ||= [dns_lookup(hostname)].delete_if { |c| c == self }.compact
end
diff --git a/lib/net/dns/ptr_record.rb b/lib/net/dns/ptr_record.rb
index a9a8099…81b7d76 100644
— a/lib/net/dns/ptr_record.rb
+++ b/lib/net/dns/ptr_record.rb
@@ -23,7 +23,7 @@ module Net
@conflicts ||= [dns_lookup(ip)].delete_if{|c| c == self}.compact
end
–
1.7.5.4
If a collision-only situation is detected then change the GUI to
indicate that the next save will be forced.
Signed-off-by: Paul Kelly <paul.ian.kelly@googlemail.com>
···
---
app/controllers/hosts_controller.rb | 10 ++++++++++
app/helpers/layout_helper.rb | 6 ++++--
app/models/host.rb | 9 +++++++++
app/views/hosts/_form.html.erb | 4 ++--
4 files changed, 25 insertions(+), 4 deletions(-)
diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb
index bd848c2…c14efbc 100644
— a/app/controllers/hosts_controller.rb
+++ b/app/controllers/hosts_controller.rb
@@ -91,6 +91,7 @@ class HostsController < ApplicationController
process_success :success_redirect => @host
else
load_vars_for_ajax
-
offer_to_overwrite_conflicts
process_error
end
end
@@ -105,6 +106,7 @@ class HostsController < ApplicationController
process_success :success_redirect => @host
else
load_vars_for_ajax
-
offer_to_overwrite_conflicts
process_error
end
end
@@ -539,4 +541,12 @@ class HostsController < ApplicationController
@host.request_url = request.host_with_port if @host.respond_to?(:request_url)
end
-
if a save failed and the only reason was network conflicts then flag this so that the view
-
is rendered differently and the next save operation will be forced
-
def offer_to_overwrite_conflicts
-
if @host.errors[:conflict].count == @host.errors.count
-
@host.overwrite = "true"
-
end
-
end
-
end
diff --git a/app/helpers/layout_helper.rb b/app/helpers/layout_helper.rb
index 8057c88…5349de4 100644
— a/app/helpers/layout_helper.rb
+++ b/app/helpers/layout_helper.rb
@@ -75,10 +75,12 @@ module LayoutHelper
end
end
- def submit_or_cancel f, overwrite = false
"
".html_safe + content_tag(:p, :class => “ra”) do
-
text = overwrite ? "Overwrite" : "Submit"
-
options = overwrite ? {:class => "btn danger"} : {:class => "btn primary"}
link_to("Cancel", eval("#{controller_name}_path"), :class => "btn") + " " +
-
f.submit(text, options)
end
end
diff --git a/app/models/host.rb b/app/models/host.rb
index 64b1a12…f895ee3 100644
— a/app/models/host.rb
+++ b/app/models/host.rb
@@ -601,6 +601,15 @@ class Host < Puppet::Rails::Host
{}
end
- def overwrite
-
@overwrite ||= false
- end
-
-
We have to coerce the value back to boolean. It is not done for us by the framework.
- def overwrite=(value)
-
@overwrite = value == “true”
- end
- private
align common mac and ip address input
def normalize_addresses
diff --git a/app/views/hosts/_form.html.erb b/app/views/hosts/_form.html.erb
index 8041c74…6a5e99d 100644
— a/app/views/hosts/_form.html.erb
+++ b/app/views/hosts/_form.html.erb
@@ -66,6 +66,6 @@
<%= textarea_f f, :comment, :help_block => “Additional information about this host”, :class => “xxlarge”, :rows => “3” %>
-
- <%= submit_or_cancel f %>
- <%= f.hidden_field :overwrite %>
- <%= submit_or_cancel f, @host.overwrite %>
<% end %>
–
1.7.5.4
Signed-off-by: Paul Kelly <paul.ian.kelly@googlemail.com>
···
---
lib/net/dns.rb | 4 ----
lib/net/dns/a_record.rb | 4 ++++
lib/net/dns/ptr_record.rb | 4 ++++
3 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/lib/net/dns.rb b/lib/net/dns.rb
index fc83b77…7d8b2b1 100644
— a/lib/net/dns.rb
+++ b/lib/net/dns.rb
@@ -45,10 +45,6 @@ module Net
raise “Must provide a DNS resolver” unless resolver.is_a?(Resolv::DNS)
end
diff --git a/lib/net/dns/a_record.rb b/lib/net/dns/a_record.rb
index f0d7960…98e1a45 100644
— a/lib/net/dns/a_record.rb
+++ b/lib/net/dns/a_record.rb
@@ -7,6 +7,10 @@ module Net
@type = "A"
end
diff --git a/lib/net/dns/ptr_record.rb b/lib/net/dns/ptr_record.rb
index 81b7d76…44b5ad1 100644
— a/lib/net/dns/ptr_record.rb
+++ b/lib/net/dns/ptr_record.rb
@@ -6,6 +6,10 @@ module Net
@type = "PTR"
end
–
1.7.5.4
Signed-off-by: Paul Kelly <paul.ian.kelly@googlemail.com>
···
---
app/controllers/hosts_controller.rb | 2 +-
app/models/host.rb | 2 +-
app/models/orchestration.rb | 6 ++--
app/models/orchestration/dhcp.rb | 53 +++++++++++++++++++++++++++++++----
app/models/orchestration/dns.rb | 37 +++++++++++++++++++++--
app/views/hosts/_form.html.erb | 4 +-
lib/core_extensions.rb | 6 ++++
7 files changed, 93 insertions(+), 17 deletions(-)
diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb
index c14efbc…3112494 100644
— a/app/controllers/hosts_controller.rb
+++ b/app/controllers/hosts_controller.rb
@@ -544,7 +544,7 @@ class HostsController < ApplicationController
if a save failed and the only reason was network conflicts then flag this so that the view
is rendered differently and the next save operation will be forced
def offer_to_overwrite_conflicts
- if @host.errors[:conflict].count == @host.errors.count
- if @host.errors.are_all_conflicts?
@host.overwrite = "true"
end
end
diff --git a/app/models/host.rb b/app/models/host.rb
index f895ee3…0033ea3 100644
— a/app/models/host.rb
+++ b/app/models/host.rb
@@ -601,7 +601,7 @@ class Host < Puppet::Rails::Host
{}
end
- def overwrite?
@overwrite ||= false
end
diff --git a/app/models/orchestration.rb b/app/models/orchestration.rb
index ad921fc…e357d98 100644
— a/app/models/orchestration.rb
+++ b/app/models/orchestration.rb
@@ -11,8 +11,8 @@ module Orchestration
before_validation :setup_clone
# extend our Host model to know how to handle subsystems
@@ -53,7 +53,7 @@ module Orchestration
# not care about their return status.
def valid?(context = nil)
super
@@ -99,7 +99,7 @@ module Orchestration
end
# if we have no failures - we are done
diff --git a/app/models/orchestration/dhcp.rb b/app/models/orchestration/dhcp.rb
index ac07ad3…0f0be4a 100644
— a/app/models/orchestration/dhcp.rb
+++ b/app/models/orchestration/dhcp.rb
@@ -2,7 +2,7 @@ module Orchestration::DHCP
def self.included(base)
base.send :include, InstanceMethods
base.class_eval do
-
after_validation :validate_dhcp, :queue_remove_dhcp_conflicts, :queue_dhcp
before_destroy :queue_dhcp_destroy
validate :ip_belongs_to_subnet?, :valid_jumpstart_model
end
@@ -11,11 +11,11 @@ module Orchestration::DHCP
module InstanceMethods
def dhcp?
-
sp_valid? and !sp_subnet.nil? and sp_subnet.dhcp? and (overwrite? ? errors.are_all_conflicts? : errors.empty?)
end
def dhcp_record
@@ -30,22 +30,53 @@ module Orchestration::DHCP
protected
-
def validate_dhcp
-
# This is an expensive operation and we will only do it if the DNS validation failed. This will ensure
-
# that we report on both DNS and DHCP conflicts when we offer to remove collisions. It retrieves and
-
# caches the conflicting records so we must always do it when overwriting
-
return unless errors.are_all_conflicts? or overwrite?
-
-
return unless dhcp?
-
status = true
-
status = failure("- #{dhcp_record.conflicts[0]} exists in DHCP", nil, :conflict) if dhcp_record.conflicting?
-
if sp_dhcp?
-
(status = failure("- #{sp_dhcp_record.conflicts[0]} exists in DHCP", nil, :conflict) and status) if sp_dhcp_record.conflicting?
-
end
-
overwrite? ? errors.are_all_conflicts? : status
-
end
-
def set_dhcp
dhcp_record.create
end
-
def set_dhcp_conflicts
-
dhcp_record.conflicts.each{|conflict| conflict.create}
-
end
-
def set_sp_dhcp
sp_dhcp_record.create
end
-
def set_sp_dhcp_conflicts
-
sp_dhcp_record.conflicts.each{|conflict| conflict.create}
-
end
-
def del_dhcp
dhcp_record.destroy
end
-
def del_dhcp_conflicts
-
dhcp_record.conflicts.each{|conflict| conflict.destroy}
-
end
-
def del_sp_dhcp
sp_dhcp_record.destroy
end
-
def del_sp_dhcp_conflicts
-
sp_dhcp_record.conflicts.each{|conflict| conflict.destroy}
-
end
-
private
returns a hash of dhcp record settings
def dhcp_attrs
@@ -84,13 +115,12 @@ module Orchestration::DHCP
end
def queue_dhcp
-
logger.debug "Scheduling new DHCP reservations"
queue.create(:name => "DHCP Settings for #{self}", :priority => 10,
:action => [self, :set_dhcp]) if dhcp?
queue.create(:name => "DHCP Settings for #{sp_name}", :priority => 15,
@@ -143,6 +173,17 @@ module Orchestration::DHCP
true
end
- def queue_remove_dhcp_conflicts
-
return unless dhcp? and errors.are_all_conflicts?
-
return unless overwrite?
-
logger.debug "Scheduling DHCP conflicts removal"
-
queue.create(:name => "DHCP conflicts removal for #{self}", :priority => 5,
-
:action => [self, :del_dhcp_conflicts]) if dhcp_record.conflicting?
-
queue.create(:name => "DHCP conflicts removal for #{sp_name}", :priority => 5,
-
:action => [self, :del_sp_dhcp_conflicts]) if sp_valid? and sp_dhcp_record.conflicting?
-
true
- end
- def ip_belongs_to_subnet?
return if subnet.nil? or ip.nil?
return unless dhcp?
diff --git a/app/models/orchestration/dns.rb b/app/models/orchestration/dns.rb
index 4e21836…6c1f5bd 100644
— a/app/models/orchestration/dns.rb
+++ b/app/models/orchestration/dns.rb
@@ -2,7 +2,7 @@ module Orchestration::DNS
def self.included(base)
base.send :include, InstanceMethods
base.class_eval do
-
after_validation :validate_dns, :queue_remove_dns_conflicts, :queue_dns
before_destroy :queue_dns_destroy
end
end
@@ -10,7 +10,7 @@ module Orchestration::DNS
module InstanceMethods
def dns?
-
!domain.nil? and !domain.proxy.nil? and (overwrite? ? errors.are_all_conflicts? : errors.empty?)
end
def dns_a_record
@@ -29,23 +29,41 @@ module Orchestration::DNS
dns_a_record.create
end
-
def set_conflicting_dns_a_record
-
dns_a_record.conflicts[0].create
-
end
-
def set_dns_ptr_record
dns_ptr_record.create
end
-
def set_conflicting_dns_ptr_record
-
dns_ptr_record.conflicts[0].create
-
end
-
def del_dns_a_record
dns_a_record.destroy
end
-
def del_conflicting_dns_a_record
-
dns_a_record.conflicts[0].destroy
-
end
-
def del_dns_ptr_record
dns_ptr_record.destroy
end
-
def del_conflicting_dns_ptr_record
-
dns_ptr_record.conflicts[0].destroy
-
end
-
def validate_dns
return unless dns?
return unless new_record?
-
failure("- #{hostname} A record is already in DNS", nil, :conflict) if dns_a_record.conflicting?
-
failure("- #{ip} PTR is already in DNS", nil, :conflict) if dns_ptr_record.conflicting?
-
return if overwrite?
-
status = true
-
(status = failure("- #{dns_a_record.conflicts[0]} exists in DNS ", nil, :conflict)) if dns_a_record.conflicting?
-
(status = (failure("- #{dns_ptr_record.conflicts[0]} exists in DNS", nil, :conflict)) and status) if dns_ptr_record.conflicting?
end
def dns_record_attrs
@@ -60,6 +78,7 @@ module Orchestration::DNS
end
def queue_dns_create
-
logger.debug "Scheduling new DNS entries"
queue.create(:name => "DNS record for #{self}", :priority => 3,
:action => [self, :set_dns_a_record])
queue.create(:name => "Reverse DNS record for #{self}", :priority => 3,
@@ -86,5 +105,15 @@ module Orchestration::DNS
:action => [self, :del_dns_ptr_record])
end
- def queue_remove_dns_conflicts
-
return unless dns? and errors.empty?
-
return unless overwrite?
-
logger.debug "Scheduling DNS conflict removal"
-
queue.create(:name => "Remove conflicting DNS record for #{self}", :priority => 1,
-
:action => [self, :del_conflicting_dns_a_record])
-
queue.create(:name => "Remove conflicting Reverse DNS record for #{self}", :priority => 1,
-
:action => [self, :del_conflicting_dns_ptr_record])
-
- end
end
end
diff --git a/app/views/hosts/_form.html.erb b/app/views/hosts/_form.html.erb
index 6a5e99d…5e8bf9b 100644
— a/app/views/hosts/_form.html.erb
+++ b/app/views/hosts/_form.html.erb
@@ -66,6 +66,6 @@
<%= textarea_f f, :comment, :help_block => “Additional information about this host”, :class => “xxlarge”, :rows => “3” %>
- <%= f.hidden_field :overwrite %>
- <%= submit_or_cancel f, @host.overwrite %>
+ <%= f.hidden_field :overwrite? %>
+ <%= submit_or_cancel f, @host.overwrite? %>
<% end %>
diff --git a/lib/core_extensions.rb b/lib/core_extensions.rb
index 12e6fd6..926e726 100644
--- a/lib/core_extensions.rb
+++ b/lib/core_extensions.rb
@@ -86,3 +86,9 @@ class String
end
end
end
+
+class ActiveModel::Errors
+ def are_all_conflicts?
+ self[:conflict].count == self.count
+ end
+end
\ No newline at end of file
--
1.7.5.4
Signed-off-by: Paul Kelly <paul.ian.kelly@googlemail.com>
···
---
test/test_helper.rb | 1 +
test/unit/orchestration/dhcp_test.rb | 4 ++--
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 615ad5f…fb3130e 100644
— a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -44,6 +44,7 @@ class ActiveSupport::TestCase
Net::DNS::PTRRecord.any_instance.stubs(:conflicting?).returns(false)
Net::DHCP::Record.any_instance.stubs(:create).returns(true)
Net::DHCP::SparcRecord.any_instance.stubs(:create).returns(true)
- Net::DHCP::Record.any_instance.stubs(:conflicting?).returns(false)
end
def disable_orchestration
diff --git a/test/unit/orchestration/dhcp_test.rb b/test/unit/orchestration/dhcp_test.rb
index 43842eb…1640c42 100644
— a/test/unit/orchestration/dhcp_test.rb
+++ b/test/unit/orchestration/dhcp_test.rb
@@ -46,7 +46,7 @@ class DhcpOrchestrationTest < ActiveSupport::TestCase
assert h.new_record?
h.name = "dummy-123"
h.ip = “2.3.4.101”
- h.mac = "bb:bb:bb:bb:bb:bb"
assert h.valid?
assert_equal h.queue.items.select {|x| x.action.last == :set_dhcp }.size, 1
assert h.queue.items.select {|x| x.action.last == :del_dhcp }.empty?
@@ -57,7 +57,7 @@ class DhcpOrchestrationTest < ActiveSupport::TestCase
assert h.new_record?
h.name = "dummy-123"
h.ip = “2.3.4.101”
- h.mac = "bb:bb:bb:bb:bb:bb"
h.sp_name = "dummy-bmc"
h.sp_ip = "2.3.4.102"
h.sp_mac = “aa:bb:cd:cd:ee:ff”
–
1.7.5.4