Class: SpecInfra::Command::Base

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

Direct Known Subclasses

AIX, Darwin, FreeBSD, Linux, OpenBSD, Solaris

Defined Under Namespace

Classes: NotImplementedError

Instance Method Summary collapse

Instance Method Details

#check_access_by_user(file, user, access) ⇒ Object



319
320
321
# File 'lib/specinfra/command/base.rb', line 319

def check_access_by_user(file, user, access)
  raise NotImplementedError.new
end

#check_authorized_key(user, key) ⇒ Object



282
283
284
285
# File 'lib/specinfra/command/base.rb', line 282

def check_authorized_key(user, key)
  key.sub!(/\s+\S*$/, '') if key.match(/^\S+\s+\S+\s+\S*$/)
  "grep -w -- #{escape(key)} ~#{escape(user)}/.ssh/authorized_keys"
end

#check_belonging_group(user, group) ⇒ Object



256
257
258
# File 'lib/specinfra/command/base.rb', line 256

def check_belonging_group(user, group)
  "id #{escape(user)} | awk '{print $3}' | grep -- #{escape(group)}"
end

#check_container(container) ⇒ Object



340
341
342
# File 'lib/specinfra/command/base.rb', line 340

def check_container(container)
  raise NotImplementedError.new
end

#check_cotainer_running(container) ⇒ Object



344
345
346
# File 'lib/specinfra/command/base.rb', line 344

def check_cotainer_running(container)
  raise NotImplementedError.new
end

#check_cron_entry(user, entry) ⇒ Object



202
203
204
205
206
207
208
209
# File 'lib/specinfra/command/base.rb', line 202

def check_cron_entry(user, entry)
  entry_escaped = entry.gsub(/\*/, '\\*').gsub(/\[/, '\\[').gsub(/\]/, '\\]')
  if user.nil?
    "crontab -l | grep -v \"#\" -- | grep -- #{escape(entry_escaped)}"
  else
    "crontab -u #{escape(user)} -l | grep -v \"#\" | grep -- #{escape(entry_escaped)}"
  end
end

#check_directory(directory) ⇒ Object



74
75
76
# File 'lib/specinfra/command/base.rb', line 74

def check_directory(directory)
  "test -d #{escape(directory)}"
end

#check_enabled(service, level = 3) ⇒ Object



19
20
21
# File 'lib/specinfra/command/base.rb', line 19

def check_enabled(service, level=3)
  raise NotImplementedError.new
end

#check_file(file) ⇒ Object



58
59
60
# File 'lib/specinfra/command/base.rb', line 58

def check_file(file)
  "test -f #{escape(file)}"
end

#check_file_checksum(file, expected) ⇒ Object



152
153
154
155
# File 'lib/specinfra/command/base.rb', line 152

def check_file_checksum(file, expected)
  regexp = "^#{expected}"
  "cksum #{escape(file)} | grep -iw -- #{escape(regexp)}"
end

#check_file_contain(file, expected_pattern) ⇒ Object



140
141
142
# File 'lib/specinfra/command/base.rb', line 140

def check_file_contain(file, expected_pattern)
  "#{check_file_contain_with_regexp(file, expected_pattern)} || #{check_file_contain_with_fixed_strings(file, expected_pattern)}"
end

#check_file_contain_lines(file, expected_lines, from = nil, to = nil) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/specinfra/command/base.rb', line 176

def check_file_contain_lines(file, expected_lines, from=nil, to=nil)
  require 'digest/md5'
  from ||= '1'
  to ||= '$'
  sed = "sed -n #{escape(from)},#{escape(to)}p #{escape(file)}"
  head_line = expected_lines.first.chomp
  lines_checksum = Digest::MD5.hexdigest(expected_lines.map(&:chomp).join("\n") + "\n")
  afterwards_length = expected_lines.length - 1
  "#{sed} | grep -A #{escape(afterwards_length)} -F -- #{escape(head_line)} | md5sum | grep -qiw -- #{escape(lines_checksum)}"
end

#check_file_contain_with_fixed_strings(file, expected_pattern) ⇒ Object



148
149
150
# File 'lib/specinfra/command/base.rb', line 148

def check_file_contain_with_fixed_strings(file, expected_pattern)
  "grep -qF -- #{escape(expected_pattern)} #{escape(file)}"
end

#check_file_contain_with_regexp(file, expected_pattern) ⇒ Object



144
145
146
# File 'lib/specinfra/command/base.rb', line 144

def check_file_contain_with_regexp(file, expected_pattern)
  "grep -q -- #{escape(expected_pattern)} #{escape(file)}"
end

#check_file_contain_within(file, expected_pattern, from = nil, to = nil) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/specinfra/command/base.rb', line 167

