2022-11-24 02:08:58 +00:00
|
|
|
|
import re
|
|
|
|
|
import subprocess
|
2023-10-11 22:01:31 +00:00
|
|
|
|
from subprocess import Popen, PIPE
|
2022-11-24 02:08:58 +00:00
|
|
|
|
|
2023-06-19 21:25:04 +00:00
|
|
|
|
from selfprivacy_api.utils.huey import huey
|
2023-06-18 04:37:27 +00:00
|
|
|
|
|
2023-06-19 21:25:04 +00:00
|
|
|
|
from selfprivacy_api.jobs import JobStatus, Jobs, Job
|
2022-12-19 02:27:44 +00:00
|
|
|
|
|
|
|
|
|
|
2023-10-11 22:01:31 +00:00
|
|
|
|
COMPLETED_WITH_ERROR = (
|
|
|
|
|
"We are sorry, сompleted with an error, report it to support chat"
|
|
|
|
|
)
|
|
|
|
|
RESULT_WAS_NOT_FOUND_ERROR = "We are sorry, garbage collection result was not found, something went wrong, report it to support chat :("
|
2023-06-21 06:52:31 +00:00
|
|
|
|
CLEAR_COMPLETED = "Cleaning completed."
|
2022-11-24 02:08:58 +00:00
|
|
|
|
|
|
|
|
|
|
2023-10-11 22:01:31 +00:00
|
|
|
|
def run_nix_store_print_dead() -> PIPE:
|
|
|
|
|
subprocess.run(
|
|
|
|
|
["nix-env", "-p", "/nix/var/nix/profiles/system", "--delete-generations old"],
|
|
|
|
|
check=False,
|
|
|
|
|
)
|
2023-06-21 06:52:31 +00:00
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
return subprocess.check_output(["nix-store", "--gc", "--print-dead"]).decode(
|
|
|
|
|
"utf-8"
|
|
|
|
|
)
|
2022-11-24 02:08:58 +00:00
|
|
|
|
|
|
|
|
|
|
2023-10-11 22:01:31 +00:00
|
|
|
|
def run_nix_collect_garbage() -> subprocess.Popen:
|
2022-12-13 01:44:52 +00:00
|
|
|
|
return subprocess.Popen(
|
2023-06-21 06:52:31 +00:00
|
|
|
|
["nix-store", "--gc"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT
|
2022-12-19 02:27:44 +00:00
|
|
|
|
).stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_line(line):
|
2023-10-11 22:18:29 +00:00
|
|
|
|
"""
|
|
|
|
|
We parse the string for the presence of a final line,
|
|
|
|
|
with the final amount of space cleared.
|
|
|
|
|
Simply put, we're just looking for a similar string:
|
|
|
|
|
"1537 store paths deleted, 339.84 MiB freed".
|
|
|
|
|
"""
|
2022-12-19 02:27:44 +00:00
|
|
|
|
pattern = re.compile(r"[+-]?\d+\.\d+ \w+(?= freed)")
|
2023-06-18 04:37:27 +00:00
|
|
|
|
match = re.search(pattern, line)
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
2022-12-13 01:44:52 +00:00
|
|
|
|
if match is None:
|
2022-12-19 02:27:44 +00:00
|
|
|
|
return (
|
2023-10-11 22:40:47 +00:00
|
|
|
|
JobStatus.ERROR,
|
2022-12-19 02:27:44 +00:00
|
|
|
|
100,
|
|
|
|
|
COMPLETED_WITH_ERROR,
|
2022-12-27 00:15:02 +00:00
|
|
|
|
RESULT_WAS_NOT_FOUND_ERROR,
|
2022-12-03 18:27:10 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-12-13 01:44:52 +00:00
|
|
|
|
else:
|
2022-12-19 02:27:44 +00:00
|
|
|
|
return (
|
|
|
|
|
JobStatus.FINISHED,
|
|
|
|
|
100,
|
|
|
|
|
CLEAR_COMPLETED,
|
|
|
|
|
f"{match.group(0)} have been cleared",
|
2022-12-13 01:44:52 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-10-11 22:01:31 +00:00
|
|
|
|
def process_stream(job, stream, total_dead_packages):
|
2023-03-23 17:49:30 +00:00
|
|
|
|
completed_packages = 0
|
2023-06-26 19:33:41 +00:00
|
|
|
|
prev_progress = 0
|
2022-12-13 01:44:52 +00:00
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
for line in stream:
|
2023-06-20 19:25:54 +00:00
|
|
|
|
line = line.decode("utf-8")
|
|
|
|
|
|
2022-12-13 01:44:52 +00:00
|
|
|
|
if "deleting '/nix/store/" in line:
|
2023-03-23 17:49:30 +00:00
|
|
|
|
completed_packages += 1
|
|
|
|
|
percent = int((completed_packages / total_dead_packages) * 100)
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
2023-06-26 19:33:41 +00:00
|
|
|
|
if percent - prev_progress >= 5:
|
|
|
|
|
Jobs.update(
|
2023-10-11 22:18:29 +00:00
|
|
|
|
job=Job,
|
2023-06-26 19:33:41 +00:00
|
|
|
|
status=JobStatus.RUNNING,
|
|
|
|
|
progress=percent,
|
|
|
|
|
status_text="Cleaning...",
|
|
|
|
|
)
|
|
|
|
|
prev_progress = percent
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
2022-12-13 01:44:52 +00:00
|
|
|
|
elif "store paths deleted," in line:
|
2022-12-19 02:27:44 +00:00
|
|
|
|
status = parse_line(line)
|
2023-06-18 04:37:27 +00:00
|
|
|
|
Jobs.update(
|
2023-10-11 22:18:29 +00:00
|
|
|
|
job=Job,
|
2022-12-19 02:27:44 +00:00
|
|
|
|
status=status[0],
|
|
|
|
|
progress=status[1],
|
|
|
|
|
status_text=status[2],
|
|
|
|
|
result=status[3],
|
|
|
|
|
)
|
|
|
|
|
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
def get_dead_packages(output):
|
|
|
|
|
dead = len(re.findall("/nix/store/", output))
|
2023-06-21 06:52:31 +00:00
|
|
|
|
percent = 0
|
2022-12-19 02:27:44 +00:00
|
|
|
|
if dead != 0:
|
|
|
|
|
percent = 100 / dead
|
|
|
|
|
return dead, percent
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
|
|
|
|
|
2023-06-19 21:25:04 +00:00
|
|
|
|
@huey.task()
|
2023-10-11 22:40:47 +00:00
|
|
|
|
def calculate_and_clear_dead_packages(job: Job):
|
2023-06-18 04:37:27 +00:00
|
|
|
|
Jobs.update(
|
2023-10-11 22:18:29 +00:00
|
|
|
|
job=Job,
|
2022-12-13 01:44:52 +00:00
|
|
|
|
status=JobStatus.RUNNING,
|
|
|
|
|
progress=0,
|
2023-06-21 06:52:31 +00:00
|
|
|
|
status_text="Calculate the number of dead packages...",
|
2022-12-13 01:44:52 +00:00
|
|
|
|
)
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
2023-10-11 22:09:13 +00:00
|
|
|
|
dead_packages, package_equal_to_percent = get_dead_packages(
|
2023-04-05 10:49:56 +00:00
|
|
|
|
run_nix_store_print_dead()
|
|
|
|
|
)
|
2022-12-13 01:44:52 +00:00
|
|
|
|
|
|
|
|
|
if dead_packages == 0:
|
2023-06-18 04:37:27 +00:00
|
|
|
|
Jobs.update(
|
2023-10-11 22:18:29 +00:00
|
|
|
|
job=Job,
|
2022-12-13 01:44:52 +00:00
|
|
|
|
status=JobStatus.FINISHED,
|
|
|
|
|
progress=100,
|
|
|
|
|
status_text="Nothing to clear",
|
|
|
|
|
result="System is clear",
|
|
|
|
|
)
|
2022-12-19 02:27:44 +00:00
|
|
|
|
return
|
2022-12-13 01:44:52 +00:00
|
|
|
|
|
2023-06-18 04:37:27 +00:00
|
|
|
|
Jobs.update(
|
2023-10-11 22:18:29 +00:00
|
|
|
|
job=Job,
|
2022-12-13 01:44:52 +00:00
|
|
|
|
status=JobStatus.RUNNING,
|
|
|
|
|
progress=0,
|
|
|
|
|
status_text=f"Found {dead_packages} packages to remove!",
|
2022-11-24 02:08:58 +00:00
|
|
|
|
)
|
2022-12-13 01:44:52 +00:00
|
|
|
|
|
2023-10-11 22:01:31 +00:00
|
|
|
|
process_stream(job, run_nix_collect_garbage(), package_equal_to_percent)
|
2023-06-18 04:37:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start_nix_collect_garbage() -> Job:
|
|
|
|
|
job = Jobs.add(
|
|
|
|
|
type_id="maintenance.collect_nix_garbage",
|
|
|
|
|
name="Collect garbage",
|
2023-06-21 06:52:31 +00:00
|
|
|
|
description="Cleaning up unused packages",
|
2023-06-18 04:37:27 +00:00
|
|
|
|
)
|
|
|
|
|
calculate_and_clear_dead_packages(job=job)
|
|
|
|
|
return job
|