Skip to content

Commit

Permalink
Removing cacheing
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Kelly committed Oct 24, 2010
1 parent 77311f8 commit bd7e92c
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 59 deletions.
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def settings_dropdown
['Host Groups', hostgroups_url],
['Installation Medias', medias_url],
['LDAP Authentication', auth_source_ldaps_url],
['Networtk database', netdbs_url],
['Operating Systems', operatingsystems_url],
['Partition Tables', ptables_url],
['Puppet Classes', puppetclasses_url],
Expand Down
2 changes: 1 addition & 1 deletion app/views/hosts/_unattended.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<td>Domain</td>
<td><%= f.collection_select :domain_id, Domain.all, :id, :to_label %></td>
<td>Subnet</td>
<% if sn = Subnet.first.dhcp; DHCP.cache_server @dhcp_servers, @dhcp, @user_data, dhcp; end -%>
<% if sn = Subnet.first.dhcp; DHCP.load_server dhcp; end -%>
<td><%= f.collection_select :subnet_id, Subnet.all, :id, :to_label %></td>
<td>IP</td>
<td><%= f.text_field :ip, :size => 16 %></td>
Expand Down
4 changes: 2 additions & 2 deletions app/views/netdbs/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% title "New Subnet" %>
<% title "New network database" %>

<%= render :partial => 'form' %>

<p><%= link_to "Back to List", subnets_path %></p>
<p><%= link_to "Back to List", netdbs_path %></p>
4 changes: 2 additions & 2 deletions app/views/subnets/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<%= f.text_field :ranges, :title => "A list of comma separated single IPs or start-end couples." %>
</p>
<p>
<%= f.label :priority %><br />
<%= f.text_field :priority %>
<%= f.label :dhcp_id %><br />
<%= f.collection_select :dhcp_id, Netdb.servertype_name_is("DHCP"), :id, :name %>
</p>
<p>
<%= f.label :vlanid %><br />
Expand Down
4 changes: 2 additions & 2 deletions app/views/subnets/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<th>Number</th>
<th>Mask</th>
<th>Ranges</th>
<th>Priority</th>
<th>DHCP server</th>
<th>VLAN id</th>
<th></th>
</tr>
Expand All @@ -18,7 +18,7 @@
<td><%=subnet.number%></td>
<td><%=subnet.mask%></td>
<td><%=subnet.ranges%></td>
<td><%=subnet.priority%></td>
<td><%=subnet.dhcp.try :name%></td>
<td><%=subnet.vlanid%></td>
<td align="right">
<%= link_to "Destroy", subnet, :confirm => "Delete #{subnet.name}?", :method => :delete %>
Expand Down
1 change: 0 additions & 1 deletion lib/implementation
Submodule implementation deleted from 4fb42a
5 changes: 4 additions & 1 deletion lib/netdb_manager/action_controller_ext.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module NetdbManager
mattr_accessor :dhcp_servers, :dhcp, :user_data

module ActionControllerExtensions
def self.included(base) #:nodoc:
base.send :include, InstanceMethods
Expand All @@ -9,12 +11,13 @@ def self.included(base) #:nodoc:

module InstanceMethods
def process_with_netdb_support *args
require_dependency 'implementation/lib/dhcp'
#require_dependency 'implementation/lib/dhcp'
require_dependency 'netdb_manager/host_ext'
require_dependency 'netdb_manager/user_ext'
require_dependency 'netdb_manager/subnet_ext'
require_dependency 'netdb_manager/application_controller_ext'
require_dependency 'netdb_manager/hosts_controller_ext'
#require_dependency 'resolv'
process_without_netdb_support *args
end
end
Expand Down
27 changes: 8 additions & 19 deletions lib/netdb_manager/application_controller_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,26 @@ def load_netdb_caches
return true if SETTINGS[:unattended] and SETTINGS[:unattended] == false

# Fetch the list of server that are memcached
@dhcp_servers = Rails.cache.fetch("dhcp_servers", :expires_in => NET_TTL){[]}.dup
raise RuntimeException, "Unable to create DHCP memcache storage" unless @dhcp_servers
# NetdbManager.dhcp_servers = Rails.cache.fetch("dhcp_servers", :expires_in => NET_TTL){[]}.dup
# raise RuntimeException, "Unable to create DHCP memcache storage" unless NetdbManager.dhcp_servers

# Fetch the memcached servers
@dhcp = {}
for server in @dhcp_servers
@dhcp[server] = Rails.cache.fetch(server, :expires_in => NET_TTL){{}}.dup
raise RuntimeError, "Unable to retrieve server data for #{server}" if @dhcp[server].size == 0
NetdbManager.dhcp = {}
for server in NetdbManager.dhcp_servers
NetdbManager.dhcp[server] = Rails.cache.fetch(server, :expires_in => NET_TTL){{}}.dup
raise RuntimeError, "Unable to retrieve server data for #{server}" if NetdbManager.dhcp[server].size == 0
end

