summaryrefslogtreecommitdiffstats
path: root/src/retrace/abrt-retrace-reposync
blob: 30b437f2c04ec5a2b29c420a646d640bad56e12b (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
#!/usr/bin/python

import argparse
import os
import pwd
import sys
from retrace import *

sys.path = ["/usr/share/abrt-retrace"] + sys.path
from plugins import *

TARGET_USER = "abrt"

if __name__ == "__main__":
    # parse arguments
    argparser = argparse.ArgumentParser(description="Retrace Server repository downloader")
    argparser.add_argument("distribution", type=str, help="Distribution name")
    argparser.add_argument("version", type=str, help="Release version")
    argparser.add_argument("architecture", type=str, help="CPU architecture")
    args = argparser.parse_args()

    distribution = args.distribution
    version = args.version
    arch = args.architecture

    if arch == "i686":
        arch = "i386"

    # drop privilegies if possible
    try:
        pw = pwd.getpwnam(TARGET_USER)
        os.setgid(pw.pw_gid)
        os.setuid(pw.pw_uid)
        print "Privilegies set to '%s'." % TARGET_USER
    except KeyError:
        print "User '%s' does not exist. Running with default privilegies." % TARGET_USER
    except OSError:
        print "Unable to switch UID or GID. Running with default privilegies."

    # load plugin
    plugin = None
    for iplugin in PLUGINS:
        if iplugin.distribution == distribution:
            plugin = iplugin
            break

    if not plugin:
        print "Unknown distribution: '%s'" % distribution
        sys.exit(1)

    lockfile = "/tmp/abrt-retrace-lock-%s-%s-%s" % (distribution, version, arch)

    if os.path.isfile(lockfile):
        print "Another process with repository download is running."
        sys.exit(2)

    # set lock
    if not lock(lockfile):
        print "Unable to set lock."
        sys.exit(3)

    null = open("/dev/null", "w")

    targetdir = "%s/%s-%s-%s" % (CONFIG["RepoDir"], distribution, version, arch)

    # run rsync
    for repo in plugin.repos:
        retcode = -1
        for mirror in repo:
            repourl = mirror.replace("$ARCH", arch).replace("$VER", version)

            print "Running rsync on '%s'..." % repourl,
            sys.stdout.flush()

            if repourl.startswith("rsync://"):
                files = [repourl]
            else:
                files = []
                try:
                    for package in os.listdir(repourl):
                        files.append("%s/%s" % (repourl, package))
                except Exception as ex:
                    print "Error: %s. Trying another mirror..." % ex
                    continue

            pipe = Popen(["rsync", "-t"] + files + [targetdir], stdout=null, stderr=null)
            pipe.wait()
            retcode = pipe.returncode

            if retcode == 0:
                print "OK"
                break

            print "Error. Trying another mirror..."

        if retcode != 0:
            print "No more mirrors to try."

    # run createrepo
    print "Running createrepo on '%s'..." % targetdir,
    sys.stdout.flush()

    pipe = Popen(["createrepo", targetdir], stdout=null, stderr=null)
    pipe.wait()

    null.close()

    if pipe.returncode != 0:
        print "Failed"
        unlock(lockfile)
        sys.exit(4)

    print "OK"

    # remove lock
    if not unlock(lockfile):
        print "Unable to remove lock."
        sys.exit(5)