Skip to content

Commit d2c0aa3

Browse files
committed
improve code style
1 parent 8ba465d commit d2c0aa3

3 files changed

Lines changed: 101 additions & 20 deletions

File tree

.rubocop.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
AllCops:
3+
Exclude:
4+
- Gemfile
5+
- Rakefile
6+
- 'test/**/*'
7+
- 'vendor/**/*'
8+
Documentation:
9+
Enabled: false
10+
AlignParameters:
11+
Enabled: true
12+
Encoding:
13+
Enabled: false
14+
HashSyntax:
15+
Enabled: true
16+
LineLength:
17+
Enabled: false
18+
EmptyLinesAroundBlockBody:
19+
Enabled: false
20+
MethodLength:
21+
Max: 40
22+
NumericLiterals:
23+
MinDigits: 10
24+
Metrics/CyclomaticComplexity:
25+
Max: 10
26+
Metrics/PerceivedComplexity:
27+
Max: 11
28+
Metrics/AbcSize:
29+
Max: 33
30+
Style/PercentLiteralDelimiters:
31+
PreferredDelimiters:
32+
'%': '{}'
33+
'%i': ()
34+
'%q': '{}'
35+
'%Q': ()
36+
'%r': '{}'
37+
'%s': ()
38+
'%w': '{}'
39+
'%W': ()
40+
'%x': ()
41+
Style/AlignHash:
42+
Enabled: false
43+
Style/PredicateName:
44+
Enabled: false
45+
Style/ZeroLengthPredicate:
46+
Enabled: false
47+
Style/NumericPredicate:
48+
Enabled: false
49+
Style/ClassAndModuleChildren:
50+
Enabled: false
51+
Style/ConditionalAssignment:
52+
Enabled: false
53+
Style/BracesAroundHashParameters:
54+
Enabled: false
55+
Style/AndOr:
56+
Enabled: false
57+
Style/Not:
58+
Enabled: false
59+
Style/FileName:
60+
Enabled: false
61+
Style/TrailingCommaInLiteral:
62+
EnforcedStyleForMultiline: comma
63+
Style/TrailingCommaInArguments:
64+
EnforcedStyleForMultiline: comma
65+
Style/NegatedIf:
66+
Enabled: false
67+
Style/UnlessElse:
68+
Enabled: false
69+
BlockDelimiters:
70+
Enabled: false
71+
Style/SpaceAroundOperators:
72+
Enabled: false
73+
Style/IfUnlessModifier:
74+
Enabled: false

controls/patches.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99

1010
control 'verify-patches' do
1111
impact 0.3
12-
title "All patches should be installed"
12+
title 'Operating system is up-to-date'
1313
describe linux_update do
14-
it { should be_uptodate}
14+
it { should be_uptodate }
1515
end
1616
end
1717

1818
control 'patches' do
1919
impact 0.3
20-
title "All updates are installed"
20+
title 'All operating system updates are installed'
2121
linux_update.updates.each { |update|
2222
describe package(update['name']) do
23-
its('version') { should eq update['version']}
23+
its('version') { should eq update['version'] }
2424
end
2525
}
2626
end

libraries/linux_updates.rb

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,35 @@ class LinuxUpdateManager < Inspec.resource(1)
1616
def initialize
1717
if inspec.os.redhat?
1818
@update_mgmt = RHELUpdateFetcher.new(inspec)
19-
else inspec.os.debian?
19+
elsif inspec.os.debian?
2020
@update_mgmt = UbuntuUpdateFetcher.new(inspec)
2121
end
2222
return skip_resource 'The `linux_update` resource is not supported on your OS.' if @update_mgmt.nil?
2323
end
2424

2525
def updates
26+
return [] if @update_mgmt.nil?
2627
u = @update_mgmt.updates
2728
return [] if u.nil? || u.empty?
2829
u['available']
2930
end
3031

