Skip to content

Commit

Permalink
wipi
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Kelly committed Oct 6, 2010
1 parent c4b4755 commit 7443766
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 72 deletions.
2 changes: 1 addition & 1 deletion lib/dhcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# The second level class is vendor specific Currently we support Microsoft Servers
# via a web based gateway to the netsh.exe program and the InfoBlox DHCP server, which
# is accessed directly
# The DHCP entries are cached in a Memcache store and are share among all users of the
# The DHCP entries are cached in a Memcache store and are shared among all users of the
# system
module DHCP
# There has been a problem with the gateway transport or the data
Expand Down
4 changes: 2 additions & 2 deletions lib/netdb_manager.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Patch the host with a transactional update facility that we use to hook our update code
require 'netdb_manager/active_record_ext'
# Patch the ActionController with a alias_method_chain that loads our modifications to the models
require 'netdb_manager/action_controller_ext'

ActionController::Base.prepend_view_path(File.join(File.dirname(__FILE__), '..', 'app', 'views'))

Expand Down
20 changes: 20 additions & 0 deletions lib/netdb_manager/action_controller_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module NetdbManager
module ActionControllerExtensions
def self.included(base) #:nodoc:
base.send :include, InstanceMethods
base.class_eval do
alias_method_chain :process, :netdb_support
end
end

module InstanceMethods
def process_with_netdb_support *args
require_dependency 'netdb_manager/host_ext'
require_dependency 'netdb_manager/user_ext'
require_dependency 'netdb_manager/application_controller_ext'
process_without_netdb_support *args
end
end
end
end
ActionController::Base.send :include, NetdbManager::ActionControllerExtensions
20 changes: 0 additions & 20 deletions lib/netdb_manager/active_record_ext.rb

This file was deleted.

46 changes: 46 additions & 0 deletions lib/netdb_manager/application_controller_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module NetdbManager
module ApplicationControllerExtensions
NET_TTL = 7200

def self.included(base) #:nodoc:
base.send :include, InstanceMethods
base.class_eval do
before_filter :initialise_caches
end
end

module InstanceMethods
def initialise_caches
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

@user_cache = Rails.cache.fetch("user_cache", :expires_in => NET_TTL){
{}
}.dup # For some reason the object is frozen in this implementation of the cache!
raise RuntimeException, "Unable to create password cache storage" unless @pass

# The DHCP instance needs access to the session as some of its DHCPServer implementations need to know about the user
per_user_data = @user_cache[login]
@dhcp.personalise(per_user_data)
true
end
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
end
end
end
end
ApplicaitonController::Base.send :include, NetdbManager::ApplicationControllerExtensions
35 changes: 3 additions & 32 deletions lib/netdb_manager/host_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,13 @@ def self.included(base)

base.extend ClassMethods
base.send :include, InstanceMethods
base.send :after_save, :transactional_update
base.class_eval do
after_save :transactional_update
end
true
end

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
end
def delDHCP dhcpServer
status = log_status("Delete a DHCP reservation for #{name}/#{ip}", dhcpServer){
dhcpServer.delReservation self
Expand Down Expand Up @@ -76,21 +66,6 @@ def log_status message, server, &block
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
end

def transactional_update
puts "performing transactional update"
Rails.logger.debug "performing transactional update"
Expand All @@ -107,10 +82,6 @@ def transactional_update
end

module ClassMethods
def reload_network_data
Rails.cache.clear
head :created
end
end
end
end
Expand Down
31 changes: 14 additions & 17 deletions lib/netdb_manager/user_ext.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
module NetdbManager
module UserExtensions
NET_TTL = 7200
def self.included(base) #:nodoc:
base.class_eval { alias_method_chain :try_to_login, :networking_support }
base.extend ClassMethods
base.send :include, InstanceMethods
base.class_eval do
class << self
alias_method_chain :try_to_login, :netdb_support
end
end
end

module InstanceMethods
end

module ClassMethods

def try_to_login_with_networking_support(login, password)
if user = try_to_login_without_network_support
User.initialise_network_support
def try_to_login_with_netdb_support(login, password)
if user = self.try_to_login_without_netdb_support(login, password)
User.capture_user_data user
end
user
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)
def capture_user_data user
if @user_cache
@user_cache[user.login] = {:password => user.password, :rejected => {}}
Rails.cache.write "user_cache", @user_cache, :expires_in => NET_TTL
end
true
end
end
end
end
# And yes we need to put this in ActiveRecord and NOT User
User.send :include, NetdbManager::UserExtensions

0 comments on commit 7443766

Please sign in to comment.