def check_file_contain_within(file, expected_pattern, from=nil, to=nil)
  from ||= '1'
  to ||= '$'
  sed = "sed -n #{escape(from)},#{escape(to)}p #{escape(file)}"
  checker_with_regexp = check_file_contain_with_regexp("-", expected_pattern)
  checker_with_fixed  = check_file_contain_with_fixed_strings("-", expected_pattern)
  "#{sed} | #{checker_with_regexp} || #{sed} | #{checker_with_fixed}"
end

#check_file_md5checksum(file, expected) ⇒ Object



157
158
159
160
# File 'lib/specinfra/command/base.rb', line 157

def check_file_md5checksum(file, expected)
  regexp = "^#{expected}"
  "md5sum #{escape(file)} | grep -iw -- #{escape(regexp)}"
end

#check_file_sha256checksum(file, expected) ⇒ Object



162
163
164
165
# File 'lib/specinfra/command/base.rb', line 162

def check_file_sha256checksum(file, expected)
  regexp = "^#{expected}"
  "sha256sum #{escape(file)} | grep -iw -- #{escape(regexp)}"
end

#check_gid(group, gid) ⇒ Object



264
265
266
267
# File 'lib/specinfra/command/base.rb', line 264

def check_gid(group, gid)
  regexp = "^#{group}"
  "getent group | grep -w -- #{escape(regexp)} | cut -f 3 -d ':' | grep -w -- #{escape(gid)}"
end

#check_group(group) ⇒ Object



82
83
84
# File 'lib/specinfra/command/base.rb', line 82

def check_group(group)
  "getent group #{escape(group)}"
end

#check_grouped(file, group) ⇒ Object



197
198
199
200
# File 'lib/specinfra/command/base.rb', line 197

def check_grouped(file, group)
  regexp = "^#{group}$"
  "stat -c %G #{escape(file)} | grep -- #{escape(regexp)}"
end

#check_home_directory(user, path_to_home) ⇒ Object



278
279
280
# File 'lib/specinfra/command/base.rb', line 278

def check_home_directory(user, path_to_home)
  "getent passwd #{escape(user)} | cut -f 6 -d ':' | grep -w -- #{escape(path_to_home)}"
end

#check_immutable(file) ⇒ Object



348
349
350
# File 'lib/specinfra/command/base.rb', line 348

def check_immutable(file)
  raise NotImplementedError.new
end

#check_installed(package, version = nil) ⇒ Object



86
87
88
# File 'lib/specinfra/command/base.rb', line 86

def check_installed(package, version=nil)
  raise NotImplementedError.new
end

#check_installed_by_cpan(name, version = nil) ⇒ Object



249
250
251
252
253
254
# File 'lib/specinfra/command/base.rb', line 249

def check_installed_by_cpan(name, version=nil)
  regexp = "^#{name}"
  cmd = "cpan -l | grep -w -- #{escape(regexp)}"
  cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
  cmd
end

#check_installed_by_gem(name, version = nil) ⇒ Object



215
216
217
218
219
220
# File 'lib/specinfra/command/base.rb', line 215

def check_installed_by_gem(name, version=nil)
  regexp = "^#{name}"
  cmd = "gem list --local | grep -w -- #{escape(regexp)}"
  cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
  cmd
end

#check_installed_by_npm(name, version = nil) ⇒ Object



222
223
224
225
226
# File 'lib/specinfra/command/base.rb', line 222

def check_installed_by_npm(name, version=nil)
  cmd = "npm ls #{escape(name)} -g"
  cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
  cmd
end

#check_installed_by_pear(name, version = nil) ⇒ Object



235
236
237
238
239
240
# File 'lib/specinfra/command/base.rb', line 235

def check_installed_by_pear(name, version=nil)
  regexp = "^#{name}"
  cmd = "pear list | grep -w -- #{escape(regexp)}"
  cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
  cmd
end

#check_installed_by_pecl(name, version = nil) ⇒ Object



228
229
230
231
232
233
# File 'lib/specinfra/command/base.rb', line 228

def check_installed_by_pecl(name, version=nil)
  regexp = "^#{name}"
  cmd = "pecl list | grep -w -- #{escape(regexp)}"
  cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
  cmd
end

#check_installed_by_pip(name, version = nil) ⇒ Object



242
243
244
245
246
247
# File 'lib/specinfra/command/base.rb', line 242

def check_installed_by_pip(name, version=nil)
  regexp = "^#{name}"
  cmd = "pip list | grep -w -- #{escape(regexp)}"
  cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
  cmd
end

#check_ipfilter_rule(rule) ⇒ Object



299
300
301
# File 'lib/specinfra/command/base.rb', line 299

def check_ipfilter_rule(rule)
  raise NotImplementedError.new
end

#check_ipnat_rule(rule) ⇒ Object



