Class: SpecInfra::Command::Base
- Inherits:
-
Object
- Object
- SpecInfra::Command::Base
show all
- Defined in:
- lib/specinfra/command/base.rb
Defined Under Namespace
Classes: NotImplementedError
Instance Method Summary
collapse
-
#check_access_by_user(file, user, access) ⇒ Object
-
#check_authorized_key(user, key) ⇒ Object
-
#check_belonging_group(user, group) ⇒ Object
-
#check_container(container) ⇒ Object
-
#check_cotainer_running(container) ⇒ Object
-
#check_cron_entry(user, entry) ⇒ Object
-
#check_directory(directory) ⇒ Object
-
#check_enabled(service, level = 3) ⇒ Object
-
#check_file(file) ⇒ Object
-
#check_file_checksum(file, expected) ⇒ Object
-
#check_file_contain(file, expected_pattern) ⇒ Object
-
#check_file_contain_lines(file, expected_lines, from = nil, to = nil) ⇒ Object
-
#check_file_contain_with_fixed_strings(file, expected_pattern) ⇒ Object
-
#check_file_contain_with_regexp(file, expected_pattern) ⇒ Object
-
#check_file_contain_within(file, expected_pattern, from = nil, to = nil) ⇒ Object
-
#check_file_md5checksum(file, expected) ⇒ Object
-
#check_file_sha256checksum(file, expected) ⇒ Object
-
#check_gid(group, gid) ⇒ Object
-
#check_group(group) ⇒ Object
-
#check_grouped(file, group) ⇒ Object
-
#check_home_directory(user, path_to_home) ⇒ Object
-
#check_installed(package, version = nil) ⇒ Object
-
#check_installed_by_cpan(name, version = nil) ⇒ Object
-
#check_installed_by_gem(name, version = nil) ⇒ Object
-
#check_installed_by_npm(name, version = nil) ⇒ Object
-
#check_installed_by_pear(name, version = nil) ⇒ Object
-
#check_installed_by_pecl(name, version = nil) ⇒ Object
-
#check_installed_by_pip(name, version = nil) ⇒ Object
-
#check_ipfilter_rule(rule) ⇒ Object
-
#check_ipnat_rule(rule) ⇒ Object
-
#check_iptables_rule(rule, table = nil, chain = nil) ⇒ Object
-
#check_ipv4_address(interface, ip_address) ⇒ Object
-
#check_kernel_module_loaded(name) ⇒ Object
-
#check_link(link, target) ⇒ Object
-
#check_listening(port) ⇒ Object
-
#check_listening_with_protocol(port, protocol) ⇒ Object
-
#check_login_shell(user, path_to_shell) ⇒ Object
-
#check_mail_alias(recipient, target) ⇒ Object
-
#check_mode(file, mode) ⇒ Object
-
#check_monitored_by_god(service) ⇒ Object
-
#check_monitored_by_monit(service) ⇒ Object
-
#check_mounted(path) ⇒ Object
-
#check_owner(file, owner) ⇒ Object
-
#check_process(process) ⇒ Object
-
#check_process_count(process, count) ⇒ Object
-
#check_reachable(host, port, proto, timeout) ⇒ Object
-
#check_resolvable(name, type) ⇒ Object
-
#check_routing_table(destination) ⇒ Object
-
#check_running(service) ⇒ Object
-
#check_running_under_supervisor(service) ⇒ Object
-
#check_running_under_upstart(service) ⇒ Object
-
#check_selinux(mode) ⇒ Object
-
#check_service_installed(service) ⇒ Object
-
#check_service_start_mode(service, mode) ⇒ Object
-
#check_socket(file) ⇒ Object
-
#check_svcprop(svc, property, value) ⇒ Object
-
#check_svcprops(svc, property) ⇒ Object
-
#check_uid(user, uid) ⇒ Object
-
#check_user(user) ⇒ Object
-
#check_yumrepo(repository) ⇒ Object
-
#check_yumrepo_enabled(repository) ⇒ Object
-
#check_zfs(zfs, property = nil, value = nil) ⇒ Object
-
#escape(target) ⇒ Object
-
#get_file_content(file) ⇒ Object
-
#get_ipaddress_of_host(name) ⇒ Object
-
#get_mode(file) ⇒ Object
-
#get_package_version(package, opts = nil) ⇒ Object
-
#get_process(process, opts) ⇒ Object
Instance Method Details
#check_access_by_user(file, user, access) ⇒ Object
307
308
309
|
# File 'lib/specinfra/command/base.rb', line 307
def check_access_by_user(file, user, access)
raise NotImplementedError.new
end
|
#check_authorized_key(user, key) ⇒ Object
270
271
272
273
|
# File 'lib/specinfra/command/base.rb', line 270
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
248
249
250
|
# File 'lib/specinfra/command/base.rb', line 248
def check_belonging_group(user, group)
"id #{escape(user)} | awk '{print $3}' | grep -- #{escape(group)}"
end
|
#check_container(container) ⇒ Object
328
329
330
|
# File 'lib/specinfra/command/base.rb', line 328
def check_container(container)
raise NotImplementedError.new
end
|
#check_cotainer_running(container) ⇒ Object
332
333
334
|
# File 'lib/specinfra/command/base.rb', line 332
def check_cotainer_running(container)
raise NotImplementedError.new
end
|
#check_cron_entry(user, entry) ⇒ Object
194
195
196
197
198
199
200
201
|
# File 'lib/specinfra/command/base.rb', line 194
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
144
145
146
147
|
# File 'lib/specinfra/command/base.rb', line 144
def check_file_checksum(file, expected)
regexp = "^#{expected}"
"cksum #{escape(file)} | grep -iw -- #{escape(regexp)}"
end
|
#check_file_contain(file, expected_pattern) ⇒ Object
132
133
134
|
# File 'lib/specinfra/command/base.rb', line 132
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
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/specinfra/command/base.rb', line 168
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
140
141
142
|
# File 'lib/specinfra/command/base.rb', line 140
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
136
137
138
|
# File 'lib/specinfra/command/base.rb', line 136
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
159
160
161
162
163
164
165
166
|
# File 'lib/specinfra/command/base.rb', line 159
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
149
150
151
152
|
# File 'lib/specinfra/command/base.rb', line 149
def check_file_md5checksum(file, expected)
regexp = "^#{expected}"
"md5sum #{escape(file)} | grep -iw -- #{escape(regexp)}"
end
|
#check_file_sha256checksum(file, expected) ⇒ Object
154
155
156
157
|
# File 'lib/specinfra/command/base.rb', line 154
def check_file_sha256checksum(file, expected)
regexp = "^#{expected}"
"sha256sum #{escape(file)} | grep -iw -- #{escape(regexp)}"
end
|
#check_gid(group, gid) ⇒ Object
252
253
254
255
|
# File 'lib/specinfra/command/base.rb', line 252
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
189
190
191
192
|
# File 'lib/specinfra/command/base.rb', line 189
def check_grouped(file, group)
regexp = "^#{group}$"
"stat -c %G #{escape(file)} | grep -- #{escape(regexp)}"
end
|
#check_home_directory(user, path_to_home) ⇒ Object
266
267
268
|
# File 'lib/specinfra/command/base.rb', line 266
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
241
242
243
244
245
246
|
# File 'lib/specinfra/command/base.rb', line 241
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
207
208
209
210
211
212
|
# File 'lib/specinfra/command/base.rb', line 207
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
214
215
216
217
218
|
# File 'lib/specinfra/command/base.rb', line 214
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
227
228
229
230
231
232
|
# File 'lib/specinfra/command/base.rb', line 227
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
220
221
222
223
224
225
|
# File 'lib/specinfra/command/base.rb', line 220
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
234
235
236
237
238
239
|
# File 'lib/specinfra/command/base.rb', line 234
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
287
288
289
|
# File 'lib/specinfra/command/base.rb', line 287
def check_ipfilter_rule(rule)
raise NotImplementedError.new
end
|
#check_ipnat_rule(rule) ⇒ Object
291
292
293
|
# File 'lib/specinfra/command/base.rb', line 291
def check_ipnat_rule(rule)
raise NotImplementedError.new
end
|
#check_iptables_rule(rule, table = nil, chain = nil) ⇒ Object
275
276
277
|
# File 'lib/specinfra/command/base.rb', line 275
def check_iptables_rule(rule, table=nil, chain=nil)
raise NotImplementedError.new
end
|
#check_ipv4_address(interface, ip_address) ⇒ Object
315
316
317
|
# File 'lib/specinfra/command/base.rb', line 315
def check_ipv4_address(interface, ip_address)
raise NotImplementedError.new
end
|
#check_kernel_module_loaded(name) ⇒ Object
311
312
313
|
# File 'lib/specinfra/command/base.rb', line 311
def check_kernel_module_loaded(name)
raise NotImplementedError.new
end
|
#check_link(link, target) ⇒ Object
203
204
205
|
# File 'lib/specinfra/command/base.rb', line 203
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
262
263
264
|
# File 'lib/specinfra/command/base.rb', line 262
def check_login_shell(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
319
320
321
322
|
# File 'lib/specinfra/command/base.rb', line 319
def check_mail_alias(recipient, target)
target = "[[:space:]]#{target}"
"getent aliases #{escape(recipient)} | grep -- #{escape(target)}$"
end
|
#check_mode(file, mode) ⇒ Object
179
180
181
182
|
# File 'lib/specinfra/command/base.rb', line 179
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
184
185
186
187
|
# File 'lib/specinfra/command/base.rb', line 184
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_process_count(process, count) ⇒ Object
124
125
126
|
# File 'lib/specinfra/command/base.rb', line 124
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
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
303
304
305
|
# File 'lib/specinfra/command/base.rb', line 303
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
#check_svcprops(svc, property) ⇒ Object
#check_uid(user, uid) ⇒ Object
257
258
259
260
|
# File 'lib/specinfra/command/base.rb', line 257
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
279
280
281
|
# File 'lib/specinfra/command/base.rb', line 279
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
324
325
326
|
# File 'lib/specinfra/command/base.rb', line 324
def get_file_content(file)
"cat #{file} 2> /dev/null || echo -n"
end
|
#get_ipaddress_of_host(name) ⇒ Object
340
341
342
|
# File 'lib/specinfra/command/base.rb', line 340
def get_ipaddress_of_host(name)
"getent hosts #{escape(name)} | awk '{print $1}'"
end
|
#get_mode(file) ⇒ Object
283
284
285
|
# File 'lib/specinfra/command/base.rb', line 283
def get_mode(file)
"stat -c %a #{escape(file)}"
end
|
#get_package_version(package, opts = nil) ⇒ Object
336
337
338
|
# File 'lib/specinfra/command/base.rb', line 336
def get_package_version(package, opts=nil)
raise NotImplementedError.new
end
|
#get_process(process, opts) ⇒ Object
128
129
130
|
# File 'lib/specinfra/command/base.rb', line 128
def get_process(process, opts)
"ps -C #{escape(process)} -o #{opts[:format]} | head -1"
end
|