]> git.ipfire.org Git - ipfire.org.git/blob - build/rpc.py
Changed footer on build.ipfire.org.
[ipfire.org.git] / build / rpc.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2008 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import os
23 import sys
24 import cgi
25 import time
26 import random
27
28 sys.path.append(".")
29
30 from builder import Builder, getAllBuilders
31 from constants import config
32
33 class Response:
34 def __init__(self, config):
35 self.config = config
36
37 self.code = "200"
38 self.mesg = ""
39
40 def __call__(self, exit=0):
41 print "Status: %s" % self.code
42 print "Content-type: text/plain"
43 print
44 print "%s" % self.mesg
45 if exit:
46 os._exit(0)
47
48 def set_code(self, code):
49 self.code = code
50
51 def set_mesg(self, mesg):
52 self.mesg = mesg
53
54 def write(self, s):
55 if self.mesg:
56 self.mesg += "\n"
57 self.mesg += "[%s] - %s" % (time.ctime(), s,)
58
59 response = Response(config)
60
61 data = cgi.FieldStorage()
62
63 uuid = data.getfirst("uuid")
64 action = data.getvalue('action')
65 if action == "set":
66 if not uuid:
67 response.set_code("406")
68 response.set_mesg("UUID is not valid!")
69 response(1)
70
71 builder = Builder(config, uuid)
72
73 key = None
74 for key in [ "distcc", "duration", "hostname", "jobs", "log", "state", "package", "target", "cpu", "machine" ]:
75 for value in data.getlist(key):
76 ret = builder.set(key, value)
77 if ret:
78 response.write(ret)
79 elif action == "get":
80 for key in [ "distcc", ]:
81 if key == "distcc":
82 for value in data.getlist(key):
83 if value == "raw":
84 builders = []
85 jobs = "4"
86 for builder in getAllBuilders():
87 if uuid == builder.uuid:
88 jobs = builder.jobs()
89 continue
90 builders.append("%s" % builder.distcc)
91 string = "localhost/%s\n--randomize\n" % (jobs or "4",)
92 while True:
93 if not builders: break
94 rand = random.randint(0, len(builders)-1)
95 if builders[rand]:
96 string = "%s%s\n" % (string, builders[rand],)
97 builders.pop(rand)
98 response.set_mesg(string)
99
100 else:
101 response.set_code("501")
102 response.set_mesg("Don't know what to do with command \"%s\"" % action)
103 response(1)
104
105 response()