@@ -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
4649end
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
6972end
7073
7174class 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
8377echo -n '{"installed":['
8478dpkg-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
9488echo -n '{"available":['
9589DEBIAN_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'
9791echo -n ']}'
9892EOH
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
101108end
102109
103110class 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