3132
def uptodate?
33+
return nil if @update_mgmt.nil?
3234
u = @update_mgmt.updates
3335
return false if u.nil? || !u['available'].empty?
3436
true
3537
end
3638

3739
def packages
40+
return [] if @update_mgmt.nil?
3841
p = @update_mgmt.packages
3942
return [] if p.nil? || u.empty?
4043
p['installed']
4144
end
4245

4346
def to_s
44-
"Linux Update"
47+
'Linux Update'
4548
end
4649
end
4750

@@ -62,24 +65,15 @@ def parse_json(script)
6265
cmd = @inspec.bash(script)
6366
begin
6467
JSON.parse(cmd.stdout)
65-
rescue JSON::ParserError => e
68+
rescue JSON::ParserError => _e
6669
return []
6770
end
6871
end
6972
end
7073

7174
class UbuntuUpdateFetcher < UpdateFetcher
72-
# ubuntu base
73-
UBUNTU_BASE = <<-EOH
74-
#!/bin/sh
75-
DEBIAN_FRONTEND=noninteractive apt-get update >/dev/null 2>&1
76-
readlock() { cat /proc/locks | awk '{print $5}' | grep -v ^0 | xargs -I {1} find /proc/{1}/fd -maxdepth 1 -exec readlink {} \\; | grep '^/var/lib/dpkg/lock$'; }
77-
while test -n "$(readlock)"; do sleep 1; done
78-
echo " "
79-
EOH
80-
8175
def packages
82-
ubuntu_packages = UBUNTU_BASE + <<-EOH
76+
ubuntu_packages = ubuntu_base + <<-EOH
8377
echo -n '{"installed":['
8478
dpkg-query -W -f='${Status}\\t${Package}\\t${Version}\\t${Architecture}\\n' |\\
8579
grep '^install ok installed\\s' |\\
@@ -90,14 +84,27 @@ def packages
9084
end
9185

9286
def updates
93-
ubuntu_updates = UBUNTU_BASE + <<-EOH
87+
ubuntu_updates = ubuntu_base + <<-EOH
9488
echo -n '{"available":['
9589
DEBIAN_FRONTEND=noninteractive apt-get upgrade --dry-run | grep Inst | tr -d '[]()' |\\
9690
awk '{ printf "{\\"name\\":\\""$2"\\",\\"version\\":\\""$4"\\",\\"repo\\":\\""$5"\\",\\"arch\\":\\""$6"\\"}," }' | rev | cut -c 2- | rev | tr -d '\\n'
9791
echo -n ']}'
9892
EOH
9993
parse_json(ubuntu_updates)
10094
end
95+
96+
private
97+
98+
def ubuntu_base
99+
base = <<-EOH
100+
#!/bin/sh
101+
DEBIAN_FRONTEND=noninteractive apt-get update >/dev/null 2>&1
102+
readlock() { cat /proc/locks | awk '{print $5}' | grep -v ^0 | xargs -I {1} find /proc/{1}/fd -maxdepth 1 -exec readlink {} \\; | grep '^/var/lib/dpkg/lock$'; }
103+
while test -n "$(readlock)"; do sleep 1; done
104+
echo " "
105+
EOH
106+
base
107+
end
101108
end
102109

103110
class RHELUpdateFetcher < UpdateFetcher
@@ -121,15 +128,15 @@ def updates
121128
cmd = @inspec.bash(rhel_updates)
122129
unless cmd.exit_status == 0
123130
# essentially we want https://github.com/chef/inspec/issues/1205
124-
STDERR.puts "Could not determine patch status."
131+
STDERR.puts 'Could not determine patch status.'
125132
return nil
126133
end
127134

128135
first = cmd.stdout.index('{')
129136
res = cmd.stdout.slice(first, cmd.stdout.size - first)
130137
begin
131138
JSON.parse(res)
132-
rescue JSON::ParserError => e
139+
rescue JSON::ParserError => _e
133140
return []
134141
end
135142
end

0 commit comments

Comments
 (0)