303
304
305
# File 'lib/specinfra/command/base.rb', line 303

def check_ipnat_rule(rule)
  raise NotImplementedError.new
end

#check_iptables_rule(rule, table = nil, chain = nil) ⇒ Object



287
288
289
# File 'lib/specinfra/command/base.rb', line 287

def check_iptables_rule(rule, table=nil, chain=nil)
  raise NotImplementedError.new
end

#check_ipv4_address(interface, ip_address) ⇒ Object



327
328
329
# File 'lib/specinfra/command/base.rb', line 327

def check_ipv4_address(interface, ip_address)
  raise NotImplementedError.new
end

#check_kernel_module_loaded(name) ⇒ Object



323
324
325
# File 'lib/specinfra/command/base.rb', line 323

def check_kernel_module_loaded(name)
  raise NotImplementedError.new
end


211
212
213
# File 'lib/specinfra/command/base.rb', line 211

def check_link(link, target)
  "stat -c %N #{escape(link)} | egrep -e \"-> .#{escape(target)}.\""
end

#check_listening(port) ⇒ Object



98
99
100
101
# File 'lib/specinfra/command/base.rb', line 98

def check_listening(port)
  regexp = ":#{port} "
  "netstat -tunl | grep -- #{escape(regexp)}"
end

#check_listening_with_protocol(port, protocol) ⇒ Object



103
104
105
106
# File 'lib/specinfra/command/base.rb', line 103

def check_listening_with_protocol(port, protocol)
  regexp = "^#{protocol} .*:#{port} "
  "netstat -tunl | grep -- #{escape(regexp)}"
end

#check_login_shell(user, path_to_shell) ⇒ Object



274
275
276
# File 'lib/specinfra/command/base.rb', line 274

def (user, path_to_shell)
  "getent passwd #{escape(user)} | cut -f 7 -d ':' | grep -w -- #{escape(path_to_shell)}"
end

#check_mail_alias(recipient, target) ⇒ Object



331
332
333
334
# File 'lib/specinfra/command/base.rb', line 331

def check_mail_alias(recipient, target)
  target = "[[:space:]]#{target}"
  "getent aliases #{escape(recipient)} | grep -- #{escape(target)}$"
end

#check_mode(file, mode) ⇒ Object



187
188
189
190
# File 'lib/specinfra/command/base.rb', line 187

def check_mode(file, mode)
  regexp = "^#{mode}$"
  "stat -c %a #{escape(file)} | grep -- #{escape(regexp)}"
end

#check_monitored_by_god(service) ⇒ Object



124
125
126
# File 'lib/specinfra/command/base.rb', line 124

def check_monitored_by_god(service)
  "god status #{escape(service)}"
end

#check_monitored_by_monit(service) ⇒ Object



120
121
122
# File 'lib/specinfra/command/base.rb', line 120

def check_monitored_by_monit(service)
  "monit status"
end

#check_mounted(path) ⇒ Object



31
32
33
34
# File 'lib/specinfra/command/base.rb', line 31

def check_mounted(path)
  regexp = "on #{path}"
  "mount | grep -w -- #{escape(regexp)}"
end

#check_owner(file, owner) ⇒ Object



192
193
194
195
# File 'lib/specinfra/command/base.rb', line 192

def check_owner(file, owner)
  regexp = "^#{owner}$"
  "stat -c %U #{escape(file)} | grep -- #{escape(regexp)}"
end

#check_primary_group(group) ⇒ Object



260
261
262
# File 'lib/specinfra/command/base.rb', line 260

def check_primary_group(group)
  raise NotImplementedError.new
end

#check_process(process) ⇒ Object



128
129
130
# File 'lib/specinfra/command/base.rb', line 128

def check_process(process)
  "ps aux | grep -w -- #{escape(process)} | grep -qv grep"
end

#check_process_count(process, count) ⇒ Object



132
133
134
# File 'lib/specinfra/command/base.rb', line 132

def check_process_count(process, count)
  "test $(ps aux | grep -w -- #{escape(process)} | grep -v grep | wc -l) -eq #{escape(count)}"
end

#check_reachable(host, port, proto, timeout) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/specinfra/command/base.rb', line 40

def check_reachable(host, port, proto, timeout)
  if port.nil?
    "ping -w #{escape(timeout)} -c 2 -n #{escape(host)}"
  else
    "nc -vvvvz#{escape(proto[0].chr)} #{escape(host)} #{escape(port)} -w #{escape(timeout)}"
  end
end

#check_resolvable(name, type) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/specinfra/command/base.rb', line 48

def check_resolvable(name, type)
  if type == "dns"
    "nslookup -timeout=1 #{escape(name)}"
  elsif type == "hosts"
    "grep -w -- #{escape(name)} /etc/hosts"
  else
    "getent hosts #{escape(name)}"
  end
