Class: VagrantTestSubject::VM::RedHat

Inherits:
VagrantTestSubject::VM show all
Defined in:
lib/vagrant-test-subject/os/redhat.rb

Constant Summary collapse

@@SERVICE_NAMES =
{
  'trafficserver' => 'trafficserver'
}

Instance Attribute Summary

Attributes inherited from VagrantTestSubject::VM

#external_ip, #ssh, #vbox_guid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VagrantTestSubject::VM

attach, #has_matching_process_listening?, #http_get, #https_get, #list_internal_ports, #listening_on_external_ip?, #listening_on_localhost?, #listening_on_portmap?, #map_port

Constructor Details

#initialize(*args) ⇒ RedHat

Returns a new instance of RedHat.



8
9
10
# File 'lib/vagrant-test-subject/os/redhat.rb', line 8

def initialize(*args)
  super
end

Class Method Details

.register_service_alias(human_name, os_specific_name) ⇒ Object



12
13
14
# File 'lib/vagrant-test-subject/os/redhat.rb', line 12

def self.register_service_alias(human_name, os_specific_name)
  @@SERVICE_NAMES[human_name] = os_specific_name
end

Instance Method Details

#has_running_service?(human_service_name) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/vagrant-test-subject/os/redhat.rb', line 16

def has_running_service? (human_service_name)
  redhat_service_name = @@SERVICE_NAMES[human_service_name] || human_service_name
  output = self.ssh.exec!("/sbin/service #{redhat_service_name} status; echo exit_code:$?")
  /exit_code:(?<exit_code>\d+)/ =~ output
  return exit_code.to_i == 0 
end

#listening_portsObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/vagrant-test-subject/os/redhat.rb', line 23

def listening_ports
  cmd = "netstat -ln --inet | tail -n +3 | awk '{print $4}'"
  open_ports = []
  self.ssh.exec!(cmd).split("\n").each do |line|
    ip, port = line.split(':')
    next unless ip
    open_ports << { :ip => ip, :port => port.to_i }
  end
  open_ports
end

#process_name_listening(ip, vm_port) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vagrant-test-subject/os/redhat.rb', line 34

def process_name_listening (ip, vm_port)
  # Note the trailing space after port - prevents 127.0.0.1:80 from matching 127.0.0.1:8084
  cmd = "sudo netstat -lnp --inet | grep '#{ip}:#{vm_port} ' | awk '{print $7}'"
  output = self.ssh.exec!(cmd)
  if output.nil? then
    cmd = "sudo netstat -lnp --inet | grep '0.0.0.0:#{vm_port} ' | awk '{print $7}'"
    output = self.ssh.exec!(cmd)
  end       
  return nil if output.nil?
  output.chomp!

  pid, truncated_process = output.split('/');
  unless pid =~ /^\d+$/ then
    raise "Not sure what this means, when running netstat -nlp:\n#{output}"
  end

  cmd = "ps -fp #{pid} | tail -n +2 | awk '{print $8}'"
  process_string = self.ssh.exec!(cmd).chomp()
  return process_string
end