11# encoding: utf-8
2+ # frozen_string_literal: true
3+
24# copyright: 2016, Christoph Hartmann
35# copyright: 2016, Dominik Richter
46# license: MPLv2
@@ -25,6 +27,7 @@ class LinuxUpdateManager < Inspec.resource(1)
2527
2628 # Since Amazon Linux is based on RedHat, they may use the same method.
2729 def initialize
30+ super
2831 case inspec . os [ :family ]
2932 when 'redhat' , 'amazon'
3033 @update_mgmt = RHELUpdateFetcher . new ( inspec )
@@ -99,17 +102,17 @@ def parse_json(script)
99102 begin
100103 JSON . parse ( cmd . stdout )
101104 rescue JSON ::ParserError => _e
102- return [ ]
105+ [ ]
103106 end
104107 end
105108end
106109
107110PatchEntry = Struct . new ( :name , :version , :arch , :category , :severity ) do
108111 def to_s
109112 r = "System Patch #{ name } (v#{ version } #{ arch } "
110- r += ", #{ category } " unless category . nil?
111- r += ", #{ severity } " unless severity . nil?
112- r + ')'
113+ r += ", #{ category } " unless category . nil?
114+ r += ", #{ severity } " unless severity . nil?
115+ " #{ r } )"
113116 end
114117end
115118
@@ -135,10 +138,8 @@ def updates
135138 private
136139
137140 def zypper_xml ( cmd )
138- out = @inspec . command ( 'zypper --xmlout ' +cmd )
139- if out . exit_status != 0
140- fail_resource ( 'Cannot retrieve package updates from the OS: ' +out . stderr )
141- end
141+ out = @inspec . command ( "zypper --xmlout #{ cmd } " )
142+ fail_resource ( "Cannot retrieve package updates from the OS: #{ out . stderr } " ) if out . exit_status != 0
142143 out . stdout . force_encoding ( 'UTF-8' )
143144 end
144145
@@ -149,7 +150,7 @@ def extract_xml_updates(updates_el)
149150 REXML ::XPath . each ( updates_el , 'update' ) do |el |
150151 a = el . attributes
151152 res . push (
152- PatchEntry . new ( a [ 'name' ] , a [ 'edition' ] , a [ 'arch' ] , a [ 'category' ] , a [ 'severity' ] ) ,
153+ PatchEntry . new ( a [ 'name' ] , a [ 'edition' ] , a [ 'arch' ] , a [ 'category' ] , a [ 'severity' ] )
153154 )
154155 end
155156 res
@@ -158,65 +159,66 @@ def extract_xml_updates(updates_el)
158159
159160class DebianUpdateFetcher < UpdateFetcher
160161 def packages
161- debian_packages = debian_base + <<- PRINT_JSON
162- echo -n '{"installed":['
163- dpkg-query -W -f='${Status}\\ t${Package}\\ t${Version}\\ t${Architecture}\\ n' |\\
164- grep '^install ok installed\\ s' |\\
165- awk '{ printf "{\\ "name\\ ":\\ ""$4"\\ ",\\ "version\\ ":\\ ""$5"\\ ",\\ "arch\\ ":\\ ""$6"\\ "}," }' | rev | cut -c 2- | rev | tr -d '\\ n'
166- echo -n ']}'
162+ debian_packages = debian_base + <<~ PRINT_JSON
163+ echo -n '{"installed":['
164+ dpkg-query -W -f='${Status}\\ t${Package}\\ t${Version}\\ t${Architecture}\\ n' |\\
165+ grep '^install ok installed\\ s' |\\
166+ awk '{ printf "{\\ "name\\ ":\\ ""$4"\\ ",\\ "version\\ ":\\ ""$5"\\ ",\\ "arch\\ ":\\ ""$6"\\ "}," }' | rev | cut -c 2- | rev | tr -d '\\ n'
167+ echo -n ']}'
167168 PRINT_JSON
168169 parse_json ( debian_packages )
169170 end
170171
171172 def updates
172- debian_updates = debian_base + <<- PRINT_JSON
173- echo -n '{"available":['
174- DEBIAN_FRONTEND=noninteractive apt upgrade --dry-run | grep Inst | tr -d '[]()' |\\
175- awk '{ printf "{\\ "name\\ ":\\ ""$2"\\ ",\\ "version\\ ":\\ ""$4"\\ ",\\ "repo\\ ":\\ ""$5"\\ ",\\ "arch\\ ":\\ ""$6"\\ "}," }' | rev | cut -c 2- | rev | tr -d '\\ n'
176- echo -n ']}'
173+ debian_updates = debian_base + <<~ PRINT_JSON
174+ echo -n '{"available":['
175+ DEBIAN_FRONTEND=noninteractive apt upgrade --dry-run | grep Inst | tr -d '[]()' |\\
176+ awk '{ printf "{\\ "name\\ ":\\ ""$2"\\ ",\\ "version\\ ":\\ ""$4"\\ ",\\ "repo\\ ":\\ ""$5"\\ ",\\ "arch\\ ":\\ ""$6"\\ "}," }' | rev | cut -c 2- | rev | tr -d '\\ n'
177+ echo -n ']}'
177178 PRINT_JSON
178179 parse_json ( debian_updates )
179180 end
180181
181182 private
182183
183184 def debian_base
184- base = <<- PRINT_JSON
185- #!/bin/sh
186- COMMAND="DEBIAN_FRONTEND=noninteractive apt update >>/dev/null 2>&1"
187- eval $COMMAND
188- while [ $? -ne 0 ]
189- do
190- sleep 30s
191- eval $COMMAND
192- done
193- echo " "
185+ <<~ PRINT_JSON
186+ #!/bin/sh
187+ COMMAND="DEBIAN_FRONTEND=noninteractive apt update >>/dev/null 2>&1"
188+ eval $COMMAND
189+ while [ $? -ne 0 ]
190+ do
191+ sleep 30s
192+ eval $COMMAND
193+ done
194+ echo " "
194195 PRINT_JSON
195- base
196196 end
197197end
198198
199199class RHELUpdateFetcher < UpdateFetcher
200200 def packages
201- rhel_packages = <<-PRINT_JSON
202- sleep 2 && echo " "
203- echo -n '{"installed":['
204- rpm -qa --queryformat '"name":"%{NAME}","version":"%{VERSION}-%{RELEASE}","arch":"%{ARCH}"\\ n' |\\
205- awk '{ printf "{"$1"}," }' | rev | cut -c 2- | rev | tr -d '\\ n'
206- echo -n ']}'
201+ # rubocop:disable Style/FormatStringToken
202+ rhel_packages = <<~PRINT_JSON
203+ sleep 2 && echo " "
204+ echo -n '{"installed":['
205+ rpm -qa --queryformat '"name":"%{NAME}","version":"%{VERSION}-%{RELEASE}","arch":"%{ARCH}"\\ n' |\\
206+ awk '{ printf "{"$1"}," }' | rev | cut -c 2- | rev | tr -d '\\ n'
207+ echo -n ']}'
207208 PRINT_JSON
208209 parse_json ( rhel_packages )
210+ # rubocop:enable Style/FormatStringToken
209211 end
210212
211213 def updates
212- rhel_updates = <<- PRINT_JSON
213- #!/bin/sh
214- python -c 'import sys; sys.path.insert(0, "/usr/share/yum-cli"); import cli; ybc = cli.YumBaseCli(); ybc.setCacheDir("/tmp"); list = ybc.returnPkgLists(["updates"]);res = ["{\\ "name\\ ":\\ ""+x.name+"\\ ", \\ "version\\ ":\\ ""+x.version+"-"+x.release+"\\ ",\\ "arch\\ ":\\ ""+x.arch+"\\ ",\\ "repository\\ ":\\ ""+x.repo.id+"\\ "}" for x in list.updates]; print "{\\ "available\\ ":["+",".join(res)+"]}"'
214+ rhel_updates = <<~ PRINT_JSON
215+ #!/bin/sh
216+ python -c 'import sys; sys.path.insert(0, "/usr/share/yum-cli"); import cli; ybc = cli.YumBaseCli(); ybc.setCacheDir("/tmp"); list = ybc.returnPkgLists(["updates"]);res = ["{\\ "name\\ ":\\ ""+x.name+"\\ ", \\ "version\\ ":\\ ""+x.version+"-"+x.release+"\\ ",\\ "arch\\ ":\\ ""+x.arch+"\\ ",\\ "repository\\ ":\\ ""+x.repo.id+"\\ "}" for x in list.updates]; print "{\\ "available\\ ":["+",".join(res)+"]}"'
215217 PRINT_JSON
216218 cmd = @inspec . bash ( rhel_updates )
217- unless cmd . exit_status == 0
219+ unless cmd . exit_status . zero?
218220 # essentially we want https://github.com/chef/inspec/issues/1205
219- STDERR . puts 'Could not determine patch status.'
221+ warn 'Could not determine patch status.'
220222 return nil
221223 end
222224
@@ -225,7 +227,7 @@ def updates
225227 begin
226228 JSON . parse ( res )
227229 rescue JSON ::ParserError => _e
228- return [ ]
230+ [ ]
229231 end
230232 end
231233end
0 commit comments