aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilo Casagrande <milo@ubuntu.com>2013-01-31 13:18:05 +0100
committerMilo Casagrande <milo@ubuntu.com>2013-01-31 13:18:05 +0100
commitdd748e44ba562e73a92f8c640e6a882a80a5abd4 (patch)
tree1628831856ae98b7a4e493dca355914820666cfd
parent8f9e327688237fd62a0fd62ee7221e612c22a49a (diff)
downloadrhodecode-config-dd748e44ba562e73a92f8c640e6a882a80a5abd4.tar.gz
Added logging setup.
-rw-r--r--scripts/mirror-repos59
1 files changed, 56 insertions, 3 deletions
diff --git a/scripts/mirror-repos b/scripts/mirror-repos
index d013b14..22228ed 100644
--- a/scripts/mirror-repos
+++ b/scripts/mirror-repos
@@ -2,12 +2,14 @@
# Copyright (C) 2013 Linaro Ltd.
import argparse
+import logging
import os
+import pwd
import subprocess
import sys
import urlparse
-import pwd
+from logging.handlers import TimedRotatingFileHandler
from tempfile import gettempdir
@@ -21,6 +23,18 @@ DEFAULT_API_HOST = "http://0.0.0.0:5000"
LOCK_FILE_NAME = "mirror-repos.lock"
LOCK_FILE = os.path.join(gettempdir(), LOCK_FILE_NAME)
+FILE_NAME = os.path.basename(__file__)
+# Default log directory and log file.
+DEFAULT_LOG_DIR = "/var/log/rhodecode"
+LOG_FILE_NAME = FILE_NAME + ".log"
+# When to rotate logs.
+DEFAULT_ROTATING_TIME = 'midnight'
+# How many old logs to keep.
+KEEP_MAX_LOGS = 10
+
+# Default logger.
+logger = logging.getLogger(FILE_NAME)
+
def args_parser():
"""Sets up the argument parser."""
@@ -44,9 +58,41 @@ def args_parser():
default=DEFAULT_API_HOST,
help="The host URL where API interface is located. "
"Defaults to '%s'." % DEFAULT_API_HOST)
+ parser.add_argument("--log-dir",
+ default=DEFAULT_LOG_DIR,
+ help="Directory to store logs. Defaults to '%s'." %
+ DEFAULT_LOG_DIR)
+ parser.add_argument("--debug",
+ action="store_true",
+ help="Print debugging statements.")
return parser
+def setup_logging(debug, log_dir):
+ """Sets up logging.
+
+ :param debug: If the level should be set to DEBUG.
+ :type bool
+ :param log_dir: Where to store file based logs.
+ """
+ th_formatter = "%(asctime)s %(levelname)-8s %(message)s"
+ log_file = os.path.join(log_dir, LOG_FILE_NAME)
+
+ timed_handler = TimedRotatingFileHandler(log_file,
+ when=DEFAULT_ROTATING_TIME,
+ backupCount=KEEP_MAX_LOGS)
+ timed_handler.setFormatter(logging.Formatter(th_formatter))
+
+ if debug:
+ logger.setLevel(logging.DEBUG)
+ timed_handler.setLevel(logging.DEBUG)
+ else:
+ logger.setLevel(logging.INFO)
+ timed_handler.setLevel(logging.INFO)
+
+ logger.addHandler(timed_handler)
+
+
def check_args(args, parser):
"""Checks command line arguments passed.
@@ -106,8 +152,11 @@ def mirror_repos(file, dest, user=None):
full_repo = urlparse.urljoin(BASE_PATH, line)
cmd_args = ["git", "clone", "--mirror", full_repo]
- print "Cloning repository %s..." % full_repo
+ logger.info("Cloning repository %s..." % full_repo)
execute_command(cmd_args, work_dir=full_path, user=user)
+ else:
+ logger.debug("Skipping repository '%s', does not seem a valid "
+ "git one." % base_dir)
def rescan_git_directory(api_key, api_host, user=None):
@@ -158,7 +207,10 @@ def execute_command(cmd_args, as_sudo=True, user=None, work_dir=os.getcwd()):
p_out, p_err = process.communicate()
if process.returncode != 0:
- print "Error executing the following command: %s" % " ".join(cmd_args)
+ logger.error("Error executing the following command: %s" %
+ " ".join(cmd_args))
+ logger.debug("The full command line is: %s" % " ".join(exec_args))
+ logger.debug(p_err)
if __name__ == '__main__':
@@ -169,6 +221,7 @@ if __name__ == '__main__':
if os.path.exists(LOCK_FILE):
print "Another process is still running: cannot acquire lock."
else:
+ setup_logging(args.debug, args.log_dir)
try:
with open(LOCK_FILE, 'w'):
mirror_repos(args.repos_list,