-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup_system.py
127 lines (103 loc) · 4.21 KB
/
cleanup_system.py
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
#! /usr/bin/env python3
#
# cross-paltform script for cleaning up machines.
#
# on POSIX-machines this script will:
# - remove old packages
# - cleanup package cache
# - on systemd-hosts: remove journals older then 7 days
# - remove all files inside /tmp/
import argparse
import os
import platform
import shutil
import subprocess
import sys
from pathlib import Path
def writeOutput(str, silent):
"""
A simple wrapper around print(), which can be used for silence outputs
"""
if not silent:
print(str)
def main():
parser = argparse.ArgumentParser()
parser.add_argument
parser.add_argument(
"--trmm",
help="running in tactical rmm compability mode",
action="store_true"
)
parser.add_argument(
"--silence",
help="silencing output",
default=False,
action="store_true"
)
args = parser.parse_args()
if args.trmm:
writeOutput("running script in TRMM copatibility mode", args.silence)
match platform.system():
case 'Windows':
writeOutput("cleanup script is running on MS Windows", args.silence)
# @ToDo:
# - remove old eventLogs
# - remove old restore points
# - cleanup %TMPDIR%
# - remove dump files (memmory & mini dump)
# - remove old updates
# - remove unused chocolatey artefacts
# - cleanup winget installations
case "Linux":
writeOutput("cleanup will be running on Linux", args.silence)
if os.getuid != 0:
writeOutput("script is not invoced with sufficient rights - terminate", args.silence)
sys.exit()
# check if script is invoced by sytemd on Linux systems
if os.getenv("INVOCATION_ID"):
writeOutput("cleanup process is invocated by systemd", args.silence)
# chreating snapshot if timeshift is presend on system
if shutil.which("timeshift"):
writeOutput("found timeshift installed on system: creating snapshot", args.silence)
subprocess.run(["timeshift", "--create", "--tags 0", "--scripted"])
# checking for distribution specific things
distri = platform.freedesktop_os_release()
match distri["ID"].lower():
# manage deb based distributions
case "debian" | "ubuntu":
writeOutput("detected apt based system - initiate system cleaning", args.silence)
subprocess.run(["apt", "clean"])
subprocess.run(["apt", "autoremove", "--purge"])
for deb in Path("/var/cache/apt/archives/").glob("*.deb"):
os.remove(deb)
# manage rpm based distributions
case "fedora":
writeOutput("detected rpm based system - initiate system cleaning", args.silence)
subprocess.run(["dnf", "autoremove"])
subprocess.run(["dnf", "system-upgrade", "clean"])
subprocess.run(["dnf", "clean", "packages"])
# manage all other Linux distributions
case other:
writeOutput("could not identify linux distribution", args.silence)
#####
## proceed with not package manager specific tasks
#####
# removing unused oci container
if shutil.which("podman"):
writeOutput("found podman on system - will remove old and unsused images", args.silence)
subprocess.run("podman", "system", "prune -a -f")
# minimize jouarnd logs
if shutil.which("journalctl"):
writeOutput("removing journald entries older then 7 days", args.silence)
subprocess.run("journalctl", "--vacuum-time=7d")
# cleaning /tmp
writeOutput("removing files from /tmp", args.silence)
for f in Path("/tmp/").glob("*"):
os.remove(f)
case "Darwin":
pass
case other:
pass
writeOutput("all cleanup tasks are executed", args.silence)
if __name__ == "__main__":
main