Last active
November 3, 2016 13:46
-
-
Save rockpapergoat/5102035 to your computer and use it in GitHub Desktop.
script to provide some kernel purging routines for ubuntu/debian. this is a first pass at a maintenance script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# 130306, nate | |
# automate purging old kernels from /boot and updating/upgrading | |
require 'optparse' | |
def get_installed_kernels | |
installed = %x(/usr/bin/dpkg --get-selections).split("\n").map! {|e| e.sub!(/\t+install/,"")} | |
kernels = installed.select {|pkg| pkg =~ %r[linux-image]} | |
end | |
def purge_old_kernels(kernels,cutoff) | |
kernels.sort![1..-2] | |
#kernels.pop | |
kernels[1..-cutoff].each do |kernel| | |
puts "uninstalling #{kernel}...\n\n" | |
%x(/usr/bin/dpkg --purge #{kernel}) | |
end | |
end | |
def apt_fix_broken | |
%x(/usr/bin/apt-get -yf install) | |
end | |
def apt_autoremove | |
%x(/usr/bin/apt-get -y autoremove) | |
end | |
def apt_update_upgrade | |
%x(/usr/bin/apt-get -y update && /usr/bin/apt-get -y upgrade) | |
end | |
options = {} | |
optparse = OptionParser.new do |opts| | |
opts.on( '-h', '--help', 'Display this screen' ) do | |
opts.separator "" | |
opts.separator "Order should be: #{$0} -k VERSION -faU" | |
opts.separator "" | |
puts opts | |
exit | |
end | |
opts.on( '-A', '--all', "Perform all operations in sequence" ) do | |
options[:kernels] = 4 | |
purge_old_kernels(get_installed_kernels,options[:kernels]) | |
apt_fix_broken | |
apt_autoremove | |
apt_update_upgrade | |
end | |
opts.on( '-a', '--autoremove', "Run apt-get autoremove" ) do | |
options[:simple] = true | |
apt_autoremove | |
end | |
opts.on( '-f', '--fix-broken', "Run apt-get -f install" ) do | |
options[:simple] = true | |
apt_fix_broken | |
end | |
options[:opt] = false | |
opts.on( '-k', '--kernel [KERNELS_TO_KEEP]', Integer, "List the number of kernels to keep. Example: -k 4" ) do |k| | |
options[:opt] = k || 4 | |
purge_old_kernels(get_installed_kernels,options[:opt]) | |
end | |
opts.on( '-l', '--list-kernels', "Lists currently installed kernels" ) do | |
options[:simple] = true | |
get_installed_kernels.each {|k| puts k} | |
end | |
opts.on( '-n', '--noop', "Just show passed options and quit" ) do | |
options[:simple] = true | |
p options | |
exit | |
end | |
opts.on( '-U', '--update-upgrade', "Run apt-get update && apt-get upgrade" ) do | |
options[:simple] = true | |
puts "upgrading now..." | |
apt_update_upgrade | |
end | |
# opts.on( '-R', '--reboot', "Reboot this host upon completion of updates" ) do | |
# options[:simple] = true | |
# puts "rebooting now..." | |
# end | |
end | |
optparse.parse! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment