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



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

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

#check_authorized_key(user, key) ⇒ Object



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

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



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

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

#check_cron_entry(user, entry) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/serverspec/commands/base.rb', line 110

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



47
48
49
# File 'lib/serverspec/commands/base.rb', line 47

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

#check_enabled(service) ⇒ Object



12
13
14
# File 'lib/serverspec/commands/base.rb', line 12

def check_enabled service
  raise NotImplementedError.new
end

#check_file(file) ⇒ Object



43
44
45
# File 'lib/serverspec/commands/base.rb', line 43

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

#check_file_contain(file, expected_pattern) ⇒ Object



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

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



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

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



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

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

#check_gid(group, gid) ⇒ Object



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

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



55
56
57
# File 'lib/serverspec/commands/base.rb', line 55

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

#check_grouped(file, group) ⇒ Object



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

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

#check_home_directory(user, path_to_home) ⇒ Object



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

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



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

def check_installed package
  raise NotImplementedError.new
end

#check_installed_by_gem(name, version = nil) ⇒ Object



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

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_ipfilter_rule(rule) ⇒ Object



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

def check_ipfilter_rule rule
  raise NotImplementedError.new
end

#check_ipnat_rule(rule) ⇒ Object



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

def check_ipnat_rule rule
  raise NotImplementedError.new
end

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



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

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


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

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

#check_listening(port) ⇒ Object



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

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

#check_login_shell(user, path_to_shell) ⇒ Object



145
146
147
# File 'lib/serverspec/commands/base.rb', line 145

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



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

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

#check_mounted(path) ⇒ Object



16
17
18
19
# File 'lib/serverspec/commands/base.rb', line 16

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

#check_owner(file, owner) ⇒ Object



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

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

#check_process(process) ⇒ Object



76
77
78
# File 'lib/serverspec/commands/base.rb', line 76

def check_process process
  "ps aux | grep -w -- #{escape(process)} | grep -qv grep"
end

#check_reachable(host, port, proto, timeout) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/serverspec/commands/base.rb', line 25

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



33
34
35
36
37
38
39
40
41
# File 'lib/serverspec/commands/base.rb', line 33

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



21
22
23
# File 'lib/serverspec/commands/base.rb', line 21

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

#check_running(service) ⇒ Object



68
69
70
# File 'lib/serverspec/commands/base.rb', line 68

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

#check_running_under_supervisor(service) ⇒ Object



72
73
74
# File 'lib/serverspec/commands/base.rb', line 72

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

#check_selinux(mode) ⇒ Object



186
187
188
# File 'lib/serverspec/commands/base.rb', line 186

def check_selinux mode
  raise NotImplementedError.new
end

#check_svcprop(svc, property, value) ⇒ Object



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

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

#check_svcprops(svc, property) ⇒ Object



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

def check_svcprops svc, property
  raise NotImplementedError.new
end

#check_uid(user, uid) ⇒ Object



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

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

#check_user(user) ⇒ Object



51
52
53
# File 'lib/serverspec/commands/base.rb', line 51

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

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



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

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



166
167
168
# File 'lib/serverspec/commands/base.rb', line 166

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