Skip to content

Commit 59700a2

Browse files
committed
Updated to use a while loop for releasing "apt update" lock
The previous implementation waited for the lock to release, this version retries until a successful exit. The 'ubuntu' names were replaced with 'debian' to reflect the correct origins. Signed-off-by: Matt Ray <github@mattray.dev>
1 parent 03eb6c6 commit 59700a2

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This [InSpec](http://inspec.io/) profile verifies that all updates have been ins
88

99
- RHEL 6/7
1010
- CentOS 6/7
11+
- Debian 8/9/10
1112
- Ubuntu 12.04+
1213
- OpenSUSE, SuSE 11/12
1314

libraries/linux_updates.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class LinuxUpdateManager < Inspec.resource(1)
1818
# if inspec.os.redhat?
1919
# @update_mgmt = RHELUpdateFetcher.new(inspec)
2020
# elsif inspec.os.debian?
21-
# @update_mgmt = UbuntuUpdateFetcher.new(inspec)
21+
# @update_mgmt = DebianUpdateFetcher.new(inspec)
2222
# end
2323
# return skip_resource 'The `linux_update` resource is not supported on your OS.' if @update_mgmt.nil?
2424
# end
@@ -29,7 +29,7 @@ def initialize
2929
when 'redhat', 'amazon'
3030
@update_mgmt = RHELUpdateFetcher.new(inspec)
3131
when 'debian'
32-
@update_mgmt = UbuntuUpdateFetcher.new(inspec)
32+
@update_mgmt = DebianUpdateFetcher.new(inspec)
3333
when 'suse'
3434
@update_mgmt = SuseUpdateFetcher.new(inspec)
3535
end
@@ -156,36 +156,40 @@ def extract_xml_updates(updates_el)
156156
end
157157
end
158158

159-
class UbuntuUpdateFetcher < UpdateFetcher
159+
class DebianUpdateFetcher < UpdateFetcher
160160
def packages
161-
ubuntu_packages = ubuntu_base + <<-PRINT_JSON
161+
debian_packages = debian_base + <<-PRINT_JSON
162162
echo -n '{"installed":['
163163
dpkg-query -W -f='${Status}\\t${Package}\\t${Version}\\t${Architecture}\\n' |\\
164164
grep '^install ok installed\\s' |\\
165165
awk '{ printf "{\\"name\\":\\""$4"\\",\\"version\\":\\""$5"\\",\\"arch\\":\\""$6"\\"}," }' | rev | cut -c 2- | rev | tr -d '\\n'
166166
echo -n ']}'
167167
PRINT_JSON
168-
parse_json(ubuntu_packages)
168+
parse_json(debian_packages)
169169
end
170170

171171
def updates
172-
ubuntu_updates = ubuntu_base + <<-PRINT_JSON
172+
debian_updates = debian_base + <<-PRINT_JSON
173173
echo -n '{"available":['
174-
DEBIAN_FRONTEND=noninteractive apt-get upgrade --dry-run | grep Inst | tr -d '[]()' |\\
174+
DEBIAN_FRONTEND=noninteractive apt upgrade --dry-run | grep Inst | tr -d '[]()' |\\
175175
awk '{ printf "{\\"name\\":\\""$2"\\",\\"version\\":\\""$4"\\",\\"repo\\":\\""$5"\\",\\"arch\\":\\""$6"\\"}," }' | rev | cut -c 2- | rev | tr -d '\\n'
176176
echo -n ']}'
177177
PRINT_JSON
178-
parse_json(ubuntu_updates)
178+
parse_json(debian_updates)
179179
end
180180

181181
private
182182

183-
def ubuntu_base
183+
def debian_base
184184
base = <<-PRINT_JSON
185185
#!/bin/sh
186-
DEBIAN_FRONTEND=noninteractive apt-get update >/dev/null 2>&1
187-
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$'; }
188-
while test -n "$(readlock)"; do sleep 1; done
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
189193
echo " "
190194
PRINT_JSON
191195
base

0 commit comments

Comments
 (0)