-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdyshell
More file actions
executable file
·100 lines (77 loc) · 2.74 KB
/
dyshell
File metadata and controls
executable file
·100 lines (77 loc) · 2.74 KB
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
#!/bin/bash
BASENAME="dyshell"
preferred_name=$BASENAME":latest"
if [ "$#" -eq 1 ]; then
if [ "$1" = "-h" ]; then
echo "USAGE:"
echo " '-b' to build new docker (optional)"
echo " '-c <command>' to run command (optional)"
echo " '-s' to execute docker in root (optional)"
exit 0
fi
fi
system_cleanup() {
echo "Disk usage before cleanup"
df -h
echo -e "Removing unnecessary packages"
sudo apt-get autoremove -y >/dev/null 2>&1
sudo apt-get autoclean -y >/dev/null 2>&1
echo -e "Cleaning package cache"
sudo apt-get clean >/dev/null 2>&1
echo -e "Removing orphaned packages"
sudo deborphan | xargs -r sudo apt-get -y remove >/dev/null 2>&1
echo -e "Cleaning thumbnail cache"
rm -rf ~/.cache/thumbnails/*
echo -e "Cleaning system temporary files"
sudo rm -rf /tmp/* /var/tmp/*
echo -e "Cleaning journal logs (optional, keep last 2 weeks)"
sudo journalctl --vacuum-time=2weeks
echo -e "Cleaning old log files"
sudo find /var/log -type f -name "*.log" -exec truncate -s 0 {} \;
echo -e "Disk usage after cleanup"
df -h
echo -e "\nCleanup completed safely!"
}
system_cleanup
# Default values:
build_docker=false
is_sudo=false
command=""
# It's the : after d that signifies that it takes an option argument.
while getopts bsc: opt; do
case $opt in
b) build_docker=true ;;
s) is_sudo=true ;;
c) command=$OPTARG ;;
*) echo 'error in command line parsing' >&2
exit 1
esac
done
shift "$(( OPTIND - 1 ))"
"$build_docker" && echo 'Got the -b option'
"$is_sudo" && echo 'Got the -s option'
printf 'Option -c: %s\n' "$command"
if [ "$build_docker" = "true" ]; then
docker build -t $preferred_name .
fi
docker images | grep $BASENAME
if [ "$?" -ne 0 ]; then
echo "it means there is no built docker, cannot proceed"
exit 1
fi
# here, we probably have the docker, then run it
if [ "$command" != "" ]; then
echo "command running"
if [ "$is_sudo" = "true" ]; then
docker run --rm -t --net=host -v $(pwd):/workspace --entrypoint=/bin/bash $preferred_name -c "$command"
else
docker run --rm -t --net=host -v $(pwd):/workspace -v /etc/passwd:/etc/passwd:ro -v /etc/group:/etc/group:ro -u $(id -u ${USER}):$(id -g ${USER}) --entrypoint=/bin/bash $preferred_name -c "$command"
fi
exit 0
fi
# if no command run, lets start the docker for personal use
if [ "$is_sudo" = "true" ]; then
docker run --rm -it --net=host -v $(pwd):/workspace --entrypoint=/bin/bash $preferred_name
else
docker run --rm -it --net=host -v $(pwd):/workspace -v /etc/passwd:/etc/passwd:ro -v /etc/group:/etc/group:ro -u $(id -u ${USER}):$(id -g ${USER}) --entrypoint=/bin/bash $preferred_name
fi