]> git.ipfire.org Git - pakfire.git/blame - pakfire/server/slave.py
autobuilder: Switch back to download source package from the webserver.
[pakfire.git] / pakfire / server / slave.py
CommitLineData
677ff42a
MT
1#!/usr/bin/python
2
3import logging
4import os
5import socket
4f363544 6import tempfile
677ff42a
MT
7import xmlrpclib
8
1f888d5b 9import pakfire.api
7c8f2953 10import pakfire.base
a52f536c
MT
11import pakfire.downloader
12import pakfire.packages
1f888d5b 13import pakfire.util
a52f536c 14
1f888d5b 15from pakfire.constants import *
a52f536c 16
1f888d5b
MT
17from base import MasterSlave
18
19class Slave(MasterSlave):
7c8f2953
MT
20 def __init__(self, **pakfire_args):
21 self.pakfire = pakfire.base.Pakfire(**pakfire_args)
677ff42a
MT
22
23 server = self.pakfire.config._slave.get("server")
24
25 logging.info("Establishing RPC connection to: %s" % server)
26
27 self.conn = xmlrpclib.Server(server)
28
29 def keepalive(self):
30 """
31 Send the server a keep-alive to say that we are still there.
32 """
a52f536c 33 hostname = self.hostname
677ff42a
MT
34 l1, l5, l15 = os.getloadavg()
35
36 logging.info("Sending the server a keepalive: %s" % hostname)
37
a52f536c
MT
38 # Get all supported architectures and send them to the server.
39 arches = [a for a in self.pakfire.supported_arches]
40 arches.sort()
41
42 self.conn.keepalive(hostname, l5, arches)
43
44 def update_build_status(self, build_id, status, message=""):
45 self.conn.update_build_state(build_id, status, message)
46
47 def build_job(self):
48 build = self.conn.build_job(self.hostname)
49
50 # If the server has got no job for us, we end right here.
51 if not build:
52 return
53
4f363544
MT
54 print build
55
a52f536c 56 build_id = build["id"]
1f888d5b 57 filename = build["name"]
4f363544 58 download = build["download"]
1f888d5b 59 hash1 = build["hash1"]
a52f536c 60
4f363544
MT
61 # Create a temporary file and a directory for the resulting files.
62 tmpdir = tempfile.mkdtemp()
63 tmpfile = os.path.join(tmpdir, filename)
64
65 # Get a package grabber and add mirror download capabilities to it.
66 grabber = pakfire.downloader.PackageDownloader()
a52f536c
MT
67
68 try:
4f363544
MT
69 # Download the source.
70 grabber.urlgrab(download, filename=tmpfile)
71
1f888d5b 72 # Check if the download checksum matches.
4f363544 73 if pakfire.util.calc_hash1(tmpfile) == hash1:
1f888d5b
MT
74 print "Checksum matches: %s" % hash1
75 else:
76 raise DownloadError, "Download was corrupted"
77
1f888d5b 78 # Update the build status on the server.
a52f536c
MT
79 self.update_build_status(build_id, "running")
80
1f888d5b 81 # Run the build.
4f363544
MT
82 pakfire.api.build(tmpfile, build_id=build_id,
83 resultdirs=[tmpdir,])
1f888d5b
MT
84
85 self.update_build_status(build_id, "uploading")
86
4f363544 87 for dir, subdirs, files in os.walk(tmpdir):
1f888d5b
MT
88 for file in files:
89 file = os.path.join(dir, file)
4f363544
MT
90 if file == tmpfile:
91 continue
1f888d5b
MT
92
93 pkg = pakfire.packages.open(self.pakfire, None, file)
94
95 self.upload_package_file(build["source_id"], build["pkg_id"], pkg)
a52f536c
MT
96
97 except DependencyError, e:
1f888d5b
MT
98 message = "%s: %s" % (e.__class__.__name__, e)
99 self.update_build_status(build_id, "dependency_error", message)
a52f536c 100
1f888d5b 101 except Exception, e:
1f888d5b
MT
102 message = "%s: %s" % (e.__class__.__name__, e)
103 self.update_build_status(build_id, "failed", message)
7c8f2953 104 raise
a52f536c 105
7c8f2953
MT
106 else:
107 self.update_build_status(build_id, "finished")
677ff42a 108
1f888d5b 109 finally:
4f363544
MT
110 # Cleanup the files we created.
111 pakfire.util.rm(tmpdir)