]> git.ipfire.org Git - ipfire.org.git/blobdiff - build/rpc.py
We also verify the target.
[ipfire.org.git] / build / rpc.py
index 810584853dfa46150776ebfc0358d743afd088f6..686ce37c95ece8d9d8550569590f4afe89b4f363 100644 (file)
@@ -2,7 +2,7 @@
 ###############################################################################
 #                                                                             #
 # IPFire.org - A linux based firewall                                         #
-# Copyright (C) 2008  Michael Tremer & Christian Schmidt                      #
+# Copyright (C) 2008,2009 Michael Tremer & Christian Schmidt                  #
 #                                                                             #
 # This program is free software: you can redistribute it and/or modify        #
 # it under the terms of the GNU General Public License as published by        #
 import os
 import sys
 import cgi
-import time
-import random
 
 sys.path.append(".")
 
 from builder import Builder, getAllBuilders
 from constants import config
 
-class Response:
-       def __init__(self, config):
-               self.config = config
-               
-               self.code = "200"
-               self.mesg = "OK"
-       
-       def __call__(self, exit=0):
-               print "Status: %s" % self.code
-               print "Content-type: text/plain"
-               print
-               print "%s" % self.mesg
-               if exit:
-                       os._exit(0)
-       
-       def set_code(self, code):
-               self.code = code
-       
-       def set_mesg(self, mesg):
-               self.mesg = mesg
+ALLOWED_ACTIONS_SET = (        "distcc", "duration", "hostname", "jobs", "log", "state",
+                                               "package", "target", "toolchain", "cpu", "machine",)
+ALLOWED_ACTIONS_GET = ( "distcc",)
 
-response = Response(config)
+def run(uuid, action):
+       myself = Builder(config, uuid)
 
-data = cgi.FieldStorage()
+       if action == "get":
+               for key in ALLOWED_ACTIONS_GET:
+                       if key == "distcc":
+                               for value in data.getlist(key):
+                                       if value == "raw":
+                                               builders = getAllBuilders()
+                                               print "--randomize"
+                                               for builder in builders:
+                                                       # Print "localhost" for ourself
+                                                       if myself.uuid == builder.uuid:
+                                                               print "localhost/%s" % (builder.jobs() or "4")
+                                                       else:
+                                                               if ((myself.toolchain() == builder.toolchain()) and \
+                                                                       (myself.target() == builder.target())):
+                                                                       print "%s" % (builder.distcc,)
 
-uuid = data.getfirst("uuid")
-action  = data.getvalue('action')
-if action == "set":
-       if not uuid:
-               response.set_code("406")
-               response.set_mesg("UUID is not valid!")
-               response(1)
-       
-       builder = Builder(config, uuid)
-       
-       key = None
-       for key in [ "distcc", "duration", "hostname", "state", "package", ]:
-               for value in data.getlist(key):
-                       builder.set(key, value)
-elif action == "get":
-       for key in [ "distcc", ]:
-               if key == "distcc":
+       elif action == "set":
+               for key in ALLOWED_ACTIONS_SET:
                        for value in data.getlist(key):
-                               if value == "raw":
-                                       builders = []
-                                       for builder in getAllBuilders():
-                                               if uuid == builder.uuid: continue
-                                               builders.append("%s" % builder.distcc)
-                                       string = "localhost/1\n--randomize\n"
-                                       while True:
-                                               if not builders: break
-                                               rand = random.randint(0, len(builders)-1)
-                                               if builders[rand]:
-                                                       string = "%s%s\n" % (string, builders[rand],)
-                                               builders.pop(rand)
-                                       response.set_mesg(string)
-                                       
-else:
-       response.set_code("501")
-       response.set_mesg("Don't know what to do with command \"%s\"" % action)
-       response(1)
+                               print myself.set(key, value)
+
+data = cgi.FieldStorage()
+
+print "Status: 200 - OK" # We always send okay.
+print
 
-response()
+try:
+       uuid   = data.getfirst("uuid")
+       action = data.getfirst("action")
+       if uuid and action:
+               run(uuid, action)
+except SystemExit:
+       pass