Class: Serverspec::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/serverspec/commands/base.rb

Direct Known Subclasses

Darwin, Linux, Solaris

Defined Under Namespace

Classes: NotImplementedError

Instance Method Summary collapse

Instance Method Details

#check_access_by_user(file, user, access) ⇒ Object



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

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

#check_authorized_key(user, key) ⇒ Object



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

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



174
175
176
# File 'lib/serverspec/commands/base.rb', line 174

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

#check_cron_entry(user, entry) ⇒ Object



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

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



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

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



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

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



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

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

#check_gid(group, gid) ⇒ Object



178
179
180
181
# File 'lib/serverspec/commands/base.rb', line 178

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



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

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

#check_home_directory(user, path_to_home) ⇒ Object



192
193
194
# File 'lib/serverspec/commands/base.rb', line 192

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



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

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



160
161
162
163
164
# File 'lib/serverspec/commands/base.rb', line 160

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



166
167
168
169
170
171
172
# File 'lib/serverspec/commands/base.rb', line 166

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



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

def check_ipfilter_rule(rule)
  raise NotImplementedError.new
end

#check_ipnat_rule(rule) ⇒ Object



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

def check_ipnat_rule(rule)
  raise NotImplementedError.new
end

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



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

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

#check_kernel_module_loaded(name) ⇒ Object



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

def check_kernel_module_loaded(name)
  raise NotImplementedError.new
end


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

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_listening_with_protocol(port, protocol) ⇒ Object



80
81
82
83
# File 'lib/serverspec/commands/base.rb', line 80

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

#check_login_shell(user, path_to_shell) ⇒ Object



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

def (user, path_to_shell)
  "getent passwd #{escape(user)} | cut -f 7 -d ':' | grep -w -- #{escape(path_to_shell)}"
end

#check_mode(file, mode) ⇒ Object



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

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

#check_monitored_by_god(service) ⇒ Object



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

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

#check_monitored_by_monit(service) ⇒ Object



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

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



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

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

#check_process(process) ⇒ Object



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

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



85
86
87
# File 'lib/serverspec/commands/base.rb', line 85

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

#check_running_under_supervisor(service) ⇒ Object



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

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

#check_running_under_upstart(service) ⇒ Object



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

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

#check_selinux(mode) ⇒ Object



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

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



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

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

#check_svcprops(svc, property) ⇒ Object



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

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

#check_uid(user, uid) ⇒ Object



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

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



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

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



209
210
211
# File 'lib/serverspec/commands/base.rb', line 209

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