コード例 #1
0
ファイル: cluster.py プロジェクト: CyrilSha/metagam
 def do_query_service(self, service_id, svc, url, timeout, verbose=False, *args, **kwargs):
     try:
         with Timeout.push(timeout):
             cnn = HTTPConnection()
             addr = (svc.get("addr").encode("utf-8"), svc.get("port"))
             try:
                 cnn.connect(addr)
             except IOError as e:
                 raise ClusterError("Error connecting to service %s(%s:%s): %s" % (service_id, addr[0], addr[1], e))
             params = {
                 "args": json.dumps(args),
                 "kwargs": json.dumps(kwargs)
             }
             try:
                 uri = utf2str("/service/call/%s%s" % (service_id, url))
                 request = cnn.post(uri, urlencode(params))
                 request.add_header("Content-type", "application/x-www-form-urlencoded")
                 request.add_header("Connection", "close")
                 if verbose:
                     self.debug("Query http://%s:%s%s", addr[0], addr[1], uri)
                 response = cnn.perform(request)
                 if response.status_code != 200:
                     raise ClusterError("Service %s (%s:%d) returned status %d for URL %s" % (service_id, addr[0], addr[1], response.status_code, uri))
                 res = json.loads(response.body)
                 if res.get("error"):
                     raise ClusterError(u"Service %s returned error: %s" % (service_id, res["error"]))
                 return res.get("retval")
             finally:
                 cnn.close()
     except TimeoutError:
         raise ClusterError("Timeout querying %s of service %s" % (url, service_id))
コード例 #2
0
ファイル: processes.py プロジェクト: CyrilSha/metagam
 def load(self, tag):
     if tag == "main":
         app = mg.Application(self.inst, tag)
         app.domain = app.main_host
         app.canonical_domain = "www.%s" % app.domain
         app.modules.load(["mg.constructor.admin.Constructor"])
         return app
     try:
         project = self.inst.int_app.obj(Project, tag)
     except ObjectNotFoundException:
         app = None
     else:
         domain = project.get("domain")
         if domain:
             canonical_domain = "www.%s" % domain
         else:
             main_app = self.get_by_tag("main")
             main_host = self.main_host
             projects_domain = main_app.config.get("constructor.projects-domain", main_host)
             domain = "%s.%s" % (tag, projects_domain)
             canonical_domain = domain
         storage = project.get("storage", 0)
         if storage == 2:
             app = mg.Application(self.inst, tag, storage, utf2str(project.get("keyspace")))
         else:
             app = mg.Application(self.inst, tag, storage)
         app.hooks.dynamic = True
         app.domain = domain
         app.canonical_domain = canonical_domain
         app.project = project
         lang = project.get("lang")
         if lang:
             app.lang = lang
         app.modules.load(["mg.constructor.project.ConstructorProject"])
     return app
コード例 #3
0
 def do_query_service(self,
                      service_id,
                      svc,
                      url,
                      timeout,
                      verbose=False,
                      *args,
                      **kwargs):
     try:
         with Timeout.push(timeout):
             cnn = HTTPConnection()
             addr = (svc.get("addr").encode("utf-8"), svc.get("port"))
             try:
                 cnn.connect(addr)
             except IOError as e:
                 raise ClusterError(
                     "Error connecting to service %s(%s:%s): %s" %
                     (service_id, addr[0], addr[1], e))
             params = {
                 "args": json.dumps(args),
                 "kwargs": json.dumps(kwargs)
             }
             try:
                 uri = utf2str("/service/call/%s%s" % (service_id, url))
                 request = cnn.post(uri, urlencode(params))
                 request.add_header("Content-type",
                                    "application/x-www-form-urlencoded")
                 request.add_header("Connection", "close")
                 if verbose:
                     self.debug("Query http://%s:%s%s", addr[0], addr[1],
                                uri)
                 response = cnn.perform(request)
                 if response.status_code != 200:
                     raise ClusterError(
                         "Service %s (%s:%d) returned status %d for URL %s"
                         % (service_id, addr[0], addr[1],
                            response.status_code, uri))
                 res = json.loads(response.body)
                 if res.get("error"):
                     raise ClusterError(u"Service %s returned error: %s" %
                                        (service_id, res["error"]))
                 return res.get("retval")
             finally:
                 cnn.close()
     except TimeoutError:
         raise ClusterError("Timeout querying %s of service %s" %
                            (url, service_id))
コード例 #4
0
 def load(self, tag):
     if tag == "main":
         app = mg.Application(self.inst, tag)
         app.domain = app.main_host
         app.canonical_domain = "www.%s" % app.domain
         app.modules.load(["mg.constructor.admin.Constructor"])
         return app
     try:
         project = self.inst.int_app.obj(Project, tag)
     except ObjectNotFoundException:
         app = None
     else:
         domain = project.get("domain")
         if domain:
             canonical_domain = "www.%s" % domain
         else:
             main_app = self.get_by_tag("main")
             main_host = self.main_host
             projects_domain = main_app.config.get(
                 "constructor.projects-domain", main_host)
             domain = "%s.%s" % (tag, projects_domain)
             canonical_domain = domain
         storage = project.get("storage", 0)
         if storage == 2:
             app = mg.Application(self.inst, tag, storage,
                                  utf2str(project.get("keyspace")))
         else:
             app = mg.Application(self.inst, tag, storage)
         app.hooks.dynamic = True
         app.domain = domain
         app.canonical_domain = canonical_domain
         app.project = project
         lang = project.get("lang")
         if lang:
             app.lang = lang
         app.modules.load(["mg.constructor.project.ConstructorProject"])
     return app