specinfra 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/specinfra.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "specinfra/version"
2
2
  require "specinfra/helper"
3
3
  require "specinfra/backend"
4
+ require "specinfra/command"
4
5
  require "specinfra/configuration"
5
6
 
6
7
  include SpecInfra
@@ -23,7 +24,7 @@ if defined?(RSpec)
23
24
  c.add_setting :winrm, :default => nil
24
25
  SpecInfra.configuration.defaults.each { |k, v| c.add_setting k, :default => v }
25
26
  c.before :each do
26
- backend.set_example(example) if defined?(SPEC_TYPE)
27
+ backend.set_example(example)
27
28
  end
28
29
  end
29
30
  end
@@ -0,0 +1,21 @@
1
+ require "specinfra/command/base"
2
+
3
+ # Linux
4
+ require "specinfra/command/linux"
5
+ require "specinfra/command/debian"
6
+ require "specinfra/command/gentoo"
7
+ require "specinfra/command/plamo"
8
+ require "specinfra/command/redhat"
9
+
10
+ # Solaris
11
+ require "specinfra/command/solaris"
12
+ require "specinfra/command/solaris10"
13
+ require "specinfra/command/solaris11"
14
+ require "specinfra/command/smartos"
15
+
16
+ # Others
17
+ require "specinfra/command/aix"
18
+ require "specinfra/command/darwin"
19
+ require "specinfra/command/freebsd"
20
+ require "specinfra/command/windows"
21
+
@@ -0,0 +1,66 @@
1
+ module SpecInfra
2
+ module Command
3
+ class AIX < Base
4
+ class NotImplementedError < Exception; end
5
+
6
+ def check_access_by_user(file, user, access)
7
+ "su -s sh -c \"test -#{access} #{file}\" #{user}"
8
+ end
9
+
10
+ def check_enabled(service,level=nil)
11
+ "lssrc -s #{escape(service)} | grep active"
12
+ end
13
+
14
+ def check_running(service)
15
+ "ps -ef | grep -v grep | grep #{escape(service)}"
16
+ end
17
+
18
+ def check_installed(package,version)
19
+ if version
20
+ "lslpp -L #{escape(package)} | awk '{print $2}' | grep -w -- #{version}"
21
+ else
22
+ "lslpp -L #{escape(package)}"
23
+ end
24
+ end
25
+
26
+ def check_listening(port)
27
+ regexp = "*.#{port} "
28
+ "netstat -an -f inet | awk '{print $4}' | grep -- #{regexp}"
29
+ #"netstat -an -f inet | awk '{print $4}' | grep -- #{escape(regexp)}"
30
+ end
31
+
32
+ def check_belonging_group(user, group)
33
+ "lsuser -a groups #{escape(user)} | awk -F'=' '{print $2}'| sed -e 's/,/ /g' |grep -w -- #{escape(group)}"
34
+ end
35
+
36
+ def check_gid(group, gid)
37
+ regexp = "^#{group}"
38
+ "cat etc/group | grep -w -- #{escape(regexp)} | cut -f 3 -d ':' | grep -w -- #{escape(gid)}"
39
+ end
40
+
41
+ def check_login_shell(user, path_to_shell)
42
+ "lsuser -a shell #{escape(user)} |awk -F'=' '{print $2}' | grep -w -- #{escape(path_to_shell)}"
43
+ end
44
+
45
+ def check_home_directory(user, path_to_home)
46
+ "lsuser -a home #{escape(user)} | awk -F'=' '{print $2}' | grep -w -- #{escape(path_to_home)}"
47
+ end
48
+
49
+ def check_mode(file, mode)
50
+ raise NotImplementedError.new
51
+ end
52
+
53
+ def check_owner(file, owner)
54
+ regexp = "^#{owner}$"
55
+ "ls -al #{escape(file)} | awk '{print $3}' | grep -- #{escape(regexp)}"
56
+ end
57
+
58
+ def check_grouped(file, group)
59
+ regexp = "^#{group}$"
60
+ "ls -al #{escape(file)} | awk '{print $4}' | grep -- #{escape(regexp)}"
61
+ end
62
+
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,305 @@
1
+ require 'shellwords'
2
+
3
+ module SpecInfra
4
+ module Command
5
+ class Base
6
+ class NotImplementedError < Exception; end
7
+
8
+ def escape(target)
9
+ str = case target
10
+ when Regexp
11
+ target.source
12
+ else
13
+ target.to_s
14
+ end
15
+
16
+ Shellwords.shellescape(str)
17
+ end
18
+
19
+ def check_enabled(service, level=3)
20
+ raise NotImplementedError.new
21
+ end
22
+
23
+ def check_yumrepo(repository)
24
+ raise NotImplementedError.new
25
+ end
26
+
27
+ def check_yumrepo_enabled(repository)
28
+ raise NotImplementedError.new
29
+ end
30
+
31
+ def check_mounted(path)
32
+ regexp = "on #{path}"
33
+ "mount | grep -w -- #{escape(regexp)}"
34
+ end
35
+
36
+ def check_routing_table(destination)
37
+ "ip route | grep -E '^#{destination} |^default '"
38
+ end
39
+
40
+ def check_reachable(host, port, proto, timeout)
41
+ if port.nil?
42
+ "ping -n #{escape(host)} -w #{escape(timeout)} -c 2"
43
+ else
44
+ "nc -vvvvz#{escape(proto[0].chr)} #{escape(host)} #{escape(port)} -w #{escape(timeout)}"
45
+ end
46
+ end
47
+
48
+ def check_resolvable(name, type)
49
+ if type == "dns"
50
+ "nslookup -timeout=1 #{escape(name)}"
51
+ elsif type == "hosts"
52
+ "grep -w -- #{escape(name)} /etc/hosts"
53
+ else
54
+ "getent hosts #{escape(name)}"
55
+ end
56
+ end
57
+
58
+ def check_file(file)
59
+ "test -f #{escape(file)}"
60
+ end
61
+
62
+ def check_socket(file)
63
+ "test -S #{escape(file)}"
64
+ end
65
+
66
+ def check_directory(directory)
67
+ "test -d #{escape(directory)}"
68
+ end
69
+
70
+ def check_user(user)
71
+ "id #{escape(user)}"
72
+ end
73
+
74
+ def check_group(group)
75
+ "getent group | grep -wq -- #{escape(group)}"
76
+ end
77
+
78
+ def check_installed(package, version=nil)
79
+ raise NotImplementedError.new
80
+ end
81
+
82
+ def check_listening(port)
83
+ regexp = ":#{port} "
84
+ "netstat -tunl | grep -- #{escape(regexp)}"
85
+ end
86
+
87
+ def check_listening_with_protocol(port, protocol)
88
+ regexp = "^#{protocol} .*:#{port} "
89
+ "netstat -tunl | grep -- #{escape(regexp)}"
90
+ end
91
+
92
+ def check_running(service)
93
+ "service #{escape(service)} status"
94
+ end
95
+
96
+ def check_running_under_supervisor(service)
97
+ "supervisorctl status #{escape(service)} | grep RUNNING"
98
+ end
99
+
100
+ def check_running_under_upstart(service)
101
+ "initctl status #{escape(service)} | grep running"
102
+ end
103
+
104
+ def check_monitored_by_monit(service)
105
+ "monit status"
106
+ end
107
+
108
+ def check_monitored_by_god(service)
109
+ "god status #{escape(service)}"
110
+ end
111
+
112
+ def check_process(process)
113
+ "ps aux | grep -w -- #{escape(process)} | grep -qv grep"
114
+ end
115
+
116
+ def check_file_contain(file, expected_pattern)
117
+ "#{check_file_contain_with_regexp(file, expected_pattern)} || #{check_file_contain_with_fixed_strings(file, expected_pattern)}"
118
+ end
119
+
120
+ def check_file_contain_with_regexp(file, expected_pattern)
121
+ "grep -q -- #{escape(expected_pattern)} #{escape(file)}"
122
+ end
123
+
124
+ def check_file_contain_with_fixed_strings(file, expected_pattern)
125
+ "grep -qF -- #{escape(expected_pattern)} #{escape(file)}"
126
+ end
127
+
128
+ def check_file_md5checksum(file, expected)
129
+ regexp = "^#{expected}"
130
+ "md5sum #{escape(file)} | grep -iw -- #{escape(regexp)}"
131
+ end
132
+
133
+ def check_file_sha256checksum(file, expected)
134
+ regexp = "^#{expected}"
135
+ "sha256sum #{escape(file)} | grep -iw -- #{escape(regexp)}"
136
+ end
137
+
138
+ def check_file_contain_within(file, expected_pattern, from=nil, to=nil)
139
+ from ||= '1'
140
+ to ||= '$'
141
+ sed = "sed -n #{escape(from)},#{escape(to)}p #{escape(file)}"
142
+ checker_with_regexp = check_file_contain_with_regexp("-", expected_pattern)
143
+ checker_with_fixed = check_file_contain_with_fixed_strings("-", expected_pattern)
144
+ "#{sed} | #{checker_with_regexp} || #{sed} | #{checker_with_fixed}"
145
+ end
146
+
147
+ def check_mode(file, mode)
148
+ regexp = "^#{mode}$"
149
+ "stat -c %a #{escape(file)} | grep -- #{escape(regexp)}"
150
+ end
151
+
152
+ def check_owner(file, owner)
153
+ regexp = "^#{owner}$"
154
+ "stat -c %U #{escape(file)} | grep -- #{escape(regexp)}"
155
+ end
156
+
157
+ def check_grouped(file, group)
158
+ regexp = "^#{group}$"
159
+ "stat -c %G #{escape(file)} | grep -- #{escape(regexp)}"
160
+ end
161
+
162
+ def check_cron_entry(user, entry)
163
+ entry_escaped = entry.gsub(/\*/, '\\*')
164
+ if user.nil?
165
+ "crontab -l | grep -- #{escape(entry_escaped)}"
166
+ else
167
+ "crontab -u #{escape(user)} -l | grep -- #{escape(entry_escaped)}"
168
+ end
169
+ end
170
+
171
+ def check_link(link, target)
172
+ "stat -c %N #{escape(link)} | grep -- #{escape(target)}"
173
+ end
174
+
175
+ def check_installed_by_gem(name, version=nil)
176
+ regexp = "^#{name}"
177
+ cmd = "gem list --local | grep -w -- #{escape(regexp)}"
178
+ cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
179
+ cmd
180
+ end
181
+
182
+ def check_installed_by_npm(name, version=nil)
183
+ cmd = "npm ls #{escape(name)} -g"
184
+ cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
185
+ cmd
186
+ end
187
+
188
+ def check_installed_by_pecl(name, version=nil)
189
+ regexp = "^#{name}"
190
+ cmd = "pecl list | grep -w -- #{escape(regexp)}"
191
+ cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
192
+ cmd
193
+ end
194
+
195
+ def check_installed_by_pear(name, version=nil)
196
+ regexp = "^#{name}"
197
+ cmd = "pear list | grep -w -- #{escape(regexp)}"
198
+ cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
199
+ cmd
200
+ end
201
+
202
+ def check_installed_by_pip(name, version=nil)
203
+ regexp = "^#{name}"
204
+ cmd = "pip list | grep -w -- #{escape(regexp)}"
205
+ cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
206
+ cmd
207
+ end
208
+
209
+ def check_installed_by_cpan(name, version=nil)
210
+ regexp = "^#{name}"
211
+ cmd = "cpan -l | grep -w -- #{escape(regexp)}"
212
+ cmd = "#{cmd} | grep -w -- #{escape(version)}" if version
213
+ cmd
214
+ end
215
+
216
+ def check_belonging_group(user, group)
217
+ "id #{escape(user)} | awk '{print $3}' | grep -- #{escape(group)}"
218
+ end
219
+
220
+ def check_gid(group, gid)
221
+ regexp = "^#{group}"
222
+ "getent group | grep -w -- #{escape(regexp)} | cut -f 3 -d ':' | grep -w -- #{escape(gid)}"
223
+ end
224
+
225
+ def check_uid(user, uid)
226
+ regexp = "^uid=#{uid}("
227
+ "id #{escape(user)} | grep -- #{escape(regexp)}"
228
+ end
229
+
230
+ def check_login_shell(user, path_to_shell)
231
+ "getent passwd #{escape(user)} | cut -f 7 -d ':' | grep -w -- #{escape(path_to_shell)}"
232
+ end
233
+
234
+ def check_home_directory(user, path_to_home)
235
+ "getent passwd #{escape(user)} | cut -f 6 -d ':' | grep -w -- #{escape(path_to_home)}"
236
+ end
237
+
238
+ def check_authorized_key(user, key)
239
+ key.sub!(/\s+\S*$/, '') if key.match(/^\S+\s+\S+\s+\S*$/)
240
+ "grep -w -- #{escape(key)} ~#{escape(user)}/.ssh/authorized_keys"
241
+ end
242
+
243
+ def check_iptables_rule(rule, table=nil, chain=nil)
244
+ raise NotImplementedError.new
245
+ end
246
+
247
+ def check_zfs(zfs, property=nil, value=nil)
248
+ raise NotImplementedError.new
249
+ end
250
+
251
+ def get_mode(file)
252
+ "stat -c %a #{escape(file)}"
253
+ end
254
+
255
+ def check_ipfilter_rule(rule)
256
+ raise NotImplementedError.new
257
+ end
258
+
259
+ def check_ipnat_rule(rule)
260
+ raise NotImplementedError.new
261
+ end
262
+
263
+ def check_svcprop(svc, property, value)
264
+ raise NotImplementedError.new
265
+ end
266
+
267
+ def check_svcprops(svc, property)
268
+ raise NotImplementedError.new
269
+ end
270
+
271
+ def check_selinux(mode)
272
+ raise NotImplementedError.new
273
+ end
274
+
275
+ def check_access_by_user(file, user, access)
276
+ raise NotImplementedError.new
277
+ end
278
+
279
+ def check_kernel_module_loaded(name)
280
+ raise NotImplementedError.new
281
+ end
282
+
283
+ def check_ipv4_address(interface, ip_address)
284
+ raise NotImplementedError.new
285
+ end
286
+
287
+ def check_mail_alias(recipient, target)
288
+ target = "[[:space:]]#{target}"
289
+ "getent aliases #{escape(recipient)} | grep -- #{escape(target)}$"
290
+ end
291
+
292
+ def get_file_content(file)
293
+ "cat #{file}"
294
+ end
295
+
296
+ def check_container(container)
297
+ raise NotImplementedError.new
298
+ end
299
+
300
+ def check_cotainer_running(container)
301
+ raise NotImplementedError.new
302
+ end
303
+ end
304
+ end
305
+ end
@@ -0,0 +1,44 @@
1
+ module SpecInfra
2
+ module Command
3
+ class Darwin < Base
4
+ def check_file_md5checksum(file, expected)
5
+ "openssl md5 #{escape(file)} | cut -d'=' -f2 | cut -c 2- | grep -E ^#{escape(expected)}$"
6
+ end
7
+
8
+ def check_file_sha256checksum(file, expected)
9
+ "openssl sha256 #{escape(file)} | cut -d'=' -f2 | cut -c 2- | grep -E ^#{escape(expected)}$"
10
+ end
11
+
12
+ def check_link(link, target)
13
+ "stat -f %Y #{escape(link)} | grep -- #{escape(target)}"
14
+ end
15
+
16
+ def check_mode(file, mode)
17
+ regexp = "^#{mode}$"
18
+ "stat -f%Lp #{escape(file)} | grep -- #{escape(regexp)}"
19
+ end
20
+
21
+ def check_owner(file, owner)
22
+ regexp = "^#{owner}$"
23
+ "stat -f %Su #{escape(file)} | grep -- #{escape(regexp)}"
24
+ end
25
+
26
+ def check_grouped(file, group)
27
+ regexp = "^#{group}$"
28
+ "stat -f %Sg #{escape(file)} | grep -- #{escape(regexp)}"
29
+ end
30
+
31
+ def get_mode(file)
32
+ "stat -f%Lp #{escape(file)}"
33
+ end
34
+
35
+ def check_access_by_user(file, user, access)
36
+ "sudo -u #{user} -s /bin/test -#{access} #{file}"
37
+ end
38
+
39
+ def install(package)
40
+ cmd = "brew install '#{package}'"
41
+ end
42
+ end
43
+ end
44
+ end