Class: Serverspec::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/serverspec/commands/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



261
262
263
# File 'lib/serverspec/commands/base.rb', line 261

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

#check_authorized_key(user, key) ⇒ Object



224
225
226
227
# File 'lib/serverspec/commands/base.rb', line 224

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



202
203
204
# File 'lib/serverspec/commands/base.rb', line 202

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

#check_cron_entry(user, entry) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/serverspec/commands/base.rb', line 162

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



66
67
68
# File 'lib/serverspec/commands/base.rb', line 66

def check_directory(directory)
  "test -d #{escape(directory)}"
end

#check_enabled(service, level = 3) ⇒ Object



19
20
21
# File 'lib/serverspec/commands/base.rb', line 19

def check_enabled(service, level=3)
  raise NotImplementedError.new
end

#check_file(file) ⇒ Object



58
59
60
# File 'lib/serverspec/commands/base.rb', line 58

def check_file(file)
  "test -f #{escape(file)}"
end

#check_file_contain(file, expected_pattern) ⇒ Object



116
117
118
# File 'lib/serverspec/commands/base.rb', line 116

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_with_fixed_strings(file, expected_pattern) ⇒ Object



124
125
126
# File 'lib/serverspec/commands/base.rb', line 124

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



120
121
122
# File 'lib/serverspec/commands/base.rb', line 120

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



138
139
140
141
142
143
144
145
# File 'lib/serverspec/commands/base.rb', line 138

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



128
129
130
131
# File 'lib/serverspec/commands/base.rb', line 128

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

#check_file_sha256checksum(file, expected) ⇒ Object



133
134
135
136
# File 'lib/serverspec/commands/base.rb', line 133

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

#check_gid(group, gid) ⇒ Object



206
207
208
209
# File 'lib/serverspec/commands/base.rb', line 206

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/serverspec/commands/base.rb', line 74

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

#check_grouped(file, group) ⇒ Object



157
158
159
160
# File 'lib/serverspec/commands/base.rb', line 157

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

#check_home_directory(user, path_to_home) ⇒ Object



220
221
222
# File 'lib/serverspec/commands/base.rb', line 220

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/serverspec/commands/base.rb', line 78

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

#check_installed_by_gem(name, version = nil) ⇒ Object



175
176
177
178
179
180
# File 'lib/serverspec/commands/base.rb', line 175

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



182
183
184
185
186
# File 'lib/serverspec/commands/base.rb', line 182

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_pecl(name, version = nil) ⇒ Object



188
189
190
191
192
193
# File 'lib/serverspec/commands/base.rb', line 188

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



195
196
197
198
199
200
# File 'lib/serverspec/commands/base.rb', line 195

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



241
242
243
# File 'lib/serverspec/commands/base.rb', line 241

def check_ipfilter_rule(rule)
  raise NotImplementedError.new
end

#check_ipnat_rule(rule) ⇒ Object



245
246
247
# File 'lib/serverspec/commands/base.rb', line 245

def check_ipnat_rule(rule)
  raise NotImplementedError.new
end

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



229
230
231
# File 'lib/serverspec/commands/base.rb', line 229

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

#check_ipv4_address(interface, ip_address) ⇒ Object



269
270
271
# File 'lib/serverspec/commands/base.rb', line 269

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

#check_kernel_module_loaded(name) ⇒ Object



265
266
267
# File 'lib/serverspec/commands/base.rb', line 265

def check_kernel_module_loaded(name)
  raise NotImplementedError.new
end


171
172
173
# File 'lib/serverspec/commands/base.rb', line 171

def check_link(link, target)
  "stat -c %N #{escape(link)} | grep -- #{escape(target)}"
end

#check_listening(port) ⇒ Object



82
83
84
85
# File 'lib/serverspec/commands/base.rb', line 82

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

#check_listening_with_protocol(port, protocol) ⇒ Object



87
88
89
90
# File 'lib/serverspec/commands/base.rb', line 87

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

#check_login_shell(user, path_to_shell) ⇒ Object



216
217
218
# File 'lib/serverspec/commands/base.rb', line 216

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



273
274
275
276
# File 'lib/serverspec/commands/base.rb', line 273

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

#check_mode(file, mode) ⇒ Object



147
148
149
150
# File 'lib/serverspec/commands/base.rb', line 147

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

#check_monitored_by_god(service) ⇒ Object



108
109
110
# File 'lib/serverspec/commands/base.rb', line 108

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

#check_monitored_by_monit(service) ⇒ Object



104
105
106
# File 'lib/serverspec/commands/base.rb', line 104

def check_monitored_by_monit(service)
  "monit status"
end

#check_mounted(path) ⇒ Object



31
32
33
34
# File 'lib/serverspec/commands/base.rb', line 31

def check_mounted(path)
  regexp = "on #{path}"
  "mount | grep -w -- #{escape(regexp)}"
end

#check_owner(file, owner) ⇒ Object



152
153
154
155
# File 'lib/serverspec/commands/base.rb', line 152

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

#check_process(process) ⇒ Object



112
113
114
# File 'lib/serverspec/commands/base.rb', line 112

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/serverspec/commands/base.rb', line 40

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



48
49
50
51
52
53
54
55
56
# File 'lib/serverspec/commands/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/serverspec/commands/base.rb', line 36

def check_routing_table(destination)
  "ip route | grep -E '^#{destination} |^default '"
end

#check_running(service) ⇒ Object



92
93
94
# File 'lib/serverspec/commands/base.rb', line 92

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

#check_running_under_supervisor(service) ⇒ Object



96
97
98
# File 'lib/serverspec/commands/base.rb', line 96

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

#check_running_under_upstart(service) ⇒ Object



100
101
102
# File 'lib/serverspec/commands/base.rb', line 100

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

#check_selinux(mode) ⇒ Object



257
258
259
# File 'lib/serverspec/commands/base.rb', line 257

def check_selinux(mode)
  raise NotImplementedError.new
end

#check_socket(file) ⇒ Object



62
63
64
# File 'lib/serverspec/commands/base.rb', line 62

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

#check_svcprop(svc, property, value) ⇒ Object



249
250
251
# File 'lib/serverspec/commands/base.rb', line 249

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

#check_svcprops(svc, property) ⇒ Object



253
254
255
# File 'lib/serverspec/commands/base.rb', line 253

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

#check_uid(user, uid) ⇒ Object



211
212
213
214
# File 'lib/serverspec/commands/base.rb', line 211

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

#check_user(user) ⇒ Object



70
71
72
# File 'lib/serverspec/commands/base.rb', line 70

def check_user(user)
  "id #{escape(user)}"
end

#check_yumrepo(repository) ⇒ Object



23
24
25
# File 'lib/serverspec/commands/base.rb', line 23

def check_yumrepo(repository)
  raise NotImplementedError.new
end

#check_yumrepo_enabled(repository) ⇒ Object



27
28
29
# File 'lib/serverspec/commands/base.rb', line 27

def check_yumrepo_enabled(repository)
  raise NotImplementedError.new
end

#check_zfs(zfs, property = nil, value = nil) ⇒ Object



233
234
235
# File 'lib/serverspec/commands/base.rb', line 233

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/serverspec/commands/base.rb', line 8

def escape(target)
  str = case target
        when Regexp
          target.source
        else
          target.to_s
        end

  Shellwords.shellescape(str)
end

#get_mode(file) ⇒ Object



237
238
239
# File 'lib/serverspec/commands/base.rb', line 237

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