Skip to content

Instantly share code, notes, and snippets.

@unixorn
Created October 3, 2017 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unixorn/7ce207d90a78a6129ca4953b9bf48990 to your computer and use it in GitHub Desktop.
Save unixorn/7ce207d90a78a6129ca4953b9bf48990 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Clean up an instance so packer can bake an AMI from it.
#
# Copyright 2017, Jiff Inc
# License: Apache 2.0
set -o pipefail
if [ ! -z "${DEBUG}" ]; then
echo '**********'
echo 'Yum repositories:'
sudo yum repolist
fi
cleanup_var_log() {
if [[ -f /tmp/zerofile ]]; then
chmod 755 /tmp/zerofile
else
echo 'zerofile was not installed to /tmp by packer!'
exit 1
fi
echo 'Cleaning up /var/log'
if [[ -n "$DEBUG" ]]; then
echo 'Pre-clean:'
sudo find /var/log -type f -exec ls -la '{}' ';'
fi
echo 'Zeroing log files'
sudo find /var/log -type f -exec /tmp/zerofile '{}' ';'
if [[ -n "$DEBUG" ]]; then
echo 'Post-clean:'
sudo find /var/log -type f -exec ls -la '{}' ';'
fi
}
cleanup_shell_histories() {
echo 'Cleaning shell history files'
# Bash
sudo find /home -name .bash_history -exec rm -fv '{}' ';'
# ZSH
sudo find /home -name .zsh_history -exec rm -fv '{}' ';'
# And for root
sudo rm -fv ~root/.zsh_history ~root/.bash_history
}
cleanup_yum_cruft() {
sudo yum clean all
}
spew_header() {
echo
echo 'AMI root filesystem size'
df -h /
echo
if [[ -f /etc/centos-release ]]; then
echo "AMI CentOS version: $(cat /etc/centos-release)"
else
echo '/etc/centos-release is missing!'
fi
}
clean_random_cruft() {
echo 'Purging .rpmnew files'
sudo find / -name '*.rpmnew' -exec rm -fv '{}' ';'
echo
echo 'Zapping packer-chef-client files'
sudo rm -fr /tmp/packer-chef-client
echo
}
clean_slash_tmp() {
echo 'Purging /tmp'
sudo rm -frv /tmp/*
echo
echo '/tmp:'
sudo ls -la /tmp
}
spew_header
cleanup_shell_histories
cleanup_yum_cruft
cleanup_var_log
clean_random_cruft
clean_slash_tmp
#!/bin/bash
#
# reset a file to zero length
if [[ -f $1 ]]; then
exec cat /dev/null > $1
else
echo "$1 is not a file"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment