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_authorized_key(user, key) ⇒ Object
-
#check_belonging_group(user, group) ⇒ Object
-
#check_cron_entry(user, entry) ⇒ Object
-
#check_directory(directory) ⇒ Object
-
#check_enabled(service) ⇒ 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_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) ⇒ Object
-
#check_ipfilter_rule(rule) ⇒ Object
-
#check_ipnat_rule(rule) ⇒ Object
-
#check_iptables_rule(rule, table = nil, chain = nil) ⇒ 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_resolvable(name, type) ⇒ Object
-
#check_running(service) ⇒ Object
-
#check_running_under_supervisor(service) ⇒ Object
-
#check_selinux(mode) ⇒ Object
-
#check_svcprop(svc, property, value) ⇒ Object
-
#check_svcprops(svc, property) ⇒ Object
-
#check_uid(user, uid) ⇒ Object
-
#check_user(user) ⇒ Object
-
#check_zfs(zfs, property = nil, value = nil) ⇒ Object
-
#get_mode(file) ⇒ Object
Instance Method Details
#check_authorized_key(user, key) ⇒ Object
116
117
118
119
|
# File 'lib/serverspec/commands/base.rb', line 116
def check_authorized_key user, key
key.sub!(/\s+\S*$/, '') if key.match(/^\S+\s+\S+\s+\S*$/)
"grep -w '#{key}' ~#{user}/.ssh/authorized_keys"
end
|
#check_belonging_group(user, group) ⇒ Object
96
97
98
|
# File 'lib/serverspec/commands/base.rb', line 96
def check_belonging_group user, group
"id #{user} | awk '{print $3}' | grep #{group}"
end
|
#check_cron_entry(user, entry) ⇒ Object
83
84
85
86
|
# File 'lib/serverspec/commands/base.rb', line 83
def check_cron_entry user, entry
entry_escaped = entry.gsub(/\*/, '\\*')
"crontab -u #{user} -l | grep \"#{entry_escaped}\""
end
|
#check_directory(directory) ⇒ Object
28
29
30
|
# File 'lib/serverspec/commands/base.rb', line 28
def check_directory directory
"test -d #{directory}"
end
|
#check_enabled(service) ⇒ Object
6
7
8
|
# File 'lib/serverspec/commands/base.rb', line 6
def check_enabled service
raise NotImplementedError.new
end
|
#check_file(file) ⇒ Object
24
25
26
|
# File 'lib/serverspec/commands/base.rb', line 24
def check_file file
"test -f #{file}"
end
|
#check_file_contain(file, expected_pattern) ⇒ Object
60
61
62
|
# File 'lib/serverspec/commands/base.rb', line 60
def check_file_contain file, expected_pattern
"grep -q '#{expected_pattern}' #{file}"
end
|
#check_file_contain_within(file, expected_pattern, from = nil, to = nil) ⇒ Object
64
65
66
67
68
69
|
# File 'lib/serverspec/commands/base.rb', line 64
def check_file_contain_within file, expected_pattern, from=nil, to=nil
from ||= '1'
to ||= '$'
checker = check_file_contain("-", expected_pattern)
"sed -n '#{from},#{to}p' #{file} | #{checker}"
end
|
#check_gid(group, gid) ⇒ Object
100
101
102
|
# File 'lib/serverspec/commands/base.rb', line 100
def check_gid group, gid
"getent group | grep -w ^#{group} | cut -f 3 -d ':' | grep -w #{gid}"
end
|
#check_group(group) ⇒ Object
36
37
38
|
# File 'lib/serverspec/commands/base.rb', line 36
def check_group group
"getent group | grep -wq #{group}"
end
|
#check_grouped(file, group) ⇒ Object
79
80
81
|
# File 'lib/serverspec/commands/base.rb', line 79
def check_grouped file, group
"stat -c %G #{file} | grep '^#{group}$'"
end
|
#check_home_directory(user, path_to_home) ⇒ Object
112
113
114
|
# File 'lib/serverspec/commands/base.rb', line 112
def check_home_directory user, path_to_home
"getent passwd #{user} | cut -f 6 -d ':' | grep -w #{path_to_home}"
end
|
#check_installed(package) ⇒ Object
40
41
42
|
# File 'lib/serverspec/commands/base.rb', line 40
def check_installed package
raise NotImplementedError.new
end
|
#check_installed_by_gem(name) ⇒ Object
92
93
94
|
# File 'lib/serverspec/commands/base.rb', line 92
def check_installed_by_gem name
"gem list --local | grep '^#{name} '"
end
|
#check_ipfilter_rule(rule) ⇒ Object
139
140
141
|
# File 'lib/serverspec/commands/base.rb', line 139
def check_ipfilter_rule rule
raise NotImplementedError.new
end
|
#check_ipnat_rule(rule) ⇒ Object
143
144
145
|
# File 'lib/serverspec/commands/base.rb', line 143
def check_ipnat_rule rule
raise NotImplementedError.new
end
|
#check_iptables_rule(rule, table = nil, chain = nil) ⇒ Object
121
122
123
124
125
126
127
128
129
|
# File 'lib/serverspec/commands/base.rb', line 121
def check_iptables_rule rule, table=nil, chain=nil
cmd = "iptables"
cmd += " -t #{table}" if table
cmd += " -S"
cmd += " #{chain}" if chain
rule.gsub!(/\-/, '\\-')
cmd += " | grep '#{rule}'"
cmd
end
|
#check_link(link, target) ⇒ Object
88
89
90
|
# File 'lib/serverspec/commands/base.rb', line 88
def check_link link, target
"stat -c %N #{link} | grep #{target}"
end
|
#check_listening(port) ⇒ Object
44
45
46
|
# File 'lib/serverspec/commands/base.rb', line 44
def check_listening port
"netstat -tunl | grep ':#{port} '"
end
|
#check_login_shell(user, path_to_shell) ⇒ Object
108
109
110
|
# File 'lib/serverspec/commands/base.rb', line 108
def check_login_shell user, path_to_shell
"getent passwd #{user} | cut -f 7 -d ':' | grep -w #{path_to_shell}"
end
|
#check_mode(file, mode) ⇒ Object
71
72
73
|
# File 'lib/serverspec/commands/base.rb', line 71
def check_mode file, mode
"stat -c %a #{file} | grep '^#{mode}$'"
end
|
#check_mounted(path) ⇒ Object
10
11
12
|
# File 'lib/serverspec/commands/base.rb', line 10
def check_mounted path
"mount | grep -w 'on #{path}'"
end
|
#check_owner(file, owner) ⇒ Object
75
76
77
|
# File 'lib/serverspec/commands/base.rb', line 75
def check_owner file, owner
"stat -c %U #{file} | grep '^#{owner}$'"
end
|
#check_process(process) ⇒ Object
56
57
58
|
# File 'lib/serverspec/commands/base.rb', line 56
def check_process process
"ps aux | grep -w #{process} | grep -qv grep"
end
|
#check_resolvable(name, type) ⇒ Object
14
15
16
17
18
19
20
21
22
|
# File 'lib/serverspec/commands/base.rb', line 14
def check_resolvable name, type
if type == "dns"
"nslookup -timeout=1 #{name}"
elsif type == "hosts"
"grep -w #{name} /etc/hosts"
else
"getent hosts #{name}"
end
end
|
#check_running(service) ⇒ Object
48
49
50
|
# File 'lib/serverspec/commands/base.rb', line 48
def check_running service
"service #{service} status"
end
|
#check_running_under_supervisor(service) ⇒ Object
52
53
54
|
# File 'lib/serverspec/commands/base.rb', line 52
def check_running_under_supervisor service
"supervisorctl status #{service}"
end
|
#check_selinux(mode) ⇒ Object
155
156
157
|
# File 'lib/serverspec/commands/base.rb', line 155
def check_selinux mode
"/usr/sbin/getenforce | grep -i '#{mode}'"
end
|
#check_svcprop(svc, property, value) ⇒ Object
147
148
149
|
# File 'lib/serverspec/commands/base.rb', line 147
def check_svcprop svc, property, value
raise NotImplementedError.new
end
|
#check_svcprops(svc, property) ⇒ Object
151
152
153
|
# File 'lib/serverspec/commands/base.rb', line 151
def check_svcprops svc, property
raise NotImplementedError.new
end
|
#check_uid(user, uid) ⇒ Object
104
105
106
|
# File 'lib/serverspec/commands/base.rb', line 104
def check_uid user, uid
"id #{user} | grep '^uid=#{uid}('"
end
|
#check_user(user) ⇒ Object
32
33
34
|
# File 'lib/serverspec/commands/base.rb', line 32
def check_user user
"id #{user}"
end
|
#check_zfs(zfs, property = nil, value = nil) ⇒ Object
131
132
133
|
# File 'lib/serverspec/commands/base.rb', line 131
def check_zfs zfs, property=nil, value=nil
raise NotImplementedError.new
end
|
#get_mode(file) ⇒ Object
135
136
137
|
# File 'lib/serverspec/commands/base.rb', line 135
def get_mode(file)
"stat -c %a #{file}"
end
|