-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Kelly
committed
Oct 6, 2010
1 parent
251af69
commit c4b4755
Showing
7 changed files
with
253 additions
and
118 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
#require_dependency File.join(File.dirname(__FILE__), "..", "..", "..", "config", "initializers", "foreman") | ||
require 'netdb_manager' | ||
ActiveSupport::Dependencies.load_once_paths.delete lib_path |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
ActiveSupport::Dependencies.load_once_paths.delete File.dirname(__FILE__) | ||
# Patch the host with a transactional update facility that we use to hook our update code | ||
require 'netdb_manager/host_ext' | ||
require 'netdb_manager/active_record_ext' | ||
|
||
# Rails Engines works against us in some ways as it ensures that the last loaded module overrides the first. | ||
# As the application's version of application_helper.rb is loaded last it overrides our overrides. | ||
# Never mind, just load our overrides again, as the last loaded module. Maybe I should drop engines. . . . | ||
require_or_load File.join(File.dirname(__FILE__), '..',"app", "helpers", "application_helper.rb") | ||
ActionController::Base.prepend_view_path(File.join(File.dirname(__FILE__), '..', 'app', 'views')) | ||
|
||
#HostsController.prepend_view_path(File.join(File.dirname(__FILE__), '..', 'app', 'views')) | ||
# Our code overrides the application code | ||
Engines.disable_application_code_loading = true | ||
|
||
# If we define a view then this is the one we should use. | ||
Engines.disable_application_view_loading = true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module NetdbManager | ||
module ActiveRecordExtensions | ||
def self.included(base) #:nodoc: | ||
base.send :include, InstanceMethods | ||
base.class_eval { alias_method_chain :before_save, :netdb_support } | ||
end | ||
|
||
module InstanceMethods | ||
def before_save_with_netdb_support | ||
if self.class.to_s == "Host" | ||
require_dependency 'netdb_manager/host_ext' | ||
elsif self.class.to_s == "User" | ||
require_dependency 'netdb_manager/user_ext' | ||
end | ||
before_save_without_netdb_support | ||
end | ||
end | ||
end | ||
end | ||
ActiveRecord::Callbacks.send :include, NetdbManager::ActiveRecordExtensions |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,118 @@ | ||
module NetdbManager | ||
def self.included(base) | ||
# This implementation requires memcache | ||
if [Rails.configuration.cache_store].flatten[0] == :mem_cache_store | ||
require 'dhcp' | ||
require 'iscdhcp' | ||
require 'ipaddr' | ||
include DHCP | ||
else | ||
message = "*********************************************************************\n" + | ||
"DHCP and DNS management require that you install the memcache service\n" + | ||
"and that you add this line to environment.db \n" + | ||
"config.cache_store = :mem_cache_store \n" + | ||
"and edit config.initializers/session_store to set = :mem_cache_store \n" + | ||
"*********************************************************************\n" | ||
RAILS_DEFAULT_LOGGER.warn message | ||
puts message | ||
exit | ||
end | ||
|
||
base.extend ClassMethods | ||
base.send :include, InstanceMethods | ||
end | ||
|
||
module InstanceMethods | ||
def save_network_data | ||
return true if RAILS_ENV == "test" | ||
if @dhcp | ||
dhcpServer = @dhcp.serverFor subnet.number | ||
if new_record? | ||
setDHCP dhcpServer | ||
end | ||
module HostExtensions | ||
def self.included(base) | ||
# This implementation requires memcache | ||
if [Rails.configuration.cache_store].flatten[0] == :mem_cache_store | ||
require_dependency 'dhcp' | ||
require_dependency 'iscdhcp' | ||
require_dependency 'ipaddr' | ||
include DHCP | ||
else | ||
true # No netdb management unless we use memcache | ||
message = "*********************************************************************\n" + | ||
"DHCP and DNS management require that you install the memcache service\n" + | ||
"and that you add this line to environment.db \n" + | ||
"config.cache_store = :mem_cache_store \n" + | ||
"and edit config.initializers/session_store to set = :mem_cache_store \n" + | ||
"*********************************************************************\n" | ||
RAILS_DEFAULT_LOGGER.warn message | ||
puts message | ||
exit | ||
end | ||
|
||
base.extend ClassMethods | ||
base.send :include, InstanceMethods | ||
base.send :after_save, :transactional_update | ||
true | ||
end | ||
def delDHCP dhcpServer | ||
status = log_status("Delete a DHCP reservation for #{name}/#{ip}", dhcpServer){ | ||
dhcpServer.delReservation self | ||
} | ||
return status unless sp_valid? | ||
log_status("Delete a DHCP reservation for #{sp_name}/#{sp_ip}", dhcpServer){ | ||
dhcpServer.delReservation self, true | ||
} | ||
end | ||
# Updates the DHCP scope to add a reservation for this host | ||
# [+dhcpServer+] : A DHCPServer object | ||
# +returns+ : Boolean true on success | ||
def setDHCP dhcpServer | ||
status = log_status("Add a DHCP reservation for #{name}/#{ip}", dhcpServer){ | ||
dhcpServer.setReservation self | ||
} | ||
return status unless sp_valid? | ||
log_status("Add a DHCP reservation for #{sp_name}/#{sp_ip}", dhcpServer){ | ||
dhcpServer.setReservation self, true | ||
} | ||
end | ||
|
||
def log_status message, server, &block | ||
if server | ||
logger.info "#{message}" | ||
unless result = yield(block) | ||
first, rest = message.match(/(\w*)(.*)/)[1,2] | ||
message = "Failed to " + first.downcase + rest + ": #{server.message}" | ||
errors.add_to_base server.message | ||
logger.error message | ||
|
||
module InstanceMethods | ||
#after_save :transactional_update | ||
def save_network_data | ||
return true if RAILS_ENV == "test" | ||
if @dhcp | ||
dhcpServer = @dhcp.serverFor subnet.number | ||
if new_record? | ||
setDHCP dhcpServer | ||
end | ||
else | ||
true # No netdb management unless we use memcache | ||
end | ||
result | ||
else | ||
errors.add_to_base("Access denied") | ||
false | ||
end | ||
end | ||
|
||
def transactional_update | ||
begin | ||
initialise_network_cache | ||
save_network_data | ||
def delDHCP dhcpServer | ||
status = log_status("Delete a DHCP reservation for #{name}/#{ip}", dhcpServer){ | ||
dhcpServer.delReservation self | ||
} | ||
return status unless sp_valid? | ||
log_status("Delete a DHCP reservation for #{sp_name}/#{sp_ip}", dhcpServer){ | ||
dhcpServer.delReservation self, true | ||
} | ||
end | ||
# Updates the DHCP scope to add a reservation for this host | ||
# [+dhcpServer+] : A DHCPServer object | ||
# +returns+ : Boolean true on success | ||
def setDHCP dhcpServer | ||
status = log_status("Add a DHCP reservation for #{name}/#{ip}", dhcpServer){ | ||
dhcpServer.setReservation self | ||
} | ||
return status unless sp_valid? | ||
log_status("Add a DHCP reservation for #{sp_name}/#{sp_ip}", dhcpServer){ | ||
dhcpServer.setReservation self, true | ||
} | ||
end | ||
|
||
def log_status message, server, &block | ||
if server | ||
logger.info "#{message}" | ||
unless result = yield(block) | ||
first, rest = message.match(/(\w*)(.*)/)[1,2] | ||
message = "Failed to " + first.downcase + rest + ": #{server.message}" | ||
errors.add_to_base server.message | ||
logger.error message | ||
end | ||
result | ||
else | ||
errors.add_to_base("Access denied") | ||
false | ||
end | ||
end | ||
|
||
def initialise_network_cache | ||
return true unless @user | ||
return true if SETTINGS[:unattended] and SETTINGS[:unattended] == false | ||
|
||
@dhcp = Rails.cache.fetch(:dhcp, :expires_in => NET_TTL){ | ||
DHCP::Dhcp.new(session) | ||
}.dup # For some reason the object is frozen in this implementation of the cache! | ||
raise RuntimeException, "Unable to create DHCP memcache storage" unless @dhcp | ||
|
||
# The DHCP instance needs access to the session as some of its DHCPServer implementations need to know about the user | ||
per_user_dhcp_data = session[:dhcp_data] ||= {:user => @user.login} | ||
@dhcp.personalise(per_user_dhcp_data) | ||
true | ||
rescue => e | ||
errors.add_to_base "Failed to update the network databases" | ||
raise ActiveRecord::Rollback + e.message | ||
false | ||
end | ||
|
||
def transactional_update | ||
puts "performing transactional update" | ||
Rails.logger.debug "performing transactional update" | ||
begin | ||
initialise_network_cache | ||
save_network_data | ||
true | ||
rescue | ||
errors.add_to_base "Failed to update the network databases" | ||
raise | ||
false | ||
end | ||
end | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def initialise_network_cache | ||
return true unless @user | ||
return true if SETTINGS[:unattended] and SETTINGS[:unattended] == false | ||
|
||
@dhcp = Rails.cache.fetch(:dhcp, :expires_in => NET_TTL){ | ||
DHCP::Dhcp.new(session) | ||
}.dup # For some reason the object is frozen in this implementation of the cache! | ||
raise RuntimeException, "Unable to create DHCP memcache storage" unless @dhcp | ||
|
||
# The DHCP instance needs access to the session as some of its DHCPServer implementations need to know about the user | ||
per_user_dhcp_data = session[:dhcp_data] ||= {:user => @user.login} | ||
@dhcp.personalise(per_user_dhcp_data) | ||
true | ||
end | ||
|
||
def reload_network_data | ||
Rails.cache.clear | ||
head :created | ||
|
||
module ClassMethods | ||
def reload_network_data | ||
Rails.cache.clear | ||
head :created | ||
end | ||
end | ||
end | ||
end | ||
|
||
# And yes we need to put this in ActiveRecord and NOT Host | ||
ActiveRecord::Base.send :include, NetdbManager | ||
Host.send :include, NetdbManager::HostExtensions |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.