Class: SpecInfra::Command::Base

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

Direct Known Subclasses

AIX, Darwin, FreeBSD, Linux, Solaris

Defined Under Namespace

Classes: NotImplementedError

Instance Method Summary collapse

Instance Method Details

#check_access_by_user(file, user, access) ⇒ Object



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

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

#check_authorized_key(user, key) ⇒ Object



266
267
268
269
# File 'lib/specinfra/command/base.rb', line 266

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



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

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

#check_container(container) ⇒ Object



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

def check_container(container)
  raise NotImplementedError.new
end

#check_cotainer_running(container) ⇒ Object



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

def check_cotainer_running(container)
  raise NotImplementedError.new
end

#check_cron_entry(user, entry) ⇒ Object



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

def check_cron_entry(user, entry)
  entry_escaped = entry.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



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

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



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

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

#check_file_contain(file, expected_pattern) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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

#check_file_sha256checksum(file, expected) ⇒ Object



150
151
152
153
# File 'lib/specinfra/command/base.rb', line 150

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

#check_gid(group, gid) ⇒ Object



248
249
250
251
# File 'lib/specinfra/command/base.rb', line 248

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



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

def check_group(group)
  "getent group | grep -wq -- #{escape(group)}"
end

#check_grouped(file, group) ⇒ Object



185
186
187
188
# File 'lib/specinfra/command/base.rb', line 185

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

#check_home_directory(user, path_to_home) ⇒ Object



262
263
264
# File 'lib/specinfra/command/base.rb', line 262

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

#check_installed(package, version = nil) ⇒ Object



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

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

#check_installed_by_cpan(name, version = nil) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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



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

def check_ipfilter_rule(rule)
  raise NotImplementedError.new
end

#check_ipnat_rule(rule) ⇒ Object



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

def check_ipnat_rule(rule)
  raise NotImplementedError.new
end

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



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

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

#check_ipv4_address(interface, ip_address) ⇒ Object



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

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

#check_kernel_module_loaded(name) ⇒ Object



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

def check_kernel_module_loaded(name)
  raise NotImplementedError.new
end


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

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

#check_listening(port) ⇒ Object



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

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

#check_listening_with_protocol(port, protocol) ⇒ Object



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

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

#check_login_shell(user, path_to_shell) ⇒ Object



258
259
260
# File 'lib/specinfra/command/base.rb', line 258

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



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

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

#check_mode(file, mode) ⇒ Object



175
176
177
178
# File 'lib/specinfra/command/base.rb', line 175

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

#check_monitored_by_god(service) ⇒ Object



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

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

#check_monitored_by_monit(service) ⇒ Object



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

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



180
181
182
183
# File 'lib/specinfra/command/base.rb', line 180

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

#check_process(process) ⇒ Object



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

def check_process(process)
  "ps aux | grep -w -- #{escape(process)} | grep -qv grep"
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



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

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

#check_running_under_supervisor(service) ⇒ Object



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

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

#check_running_under_upstart(service) ⇒ Object



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

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

#check_selinux(mode) ⇒ Object



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

def check_selinux(mode)
  raise NotImplementedError.new
end

#check_service_installed(service) ⇒ Object



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

def check_service_installed(service)
  raise NotImplementedError.new
end

#check_service_start_mode(service, mode) ⇒ Object



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

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

#check_socket(file) ⇒ Object



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

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

#check_svcprop(svc, property, value) ⇒ Object



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

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

#check_svcprops(svc, property) ⇒ Object



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

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

#check_uid(user, uid) ⇒ Object



253
254
255
256
# File 'lib/specinfra/command/base.rb', line 253

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

#check_user(user) ⇒ Object



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

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



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

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



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

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

#get_ipaddress_of_host(name) ⇒ Object



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

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

#get_mode(file) ⇒ Object



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

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

#get_package_version(package, opts = nil) ⇒ Object



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

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

#get_process(process, opts) ⇒ Object



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

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