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) ⇒ 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_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, level = 3) ⇒ Object
-
#check_running_under_supervisor(service, level = 3) ⇒ 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
216
217
218
|
# File 'lib/serverspec/commands/base.rb', line 216
def check_access_by_user(file, user, access)
raise NotImplementedError.new
end
|
#check_authorized_key(user, key) ⇒ Object
179
180
181
182
|
# File 'lib/serverspec/commands/base.rb', line 179
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
157
158
159
|
# File 'lib/serverspec/commands/base.rb', line 157
def check_belonging_group(user, group)
"id #{escape(user)} | awk '{print $3}' | grep -- #{escape(group)}"
end
|
#check_cron_entry(user, entry) ⇒ Object
122
123
124
125
126
127
128
129
|
# File 'lib/serverspec/commands/base.rb', line 122
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
92
93
94
|
# File 'lib/serverspec/commands/base.rb', line 92
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
100
101
102
103
104
105
|
# File 'lib/serverspec/commands/base.rb', line 100
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
96
97
98
|
# File 'lib/serverspec/commands/base.rb', line 96
def check_file_md5checksum(file, expected)
"md5sum #{escape(file)} | grep -iw -- ^#{escape(expected)}"
end
|
#check_gid(group, gid) ⇒ Object
161
162
163
164
|
# File 'lib/serverspec/commands/base.rb', line 161
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
117
118
119
120
|
# File 'lib/serverspec/commands/base.rb', line 117
def check_grouped(file, group)
regexp = "^#{group}$"
"stat -c %G #{escape(file)} | grep -- #{escape(regexp)}"
end
|
#check_home_directory(user, path_to_home) ⇒ Object
175
176
177
|
# File 'lib/serverspec/commands/base.rb', line 175
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) ⇒ Object
71
72
73
|
# File 'lib/serverspec/commands/base.rb', line 71
def check_installed(package)
raise NotImplementedError.new
end
|
#check_installed_by_gem(name, version = nil) ⇒ Object
135
136
137
138
139
140
141
|
# File 'lib/serverspec/commands/base.rb', line 135
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
143
144
145
146
147
|
# File 'lib/serverspec/commands/base.rb', line 143
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
149
150
151
152
153
154
155
|
# File 'lib/serverspec/commands/base.rb', line 149
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
196
197
198
|
# File 'lib/serverspec/commands/base.rb', line 196
def check_ipfilter_rule(rule)
raise NotImplementedError.new
end
|
#check_ipnat_rule(rule) ⇒ Object
200
201
202
|
# File 'lib/serverspec/commands/base.rb', line 200
def check_ipnat_rule(rule)
raise NotImplementedError.new
end
|
#check_iptables_rule(rule, table = nil, chain = nil) ⇒ Object
184
185
186
|
# File 'lib/serverspec/commands/base.rb', line 184
def check_iptables_rule(rule, table=nil, chain=nil)
raise NotImplementedError.new
end
|
#check_kernel_module_loaded(name) ⇒ Object
220
221
222
|
# File 'lib/serverspec/commands/base.rb', line 220
def check_kernel_module_loaded(name)
raise NotImplementedError.new
end
|
#check_link(link, target) ⇒ Object
131
132
133
|
# File 'lib/serverspec/commands/base.rb', line 131
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
171
172
173
|
# File 'lib/serverspec/commands/base.rb', line 171
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
107
108
109
110
|
# File 'lib/serverspec/commands/base.rb', line 107
def check_mode(file, mode)
regexp = "^#{mode}$"
"stat -c %a #{escape(file)} | grep -- #{escape(regexp)}"
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
112
113
114
115
|
# File 'lib/serverspec/commands/base.rb', line 112
def check_owner(file, owner)
regexp = "^#{owner}$"
"stat -c %U #{escape(file)} | grep -- #{escape(regexp)}"
end
|
#check_process(process) ⇒ Object
88
89
90
|
# File 'lib/serverspec/commands/base.rb', line 88
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, level = 3) ⇒ Object
80
81
82
|
# File 'lib/serverspec/commands/base.rb', line 80
def check_running(service, level=3)
"service #{escape(service)} status"
end
|
#check_running_under_supervisor(service, level = 3) ⇒ Object
84
85
86
|
# File 'lib/serverspec/commands/base.rb', line 84
def check_running_under_supervisor(service, level=3)
"supervisorctl status #{escape(service)}"
end
|
#check_selinux(mode) ⇒ Object
212
213
214
|
# File 'lib/serverspec/commands/base.rb', line 212
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
204
205
206
|
# File 'lib/serverspec/commands/base.rb', line 204
def check_svcprop(svc, property, value)
raise NotImplementedError.new
end
|
#check_svcprops(svc, property) ⇒ Object
208
209
210
|
# File 'lib/serverspec/commands/base.rb', line 208
def check_svcprops(svc, property)
raise NotImplementedError.new
end
|
#check_uid(user, uid) ⇒ Object
166
167
168
169
|
# File 'lib/serverspec/commands/base.rb', line 166
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
188
189
190
|
# File 'lib/serverspec/commands/base.rb', line 188
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
192
193
194
|
# File 'lib/serverspec/commands/base.rb', line 192
def get_mode(file)
"stat -c %a #{escape(file)}"
end
|