Class: Fiveruns::Dash::Host

Inherits:
Object show all
Defined in:
lib/fiveruns/dash/host.rb

Constant Summary collapse

UNIXES =
[:osx, :linux, :solaris]

Instance Method Summary collapse

Constructor Details

#initializeHost

Returns a new instance of Host.



7
8
9
# File 'lib/fiveruns/dash/host.rb', line 7

def initialize
  configure_host
end

Instance Method Details

#architectureObject



11
12
13
# File 'lib/fiveruns/dash/host.rb', line 11

def architecture
  @architecture
end

#big_endian?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/fiveruns/dash/host.rb', line 23

def big_endian?
  @big_endian
end

#configure_hostObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/fiveruns/dash/host.rb', line 91

def configure_host
  @hostname ||= `hostname`.strip!
  @big_endian = ([123].pack("s") == [123].pack("n"))
  case RUBY_PLATFORM
  when /darwin|linux/
    begin # use Sys::Uname library if present
      require 'sys/uname'
      @os_name = Sys::Uname.sysname
      @architecture = Sys::Uname.machine
      @os_version = Sys::Uname.release 
    rescue LoadError # otherwise shell out and scrape out the information
      @os_name = `uname -s`.strip
      @architecture = `uname -p`.strip
      @os_version = `uname -r`.strip
    end
  # when /win32|i386-mingw32/
  #   require "dl/win32"
  #   getVersionEx = Win32API.new("kernel32", "GetVersionExA", ['P'], 'L')
  #     
  # This line done broke on Ruby 1.9.1:
  #   lpVersionInfo = [148, 0, 0, 0, 0].pack("LLLLL") + "�0" * 128 
  #   getVersionEx.Call lpVersionInfo
  #   
  #   dwOSVersionInfoSize, dwMajorVersion, dwMinorVersion, dwBuildNumber, dwPlatformId, szCSDVersion = lpVersionInfo.unpack("LLLLLC128")
  #   @os_name = ['Windows 3.1/3.11', 'Windows 95/98', 'Windows NT/XP'][dwPlatformId]
  #   @os_version = "#{dwMajorVersion}.#{dwMinorVersion}"
  #   @architecture = ENV['PROCESSOR_ARCHITECTURE']
  end

  @ip_addresses = []
  begin # use Sys::Host library if present
    require 'sys/host'
    Sys::Host.ip_addr.each do |ip|
      addresses << ip
    end
  rescue LoadError # otherwise shell out and scrape out the information
    execute_on_osx  do
      ifconfig = `ifconfig`
      x = 0
      while true
        if ifconfig =~ /en#{x}:/
          x+=1
        else
          break
        end
      end
      x.times do |dev|
        ifconfig = `ifconfig en#{dev}`
        ifconfig.scan(/ether ([0-9a-f\:]*) /) do |mac_address|
          @mac_address ||= mac_address[0]
        end
        ifconfig.scan(/inet ([0-9\.]*) /) { |ip| @ip_addresses << ["en#{dev}", ip[0]] }
      end
    end
    execute_on_solaris do
      arp = `/usr/sbin/arp -an`.split("\n")
      re = /^(\w+).*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*((([a-f0-9]{2}):){5}[a-f0-9]{2})/
      arp.find do |line|
        line =~ re
      end
      @ip_addresses << $2
      @mac_address = $3
    end
    execute_on_linux do
      ifconfig = `/sbin/ifconfig`
      x = 0
      while true
        if ifconfig =~ /eth#{x} /
          x+=1
        else
          break
        end
      end
      x.times do |dev|
        ifconfig = `/sbin/ifconfig eth#{dev}`
        ifconfig.scan(/HWaddr ([0-9A-Fa-f\:]*) /) do |mac_address|
          @mac_address ||= mac_address[0]
        end
        ifconfig.scan(/inet addr:([0-9\.]*) /) { |ip| @ip_addresses << ["eth#{dev}", ip[0]] }
      end
      @mac_address ||= "#{@hostname}-UNKNOWN-MAC"
    end
    execute_on_windows do
      addrs = Socket.getaddrinfo(Socket.gethostname, 80)
      addrs.each do |addr|
        @ip_addresses << ['eth0', addr[3]]
      end
    end
  end
end

#execute_on_linux(&block) ⇒ Object



57
58
59
# File 'lib/fiveruns/dash/host.rb', line 57

def execute_on_linux(&block)
  block.call if RUBY_PLATFORM =~ /linux/
end

#execute_on_osx(&block) ⇒ Object



53
54
55
# File 'lib/fiveruns/dash/host.rb', line 53

def execute_on_osx(&block)
  block.call if RUBY_PLATFORM =~ /darwin/
end

#execute_on_solaris(&block) ⇒ Object



61
62
63
# File 'lib/fiveruns/dash/host.rb', line 61

def execute_on_solaris(&block)
  block.call if RUBY_PLATFORM =~ /solaris/
end

#execute_on_unix(&block) ⇒ Object



47
48
49
50
51
# File 'lib/fiveruns/dash/host.rb', line 47

def execute_on_unix(&block)
  UNIXES.each do |unix|
    send "execute_on_#{unix}", &block
  end
end

#execute_on_windows(&block) ⇒ Object



65
66
67
# File 'lib/fiveruns/dash/host.rb', line 65

def execute_on_windows(&block)
  block.call if RUBY_PLATFORM =~ /win32|i386-mingw32/
end

#hostnameObject



78
79
80
# File 'lib/fiveruns/dash/host.rb', line 78

def hostname
  @hostname
end

#ip_addressObject



82
83
84
85
# File 'lib/fiveruns/dash/host.rb', line 82

def ip_address
  address = ip_addresses[0]
  address ? address[1] : "127.0.0.1"
end

#ip_addressesObject



31
32
33
# File 'lib/fiveruns/dash/host.rb', line 31

def ip_addresses
  @ip_addresses
end

#little_endian?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/fiveruns/dash/host.rb', line 27

def little_endian?
  !@big_endian
end

#mac_addressObject



87
88
89
# File 'lib/fiveruns/dash/host.rb', line 87

def mac_address
  @mac_address
end

#os_nameObject



15
16
17
# File 'lib/fiveruns/dash/host.rb', line 15

def os_name
  @os_name
end

#os_name_match?(name) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/fiveruns/dash/host.rb', line 35

def os_name_match?(name)
  platform == name
end

#os_versionObject



19
20
21
# File 'lib/fiveruns/dash/host.rb', line 19

def os_version
  @os_version
end

#platformObject



39
40
41
42
43
44
45
# File 'lib/fiveruns/dash/host.rb', line 39

def platform
  execute_on_osx { return :osx }
  execute_on_windows { return :windows }
  execute_on_linux { return :linux }
  execute_on_solaris { return :solaris }
  :unknown
end

#result(command, args, windows_ext = nil) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/fiveruns/dash/host.rb', line 69

def result(command, args, windows_ext=nil)
  name = if windows_ext && os_name_match?(:windows)
    "#{command}.#{windows_ext}"
  else
    command.to_s
  end
  `#{name} #{args}`
end