def app_state(name): app = director_manager.get_application(name) if not app: return api_error("No app named %s" % name) return jsonify({ "application": { "name": app.name, "uris": app.get_processes() } })
def router_uri_responder(): socket = zmq_ctx.socket(zmq.REP) socket.bind("tcp://*:5222") while True: app_name = socket.recv() app = director_manager.get_application(app_name) if app and app.state == "deployed": socket.send(str(random.choice(app._processes))) else: socket.send("") gevent.sleep(0)
def worker(): """Worker that needs to be a singleton worker""" while True: task = tasks.get() print "working: %s" % task task_type = task.get("task") if task_type: if task_type == "add_node": director_manager.add_node( name=task.get("name"), ip=task.get("ip") ) elif task_type == "delete_node": director_manager.remove_node(task.get("name")) elif task_type == "deploy": publish_queue.put_nowait({ "task": "deploy", "remove": True, "app_name": task.get("app_name"), "deploy_instruction": task.get("deploy_instruction") }) elif task_type == "undeploy": publish_queue.put_nowait({ "task": "undeploy", "app_name": task.get("app_name") }) # undeployed app elif task_type == "undeployed": print "undeployed (via agent)" #director_manager. elif task_type == "add_process": app = director_manager.get_application(task.get("app_name")) if not app: print "add_process:", "The application %s doesn't exists" % task.get("app_name", "?") return app.add_process(task.get("uri"), task.get("container_id")) elif task_type == "remove_process": app_name = task.get("app_name") if app_name: app = director_manager.get_application(app_name) if not app: print "remove_process:", "the application %s doesn't exists" % task.get("app_name", "?") else: app.remove_process(task.get("container_id")) else: print "remove_process, app_name is None" elif task_type == "upscale": # upscale, app_name, app_name = task.get("app_name") app = director_manager.get_application(app_name) processes = app.get_processes() nodes = max(len(director_manager.get_nodes()), 1) def _wait_on_agent(app_name, ip): try: print "_wait_on_agent-1" r = etcd_client.watch("services/agents/%s" % hashlib.sha1(ip).hexdigest(), timeout=60) print "_wait_on_agent-2" for x in xrange(3): tasks.put_nowait({ "task": "add_min_instance_processes", "app_name": app_name, "target": ip }) except: return if True:#len(processes)/nodes > 10 and nodes < 4: instances = director_manager.add_vm_instances(1) gevent.spawn(_wait_on_agent, app_name, instances[0].private_ip_address) else: # TODO: fix unlimited... tasks.put_nowait({ "task": "add_min_instance_processes", "app_name": app_name, "target": None, }) elif task_type == "add_min_instance_processes": app_name = task.get("app_name") deploy_target = task.get("target") if not deploy_target: app = director_manager.get_application(app_name) processes = app.get_processes() nodes = {ip: 0 for ip in director_manager.get_nodes()} for x in processes: nodes[x.split(":")[0]] += 1 deploy_target=min(nodes) publish_queue.put_nowait({ "task": "deploy", "remove": False, "app_name": app_name, "deploy_instruction": {deploy_target: 1} }) gevent.sleep(0)