1+ #! /usr/bin/env bash
2+
3+ set -o errexit # Exit script when a command exits with non-zero status
4+ set -o errtrace # Exit on error inside any functions or sub-shells
5+ set -o nounset # Exit script on use of an undefined variable
6+ set -o pipefail # Return exit status of the last command in the pipe that failed
7+
8+ readonly EX_OK=0 # Successful termination
9+ readonly EX_UNKNOWN=1 # Unknown error occured
10+
11+ declare CLEANUP_LOG_FILES=true
12+ declare CLEANUP_TMP_FOLDERS=true
13+ declare CLEANUP_APT=true
14+ declare CLEANUP_PIP=true
15+
16+ # ------------------------------------------------------------------------------
17+ # Displays a error message and is able to terminate te script execution
18+ #
19+ # Arguments:
20+ # $1 Error message
21+ # $2 Exit code, script will continue execution when omitted
22+ # Returns:
23+ # None
24+ # ------------------------------------------------------------------------------
25+ display_error_message () {
26+ local status=${1}
27+ local exitcode=${2:- 0}
28+
29+ echo >&2
30+ echo " ! ERROR: ${status} "
31+ echo >&2
32+
33+ if [[ ${exitcode} -ne 0 ]]; then
34+ exit " ${exitcode} "
35+ fi
36+ }
37+
38+ # ------------------------------------------------------------------------------
39+ # Displays a notice
40+ #
41+ # Arguments:
42+ # $* Notice message to display
43+ # Returns:
44+ # Exit code
45+ # ------------------------------------------------------------------------------
46+ display_notice_message () {
47+ local status=$*
48+
49+ echo
50+ echo " NOTICE: ${status} "
51+ echo
52+ }
53+
54+ # ------------------------------------------------------------------------------
55+ # Displays a status message
56+ #
57+ # Arguments:
58+ # $* Status message to display
59+ # Returns:
60+ # Exit code
61+ # ------------------------------------------------------------------------------
62+ display_status_message () {
63+ local status=$*
64+
65+ echo " -----> ${status} "
66+ }
67+
68+ # ------------------------------------------------------------------------------
69+ # Docker build the image
70+ #
71+ # Arguments:
72+ # None
73+ # Returns:
74+ # Exit code
75+ # ------------------------------------------------------------------------------
76+ docker_clean () {
77+ display_status_message " Running Docker clean"
78+
79+ cd /
80+
81+ if [[ " ${CLEANUP_LOG_FILES} " = true ]]; then
82+ find /var/log -type f -print0 | xargs -0 truncate -s0
83+ fi
84+
85+ if [[ " ${CLEANUP_APT} " = true ]]; then
86+ apt-get autoremove --purge --yes --quiet;
87+ apt-get clean -y
88+ fi
89+
90+ if [[ " ${CLEANUP_PIP} " = true && -d " ${HOME} /.cache/pip" ]]; then
91+ rm -rf " ${HOME} /.cache/pip"
92+ fi
93+
94+ if [[ " ${CLEANUP_TMP_FOLDERS} " = true ]]; then
95+ find /tmp/ -mindepth 1 -maxdepth 1 -exec rm -rf {} +
96+ find /var/tmp/ -mindepth 1 -maxdepth 1 -exec rm -rf {} +
97+ fi
98+
99+ rm -rf /var/lib/apt/lists/*
100+ rm -rf /usr/share/doc/*
101+ rm -rf /usr/share/groff/*
102+ rm -rf /usr/share/info/*
103+ rm -rf /usr/share/linda/*
104+ rm -rf /usr/share/lintian/*
105+ rm -rf /usr/share/man/* /*
106+
107+ display_status_message ' Docker clean finished'
108+
109+ return " ${EX_OK} "
110+ }
111+
112+ display_help () {
113+ local exit_code=${1:- ${EX_OK} }
114+ local status=${2:- }
115+
116+ [[ -n " ${status} " ]] && display_error_message " ${status} "
117+
118+ cat << EOF
119+ Usage: /usr/local/bin/docker-layer-clean [options]
120+ Options:
121+ -h, Display this help and exit.
122+ -l, Do not truncate logfiles
123+ -p, Do not remove *-dev packages
124+ -a, Do not cleanup apt
125+ -t, Do not cleanup tmp folders
126+ -p, Do not cleanup python pip cache
127+ EOF
128+
129+ exit " ${exit_code} "
130+ }
131+
132+ parse_cli_arguments () {
133+ local OPTIND o
134+ while getopts " :hlpat" o; do
135+ case " ${o} " in
136+ l)
137+ CLEANUP_LOG_FILES=false
138+ ;;
139+ t)
140+ CLEANUP_TMP_FOLDERS=false
141+ ;;
142+ a)
143+ CLEANUP_APT=false
144+ ;;
145+ p)
146+ CLEANUP_PIP=false
147+ ;;
148+ h)
149+ display_help " ${EX_OK} "
150+ ;;
151+ :)
152+ display_help " ${EX_UNKNOWN} " " Option -${OPTARG} requires an argument" ;
153+ ;;
154+ \? )
155+ display_help " ${EX_UNKNOWN} " " Invalid option: -${OPTARG} " ;
156+ ;;
157+ * )
158+ display_help " ${EX_UNKNOWN} " " Missing required arguments." ;
159+ ;;
160+ esac
161+ done
162+ shift $(( OPTIND- 1 ))
163+ }
164+
165+ # ==============================================================================
166+ # RUN LOGIC
167+ # ------------------------------------------------------------------------------
168+ main () {
169+ # Parse input
170+ parse_cli_arguments " $@ "
171+ docker_clean
172+ exit " ${EX_OK} "
173+ }
174+
175+ # Bootstrap
176+ if [[ " ${BASH_SOURCE[0]} " = " ${0} " ]]; then
177+ # Direct call to file
178+ main " $@ "
179+ fi # Else file is included from another script
0 commit comments