class Ghost::Host

Constants

CreateCmd
DeleteCmd
ListCmd
ReadCmd

Public Class Methods

add(host, ip = "127.0.0.1", force = false) click to toggle source
# File lib/ghost/mac-host.rb, line 19
def add(host, ip = "127.0.0.1", force = false)
  if find_by_host(host).nil? || force
    unless ip[%r^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/]
      ip = Socket.gethostbyname(ip)[3].bytes.to_a.join('.')
    end

    %x#{CreateCmd % [host, ip]}`
    flush!
    find_by_host(host)
  else
    raise Ghost::RecordExists, "Can not overwrite existing record"
  end
end
delete(host) click to toggle source
# File lib/ghost/mac-host.rb, line 58
def delete(host)
  %x#{DeleteCmd % host.to_s}`
  flush!
end
delete_matching(pattern) click to toggle source
# File lib/ghost/mac-host.rb, line 63
def delete_matching(pattern)
  pattern = Regexp.escape(pattern)
  hosts = list.select { |h| h.to_s.match(%r#{pattern}/) }
  hosts.each do |h|
    delete(h)
  end
  flush! unless hosts.empty?
  hosts
end
empty!() click to toggle source
# File lib/ghost/mac-host.rb, line 53
def empty!
  list.each { |h| delete(h) }
  nil
end
find_by_host(host) click to toggle source
# File lib/ghost/mac-host.rb, line 33
def find_by_host(host)
  @hosts ||= {}
  @hosts[host] ||= begin
    output = %x#{ReadCmd % host}`

    if output =~ %reDSRecordNotFound/
      nil
    else
      host = parse_host(output)
      ip = parse_ip(output)

      Host.new(host, ip)
    end
  end
end
find_by_ip(ip) click to toggle source
# File lib/ghost/mac-host.rb, line 49
def find_by_ip(ip)
  nil
end
flush!() click to toggle source

Flushes the DNS Cache

# File lib/ghost/mac-host.rb, line 74
def flush!
  %xdscacheutil -flushcache`
  @hosts = {}
  true
end
list() click to toggle source
# File lib/ghost/mac-host.rb, line 13
def list
  list = %x#{ListCmd}`
  list = list.split("\n")
  list.collect { |host| Host.new(host.chomp) }
end

Protected Class Methods

new(host, ip=nil) click to toggle source
# File lib/ghost/mac-host.rb, line 95
def initialize(host, ip=nil)
  @host = host
  @ip = ip
end
parse_host(output) click to toggle source
# File lib/ghost/mac-host.rb, line 81
def parse_host(output)
  parse_value(output, 'RecordName')
end
parse_ip(output) click to toggle source
# File lib/ghost/mac-host.rb, line 85
def parse_ip(output)
  parse_value(output, 'IPAddress')
end
parse_value(output, key) click to toggle source
# File lib/ghost/mac-host.rb, line 89
def parse_value(output, key)
  match = output.match(Regexp.new("^#{key}: (.*)$"))
  match[1] unless match.nil?
end
surround_with_tokens(str) click to toggle source
# File lib/ghost/linux-host.rb, line 153
def surround_with_tokens(str)
  "\n#{@@start_token}\n#{str}#{@@end_token}\n"
end
with_exclusive_file_access(read_only = false) { |_file| ... } click to toggle source
# File lib/ghost/linux-host.rb, line 104
def with_exclusive_file_access(read_only = false)
  return_val = nil
  flag = read_only ? 'r' : 'r+'

  if @_file
    return_val = yield @_file
  else
    File.open(@@hosts_file, flag) do |f|
      f.flock File::LOCK_EX
      begin
        @_file = f
        return_val = yield f
      ensure
        @_file = nil
      end
    end
  end

  return_val
end
write_out!(hosts) click to toggle source
# File lib/ghost/linux-host.rb, line 125
def write_out!(hosts)
  with_exclusive_file_access do |f|
    new_ghosts = hosts.inject("") {|s, h| s + "#{h.ip} #{h.hostname}\n" }

    output = ""
    in_ghost_area, seen_tokens = false,false
    f.pos = 0

    f.each do |line|
      if line =~ %r^#{@@start_token}/
        in_ghost_area, seen_tokens = true,true
        output << line << new_ghosts
      elsif line =~ %r^#{@@end_token}/
        in_ghost_area = false
      end
      output << line unless in_ghost_area
    end
    if !seen_tokens
      output << surround_with_tokens( new_ghosts )
    end

    f.pos = 0
    f.print output
    f.truncate(f.pos)
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/ghost/linux-host.rb, line 13
def ==(other)
  @host == other.host && @ip = other.ip
end
host() click to toggle source
Also aliased as: to_s, name, hostname
Alias for: hostname
hostname() click to toggle source
# File lib/ghost/mac-host.rb, line 100
def hostname
  @host
end
Also aliased as: to_s, host, name
ip() click to toggle source
# File lib/ghost/mac-host.rb, line 107
def ip
  @ip ||= self.class.send(:parse_ip, dump)
end
name() click to toggle source
Alias for: hostname
to_s() click to toggle source
Alias for: hostname