Class: Serverspec::Backend::Exec

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/serverspec/backend/exec.rb

Direct Known Subclasses

Ssh

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Default action is to call check_zero with args



59
60
61
# File 'lib/serverspec/backend/exec.rb', line 59

def method_missing(meth, *args, &block)
  check_zero(meth, *args)
end

Instance Method Details

#add_pre_command(cmd) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/serverspec/backend/exec.rb', line 44

def add_pre_command(cmd)
  path = Serverspec.configuration.path || RSpec.configuration.path
  if Serverspec.configuration.pre_command
    cmd = "#{Serverspec.configuration.pre_command} && #{cmd}"
    cmd = "env PATH=#{path}:$PATH #{cmd}" if path
  end
  cmd
end

#build_command(cmd) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/serverspec/backend/exec.rb', line 36

def build_command(cmd)
  path = Serverspec.configuration.path || RSpec.configuration.path
  if path
    cmd = "env PATH=#{path}:$PATH #{cmd}"
  end
  cmd
end

#check_executable(file, by_whom) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/serverspec/backend/exec.rb', line 108

def check_executable(file, by_whom)
  mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
  mode = mode.split('')
  mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
  case by_whom
  when nil
    mode_octal & 0111 != 0
  when 'owner'
    mode_octal & 0100 != 0
  when 'group'
    mode_octal & 0010 != 0
  when 'others'
    mode_octal & 0001 != 0
  end
end

#check_mounted(path, expected_attr, only_with) ⇒ Object



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
# File 'lib/serverspec/backend/exec.rb', line 124

def check_mounted(path, expected_attr, only_with)
  ret = run_command(commands.check_mounted(path))
  if expected_attr.nil? || ret[:exit_status] != 0
    return ret[:exit_status] == 0
  end

  mount = ret[:stdout].scan(/\S+/)
  actual_attr    = { :device => mount[0], :type => mount[4] }
  mount[5].gsub(/\(|\)/, '').split(',').each do |option|
    name, val = option.split('=')
    if val.nil?
      actual_attr[name.to_sym] = true
    else
      val = val.to_i if val.match(/^\d+$/)
      actual_attr[name.to_sym] = val
    end
  end

  if ! expected_attr[:options].nil?
    expected_attr.merge!(expected_attr[:options])
    expected_attr.delete(:options)
  end

  if only_with
    actual_attr == expected_attr
  else
    expected_attr.each do |key, val|
      return false if actual_attr[key] != val
    end
    true
  end
end

#check_osObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/serverspec/backend/exec.rb', line 175

def check_os
  if run_command('ls /etc/redhat-release')[:exit_status] == 0
    'RedHat'
  elsif run_command('ls /etc/system-release')[:exit_status] == 0
    'RedHat' # Amazon Linux
  elsif run_command('ls /etc/debian_version')[:exit_status] == 0
    'Debian'
  elsif run_command('ls /etc/gentoo-release')[:exit_status] == 0
    'Gentoo'
  elsif run_command('uname -s')[:stdout] =~ /SunOS/i
    'Solaris'
  elsif run_command('uname -s')[:stdout] =~ /Darwin/i
    'Darwin'
  else
    'Base'
  end
end

#check_readable(file, by_whom) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/serverspec/backend/exec.rb', line 76

def check_readable(file, by_whom)
  mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
  mode = mode.split('')
  mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
  case by_whom
  when nil
    mode_octal & 0444 != 0
  when 'owner'
    mode_octal & 0400 != 0
  when 'group'
    mode_octal & 0040 != 0
  when 'others'
    mode_octal & 0004 != 0
  end
end

#check_routing_table(expected_attr) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/serverspec/backend/exec.rb', line 157

def check_routing_table(expected_attr)
  return false if ! expected_attr[:destination]
  ret = run_command(commands.check_routing_table(expected_attr[:destination]))
  return false if ret[:exit_status] != 0

  ret[:stdout] =~ /^(\S+)(?: via (\S+))? dev (\S+).+\r\n(?:default via (\S+))?/
  actual_attr = {
    :destination => $1,
    :gateway     => $2 ? $2 : $4,
    :interface   => expected_attr[:interface] ? $3 : nil
  }

  expected_attr.each do |key, val|
    return false if actual_attr[key] != val
  end
  true
end

#check_running(process) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/serverspec/backend/exec.rb', line 63

def check_running(process)
  ret = run_command(commands.check_running(process))
  if ret[:exit_status] == 1 || ret[:stdout] =~ /stopped/
    ret = run_command(commands.check_process(process))
  end
  ret[:exit_status] == 0
end

#check_running_under_supervisor(process) ⇒ Object



71
72
73
74
# File 'lib/serverspec/backend/exec.rb', line 71

def check_running_under_supervisor(process)
  ret = run_command(commands.check_running_under_supervisor(process))
  ret[:exit_status] == 0 && ret[:stdout] =~ /RUNNING/
end

#check_writable(file, by_whom) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/serverspec/backend/exec.rb', line 92

def check_writable(file, by_whom)
  mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
  mode = mode.split('')
  mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
  case by_whom
  when nil
    mode_octal & 0222 != 0
  when 'owner'
    mode_octal & 0200 != 0
  when 'group'
    mode_octal & 0020 != 0
  when 'others'
    mode_octal & 0002 != 0
  end
end

#check_zero(cmd, *args) ⇒ Object



53
54
55
56
# File 'lib/serverspec/backend/exec.rb', line 53

def check_zero(cmd, *args)
  ret = run_command(commands.send(cmd, *args))
  ret[:exit_status] == 0
end

#commandsObject



16
17
18
# File 'lib/serverspec/backend/exec.rb', line 16

def commands
  @commands
end

#run_command(cmd, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/serverspec/backend/exec.rb', line 20

def run_command(cmd, opts={})
  cmd = build_command(cmd)
  cmd = add_pre_command(cmd)
  stdout = `#{build_command(cmd)} 2>&1`
  # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
  #stdout, stderr, status = Open3.capture3(cmd)

  if @example
    @example.[:command] = cmd
    @example.[:stdout]  = stdout
  end

  { :stdout => stdout, :stderr => nil,
    :exit_status => $?, :exit_signal => nil }
end

#set_commands(c) ⇒ Object



8
9
10
# File 'lib/serverspec/backend/exec.rb', line 8

def set_commands(c)
  @commands = c
end

#set_example(e) ⇒ Object



12
13
14
# File 'lib/serverspec/backend/exec.rb', line 12

def set_example(e)
  @example = e
end