Skip to content

Commit

Permalink
Extended the error returns and moved to netsvcs
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Kelly committed Nov 5, 2010
1 parent d4895f0 commit a2d1332
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 22 deletions.
4 changes: 4 additions & 0 deletions app/views/domains/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<%= f.label :dns_id %><br />
<%= f.collection_select :dns_id, Netsvc.servertype_name_is("DNS"), :id, :name %>
</p>
<p>
<%= f.label :tftp_id %><br />
<%= f.collection_select :tftp_id, Netsvc.servertype_name_is("TFTP"), :id, :name %>
</p>
<%= render :partial => "common_parameters/parameters", :locals => { :f => f, :type => :domain_parameters } %>

<p><%= f.submit "Submit" %></p>
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/_settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
['Host Groups', hostgroups_url],
['Installation Medias', medias_url],
['LDAP Authentication', auth_source_ldaps_url],
['Network Datbases', netsvcs_url],
['Network Services', netsvcs_url],
['Operating Systems', operatingsystems_url],
['Partition Tables', ptables_url],
['Puppet Classes', puppetclasses_url],
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion lib/netsvc_manager/domain_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ def self.included(base) #:nodoc:
base.extend ClassMethods
base.send :include, InstanceMethods
base.class_eval do
belongs_to :dns, :class_name => 'Netsvc'
belongs_to :dns, :class_name => 'Netsvc'
belongs_to :tftp, :class_name => 'Netsvc'
end
end

Expand Down
28 changes: 18 additions & 10 deletions lib/netsvc_manager/host_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def self.included(base)
base.send :include, InstanceMethods
base.class_eval do
attr_accessor :dns, :dhcp
before_create :initialize_proxies, :check_netsvcs
after_create :create_netsvcs, :initialize_tftp
before_create :initialize_proxies, :cache_tftp_files, :check_netdbs
after_create :create_netsvcs
after_update :initialize_proxies, :update_netsvcs
after_destroy :initialize_proxies, :destroy_netsvcs
end
Expand All @@ -31,12 +31,20 @@ def self.included(base)

module InstanceMethods
# Ensure that the tftp bootfiles are available on the proxy host
def initialize_tftp

def cache_tftp_files
for bootfile_info in operatingsystem.pxe_files(media, architecture)
for prefix, path in bootfile_info do
return false unless \
log_transaction("Download a bootfile from #{path} into #{prefix}", @tftp, true) do |tftp|
tftp.fetch_boot_file :prefix => prefix.to_s, :path => path
end
end
end
end

# Checks whether DNS or DHCP entries already exist
# Returns: Boolean true if no entries exists
def check_netsvcs
def check_netdbs
continue = true
if (address = @resolver.getaddress(name) rescue false)
errors.add_to_base "#{name} is already in DNS with an address of #{address}"
Expand Down Expand Up @@ -123,8 +131,8 @@ def delDNS
}
end

def log_transaction message, server
logger.info "#{message}"
def log_transaction message, server, only_log_errors = false
logger.info "#{message}" unless only_log_errors
unless result = yield(server)
first, rest = message.match(/(\w*)(.*)/)[1,2]
message = "Failed to " + first.downcase + rest + ": #{server.error}"
Expand All @@ -135,9 +143,9 @@ def log_transaction message, server
end

def initialize_proxies
proxy_address = "http://#{subnet.dhcp.address}:4567"
@dhcp = ProxyAPI::DHCP.new(:url => proxy_address)
@dns = ProxyAPI::DNS.new(:url => proxy_address)
@dhcp = ProxyAPI::DHCP.new(:url => "http://#{subnet.dhcp.address}:4567")
@dns = ProxyAPI::DNS.new( :url => "http://#{domain.dns.address}:4567")
@tftp = ProxyAPI::TFTP.new(:url => "http://#{domain.tftp.address}:4567")
@resolver = Resolv::DNS.new :search => domain.name, :nameserver => domain.dns.address
end

Expand Down
41 changes: 31 additions & 10 deletions lib/proxy_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def initialize(args)
:headers => { :accept => :json, :content_type => :json }}
end

def hostname
match = @resource.to_s.match(/:\/\/([^\/:]+)/)
match ? match[1] : "hostname in #{@resource}"
end

def list
strip_hash_name(parse(get(path_prefix)))
Expand Down Expand Up @@ -68,7 +72,7 @@ def parse reply
def _get_ path
@resource[URI.escape(path)].get{|response, request, result| [result, response] }
rescue Exception => e
[ OpenStruct.new(:code => "500"), e.message]
[ OpenStruct.new(:code => "500"), e.message =~ /getaddrinfo/ ? "Unable to locate #{hostname}" : e.message]
end

# Perform POST operation with the supplied payload on the supplied path
Expand All @@ -78,7 +82,7 @@ def _get_ path
def _post_ payload, path
@resource[path].post(payload){|response, request, result| [result, response] }
rescue Exception => e
[ OpenStruct.new(:code => "500"), e.message]
[ OpenStruct.new(:code => "500"), e.message =~ /getaddrinfo/ ? "Unable to locate #{hostname}" : e.message]
end

# Perform PUT operation with the supplied payload on the supplied path
Expand All @@ -88,7 +92,7 @@ def _post_ payload, path
def _put_ payload, path
@resource[path].put(payload){|response, request, result| [result, response] }
rescue Exception => e
[ OpenStruct.new(:code => "500"), e.message]
[ OpenStruct.new(:code => "500"), e.message =~ /getaddrinfo/ ? "Unable to locate #{hostname}" : e.message]
end

# Perform DELETE operation on the supplied path
Expand All @@ -98,7 +102,7 @@ def _put_ payload, path
def _delete_ path
@resource[path].delete{|response, request, result| [result, response] }
rescue Exception => e
[ OpenStruct.new(:code => "500"), e.message]
[ OpenStruct.new(:code => "500"), e.message =~ /getaddrinfo/ ? "Unable to locate #{hostname}" : e.message]
end

def hash_name
Expand All @@ -112,24 +116,23 @@ def opts

class DHCP < Resource
def initialize args
@url = args[:url] || raise("Must provide a URL")
@url += "/dhcp"
@url = args[:url] + "/dhcp" || raise("Must provide a URL")
super args
end

# Retrive the Server's subnets
# Returns: Array of Hashes or false
# Example [{"network":"192.168.11.0","netmask":"255.255.255.0"},{"network":"192.168.122.0","netmask":"255.255.255.0"}]
def subnets
parse(_get_(".json"))
parse(_get_(""))
end

# Retrieves a DHCP entry
# [+subnet+] : String in dotted decimal format
# [+mac+] : String in coloned sexpulet format
# Returns : Hash or false
def get subnet, mac
parse(_get_("#{subnet}/#{mac}.json"))
parse(_get_("#{subnet}/#{mac}"))
end

# Sets a DHCP entry
Expand All @@ -152,8 +155,7 @@ def delete subnet, mac

class DNS < Resource
def initialize args
@url = args[:url] || raise("Must provide a URL")
@url += "/dns"
@url = args[:url] + "/dns" || raise("Must provide a URL")
super args
end

Expand All @@ -172,6 +174,25 @@ def delete key
parse(_delete_("#{key}"))
end
end

class TFTP < Resource
def initialize args
@url = args[:url] + "/tftp" || raise("Must provide a URL")
super args
end

def set mac, args
parse(_post_(args, mac))
end

def delete mac
parse(_delete_("#{mac}"))
end

def fetch_boot_file args
parse(_post_(args, "fetch_boot_file"))
end
end
end

if __FILE__.gsub(/\.\//, "") == $0
Expand Down

0 comments on commit a2d1332

Please sign in to comment.