-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
executable file
·232 lines (211 loc) · 5.96 KB
/
main.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash
# ---
# Script to FreeUP Disk Space on Linux Systems
# Author: Enderson Menezes
# Date: 2024-02-16
# Inspired by: https://github.com/jlumbroso/free-disk-space
# ---
# Variables
# PRINCIPAL_DIR: String
# TESTING: Boolean (true or false)
# ANDROID_FILES: Boolean (true or false)
# DOTNET_FILES: Boolean (true or false)
# HASKELL_FILES: Boolean (true or false)
# TOOL_CACHE: Boolean (true or false)
# SWAP_STORAGE: Boolean (true or false)
# PACKAGES: String (separated by space)
# REMOVE_ONE_COMMAND: Boolean (true or false)
# REMOVE_FOLDERS: String (separated by space)
# Environment Variables
# AGENT_TOOLSDIRECTORY: String
# Validate Variables
if [[ -z "${PRINCIPAL_DIR}" ]]; then
echo "Variable PRINCIPAL_DIR is not set"
exit 0
fi
if [[ -z "${TESTING}" ]]; then
TESTING="false"
fi
if [[ ${TESTING} == "true" ]]; then
echo "Testing Mode"
alias rm='echo rm'
fi
if [[ -z "${ANDROID_FILES}" ]]; then
echo "Variable ANDROID_FILES is not set"
exit 0
fi
if [[ -z "${DOTNET_FILES}" ]]; then
echo "Variable DOTNET_FILES is not set"
exit 0
fi
if [[ -z "${HASKELL_FILES}" ]]; then
echo "Variable HASKELL_FILES is not set"
exit 0
fi
if [[ -z "${TOOL_CACHE}" ]]; then
echo "Variable TOOL_CACHE is not set"
exit 0
fi
if [[ -z "${SWAP_STORAGE}" ]]; then
echo "Variable SWAP_STORAGE is not set"
exit 0
fi
if [[ -z "${PACKAGES}" ]]; then
echo "Variable PACKAGES is not set"
exit 0
fi
if [[ ${PACKAGES} != "false" ]]; then
if [[ ${PACKAGES} != *" "* ]]; then
echo "Variable PACKAGES is not a list of strings"
exit 0
fi
fi
if [[ -z "${REMOVE_ONE_COMMAND}" ]]; then
echo "Variable REMOVE_ONE_COMMAND is not set"
exit 0
fi
if [[ -z "${REMOVE_FOLDERS}" ]]; then
echo "Variable REMOVE_FOLDERS is not set"
exit 0
fi
if [[ -z "${AGENT_TOOLSDIRECTORY}" ]]; then
echo "Variable AGENT_TOOLSDIRECTORY is not set"
exit 0
fi
# Global Variables
TOTAL_FREE_SPACE=0
# Verify Needed Packages
# Verify BC
COMMAND_BC=$(command -v bc)
if ! [[ -x "${COMMAND_BC}" ]]; then
echo 'Error: bc is not installed.' >&2
exit 1
fi
# Functions
function verify_free_disk_space(){
FREE_SPACE_TMP=$(df -B1 "${PRINCIPAL_DIR}")
echo "${FREE_SPACE_TMP}" | awk 'NR==2 {print $4}'
}
function convert_bytes_to_mb(){
echo "scale=2; $1 / 1024 / 1024" | bc
}
function verify_free_space_in_mb(){
DATA_TO_CONVERT=$(verify_free_disk_space)
convert_bytes_to_mb "${DATA_TO_CONVERT}"
}
function update_and_echo_free_space(){
IS_AFTER_OR_BEFORE=$1
if [[ "${IS_AFTER_OR_BEFORE}" == "before" ]]; then
SPACE_BEFORE=$(verify_free_space_in_mb)
LINUX_TIMESTAMP_BEFORE=$(date +%s)
else
SPACE_AFTER=$(verify_free_space_in_mb)
LINUX_TIMESTAMP_AFTER=$(date +%s)
FREEUP_SPACE=$(echo "scale=2; ${SPACE_AFTER} - ${SPACE_BEFORE}" | bc)
echo "FreeUp Space: ${FREEUP_SPACE} MB"
echo "Time Elapsed: $((LINUX_TIMESTAMP_AFTER - LINUX_TIMESTAMP_BEFORE)) seconds"
TOTAL_FREE_SPACE=$(echo "scale=2; ${TOTAL_FREE_SPACE} + ${FREEUP_SPACE}" | bc)
fi
}
function remove_android_library_folder(){
echo "🤖 Removing Android Folder"
update_and_echo_free_space "before"
sudo rm -rf /usr/local/lib/android || true
update_and_echo_free_space "after"
echo "-"
}
function remove_dot_net_library_folder(){
echo "📄 Removing .NET Folder"
update_and_echo_free_space "before"
sudo rm -rf /usr/share/dotnet || true
update_and_echo_free_space "after"
echo "-"
}
function remove_haskell_library_folder(){
echo "📄 Removing Haskell Folder"
update_and_echo_free_space "before"
sudo rm -rf /opt/ghc || true
sudo rm -rf /usr/local/.ghcup || true
update_and_echo_free_space "after"
echo "-"
}
function remove_package(){
PACKAGE_NAME=$1
echo "📦 Removing ${PACKAGE_NAME}"
update_and_echo_free_space "before"
sudo apt-get remove -y "${PACKAGE_NAME}" --fix-missing > /dev/null
sudo apt-get autoremove -y > /dev/null
sudo apt-get clean > /dev/null
update_and_echo_free_space "after"
echo "-"
}
function remove_multi_packages_one_command(){
PACKAGES_TO_REMOVE=$1
MOUNT_COMMAND="sudo apt-get remove -y"
for PACKAGE in ${PACKAGES_TO_REMOVE}; do
MOUNT_COMMAND+=" ${PACKAGE}"
done
echo "📦 Removing ${PACKAGES_TO_REMOVE}"
update_and_echo_free_space "before"
${MOUNT_COMMAND} --fix-missing > /dev/null
sudo apt-get autoremove -y > /dev/null
sudo apt-get clean > /dev/null
update_and_echo_free_space "after"
echo "-"
}
function remove_tool_cache(){
echo "🧹 Removing Tool Cache"
update_and_echo_free_space "before"
sudo rm -rf "${AGENT_TOOLSDIRECTORY}" || true
update_and_echo_free_space "after"
echo "-"
}
function remove_swap_storage(){
# eye emoji see swap
echo "👁️🗨️ See Swap"
free -h
echo "🧹 Removing Swap Storage"
sudo swapoff -a || true
sudo rm -f "/mnt/swapfile" || true
echo "🧹 Removed Swap Storage"
free -h
echo "-"
}
function remove_folder(){
FOLDER=$1
echo "📁 Removing ${FOLDER}"
update_and_echo_free_space "before"
sudo rm -rf "${FOLDER}" || true
update_and_echo_free_space "after"
}
# Remove Libraries
if [[ ${ANDROID_FILES} == "true" ]]; then
remove_android_library_folder
fi
if [[ ${DOTNET_FILES} == "true" ]]; then
remove_dot_net_library_folder
fi
if [[ ${HASKELL_FILES} == "true" ]]; then
remove_haskell_library_folder
fi
if [[ ${PACKAGES} != "false" ]]; then
if [[ ${REMOVE_ONE_COMMAND} == "true" ]]; then
remove_multi_packages_one_command "${PACKAGES}"
else
for PACKAGE in ${PACKAGES}; do
remove_package "${PACKAGE}"
done
fi
fi
if [[ ${TOOL_CACHE} == "true" ]]; then
remove_tool_cache
fi
if [[ ${SWAP_STORAGE} == "true" ]]; then
remove_swap_storage
fi
if [[ ${REMOVE_FOLDERS} != "false" ]]; then
for FOLDER in ${REMOVE_FOLDERS}; do
remove_folder "${FOLDER}"
done
fi
echo "Total Free Space: ${TOTAL_FREE_SPACE} MB"