def add_backend(name): try: validate_name(name) except ValidationError as e: return str(e), 400 data = request.get_json() if not data: return "could not decode body json", 400 team = data.get('team') or data.get('tsuru.io/app-teamowner') plan = data.get('plan') flavor = data.get('flavor') if not team: return "team name is required", 400 if require_plan() and not plan: return "plan is required", 400 try: if flavor: get_manager().new_instance(name, team=team, plan_name=plan, flavor_name=flavor) else: get_manager().new_instance(name, team=team, plan_name=plan) except storage.PlanNotFoundError: return "Plan not found", 404 except storage.FlavorNotFoundError: return "Flavor not found", 404 except storage.DuplicateError: return "{} backend already exists".format(name), 409 except manager.QuotaExceededError as e: return str(e), 403 return "", 201
def update_backend(name): data = request.get_json() if not data: return "could not decode body json", 400 plan = data.get('plan') flavor = data.get('flavor') scale = data.get('scale') if not plan and not flavor and not scale: return "Invalid option. Valid update options are: scale, flavor and plan", 400 try: if scale and int(scale) <= 0: raise ValueError get_manager().update_instance(name, plan, flavor) if scale: get_manager().scale_instance(name, scale) except tasks.NotReadyError as e: return "Backend not ready: {}".format(e), 412 except storage.InstanceNotFoundError: return "Backend not found", 404 except storage.PlanNotFoundError: return "Plan not found", 404 except storage.FlavorNotFoundError: return "Flavor not found", 404 except ValueError: return "Scale option should be integer and >0", 400 return "", 204
def add_instance(): if "RPAAS_NEW_SERVICE" in os.environ: return "New instance disabled. Use {} service instead".format(os.environ["RPAAS_NEW_SERVICE"]), 405 name = request.form.get("name") try: validate_name(name) except ValidationError as e: return str(e), 400 team = request.form.get("team") if not team: return "team name is required", 400 plan = request.form.get("plan") if require_plan() and not plan: return "plan is required", 400 flavor = request.form.get("flavor") if not flavor: flavor = None tags = request.form.getlist("tags") for tag in tags: if 'flavor:' in tag: flavor = tag.split(':')[1] try: get_manager().new_instance(name, team=team, plan_name=plan, flavor_name=flavor) except storage.PlanNotFoundError: return "invalid plan", 400 except storage.FlavorNotFoundError: return "invalid flavor", 400 except storage.DuplicateError: return "{} instance already exists".format(name), 409 except manager.QuotaExceededError as e: return str(e), 403 return "", 201
def delete_backend(name): try: get_manager().remove_instance(name) except storage.InstanceNotFoundError: return "Backend not found", 404 except consul_manager.InstanceAlreadySwappedError: return "Instance with swap enabled", 412 return "", 200
def delete_route(name): path = request.form.get('path') if not path: return 'missing path', 400 try: get_manager().delete_route(name, path) except storage.InstanceNotFoundError: return "Instance not found", 404 except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 return "", 200
def bind(name): app_host = request.form.get("app-host") if not app_host: return "app-host is required", 400 try: get_manager().bind(name, app_host) except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 except storage.InstanceNotFoundError: return "Instance not found", 404 return Response(response="null", status=201, mimetype="application/json")
def unbind(name): app_host = request.form.get("app-host") if not app_host: return "app-host is required", 400 try: get_manager().unbind(name, app_host) except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 except storage.InstanceNotFoundError: return "Instance not found", 404 return "", 200
def unbind(name): app_host = request.form.get("app-host") if not app_host: return "app-host is required", 400 try: get_manager().unbind(name) except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 except storage.InstanceNotFoundError: return "Instance not found", 404 return "", 200
def add_block(name): content = request.form.get('content') block_name = request.form.get('block_name') if block_name not in ('server', 'http'): return 'invalid block_name (valid values are "server" or "http")', 400 if not content: return 'missing content', 400 try: get_manager().add_block(name, block_name, content) except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 return "", 201
def delete_lua(name): try: lua_module = request.form.get('lua_module_name') lua_module_type = request.form.get('lua_module_type') if lua_module_type not in ("server", "worker"): return 'Lua module type should be server or worker.', 400 if not lua_module: return 'You should provide a lua module name.', 400 get_manager().delete_lua(name, lua_module, lua_module_type) except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 return "", 200
def update_instance(name): plan = request.form.get("plan_name") if not plan: return "Plan is required", 404 try: get_manager().update_instance(name, plan) except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 except storage.InstanceNotFoundError: return "Instance not found", 404 except storage.PlanNotFoundError: return "Plan not found", 404 return "", 204
def cancel_restore_machine(name): if "RUN_RESTORE_MACHINE" not in os.environ: return "Restore machine not enabled", 412 machine = request.form.get("machine") if not machine: return "missing machine name", 400 try: get_manager().restore_machine_instance(name, machine, True) except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 except storage.InstanceNotFoundError: return "Instance not found", 404 except manager.InstanceMachineNotFoundError: return "Instance machine not found", 404 return "", 201
def add_lua(name): content = request.form.get('content') lua_module = request.form.get('lua_module_name') lua_module_type = request.form.get('lua_module_type') if lua_module_type not in ("server", "worker"): return 'Lua module type should be server or worker.', 400 if not lua_module: return 'You should provide a lua module name.', 400 if not content: return 'missing content', 400 try: get_manager().add_lua(name, lua_module, lua_module_type, content) except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 return "", 201
def cancel_restore_machine(name): if not check_option_enable(os.environ.get("RUN_RESTORE_MACHINE")): return "Restore machine not enabled", 412 machine = request.form.get("machine") if not machine: return "missing machine name", 400 try: get_manager().restore_machine_instance(name, machine, True) except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 except storage.InstanceNotFoundError: return "Instance not found", 404 except manager.InstanceMachineNotFoundError: return "Instance machine not found", 404 return "", 201
def scale_instance(name): quantity = request.form.get("quantity") if not quantity: return "missing quantity", 400 try: get_manager().scale_instance(name, int(quantity)) except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 except ValueError as e: msg = " ".join(e.args) if "invalid literal" in msg: return "invalid quantity: %s" % quantity, 400 return msg, 400 except storage.InstanceNotFoundError: return "Instance not found", 404 return "", 201
def retrieve_flavor(name): manager = get_manager() try: flavor = manager.storage.find_flavor(name) except storage.FlavorNotFoundError: return "flavor not found", 404 return json.dumps(flavor.to_dict())
def healings(): manager = get_manager() quantity = request.args.get("quantity", type=int) if quantity is None or quantity <= 0: quantity = 20 healing_list = manager.storage.list_healings(quantity) return json.dumps(healing_list, default=json_util.default)
def delete_certificate(name, cname): m = get_manager() try: m.delete_certificate(name) except storage.InstanceNotFoundError: return "Backend not found", 404 return "", 200
def add_https(name): domain = request.form.get('domain') if not domain: return "missing domain name", 400 plugin = request.form.get('plugin', 'default') try: get_manager().activate_ssl(name, domain, plugin) return "", 200 except storage.InstanceNotFoundError: return "Instance not found", 404 except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 except gaierror: return "can't find domain", 404 except manager.SslError, e: return str(e), 412
def node_status(name): try: node_status = get_manager().node_status(name) return Response(response=json.dumps(node_status), status=200, mimetype="application/json") except storage.InstanceNotFoundError: return "Instance not found", 404
def list_block(name): try: blocks = {'blocks': get_manager().list_blocks(name)} return Response(response=json.dumps(blocks), status=200, mimetype="application/json") except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412
def restore_instance(): instance_name = request.form.get("instance_name") if not instance_name: return "instance name required", 400 manager = get_manager() return Response(manager.restore_instance(instance_name), content_type='event/stream')
def list_routes(name): try: info = get_manager().list_routes(name) return Response(response=json.dumps(info), status=200, mimetype="application/json") except storage.InstanceNotFoundError: return "Instance not found", 404
def update_certificate(name): cert = request.form.get('cert') if cert is None: cert = request.files['cert'].read() key = request.form.get('key') if key is None: key = request.files['key'].read() try: get_manager().update_certificate(name, cert, key) except storage.InstanceNotFoundError: return "Instance not found", 404 except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 except manager.SslError: return "Invalid key or certificate", 412 return "", 200
def delete_flavor(name): manager = get_manager() try: manager.storage.delete_flavor(name) except storage.FlavorNotFoundError: return "flavor not found", 404 return ""
def delete_plan(name): manager = get_manager() try: manager.storage.delete_plan(name) except storage.PlanNotFoundError: return "plan not found", 404 return ""
def update_backend(name): data = request.get_json() if not data: return "could not decode body json", 400 plan = data.get('plan') if not plan: return "Plan is required", 400 try: get_manager().update_instance(name, plan) except tasks.NotReadyError as e: return "Backend not ready: {}".format(e), 412 except storage.InstanceNotFoundError: return "Backend not found", 404 except storage.PlanNotFoundError: return "Plan not found", 404 return "", 204
def retrieve_plan(name): manager = get_manager() try: plan = manager.storage.find_plan(name) except storage.PlanNotFoundError: return "plan not found", 404 return json.dumps(plan.to_dict())
def add_route(name): path = request.form.get('path') if not path: return 'missing path', 400 destination = request.form.get('destination') content = request.form.get('content') if not destination and not content: return 'either content xor destination are required', 400 if destination and content: return 'cannot have both content and destination', 400 try: get_manager().add_route(name, path, destination, content) except storage.InstanceNotFoundError: return "Instance not found", 404 except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412 return "", 201
def list_lua(name): try: modules = get_manager().list_lua(name) return Response(json.dumps({"modules": modules}), status=200, mimetype="application/json") except tasks.NotReadyError as e: return "Instance not ready: {}".format(e), 412
def update_plan(name): description = request.form.get("description") config = json.loads(request.form.get("config", "null")) manager = get_manager() try: manager.storage.update_plan(name, description, config) except storage.PlanNotFoundError: return "plan not found", 404 return ""
def status(name): node_status = get_manager().node_status(name) status = [] for node in node_status: status.append("{} - {}: {}".format(node, node_status[node]['address'], node_status[node]['status'])) node_status = {} node_status['status'] = "\n".join(status) return Response(response=json.dumps(node_status), status=200, mimetype="application/json")
def get_certificate(name, cname): m = get_manager() try: certificate, _ = m.get_certificate(name) except storage.InstanceNotFoundError: return "Backend not found", 404 except consul_manager.CertificateNotFoundError: return "Certificate not found", 404 return Response(response=json.dumps({'certificate': certificate}), status=200, mimetype="application/json")
def list_routes(name): try: routes = get_manager().list_upstreams(name, name) routes = ["http://{}".format(route) for route in routes] except tasks.NotReadyError as e: return "Backend not ready: {}".format(e), 412 except storage.InstanceNotFoundError: return "Backend not found", 404 return Response(response=json.dumps({"addresses": list(routes)}), status=200, mimetype="application/json")
def info(): plans = get_manager().storage.list_plans() flavors = get_manager().storage.list_flavors() options_plans = ["{} - {}".format(p.name, p.description) for p in plans] options_flavors = ["{} - {}".format(f.name, f.description) for f in flavors] options = """ scale - number of instance vms plan - set instance to plan flavor - set instance to flavor """ if options_plans: options = options + "\nAvailable plans: \n" + "\n".join(options_plans) if options_plans: options = options + "\n" if options_flavors: options = options + "\nAvailable flavors: \n" + "\n".join(options_flavors) return Response(response=json.dumps({'Router options': options}), status=200, mimetype="application/json")
def status(name): try: status = get_manager().status(name) except storage.InstanceNotFoundError: return "Instance not found", 404 if status == manager.FAILURE: return status, 500 if status == manager.PENDING: return status, 202 return status, 204