Skip to content

Instantly share code, notes, and snippets.

@chrisxaustin
Created April 20, 2018 16:10
Show Gist options
  • Save chrisxaustin/b7996f1ade2318c9b7fcc24afd8ca5c2 to your computer and use it in GitHub Desktop.
Save chrisxaustin/b7996f1ade2318c9b7fcc24afd8ca5c2 to your computer and use it in GitHub Desktop.
Salt module to clean out /boot and call pkg.autoremove
import logging
import os
from glob import glob
from distutils.version import LooseVersion
log = logging.getLogger(__name__)
def __virtual__():
'''
Restrict this to Ubuntu minions
'''
if __grains__['os_family'] != 'Debian':
return False
if not __grains__['kernelrelease']:
return False
return True
def remove():
'''
Remove unnecessary linux headers from /boot
'''
osv = __grains__['kernelrelease']
if not osv:
return False
if not os.path.exists('/boot'):
raise SaltInvocationError('/boot does not exist')
# Files to remove:
# /boot/config-4.4.0-81-generic
output = {}
output['tasks'] = {}
files = []
for base_name in ['abi', 'config', 'initrd.img', 'vmlinuz', 'System.map']:
batch = []
base_path = '/boot/'+base_name+"-"
keep = base_path + osv
if not os.path.exists(keep):
continue
for f in glob(base_path + '*'):
if f != keep:
batch.append(f)
if len(batch) > 2:
# Sort by the version number, then keep the last two
batch.sort(key=LooseVersion)
batch.pop()
batch.pop()
files.extend(batch)
for file in files:
os.remove(file)
output['removed'] = files
output['tasks']['autoremove'] = __salt__['pkg.autoremove'](purge=True)
output['tasks']['apt-get -f -y --force-yes install'] = __salt__['cmd.run']('apt-get -f -y --force-yes install')
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment