foreman/app/models/subnet.rb @ 611f5bff
06823dc7 | Ohad Levy | require 'ipaddr'
|
|
96b38b3c | Ohad Levy | class Subnet < ActiveRecord::Base
|
|
9fd7478e | Paul Kelly | include Authorization
|
|
611f5bff | Amos Benari | include Taxonomix
|
|
06823dc7 | Ohad Levy | has_many :hosts
|
|
# sps = Service processors / ilom boards etc
|
|||
has_many :sps, :class_name => "Host", :foreign_key => 'sp_subnet_id'
|
|||
belongs_to :dhcp, :class_name => "SmartProxy"
|
|||
belongs_to :tftp, :class_name => "SmartProxy"
|
|||
dd42df0a | Ohad Levy | belongs_to :dns, :class_name => "SmartProxy"
|
|
fa62ea80 | Ohad Levy | has_many :subnet_domains, :dependent => :destroy
|
|
has_many :domains, :through => :subnet_domains
|
|||
validates_presence_of :network, :mask, :name
|
|||
validates_associated :subnet_domains
|
|||
06823dc7 | Ohad Levy | validates_uniqueness_of :network
|
|
50751ff4 | Trey Dockendorf | validates_format_of :network, :mask, :with => Net::Validations::IP_REGEXP
|
|
8cc2722d | Florian Koch | validates_format_of :gateway, :dns_primary, :dns_secondary, :with => Net::Validations::IP_REGEXP, :allow_blank => true, :allow_nil => true
|
|
fa62ea80 | Ohad Levy | validate :name_should_be_uniq_across_domains
|
|
611f5bff | Amos Benari | ||
7a900b06 | Ohad Levy | validate :validate_ranges
|
|
96b38b3c | Ohad Levy | ||
611f5bff | Amos Benari | default_scope lambda {
|
|
with_taxonomy_scope do
|
|||
order('vlanid')
|
|||
end
|
|||
}
|
|||
017e1049 | Ohad Levy | before_destroy EnsureNotUsedBy.new(:hosts, :sps)
|
|
96b38b3c | Ohad Levy | ||
fe3d8f82 | Ohad Levy | scoped_search :on => [:name, :network, :mask, :gateway, :dns_primary, :dns_secondary, :vlanid], :complete_value => true
|
|
fa62ea80 | Ohad Levy | scoped_search :in => :domains, :on => :name, :rename => :domain, :complete_value => true
|
|
8104eced | Ohad Levy | ||
8bd4e480 | Ohad Levy | class Jail < ::Safemode::Jail
|
|
allow :name, :network, :mask, :cidr, :title, :to_label, :gateway, :dns_primary, :dns_secondary, :vlanid
|
|||
end
|
|||
06823dc7 | Ohad Levy | # Subnets are displayed in the form of their network network/network mask
|
|
96b38b3c | Ohad Levy | def to_label
|
|
06823dc7 | Ohad Levy | "#{network}/#{cidr}"
|
|
96b38b3c | Ohad Levy | end
|
|
be770e85 | Ohad Levy | def title
|
|
"#{name} (#{to_label})"
|
|||
end
|
|||
06823dc7 | Ohad Levy | # Subnets are sorted on their priority value
|
|
# [+other+] : Subnet object with which to compare ourself
|
|||
# +returns+ : Subnet object with higher precedence
|
|||
def <=> (other)
|
|||
611f5bff | Amos Benari | self.vlanid <=> other.vlanid
|
|
96b38b3c | Ohad Levy | end
|
|
06823dc7 | Ohad Levy | ||
96b38b3c | Ohad Levy | # Given an IP returns the subnet that contains that IP
|
|
# [+ip+] : "doted quad" string
|
|||
06823dc7 | Ohad Levy | # Returns : Subnet object or nil if not found
|
|
def self.subnet_for(ip)
|
|||
Subnet.all.each {|s| return s if s.contains? IPAddr.new(ip)}
|
|||
96b38b3c | Ohad Levy | nil
|
|
end
|
|||
06823dc7 | Ohad Levy | # Indicates whether the IP is within this subnet
|
|
# [+ip+] String: Contains 4 dotted decimal values
|
|||
# Returns Boolean: True if if ip is in this subnet
|
|||
def contains? ip
|
|||
IPAddr.new("#{network}/#{mask}", Socket::AF_INET).include? IPAddr.new(ip, Socket::AF_INET)
|
|||
end
|
|||
def cidr
|
|||
IPAddr.new(mask).to_i.to_s(2).count("1")
|
|||
end
|
|||
b1116c90 | Ohad Levy | def dhcp?
|
|
6285a614 | Ohad Levy | !!(dhcp and dhcp.url and !dhcp.url.blank?)
|
|
b1116c90 | Ohad Levy | end
|
|
6285a614 | Ohad Levy | def dhcp_proxy attrs = {}
|
|
@dhcp_proxy ||= ProxyAPI::DHCP.new({:url => dhcp.url}.merge(attrs)) if dhcp?
|
|||
end
|
|||
def tftp?
|
|||
!!(tftp and tftp.url and !tftp.url.blank?)
|
|||
end
|
|||
def tftp_proxy attrs = {}
|
|||
@tftp_proxy ||= ProxyAPI::TFTP.new({:url => tftp.url}.merge(attrs)) if tftp?
|
|||
06823dc7 | Ohad Levy | end
|
|
dd42df0a | Ohad Levy | # do we support DNS PTR records for this subnet
|
|
def dns?
|
|||
!!(dns and dns.url and !dns.url.blank?)
|
|||
end
|
|||
def dns_proxy attrs = {}
|
|||
@dns_proxy ||= ProxyAPI::DNS.new({:url => dns.url}.merge(attrs)) if dns?
|
|||
end
|
|||
e89efb28 | Ohad Levy | def unused_ip mac = nil
|
|
b1116c90 | Ohad Levy | return unless dhcp?
|
|
e89efb28 | Ohad Levy | dhcp_proxy.unused_ip(self, mac)["ip"]
|
|
06823dc7 | Ohad Levy | rescue => e
|
|
455f5d2b | Paul Kelly | logger.warn "Failed to fetch a free IP from our proxy: #{e}"
|
|
06823dc7 | Ohad Levy | nil
|
|
96b38b3c | Ohad Levy | end
|
|
10139fde | José Luis Escalante | ||
4c091cd8 | Ohad Levy | # imports subnets from a dhcp smart proxy
|
|
def self.import proxy
|
|||
return unless proxy.features.include?(Feature.find_by_name("DHCP"))
|
|||
ProxyAPI::DHCP.new(:url => proxy.url).subnets.map do |s|
|
|||
# do not import existing networks.
|
|||
attrs = { :network => s["network"], :mask => s["netmask"] }
|
|||
next if first(:conditions => attrs)
|
|||
new(attrs.update(:dhcp => proxy))
|
|||
end.compact
|
|||
end
|
|||
7a900b06 | Ohad Levy | private
|
|
def validate_ranges
|
|||
errors.add(:from, "invalid IP address") if from.present? and !from =~ Net::Validations::IP_REGEXP
|
|||
errors.add(:to, "invalid IP address") if to.present? and !to =~ Net::Validations::IP_REGEXP
|
|||
errors.add(:from, "does not belong to subnet") if from.present? and not self.contains?(f=IPAddr.new(from))
|
|||
errors.add(:to, "does not belong to subnet") if to.present? and not self.contains?(t=IPAddr.new(to))
|
|||
errors.add(:from, "can't be bigger than to range") if from.present? and t.present? and f > t
|
|||
67799065 | Ohad Levy | if from.present? or to.present?
|
|
7a900b06 | Ohad Levy | errors.add(:from, "must be specified if to is defined") if from.blank?
|
|
errors.add(:to, "must be specified if from is defined") if to.blank?
|
|||
end
|
|||
end
|
|||
fa62ea80 | Ohad Levy | ||
def name_should_be_uniq_across_domains
|
|||
return if domains.empty?
|
|||
domains.each do |d|
|
|||
conds = new_record? ? ['name = ?', name] : ['subnets.name = ? AND subnets.id != ?', name, id]
|
|||
errors.add(:name, "domain #{d} already has a subnet with this name") if d.subnets.where(conds).first
|
|||
end
|
|||
end
|
|||
06823dc7 | Ohad Levy | end
|