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



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

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



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

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



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

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



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

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_routing_table_has_entry(expected_attr) ⇒ Object



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

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



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

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



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

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".
  return false if ret.stdout =~ /stopped\/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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/specinfra/processor.rb', line 172

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