Class: Serverspec::Commands::Base
- Inherits:
-
Object
- Object
- Serverspec::Commands::Base
show all
- Defined in:
- lib/serverspec/commands/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_cron_entry(user, entry) ⇒ Object
-
#check_directory(directory) ⇒ Object
-
#check_enabled(service, level = 3) ⇒ Object
-
#check_file(file) ⇒ Object
-
#check_file_contain(file, expected_pattern) ⇒ Object
-
#check_file_contain_within(file, expected_pattern, from = nil, to = nil) ⇒ Object
-
#check_file_md5checksum(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_gem(name, version = nil) ⇒ Object
-
#check_installed_by_npm(name, version = nil) ⇒ Object
-
#check_installed_by_pecl(name, version = nil) ⇒ Object
-
#check_ipfilter_rule(rule) ⇒ Object
-
#check_ipnat_rule(rule) ⇒ Object
-
#check_iptables_rule(rule, table = nil, chain = nil) ⇒ Object
-
#check_kernel_module_loaded(name) ⇒ Object
-
#check_link(link, target) ⇒ Object
-
#check_listening(port) ⇒ Object
-
#check_login_shell(user, path_to_shell) ⇒ Object
-
#check_mode(file, mode) ⇒ Object
-
#check_monitored_by_monit(service) ⇒ Object
-
#check_mounted(path) ⇒ Object
-
#check_owner(file, owner) ⇒ Object
-
#check_process(process) ⇒ 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_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_mode(file) ⇒ Object
Instance Method Details
#check_access_by_user(file, user, access) ⇒ Object
224
225
226
|
# File 'lib/serverspec/commands/base.rb', line 224
def check_access_by_user(file, user, access)
raise NotImplementedError.new
end
|
#check_authorized_key(user, key) ⇒ Object
187
188
189
190
|
# File 'lib/serverspec/commands/base.rb', line 187
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
165
166
167
|
# File 'lib/serverspec/commands/base.rb', line 165
def check_belonging_group(user, group)
"id #{escape(user)} | awk '{print $3}' | grep -- #{escape(group)}"
end
|
#check_cron_entry(user, entry) ⇒ Object
130
131
132
133
134
135
136
137
|
# File 'lib/serverspec/commands/base.rb', line 130
def check_cron_entry(user, entry)
entry_escaped = entry.gsub(/\*/, '\\*')
if user.nil?
"crontab -l | grep -- #{escape(entry_escaped)}"
else
"crontab -u #{escape(user)} -l | grep -- #{escape(entry_escaped)}"
end
end
|
#check_directory(directory) ⇒ Object
59
60
61
|
# File 'lib/serverspec/commands/base.rb', line 59
def check_directory(directory)
"test -d #{escape(directory)}"
end
|
#check_enabled(service, level = 3) ⇒ Object
12
13
14
|
# File 'lib/serverspec/commands/base.rb', line 12
def check_enabled(service, level=3)
raise NotImplementedError.new
end
|
#check_file(file) ⇒ Object
51
52
53
|
# File 'lib/serverspec/commands/base.rb', line 51
def check_file(file)
"test -f #{escape(file)}"
end
|
#check_file_contain(file, expected_pattern) ⇒ Object
100
101
102
|
# File 'lib/serverspec/commands/base.rb', line 100
def check_file_contain(file, expected_pattern)
"grep -q -- #{escape(expected_pattern)} #{escape(file)}"
end
|
#check_file_contain_within(file, expected_pattern, from = nil, to = nil) ⇒ Object
108
109
110
111
112
113
|
# File 'lib/serverspec/commands/base.rb', line 108
def check_file_contain_within(file, expected_pattern, from=nil, to=nil)
from ||= '1'
to ||= '$'
checker = check_file_contain("-", expected_pattern)
"sed -n #{escape(from)},#{escape(to)}p #{escape(file)} | #{checker}"
end
|
#check_file_md5checksum(file, expected) ⇒ Object
104
105
106
|
# File 'lib/serverspec/commands/base.rb', line 104
def check_file_md5checksum(file, expected)
"md5sum #{escape(file)} | grep -iw -- ^#{escape(expected)}"
end
|
#check_gid(group, gid) ⇒ Object
169
170
171
172
|
# File 'lib/serverspec/commands/base.rb', line 169
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
67
68
69
|
# File 'lib/serverspec/commands/base.rb', line 67
def check_group(group)
"getent group | grep -wq -- #{escape(group)}"
end
|
#check_grouped(file, group) ⇒ Object
125
126
127
128
|
# File 'lib/serverspec/commands/base.rb', line 125
def check_grouped(file, group)
regexp = "^#{group}$"
"stat -c %G #{escape(file)} | grep -- #{escape(regexp)}"
end
|
#check_home_directory(user, path_to_home) ⇒ Object
183
184
185
|
# File 'lib/serverspec/commands/base.rb', line 183
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
71
72
73
|
# File 'lib/serverspec/commands/base.rb', line 71
def check_installed(package, version=nil)
raise NotImplementedError.new
end
|
#check_installed_by_gem(name, version = nil) ⇒ Object
143
144
145
146
147
148
149
|
# File 'lib/serverspec/commands/base.rb', line 143
def check_installed_by_gem(name, version=nil)
cmd = "gem list --local | grep -w -- ^#{escape(name)}"
if ! version.nil?
cmd = "#{cmd} | grep -w -- #{escape(version)}"
end
cmd
end
|
#check_installed_by_npm(name, version = nil) ⇒ Object
151
152
153
154
155
|
# File 'lib/serverspec/commands/base.rb', line 151
def check_installed_by_npm(name, version=nil)
cmd = "npm ls #{escape(name)} -g"
cmd = "#{cmd} | grep -w -- #{escape(version)}" unless version.nil?
cmd
end
|
#check_installed_by_pecl(name, version = nil) ⇒ Object
157
158
159
160
161
162
163
|
# File 'lib/serverspec/commands/base.rb', line 157
def check_installed_by_pecl(name, version=nil)
cmd = "pecl list | grep -w -- ^#{escape(name)}"
if ! version.nil?
cmd = "#{cmd} | grep -w -- #{escape(version)}"
end
cmd
end
|
#check_ipfilter_rule(rule) ⇒ Object
204
205
206
|
# File 'lib/serverspec/commands/base.rb', line 204
def check_ipfilter_rule(rule)
raise NotImplementedError.new
end
|
#check_ipnat_rule(rule) ⇒ Object
208
209
210
|
# File 'lib/serverspec/commands/base.rb', line 208
def check_ipnat_rule(rule)
raise NotImplementedError.new
end
|
#check_iptables_rule(rule, table = nil, chain = nil) ⇒ Object
192
193
194
|
# File 'lib/serverspec/commands/base.rb', line 192
def check_iptables_rule(rule, table=nil, chain=nil)
raise NotImplementedError.new
end
|
#check_kernel_module_loaded(name) ⇒ Object
228
229
230
|
# File 'lib/serverspec/commands/base.rb', line 228
def check_kernel_module_loaded(name)
raise NotImplementedError.new
end
|
#check_link(link, target) ⇒ Object
139
140
141
|
# File 'lib/serverspec/commands/base.rb', line 139
def check_link(link, target)
"stat -c %N #{escape(link)} | grep -- #{escape(target)}"
end
|
#check_listening(port) ⇒ Object
75
76
77
78
|
# File 'lib/serverspec/commands/base.rb', line 75
def check_listening(port)
regexp = ":#{port} "
"netstat -tunl | grep -- #{escape(regexp)}"
end
|
#check_login_shell(user, path_to_shell) ⇒ Object
179
180
181
|
# File 'lib/serverspec/commands/base.rb', line 179
def check_login_shell(user, path_to_shell)
"getent passwd #{escape(user)} | cut -f 7 -d ':' | grep -w -- #{escape(path_to_shell)}"
end
|
#check_mode(file, mode) ⇒ Object
115
116
117
118
|
# File 'lib/serverspec/commands/base.rb', line 115
def check_mode(file, mode)
regexp = "^#{mode}$"
"stat -c %a #{escape(file)} | grep -- #{escape(regexp)}"
end
|
#check_monitored_by_monit(service) ⇒ Object
92
93
94
|
# File 'lib/serverspec/commands/base.rb', line 92
def check_monitored_by_monit(service)
"monit status"
end
|
#check_mounted(path) ⇒ Object
24
25
26
27
|
# File 'lib/serverspec/commands/base.rb', line 24
def check_mounted(path)
regexp = "on #{path}"
"mount | grep -w -- #{escape(regexp)}"
end
|
#check_owner(file, owner) ⇒ Object
120
121
122
123
|
# File 'lib/serverspec/commands/base.rb', line 120
def check_owner(file, owner)
regexp = "^#{owner}$"
"stat -c %U #{escape(file)} | grep -- #{escape(regexp)}"
end
|
#check_process(process) ⇒ Object
96
97
98
|
# File 'lib/serverspec/commands/base.rb', line 96
def check_process(process)
"ps aux | grep -w -- #{escape(process)} | grep -qv grep"
end
|
#check_reachable(host, port, proto, timeout) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/serverspec/commands/base.rb', line 33
def check_reachable(host, port, proto, timeout)
if port.nil?
"ping -n #{escape(host)} -w #{escape(timeout)} -c 2"
else
"nc -vvvvz#{escape(proto[0].chr)} #{escape(host)} #{escape(port)} -w #{escape(timeout)}"
end
end
|
#check_resolvable(name, type) ⇒ Object
41
42
43
44
45
46
47
48
49
|
# File 'lib/serverspec/commands/base.rb', line 41
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
29
30
31
|
# File 'lib/serverspec/commands/base.rb', line 29
def check_routing_table(destination)
"ip route | grep -E '^#{destination} |^default '"
end
|
#check_running(service) ⇒ Object
80
81
82
|
# File 'lib/serverspec/commands/base.rb', line 80
def check_running(service)
"service #{escape(service)} status"
end
|
#check_running_under_supervisor(service) ⇒ Object
84
85
86
|
# File 'lib/serverspec/commands/base.rb', line 84
def check_running_under_supervisor(service)
"supervisorctl status #{escape(service)}"
end
|
#check_running_under_upstart(service) ⇒ Object
88
89
90
|
# File 'lib/serverspec/commands/base.rb', line 88
def check_running_under_upstart(service)
"initctl status #{escape(service)}"
end
|
#check_selinux(mode) ⇒ Object
220
221
222
|
# File 'lib/serverspec/commands/base.rb', line 220
def check_selinux(mode)
raise NotImplementedError.new
end
|
#check_socket(file) ⇒ Object
55
56
57
|
# File 'lib/serverspec/commands/base.rb', line 55
def check_socket(file)
"test -S #{escape(file)}"
end
|
#check_svcprop(svc, property, value) ⇒ Object
212
213
214
|
# File 'lib/serverspec/commands/base.rb', line 212
def check_svcprop(svc, property, value)
raise NotImplementedError.new
end
|
#check_svcprops(svc, property) ⇒ Object
216
217
218
|
# File 'lib/serverspec/commands/base.rb', line 216
def check_svcprops(svc, property)
raise NotImplementedError.new
end
|
#check_uid(user, uid) ⇒ Object
174
175
176
177
|
# File 'lib/serverspec/commands/base.rb', line 174
def check_uid(user, uid)
regexp = "^uid=#{uid}("
"id #{escape(user)} | grep -- #{escape(regexp)}"
end
|
#check_user(user) ⇒ Object
63
64
65
|
# File 'lib/serverspec/commands/base.rb', line 63
def check_user(user)
"id #{escape(user)}"
end
|
#check_yumrepo(repository) ⇒ Object
16
17
18
|
# File 'lib/serverspec/commands/base.rb', line 16
def check_yumrepo(repository)
raise NotImplementedError.new
end
|
#check_yumrepo_enabled(repository) ⇒ Object
20
21
22
|
# File 'lib/serverspec/commands/base.rb', line 20
def check_yumrepo_enabled(repository)
raise NotImplementedError.new
end
|
#check_zfs(zfs, property = nil, value = nil) ⇒ Object
196
197
198
|
# File 'lib/serverspec/commands/base.rb', line 196
def check_zfs(zfs, property=nil, value=nil)
raise NotImplementedError.new
end
|
#escape(target) ⇒ Object
8
9
10
|
# File 'lib/serverspec/commands/base.rb', line 8
def escape(target)
Shellwords.shellescape(target.to_s())
end
|
#get_mode(file) ⇒ Object
200
201
202
|
# File 'lib/serverspec/commands/base.rb', line 200
def get_mode(file)
"stat -c %a #{escape(file)}"
end
|