Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
Moved cuckoo_clean to library
Browse files Browse the repository at this point in the history
  • Loading branch information
jekil committed Feb 15, 2015
1 parent 5ed6cf0 commit 5cd2e64
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 61 deletions.
62 changes: 1 addition & 61 deletions cuckoo.py
Expand Up @@ -6,7 +6,6 @@
import argparse
import logging
import os
import shutil
import sys

try:
Expand All @@ -16,7 +15,7 @@
from lib.cuckoo.common.exceptions import CuckooCriticalError
from lib.cuckoo.common.exceptions import CuckooDependencyError
from lib.cuckoo.core.database import Database
from lib.cuckoo.core.startup import check_working_directory, check_configs
from lib.cuckoo.core.startup import check_working_directory, check_configs, cuckoo_clean
from lib.cuckoo.core.startup import check_version, create_structure
from lib.cuckoo.core.startup import init_logging, init_modules, init_console_logging
from lib.cuckoo.core.startup import init_tasks, init_yara
Expand Down Expand Up @@ -70,65 +69,6 @@ def cuckoo_init(quiet=False, debug=False, artwork=False, test=False):

os.chdir(cur_path)

def cuckoo_clean():
"""Clean up cuckoo setup.
It deletes logs, all stored data from file system and configured databases (SQL
and MongoDB.
"""
# Init logging.
# This need to init a console logger handler, because the standard
# logger (init_logging()) logs to a file which will be deleted.
create_structure()
init_console_logging()

# Initialize the database connection.
db = Database()

# Drop all tables.
db.drop()

# Check if MongoDB reporting is enabled and drop that if it is.
cfg = Config("reporting")
if cfg.mongodb and cfg.mongodb.enabled:
from pymongo import MongoClient
host = cfg.mongodb.get("host", "127.0.0.1")
port = cfg.mongodb.get("port", 27017)
mdb = cfg.mongodb.get("db", "cuckoo")
try:
conn = MongoClient(host, port)
conn.drop_database(mdb)
conn.disconnect()
except:
log.warning("Unable to drop MongoDB database: %s", mdb)

# Paths to clean.
paths = [
os.path.join(CUCKOO_ROOT, "db"),
os.path.join(CUCKOO_ROOT, "log"),
os.path.join(CUCKOO_ROOT, "storage"),
]

# Delete various directories.
for path in paths:
if os.path.isdir(path):
try:
shutil.rmtree(path)
except (IOError, OSError) as e:
log.warning("Error removing directory %s: %s", path, e)

# Delete all compiled Python objects ("*.pyc").
for dirpath, dirnames, filenames in os.walk(CUCKOO_ROOT):
for fname in filenames:
if not fname.endswith(".pyc"):
continue

path = os.path.join(CUCKOO_ROOT, dirpath, fname)

try:
os.unlink(path)
except (IOError, OSError) as e:
log.warning("Error removing file %s: %s", path, e)

def cuckoo_main(max_analysis_count=0):
cur_path = os.getcwd()
os.chdir(CUCKOO_ROOT)
Expand Down
61 changes: 61 additions & 0 deletions lib/cuckoo/core/startup.py
Expand Up @@ -3,6 +3,7 @@
# See the file 'docs/LICENSE' for copying permission.

import os
import shutil
import sys
import copy
import json
Expand Down Expand Up @@ -261,3 +262,63 @@ def find_signatures(root):
log.debug("\t `-- %s", entry)
else:
log.debug("\t |-- %s", entry)


def cuckoo_clean():
"""Clean up cuckoo setup.
It deletes logs, all stored data from file system and configured databases (SQL
and MongoDB.
"""
# Init logging.
# This need to init a console logger handler, because the standard
# logger (init_logging()) logs to a file which will be deleted.
create_structure()
init_console_logging()

# Initialize the database connection.
db = Database()

# Drop all tables.
db.drop()

# Check if MongoDB reporting is enabled and drop that if it is.
cfg = Config("reporting")
if cfg.mongodb and cfg.mongodb.enabled:
from pymongo import MongoClient
host = cfg.mongodb.get("host", "127.0.0.1")
port = cfg.mongodb.get("port", 27017)
mdb = cfg.mongodb.get("db", "cuckoo")
try:
conn = MongoClient(host, port)
conn.drop_database(mdb)
conn.disconnect()
except:
log.warning("Unable to drop MongoDB database: %s", mdb)

# Paths to clean.
paths = [
os.path.join(CUCKOO_ROOT, "db"),
os.path.join(CUCKOO_ROOT, "log"),
os.path.join(CUCKOO_ROOT, "storage"),
]

# Delete various directories.
for path in paths:
if os.path.isdir(path):
try:
shutil.rmtree(path)
except (IOError, OSError) as e:
log.warning("Error removing directory %s: %s", path, e)

# Delete all compiled Python objects ("*.pyc").
for dirpath, dirnames, filenames in os.walk(CUCKOO_ROOT):
for fname in filenames:
if not fname.endswith(".pyc"):
continue

path = os.path.join(CUCKOO_ROOT, dirpath, fname)

try:
os.unlink(path)
except (IOError, OSError) as e:
log.warning("Error removing file %s: %s", path, e)

0 comments on commit 5cd2e64

Please sign in to comment.