def parsing_server(self, content, farm_args, members_args): while content: head = str(content.pop()) if head == 'listen': listen = str(content.pop()) if listen.endswith(';'): listen = listen[:-1] if ':' in listen: listen = listen.split(':') farm_args['ip'] = listen[0] farm_args['port'] = listen[1] else: farm_args['port'] = listen elif head == 'location': farm_args['location'] = str(content.pop()) if str(content.pop()) == '{': self.parsing_location(content, farm_args, members_args) else: logger.debug("after location expected {, instead got %s " % head) raise "In Emily_Controller.parsing_server: after location expected {, instead got %s " % head elif head == '}': return True else: logger.debug("unknown word %s " % head) raise "In Emily_Controller.parsing_server: unknown word %s " % head
def load_farms(self): logger.debug("loading farms") farms={} conf_files = listdir(self.conf_dir) for conf in conf_files: if conf.endswith('.conf'): farm_id=conf[:-5] farms[farm_id] = self.load_farm(farm_id) return farms
def parsing_location(content, farm_args, members_args): while content: head = str(content.pop()) if head == 'proxy_pass': head = str(content.pop()) if "://" in head: farm_args['protocol'] = head.split("://")[0] elif head == '}': return True else: logger.debug("expected proxy_pass, instead got %s " % head) raise "In Emily_Controller.parsing_location: expected proxy_pass, instead got %s " % head
def parsing_member(content, farm_args, members_args): head = str(content.pop()) if head[head.__len__()-1] == ';': members_args[head[:-1]] = 1 return True else: weight = str(content.pop()) if weight.startswith('weight='): members_args[head] = weight.split('=')[1] else: logger.debug("unknown word %s , expected ; or weight" % weight) raise("In Emily_Controller.parsing_member: unknown word %s , expected ; or weight" % weight)
def farm_member_api(self, farm_id, member_id): if request.method == 'GET': return json.dumps( self.model.get_farm_member(farm_id, member_id).__dict__) elif request.method == 'DELETE': return json.dumps(self.model.delete_farm_member( farm_id, member_id)) else: logger.debug("unknown method: {}".format( request.method.to_string())) raise ("in Emily_View.farm_member_api, unknown method: {}".format( request.method.to_string()))
def parsing_upstream(self, content, farm_args, members_args): while content: head = str(content.pop()) if head in ['round_robin;', 'ip_hash;', 'least_conn;']: farm_args['lb_method'] = head[:-1] elif head == "server": self.parsing_member(content, farm_args, members_args) elif head == "}": return True else: logger.debug("unknown word %s " % head) raise("In Emily_Controller.parsing_upstream: unknown word %s " % head)
def request_data(request): if request.headers['Content-Type'] == 'application/json': return json.loads(request.data) elif request.headers['Content-Type'] == 'text/plain': try: return json.loads(request.data) except Exception: raise Exception("couldn't parse request {} as json".format( str(request.data))) else: logger.debug("unknown content type: %s" % request.headers['Content-Type']) raise ("unknown content type: %s" % request.headers['Content-Type'])
def farm_members_api(self, farm_id): if request.method == 'GET': members_json = [] for member in self.model.get_farm_members(farm_id): members_json.append(json.dumps(member)) return json.dumps(members_json) elif request.method == 'POST': args = self.request_data(request) return make_response(self.model.create_farm_member(farm_id, args)) else: logger.debug("unknown method: %s" % request.method.to_string()) raise ("in Emily_View.farm_members_api, unknown method: %s" % request.method.to_string())
def farms_api(self): if request.method == 'GET': farms_list = [] for farm in self.model.get_farms(): farms_list.append(json.dumps(str(farm))) return json.dumps(farms_list) elif request.method == 'POST': args = self.request_data(request) res = make_response(self.model.create_farm(args)) return res.data else: logger.debug("unknown method: %s" % request.method.to_string()) raise ("in Emily_View.farms_api, unknown method: %s" % request.method.to_string())
def farm_api(self, farm_id): if request.method == 'GET': if self.model.get_farm(farm_id): response = self.model.get_farm(farm_id).to_json() else: response = json.dumps({"error": "farm not found"}), 404 elif request.method == 'PUT': args = self.request_data(request) response = make_response(self.model.update_farm(farm_id, args)) elif request.method == 'DELETE': response = make_response(self.model.delete_farm(farm_id)) else: logger.debug("unknown method: %s" % request.method.to_string()) raise ("in Emily_View.farm_api, unknown method: %s" % request.method.to_string()) return response
def parsing_begin(self, content, farm_args, members_args): while content: head = str(content.pop()) if head == 'upstream': farm_args['name'] = content.pop() if str(content.pop()) == '{': self.parsing_upstream(content, farm_args, members_args) else: logger.debug("after upstream expected {, instead got %s " % head) raise "In Emily_Controller.parsing_begin: after upstream expected {, instead got %s " % head elif head == 'server': if str(content.pop()) == '{': self.parsing_server(content, farm_args, members_args) else: logger.debug("after server expected {, instead got %s " % head) raise "In Emily_Controller.parsing_begin: after server expected {, instead got %s " % head else: logger.debug("unknown word %s " % head) raise "In Emily_Controller.parsing_begin: unknown word %s " % head