aboutsummaryrefslogtreecommitdiffstats
path: root/bin/buildworker-janitor
blob: 3d0bb68ee592d99bad319dd5871557421ff502a9 (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
126
127
128
129
130
131
132
133
#!/usr/bin/python
'''
Created on Feb 19, 2014

__author__ = "Richard Purdie"
__copyright__ = "Copyright 2014, Linux Foundation"
__credits__ = ["Richard Purdie"]
__license__ = "GPL"
__version__ = "2.0"
__maintainer__ = "Richard Purdie"
__email__ = "richard.purdie@linuxfoundation.org"
'''

from __future__ import print_function
import signal
import os
import sys
import ConfigParser
import threading
import time

tmpfile = '/tmp/.buildworker-janitor'+os.getcwd().replace('/', '-')

if len(sys.argv) < 2:
    print("Please specify the path to the config file on the commandline as the first argument")
    sys.exit(1)

config = ConfigParser.ConfigParser()
config.read(sys.argv[1])

sections = {}
for section in config.sections():
    sections[section] = dict(config.items(section))

if "GitSettings" not in sections:
    print("There is no GitSettings section in the configuration file?")
    sys.exit(1)

gitsettings = sections["GitSettings"]

if "ogit_trash_dir" not in gitsettings:
    print("Please set OGIT_TRASH_DIR in the configuration file")
    sys.exit(1)

if "ogit_mirror_dir" not in gitsettings:
    print("Please set OGIT_MIRROR_DIR in the configuration file")
    sys.exit(1)

trashdir = gitsettings["ogit_trash_dir"].replace('"','').replace("'",'')
mirrordir = gitsettings["ogit_mirror_dir"].replace('"','').replace("'",'')

mirrorlist = [
    "git://git.yoctoproject.org/meta-fsl-arm",
    "git://git.yoctoproject.org/meta-fsl-ppc",
    "git://git.yoctoproject.org/meta-intel",
    "git://git.yoctoproject.org/meta-minnow",
    "git://git.yoctoproject.org/meta-qt3",
    "git://git.yoctoproject.org/meta-qt4",
    "git://git.yoctoproject.org/poky",
    "git://git.yoctoproject.org/eclipse-poky-kepler",
    "git://git.yoctoproject.org/eclipse-poky-juno"
]

def trash_processor(trashdir):
    print("Monitoring trashdir %s" % trashdir)
    if not os.path.exists(trashdir):
        os.makedirs(trashdir)
    if trashdir == "/":
        print("Not prepared to use a trashdir of /")
        return
    while True:
        try:
            files = os.listdir(trashdir)
            if files:
                os.system("ionice -c 3 rm %s/* -rf" % trashdir)
            else:
                time.sleep(120*60) # 30 minutes
        except Exception as e:
            print("Exception %s in trash cleaner" % str(e))
            time.sleep(60) # 1 minute timeout to prevent crazy looping
            pass
    return

def mirror_processor(mirrordir):
    print("Updating mirrors in %s" % mirrordir)
    mirrorpaths = []
    for mirror in mirrorlist:
        mirrorpaths.append(os.path.join(mirrordir, mirror.split("/")[2], mirror.split("/", 3)[3]))
        if not os.path.exists(mirrorpaths[-1] + "/.git/"):
            os.system("git clone %s %s" % (mirror, mirrorpaths[-1]))
    while True:
        for path in mirrorpaths:
            os.chdir(path)
            os.system("git fetch --prune --all")
        time.sleep(30*60) # 30 minutes
    return

#Check to see if this is running already. If so, kill it and rerun
if os.path.exists(tmpfile) and os.path.isfile(tmpfile):
    print("A prior PID file exists. Attempting to kill.")
    with open(tmpfile, 'r') as f:
        pid=f.readline()
    try:
        os.kill(int(pid), signal.SIGKILL)
        # We need to sleep for a second or two just to give the SIGKILL time
        time.sleep(2)
    except OSError as ex:
        print("""We weren't able to kill the prior buildworker-janitor. Trying again.""")
        pass
    # Check if the process that we killed is alive.
    try:
       os.kill(int(pid), 0)
    except OSError as ex:
       pass
elif os.path.exists(tmpfile) and not os.path.isfile(tmpfile):
    raise Exception("""/tmp/.buildworker-janitor is a director. remove it to continue.""")
try:
    os.unlink(tmpfile)
except:
    pass
with open(tmpfile, 'w') as f:
    print(os.getpid(), file=f)

threads = []
threads.append(threading.Thread(target=trash_processor, args=(trashdir,)))
threads[-1].start()
threads.append(threading.Thread(target=mirror_processor, args=(mirrordir,)))
threads[-1].start()

# wait for all threads to finish
for t in threads:
    t.join()
sys.exit(0)