end

#check_routing_table(destination) ⇒ Object



36
37
38
# File 'lib/specinfra/command/base.rb', line 36

def check_routing_table(destination)
  "ip route | grep -E '^#{destination} |^default '"
end

#check_running(service) ⇒ Object



108
109
110
# File 'lib/specinfra/command/base.rb', line 108

def check_running(service)
  "service #{escape(service)} status"
end

#check_running_under_supervisor(service) ⇒ Object



112
113
114
# File 'lib/specinfra/command/base.rb', line 112

def check_running_under_supervisor(service)
  "supervisorctl status #{escape(service)} | grep RUNNING"
end

#check_running_under_upstart(service) ⇒ Object



116
117
118
# File 'lib/specinfra/command/base.rb', line 116

def check_running_under_upstart(service)
  "initctl status #{escape(service)} | grep running"
end

#check_selinux(mode) ⇒ Object



315
316
317
# File 'lib/specinfra/command/base.rb', line 315

def check_selinux(mode)
  raise NotImplementedError.new
end

#check_service_installed(service) ⇒ Object



90
91
92
# File 'lib/specinfra/command/base.rb', line 90

def check_service_installed(service)
  raise NotImplementedError.new
end

#check_service_start_mode(service, mode) ⇒ Object



94
95
96
# File 'lib/specinfra/command/base.rb', line 94

def check_service_start_mode(service, mode)
  raise NotImplementedError.new
end

#check_socket(file) ⇒ Object



70
71
72
# File 'lib/specinfra/command/base.rb', line 70

def check_socket(file)
  "test -S #{escape(file)}"
end

#check_svcprop(svc, property, value) ⇒ Object



307
308
309
# File 'lib/specinfra/command/base.rb', line 307

def check_svcprop(svc, property, value)
  raise NotImplementedError.new
end

#check_svcprops(svc, property) ⇒ Object



311
312
313
# File 'lib/specinfra/command/base.rb', line 311

def check_svcprops(svc, property)
  raise NotImplementedError.new
end

#check_uid(user, uid) ⇒ Object



269
270
271
272
# File 'lib/specinfra/command/base.rb', line 269

def check_uid(user, uid)
  regexp = "^uid=#{uid}("
  "id #{escape(user)} | grep -- #{escape(regexp)}"
end

#check_user(user) ⇒ Object



78
79
80
# File 'lib/specinfra/command/base.rb', line 78

def check_user(user)
  "id #{escape(user)}"
end

#check_yumrepo(repository) ⇒ Object



23
24
25
# File 'lib/specinfra/command/base.rb', line 23

def check_yumrepo(repository)
  raise NotImplementedError.new
end

#check_yumrepo_enabled(repository) ⇒ Object



27
28
29
# File 'lib/specinfra/command/base.rb', line 27

def check_yumrepo_enabled(repository)
  raise NotImplementedError.new
end

#check_zfs(zfs, property = nil, value = nil) ⇒ Object



291
292
293
# File 'lib/specinfra/command/base.rb', line 291

def check_zfs(zfs, property=nil, value=nil)
  raise NotImplementedError.new
end

#escape(target) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/specinfra/command/base.rb', line 8

def escape(target)
  str = case target
        when Regexp
          target.source
        else
          target.to_s
        end

  Shellwords.shellescape(str)
end

#get_file_content(file) ⇒ Object



336
337
338
# File 'lib/specinfra/command/base.rb', line 336

def get_file_content(file)
  "cat #{file} 2> /dev/null || echo -n"
end

#get_file_mtime(file) ⇒ Object



62
63
64
# File 'lib/specinfra/command/base.rb', line 62

def get_file_mtime(file)
  "stat -c %Y #{escape(file)}"
end

#get_file_size(file) ⇒ Object



66
67
68
# File 'lib/specinfra/command/base.rb', line 66

def get_file_size(file)
  "stat -c %s #{escape(file)}"
end

#get_ipaddress_of_host(name) ⇒ Object



356
357
358
# File 'lib/specinfra/command/base.rb', line 356

def get_ipaddress_of_host(name)
  "getent hosts #{escape(name)} | awk '{print $1}'"
end

#get_mode(file) ⇒ Object



295
296
297
# File 'lib/specinfra/command/base.rb', line 295

def get_mode(file)
  "stat -c %a #{escape(file)}"
end

#get_package_version(package, opts = nil) ⇒ Object



352
353
354
# File 'lib/specinfra/command/base.rb', line 352

def get_package_version(package, opts=nil)
  raise NotImplementedError.new
end

#get_process(process, opts) ⇒ Object



136
137
138
# File 'lib/specinfra/command/base.rb', line 136

def get_process(process, opts)
  "ps -C #{escape(process)} -o #{opts[:format]} | head -1"
end