aboutsummaryrefslogtreecommitdiff
path: root/scripts/update-repos
blob: a72ce360553fd8b1d274491ad7abfcc945080718 (plain)
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
#!/usr/bin/env python
# Copyright (C) 2013 Linaro Ltd.

import argparse
import logging
import os
import re
import subprocess

from logging.handlers import TimedRotatingFileHandler
from tempfile import gettempdir


# Match a directory that ends with .git, but not only .git
GIT_DIRECTORY_ENDS = re.compile(".+\.git$")
# Name for a lock file.
LOCK_FILE_NAME = "update-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."""
    parser = argparse.ArgumentParser()
    parser.add_argument("--repos-dir",
                        required=True,
                        help="The directory where repositories are stored.")
    parser.add_argument("--user",
                        help="User to run the commands as.")
    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 fetch_updates(repos_dir, user):
    """Traverse the file system and fetch updates.

    :param repos_dir: The directory holding repositories.
    :param user: The use to run the commands as.
    """
    for root, dirs, files in os.walk(os.path.abspath(repos_dir)):
        if GIT_DIRECTORY_ENDS.match(root):
            if files:
                # We really are in a git repository.
                cmd_args = []
                if user:
                    cmd_args = ["sudo", "-u", user, "-H"]
                cmd_args += ["git", "fetch", "--all", "-q"]
                repo_name = os.path.basename(root)
                process = subprocess.Popen(cmd_args,
                                           cwd=root,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE)

                logger.info("Fetching updates for '%s' repository." % repo_name)
                p_out, p_err = process.communicate()
                if process.returncode != 0:
                    logger.error("Error fetching updates for repository "
                                 "'%s'." % repo_name)
                    logger.debug(p_err)
            else:
                # git repositories always have a HEAD file, or it means they
                # are empty.
                logger.debug("Skipping directory '%s': valid git one, but "
                             "looks empty." % root)
                continue
        else:
            logger.debug("Skipped directory '%s', not matching a git "
                         "one." % root)


if __name__ == '__main__':
    parser = args_parser()
    args = parser.parse_args()

    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'):
                fetch_updates(args.repos_dir, args.user)
        finally:
            os.unlink(LOCK_FILE)