Skip to content

Commit

Permalink
Update check_apt_outdated to use custom_insync
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidS committed Apr 21, 2021
1 parent 9a6bba0 commit a7f60f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
21 changes: 12 additions & 9 deletions lib/puppet/provider/check_apt_outdated/check_apt_outdated.rb
Expand Up @@ -9,27 +9,30 @@ def get(_context)
end

def set(context, changes)
params = changes.first[:should]
end

def insync?(context, name, attribute_name, is_hash, should_hash)
context.debug("Checking whether #{attribute_name} is up-to-date")
pkgcache_stat = File::Stat.new('/var/cache/apt/pkgcache.bin')

# Time differences come back in seconds
pkgcache_age_days = (Time.new - pkgcache_stat.mtime) / 60 / 60 / 24
if pkgcache_age_days > params[:allowed_pkgcache_age_days] # rubocop:disable Style/GuardClause
raise Puppet::Error, "/var/cache/apt/pkgcache.bin has been updated last #{pkgcache_age_days} days ago (expected no more than #{params[:allowed_pkgcache_age_days]} days)."
if pkgcache_age_days > should_hash[:allowed_pkgcache_age_days] # rubocop:disable Style/GuardClause
raise Puppet::Error, "/var/cache/apt/pkgcache.bin has been updated last #{pkgcache_age_days} days ago (expected allowed_pkgcache_age_days no more than #{should_hash[:allowed_pkgcache_age_days]} days)."
else
context.info("/var/cache/apt/pkgcache.bin has been updated within configured range of #{params[:allowed_pkgcache_age_days]} days (last update is #{pkgcache_age_days} days ago).")
context.info("/var/cache/apt/pkgcache.bin has been updated within configured range of #{should_hash[:allowed_pkgcache_age_days]} days (last update is #{pkgcache_age_days} days ago).")
end

apt_list_stats_by_mtime = Dir['/var/lib/apt/lists/*'].map { |f| File::Stat.new(f) }.select { |f| f.file? }.group_by { |f| f.mtime }
apt_list_stats_by_mtime = Dir['/var/lib/apt/lists/*'].map { |f| OpenStruct.new(file: f, stat: File::Stat.new(f)) }.select { |f| f.stat.file? }.group_by { |f| f.stat.mtime }
apt_list_oldest_mtime = apt_list_stats_by_mtime.keys.min
oldest_apt_list = apt_list_stats_by_mtime[apt_list_oldest_mtime].join(', ')
oldest_apt_list_names = apt_list_stats_by_mtime[apt_list_oldest_mtime].map { |f| f.file }.join(', ')

# Time differences come back in seconds
mirror_age_days = (Time.new - apt_list_oldest_mtime) / 60 / 60 / 24
if mirror_age_days > params[:allowed_mirror_age_days] # rubocop:disable Style/GuardClause
raise Puppet::Error, "upstream mirror has been updated last #{mirror_age_days} days ago (expected no more than #{params[:allowed_mirror_age_days]}; files: #{oldest_apt_list})."
if mirror_age_days > should_hash[:allowed_mirror_age_days] # rubocop:disable Style/GuardClause
raise Puppet::Error, "upstream mirror has been updated last #{mirror_age_days} days ago (expected allowed_mirror_age_days no more than #{should_hash[:allowed_mirror_age_days]}; files: #{oldest_apt_list_names})."
else
context.info("upstream mirrors have all been updated within configured range of #{params[:allowed_mirror_age_days]} days (oldest update is #{mirror_age_days} days ago).")
context.info("upstream mirrors have all been updated within configured range of #{should_hash[:allowed_mirror_age_days]} days (oldest update is #{mirror_age_days} days ago).")
end
end
end
19 changes: 12 additions & 7 deletions lib/puppet/type/check_apt_outdated.rb
Expand Up @@ -12,15 +12,15 @@
allowed_mirror_age_days => 7,
}
EOS
features: [],
features: ['custom_insync'],
attributes: {
name: {
type: 'StringPattern[/^apt$/]',
type: 'Enum[apt]',
desc: 'Always has to be "apt".',
behaviour: :namevar,
},
allowed_pkgcache_age_days: {
type: 'Number',
type: 'Numeric',
desc: <<~DESC,
How old the apt package cache may be in days, before sounding an alarm.
Expand All @@ -29,10 +29,10 @@
> Note that `apt update` ignores most download errors when rebuilding this file.
DESC
behaviour: :parameter,
defaultto: '1.1',
default: 1.1,
},
allowed_mirror_age_days: {
type: 'Number',
type: 'Numeric',
desc: <<~DESC,
How old the apt repo metadata may be in days, before sounding an alarm.
Expand All @@ -41,7 +41,12 @@
> Note that `apt update` uses the mirror\'s last modified timestamp on these files.',
DESC
behaviour: :parameter,
defaultto: '1.1',
}
default: 1.1,
},
force_sync: {
type: 'Enum[ignored]',
desc: 'This property is required to force a check, and is otherwise ignored.',
default: 'ignored',
},
},
)

0 comments on commit a7f60f2

Please sign in to comment.