88# file, You can obtain one at http://mozilla.org/MPL/2.0/.
99
1010require 'json'
11+ require 'rexml/document'
1112
1213class LinuxUpdateManager < Inspec . resource ( 1 )
1314 name 'linux_update'
@@ -29,6 +30,8 @@ def initialize
2930 @update_mgmt = RHELUpdateFetcher . new ( inspec )
3031 when 'debian'
3132 @update_mgmt = UbuntuUpdateFetcher . new ( inspec )
33+ when 'suse'
34+ @update_mgmt = SuseUpdateFetcher . new ( inspec )
3235 end
3336 return skip_resource 'The `linux_update` resource is not supported on your OS.' if @update_mgmt . nil?
3437 end
@@ -44,6 +47,8 @@ def uptodate?
4447 return nil if @update_mgmt . nil?
4548 u = @update_mgmt . updates
4649 return false if u . nil? || !u [ 'available' ] . empty?
50+ l = @update_mgmt . patches
51+ return false if l . nil? || !l . empty?
4752 true
4853 end
4954
@@ -54,6 +59,11 @@ def packages
5459 p [ 'installed' ]
5560 end
5661
62+ def patches
63+ return [ ] if @update_mgmt . nil?
64+ @update_mgmt . patches || [ ]
65+ end
66+
5767 def to_s
5868 'Linux Update'
5969 end
@@ -72,6 +82,10 @@ def updates
7282 [ ]
7383 end
7484
85+ def patches
86+ [ ]
87+ end
88+
7589 def parse_json ( script )
7690 cmd = @inspec . bash ( script )
7791 begin
@@ -82,6 +96,52 @@ def parse_json(script)
8296 end
8397end
8498
99+ class SuseUpdateFetcher < UpdateFetcher
100+ def patches
101+ out = zypper_xml ( 'list-updates -t patch' )
102+ xml = REXML ::Document . new ( out )
103+
104+ extract_xml_updates ( REXML ::XPath . first ( xml , '//update-list' ) ) +
105+ extract_xml_updates ( REXML ::XPath . first ( xml , '//blocked-update-list' ) )
106+ end
107+
108+ def updates
109+ out = zypper_xml ( 'list-updates' )
110+ xml = REXML ::Document . new ( out )
111+
112+ res = extract_xml_updates ( REXML ::XPath . first ( xml , '//update-list' ) ) +
113+ extract_xml_updates ( REXML ::XPath . first ( xml , '//blocked-update-list' ) )
114+
115+ { 'available' => res }
116+ end
117+
118+ private
119+
120+ def zypper_xml ( cmd )
121+ out = @inspec . command ( 'zypper --xmlout ' +cmd )
122+ if out . exit_status != 0
123+ fail_resource ( 'Cannot retrieve package updates from the OS: ' +out . stderr )
124+ end
125+ out . stdout . force_encoding ( 'UTF-8' )
126+ end
127+
128+ def extract_xml_updates ( updates_el )
129+ res = [ ]
130+ return res if updates_el . nil?
131+
132+ REXML ::XPath . each ( updates_el , 'update' ) do |el |
133+ a = el . attributes
134+ r = { 'name' => a [ 'name' ] }
135+ r [ 'version' ] = a [ 'edition' ] unless a [ 'arch' ] . nil?
136+ r [ 'arch' ] = a [ 'arch' ] unless a [ 'arch' ] . nil?
137+ r [ 'category' ] = a [ 'category' ] unless a [ 'category' ] . nil?
138+ r [ 'severity' ] = a [ 'severity' ] unless a [ 'severity' ] . nil?
139+ res . push ( r )
140+ end
141+ res
142+ end
143+ end
144+
85145class UbuntuUpdateFetcher < UpdateFetcher
86146 def packages
87147 ubuntu_packages = ubuntu_base + <<-EOH
0 commit comments