Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tasks/clean_cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input_method": "stdin",
"puppet_task_version": 1,
"supports_noop": false,
"description": "Clean patch caches (yum/dpkg) via a task"
}
79 changes: 79 additions & 0 deletions tasks/clean_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/opt/puppetlabs/puppet/bin/ruby

require 'rbconfig'
is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
if is_windows
puts 'Cannot run os_patching::clean_cache on Windows'
exit 1
end

require 'open3'
require 'json'
require 'syslog/logger'
require 'time'
require 'timeout'

$stdout.sync = true

log = Syslog::Logger.new 'os_patching'
starttime = Time.now.iso8601
BUFFER_SIZE = 4096

# Default output function
def output(returncode, message, debug, starttime)
endtime = Time.now.iso8601
json = {
:return => returncode,
:message => message,
:debug => debug,
:start_time => starttime,
:end_time => endtime,
}
puts JSON.pretty_generate(json)
end

# Error output function
def err(code, kind, message, starttime)
endtime = Time.now.iso8601
exitcode = code.to_s.split.last
json = {
:_error =>
{
:msg => "Task exited : #{exitcode}\n#{message}",
:kind => kind,
:details => { :exitcode => exitcode },
:start_time => starttime,
:end_time => endtime,
},
}

puts JSON.pretty_generate(json)
shortmsg = message.split("\n").first.chomp
history(starttime, shortmsg, exitcode, '', '', '')
syslog = Syslog::Logger.new 'os_patching'
syslog.error "ERROR : #{kind} : #{exitcode} : #{message}"
exit(exitcode.to_i)
end

# Cache the facts
log.debug 'Gathering facts'
full_facts, stderr, status = Open3.capture3('/opt/puppetlabs/puppet/bin/puppet', 'facts')
err(status, 'os_patching/facter', stderr, starttime) if status != 0
facts = JSON.parse(full_facts)

# Check we are on a supported platform
unless facts['values']['os']['family'] == 'RedHat' || facts['values']['os']['family'] == 'Debian'
err(200, 'os_patching/unsupported_os', 'Unsupported OS', starttime)
end

clean_cache = if facts['values']['os']['family'] == 'RedHat'
'yum clean all'
elsif facts['values']['os']['family'] == 'Debian'
'dpkg clean'
end

# Clean that cache!
clean_out, stderr, status = Open3.capture3(clean_cache)
err(status, 'os_patching/clean_cache', stderr, starttime) if status != 0
output(status,'Cache cleaned',clean_out,starttime)
log.info 'Cache cleaned'
4 changes: 4 additions & 0 deletions tasks/patch_server.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"security_only": {
"description": "Limit patches to those tagged as security related? (Defaults to false)",
"type": "Optional[Boolean]"
},
"clean_cache": {
"description": "Should the yum/dpkg caches be cleaned at the start of the task? (Defaults to false)",
"type": "Optional[Boolean]"
}
},
"puppet_task_version": 1,
Expand Down
33 changes: 28 additions & 5 deletions tasks/patch_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

$stdout.sync = true

facter = 'facter'
fact_generation = '/usr/local/bin/os_patching_fact_generation.sh'

log = Syslog::Logger.new 'os_patching'
Expand Down Expand Up @@ -158,7 +157,8 @@ def reboot_required(family, release, reboot)
begin
raw = STDIN.read
params = JSON.parse(raw)
rescue JSON::ParserError => e
#rescue JSON::ParserError => e
rescue JSON::ParserError
err(400,"os_patching/input", "Invalid JSON received: '#{raw}'", starttime)
end

Expand All @@ -173,15 +173,38 @@ def reboot_required(family, release, reboot)
starttime,
)
end
_fact_out, stderr, status = Open3.capture3(fact_generation)
err(status, 'os_patching/fact_refresh', stderr, starttime) if status != 0

# Cache the facts
log.debug 'Gathering facts'
full_facts, stderr, status = Open3.capture3('/opt/puppetlabs/puppet/bin/puppet', 'facts')

err(status, 'os_patching/facter', stderr, starttime) if status != 0
facts = JSON.parse(full_facts)

# Check we are on a supported platform
unless facts['values']['os']['family'] == 'RedHat' || facts['values']['os']['family'] == 'Debian'
err(200, 'os_patching/unsupported_os', 'Unsupported OS', starttime)
end

# Get the pinned packages
pinned_pkgs = facts['values']['os_patching']['pinned_packages']

# Should we clean the cache prior to starting?
if params['clean_cache'] && params['clean_cache'] == true
clean_cache = if facts['values']['os']['family'] == 'RedHat'
'yum clean all'
elsif facts['values']['os']['family'] == 'Debian'
'dpkg clean'
end
_fact_out, stderr, status = Open3.capture3(clean_cache)
err(status, 'os_patching/clean_cache', stderr, starttime) if status != 0
log.info 'Cache cleaned'
end

# Refresh the patching fact cache
_fact_out, stderr, status = Open3.capture3(fact_generation)
err(status, 'os_patching/fact_refresh', stderr, starttime) if status != 0


# Let's figure out the reboot gordian knot
#
# If the override is set, it doesn't matter that anything else is set to at this point
Expand Down