Skip to content

Commit

Permalink
cuckoocleanup deletewaiting to delete state
Browse files Browse the repository at this point in the history
  • Loading branch information
amadisson committed Oct 26, 2023
1 parent 5da150c commit 2f54f6c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
7 changes: 7 additions & 0 deletions common/cuckoo/common/analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ def to_human(cls, state):
f"No human readable version for state {state!r} exists"
)

@classmethod
def list(self):
return [self.UNTRACKED, self.PENDING_IDENTIFICATION, self.WAITING_MANUAL,
self.PENDING_PRE, self.TASKS_PENDING, self.NO_SELECTED,
self.FATAL_ERROR, self.FINISHED]


class Kinds:
STANDARD = "standard"

Expand Down
10 changes: 10 additions & 0 deletions core/cuckoo/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def find_analyses(older_than_days, state):
remote=False
), match_before

def find_analyses_hours(older_than_hours, state):
match_before = datetime.datetime.now() - datetime.timedelta(
hours=older_than_hours
)

return analyses.list_analyses(
state=state, older_than=match_before,
remote=False
), match_before


class AnalysisRemoteExporter:

Expand Down
27 changes: 16 additions & 11 deletions core/cuckoo/scripts/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,23 @@ def start_export(older_than_days, loglevel, without_confirm=False):
except CleanerError as e:
raise StartupError(e)

def delete_waiting(older_than_days, loglevel, without_confirm=False):
def delete_analyses(state, older_than_hours, loglevel, without_confirm=False):
from cuckoo.common.log import set_logger_level
from cuckoo.common.startup import init_global_logging, init_database
from cuckoo.common.clients import APIClient
from cuckoo.common.config import (
cfg, MissingConfigurationFileError, ConfigurationError
)
from cuckoo.common.storage import Paths
from ..clean import find_analyses, AnalysisRemoteExporter, CleanerError
from ..clean import find_analyses_hours, AnalysisRemoteExporter, CleanerError

init_global_logging(loglevel, Paths.log("export.log"))
init_global_logging(loglevel, Paths.log("delete.log"))
set_logger_level("urllib3.connectionpool", logging.ERROR)

init_database()
analyses, date = find_analyses(older_than_days, States.WAITING_MANUAL)
if not state in States.list():
exit_error(f"Invalid state: {state}")
analyses, date = find_analyses_hours(older_than_hours, state)
if not analyses:
print_info(f"No finished analyses older than {date} found.")
return
Expand Down Expand Up @@ -144,19 +146,22 @@ def remotestorage(ctx, days, yes):
call_registered_shutdowns()


@main.command("deletewaiting")
@click.argument("days", type=int)
@main.command("delete")
@click.argument("state", type=string)
@click.argument("hours", type=int)
@click.option("--yes", is_flag=True, help="Skip confirmation screen")
@click.pass_context --ye
def deletewaiting(ctx, days, yes):
@click.pass_context
def delete(ctx, state, hours, yes):
"""Delete Waiting manual analyses older than the specified
amount of days.
amount of hours.
\b
DAYS The age in days of analyses that should be deleted
STATE untracked, pending_identification, waiting_manual, pending_pre,
tasks_pending, no_selected, fatal_error, finished
HOURS The age in hours of analyses that should be deleted
"""

try:
delete_waiting(days, loglevel=ctx.parent.loglevel, without_confirm=yes)
delete_analyses(state, hours, loglevel=ctx.parent.loglevel, without_confirm=yes)
except StartupError as e:
exit_error(e)

0 comments on commit 2f54f6c

Please sign in to comment.