# Fetch the user data mecache. This holds per-user data dependant on the server implementation
@user_cache = Rails.cache.fetch("user_cache", :expires_in => NET_TTL){{}}.dup
raise RuntimeError, "Unable to create user cache storage" unless @user_cache
NetdbManager.user_data = Rails.cache.fetch("user_data", :expires_in => NET_TTL){{}}.dup
raise RuntimeError, "Unable to create user data cache storage" unless NetdbManager.user_data

# 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[@user.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
Expand Down
61 changes: 37 additions & 24 deletions lib/netdb_manager/host_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ module HostExtensions
def self.included(base)
# This implementation requires memcache
if [Rails.configuration.cache_store].flatten[0] == :mem_cache_store
require_dependency 'implementation/lib/dhcp'
#require_dependency 'iscdhcp'
require_dependency 'proxy_api'
require_dependency '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" +
"!!!!Foreman will not operate until these tasks have been completed!!!\n" +
"*********************************************************************\n"
RAILS_DEFAULT_LOGGER.warn message
puts message
Expand All @@ -22,9 +21,8 @@ def self.included(base)
base.extend ClassMethods
base.send :include, InstanceMethods
base.class_eval do
before_validation :load_netdb_caches
after_validation :check_dns
after_save :transactional_update, :save_netdb_caches
after_save :update_netdbs
end
true
end
Expand All @@ -38,29 +36,30 @@ def save_netdb_caches

end

def delDHCP dhcpServer
status = log_status("Delete a DHCP reservation for #{name}/#{ip}", dhcpServer){
dhcpServer.delReservation self
def delDHCP dhcp_server
status = log_transaction("Delete a DHCP reservation for #{name}/#{ip}", dhcp_server){
dhcp_server.delReservation self
}
return status unless sp_valid?
log_status("Delete a DHCP reservation for #{sp_name}/#{sp_ip}", dhcpServer){
dhcpServer.delReservation self, true
log_transaction("Delete a DHCP reservation for #{sp_name}/#{sp_ip}", dhcp_server){
dhcp_server.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
# [+dhcp_server+] : A DHCPServer object
# +returns+ : Boolean true on success
def setDHCP dhcp
status = log_transaction("Add a DHCP reservation for #{name}/#{ip}", dhcp_server){
#nextserver needs to be an ip
dhcp.set subnet.number, mac, :nextserver => resolver.getaddress(puppetmaster).to_s, :name => name, :filename => media.bootfile, :ip => ip
}
return status unless sp_valid?
log_status("Add a DHCP reservation for #{sp_name}/#{sp_ip}", dhcpServer){
dhcpServer.setReservation self, true
log_transaction("Add a DHCP reservation for #{sp_name}/#{sp_ip}", dhcp_server){
dhcp.set sp_subnet.number, sp_mac, :name => sp_name, :ip => sp_ip
}
end

def log_status message, server, &block
def log_transaction message, server, &block
if server
logger.info "#{message}"
unless result = yield(block)
Expand All @@ -76,18 +75,32 @@ def log_status message, server, &block
end
end

def transactional_update
puts "performing transactional update"
def update_netdbs
return true if RAILS_ENV == "test"

Rails.logger.debug "performing transactional update"
begin
save_network_data
save_dhcp_data
true
rescue
errors.add_to_base "Failed to update the network databases"
raise
rescue => e
errors.add_to_base "Failed to update the network databases: " + e.message
raise ActiveRecord::Rollback
false
end
end

def save_dhcp_data
dhcp = ProxyAPI::DHCP.new(:url => "http://#{subnet.dhcp.address}:4567")
if !dhcp.empty? and dhcp.subnets.include? subnet.number
setDHCP dhcp_server
else
raise RuntimeError, "Unable to find the subnet in the cache"
end
end

def validate
# FIXME: host.errors.add :ip, "Subnet #{subnet} cannot contain #{ip}" unless self.subnet.contains? ip
end
end

module ClassMethods
Expand Down
11 changes: 7 additions & 4 deletions lib/netdb_manager/hosts_controller_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ module HostsControllerExtensions
def self.included(base) #:nodoc:
base.send :include, InstanceMethods
base.class_eval do
before_filter :load_netdb_caches
before_filter :load_netdb_caches, :only => [:clone, :update, :create]
end

end

module InstanceMethods
def subnet_selected
dhcp = Subnet.find(params[:subnet_id]).dhcp

DHCP.cache_server @dhcp_servers, @dhcp, @user_cache, dhcp
subnet = Subnet.find(params[:subnet_id])

dhcp_server = DHCP.load_server subnet.dhcp
dhcp_server.loadSubnetData DHCP::Server[subnet.number] if dhcp_server.find_subnet(subnet.number).size == 0
NetdbManager.dhcp[subnet.dhcp.address] = dhcp_server
head :ok
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/netdb_manager/user_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def try_to_login_with_netdb_support(login, password)
end

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
if @user_data
@user_data[user.login] = {:password => user.password, :rejected => {}}
Rails.cache.write "user_cache", @user_data, :expires_in => NET_TTL
end
true
end
Expand Down
Loading

0 comments on commit bd7e92c

Please sign in to comment.