Class: Specinfra::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/specinfra/processor.rb

Class Method Summary collapse

Class Method Details

.check_file_is_executable(file, by_whom) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/specinfra/processor.rb', line 67

def self.check_file_is_executable(file, by_whom)
  cmd  = Specinfra.command.get(:get_file_mode, file)
  mode = sprintf('%04s',Specinfra.backend.run_command(cmd).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.to_s
  when ''
    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_file_is_mounted(path, expected_attr, only_with) ⇒ Object



84
85
86
87
88
89
90
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
# File 'lib/specinfra/processor.rb', line 84

def self.check_file_is_mounted(path, expected_attr, only_with)
  cmd = Specinfra.command.get(:check_file_is_mounted, path)
  ret = Specinfra.backend.run_command(cmd)
  if expected_attr.nil? || ret.failure?
    return ret.success?
  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_file_is_readable(file, by_whom) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/specinfra/processor.rb', line 33

def self.check_file_is_readable(file, by_whom)
  cmd = Specinfra.command.get(:get_file_mode, file)
  mode = sprintf('%04s',Specinfra.backend.run_command(cmd).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.to_s
  when ''
    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_file_is_writable(file, by_whom) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/specinfra/processor.rb', line 50

def self.check_file_is_writable(file, by_whom)
  cmd = Specinfra.command.get(:get_file_mode, file)
  mode = sprintf('%04s',Specinfra.backend.run_command(cmd).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.to_s
  when ''
    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_fstab_has_entry(expected_attr) ⇒ Object



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
# File 'lib/specinfra/processor.rb', line 118

def self.check_fstab_has_entry(expected_attr)
  return false unless expected_attr[:mount_point]
  cmd = Specinfra.command.get(:get_fstab_entry, expected_attr[:mount_point])
  ret = Specinfra.backend.run_command(cmd)
  return false if ret.failure?

  fstab = ret.stdout.scan(/\S+/)
  actual_attr = {
    :device      => fstab[0],
    :mount_point => fstab[1],
    :type        => fstab[2],
    :dump        => fstab[4].to_i,
    :pass        => fstab[5].to_i
  }
  fstab[3].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

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

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

.check_routing_table_has_entry(expected_attr) ⇒ Object



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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/specinfra/processor.rb', line 153

def self.check_routing_table_has_entry(expected_attr)
  return false if ! expected_attr[:destination]
  cmd = Specinfra.command.get(:get_routing_table_entry, expected_attr[:destination])
  ret = Specinfra.backend.run_command(cmd)
  return false if ret.failure?

  ret.stdout.gsub!(/\r\n/, "\n")

  if os[:family] == 'openbsd'
    match = ret.stdout.match(/^(\S+)\s+(\S+).*?(\S+[0-9]+)(\s*)$/)
    actual_attr = {
      :destination => $1,
      :gateway     => $2,
      :interface   => expected_attr[:interface] ? $3 : nil
    }
  else
    matches = ret.stdout.scan(/^(\S+)(?: via (\S+))? dev (\S+).+\n|^(\S+).+\n|\s+nexthop via (\S+)\s+dev (\S+).+/)
    if matches.length > 1
      # ECMP route
      destination = nil
      matches.each do |groups|
        if groups[3]
          destination = groups[3]
          next
        else
          next unless expected_attr[:interface] == groups[5]
        end

        actual_attr = {
          :destination => destination,
          :gateway => groups[4],
          :interface => groups[5]
        }
      end
    elsif matches.length == 1
      # Non-ECMP route
      groups = matches[0]
      actual_attr = {
        :destination => groups[0],
        :gateway     => groups[1] ? groups[1] : groups[3],
        :interface   => expected_attr[:interface] ? groups[2] : nil
      }
    end

  end

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

.check_service_is_monitored_by_monit(process) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/specinfra/processor.rb', line 21

def self.check_service_is_monitored_by_monit(process)
  cmd = Specinfra.command.get(:check_service_is_monitored_by_monit, process)
  ret = Specinfra.backend.run_command(cmd)
  return false unless ret.stdout != nil && ret.success?

  retlines = ret.stdout.split(/[\r\n]+/).map(&:strip)
  proc_index = retlines.index("Process '#{process}'")
  return false unless proc_index

  retlines[proc_index+2].match(/\Amonitoring status\s+monitored\Z/i) != nil
end

.check_service_is_running(service) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/specinfra/processor.rb', line 3

def self.check_service_is_running(service)
  cmd = Specinfra.command.get(:check_service_is_running, service)
  ret = Specinfra.backend.run_command(cmd)

  # In Ubuntu, some services are under upstart and "service foo status" returns
  # exit status 0 even though they are stopped.
  # So return false if stdout contains "stopped/waiting" or "stop/waiting".
  return false if ret.stdout =~ /stop(ped)?\/waiting/

  # If the service is not registered, check by ps command
  if ret.exit_status == 1
    cmd = Specinfra.command.get(:check_process_is_running, service)
    ret = Specinfra.backend.run_command(cmd)
  end

  ret.success?
end

.get_default_gateway(attr) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/specinfra/processor.rb', line 205

def self.get_default_gateway(attr)
  cmd = Specinfra.command.get(:get_routing_table_entry, 'default')
  ret = Specinfra.backend.run_command(cmd)
  return false if ret.failure?

  ret.stdout.gsub!(/\r\n/, "\n")

  if os[:family] == 'openbsd'
    match = ret.stdout.match(/^(\S+)\s+(\S+).*?(\S+[0-9]+)(\s*)$/)
    if attr == :gateway
      $2
    elsif attr == :interface
      $3
    end
  else
    ret.stdout =~ /^(\S+)(?: via (\S+))? dev (\S+).+\n(?:default via (\S+))?/
    if attr == :gateway
      $2 ? $2 : $4
    elsif attr == :interface
      $3
    end
  end
end