def accept(): user_factory = StoredFactory(UserEntry) user_info = j.data.serializers.json.loads(get_user_info()) tname = user_info["username"] explorer_url = j.core.identity.me.explorer.url if "testnet" in explorer_url: explorer_name = "testnet" elif "devnet" in explorer_url: explorer_name = "devnet" elif "explorer.grid.tf" in explorer_url: explorer_name = "mainnet" else: return HTTPResponse( j.data.serializers.json.dumps({"error": f"explorer {explorer_url} is not supported"}), status=500, headers={"Content-Type": "application/json"}, ) user_entry = user_factory.get(f"{explorer_name}_{tname.replace('.3bot', '')}") if user_entry.has_agreed: return HTTPResponse( j.data.serializers.json.dumps({"allowed": True}), status=200, headers={"Content-Type": "application/json"} ) else: user_entry.has_agreed = True user_entry.explorer_url = explorer_url user_entry.tname = tname user_entry.save() return HTTPResponse( j.data.serializers.json.dumps({"allowed": True}), status=201, headers={"Content-Type": "application/json"} )
def _list_vdcs(): user_info = j.data.serializers.json.loads(get_user_info()) username = user_info["username"] result = [] def get_vdc(vdc): if vdc.state == VDCSTATE.EMPTY: return if vdc.is_empty(): j.logger.warning(f"vdc {vdc.solution_uuid} is empty") vdc.state = VDCSTATE.EMPTY vdc.save() return vdc_dict = vdc.to_dict() vdc_dict.pop("s3") vdc_dict.pop("kubernetes") vdc_dict.pop("etcd") result.append(vdc_dict) threads = [] vdcs = VDCFACTORY.list(username) for vdc in vdcs: thread = gevent.spawn(get_vdc, vdc) threads.append(thread) gevent.joinall(threads) return result
def get_vdc_info(name): user_info = j.data.serializers.json.loads(get_user_info()) username = user_info["username"] vdc = VDCFACTORY.find(vdc_name=name, owner_tname=username, load_info=True) if not vdc: return HTTPResponse(status=404, headers={"Content-Type": "application/json"}) vdc_dict = vdc.to_dict() vdc_dict.pop("s3") vdc_dict.pop("kubernetes") vdc_dict.pop("etcd") vdc_dict["price"] = math.ceil(vdc.calculate_spec_price(False)) wallet = vdc.prepaid_wallet balances = wallet.get_balance() balances_data = [] for item in balances.balances: # Add only TFT balance if item.asset_code == "TFT": balances_data.append( {"balance": item.balance, "asset_code": item.asset_code, "asset_issuer": item.asset_issuer} ) vdc_dict["wallet"] = { "address": wallet.address, "network": wallet.network.value, "secret": wallet.secret, "balances": balances_data, } return HTTPResponse( j.data.serializers.json.dumps(vdc_dict), status=200, headers={"Content-Type": "application/json"} )
def cancel_solution(): user_info = j.data.serializers.json.loads(get_user_info()) data = j.data.serializers.json.loads(request.body.read()) j.sals.marketplace.solutions.cancel_solution(user_info["username"], data["wids"], delete_pool=False) return j.data.serializers.json.dumps({"result": True})
def destroy(): """This method will delete username/backups from the backupservers -------------------------------------------------------------------------- * WARNINING: THIS IS A DISTRUCTIVE ACTION. WON'T BE ABLE TO RECOVER FROM * -------------------------------------------------------------------------- """ ssh_server1 = j.clients.sshclient.get("backup_server1") ssh_server2 = j.clients.sshclient.get("backup_server2") # validiate user data = j.data.serializers.json.loads(request.body.read()) threebot_name = data.get("threebot_name", "") user_info = j.data.serializers.json.loads(get_user_info()) current_username = threebot_name.split("_")[0] logged_in_username = user_info["username"].split(".")[0] if current_username != logged_in_username: abort(401, "Unauthorized") status = "Failed to destroy backups, 3Bot name doesn't exist" try: ssh_server1.sshclient.run( f"cd ~/backup; htpasswd -D .htpasswd {threebot_name}; cd /home/backup_config; rm -r {threebot_name}" ) ssh_server2.sshclient.run( f"cd ~/backup; htpasswd -D .htpasswd {threebot_name}; cd /home/backup_config; rm -r {threebot_name}" ) status = "Destroyed successfully" except: raise j.exceptions.Value(status) return j.data.serializers.json.dumps({"data": {"status": status}})
def allowed(): user_factory = StoredFactory(UserEntry) user_info = j.data.serializers.json.loads(get_user_info()) tname = user_info["username"] instances = user_factory.list_all() for name in instances: user_entry = user_factory.get(name) if user_entry.tname == tname and user_entry.has_agreed: return j.data.serializers.json.dumps({"allowed": True}) return j.data.serializers.json.dumps({"allowed": False})
def get_deployments(solution_type: str = None, username: str = None) -> list: """ List deployments for specific solution type selected from kubectl and corresponding helm list info """ all_deployments = [] username = username or j.data.serializers.json.loads(get_user_info()).get("username") vdc_names = [vdc.vdc_name for vdc in j.sals.vdc.list(username)] for vdc_name in vdc_names: config_path = j.sals.fs.expanduser("~/.kube/config") if not j.sals.fs.exists(config_path): continue k8s_client = j.sals.kubernetes.Manager(config_path=config_path) # get deployments resources = _get_resource(k8s_client, "deployments", solution_type) # get statefulsets if no result from deployments if not resources: resources = _get_resource(k8s_client, "statefulset", solution_type) releases = [] for deployment_info in resources: namespace = deployment_info["metadata"].get("namespace", "default") deployment_info = _filter_data(deployment_info) release_name = deployment_info["Release"] if release_name in releases: continue releases.append(release_name) helm_chart_supplied_values = "{}" try: helm_chart_supplied_values = k8s_client.get_helm_chart_user_values( namespace=namespace, release=release_name ) except: pass domain = _get_resource_key(k8s_client, "ingress", namespace, release_name, ".items[0].spec.rules[0].host") if not domain: domain = _get_resource_key( k8s_client, "ingressroutetcps", namespace, release_name, ".items[0].spec.tls.domains[0].main" ) deployment_info.update( { "VDC Name": vdc_name, "Domain": domain, "User Supplied Values": j.data.serializers.json.loads(helm_chart_supplied_values), "Chart": solution_type, "Namespace": namespace, } ) all_deployments.append(deployment_info) return all_deployments
def accept(): user_factory = StoredFactory(UserEntry) user_info = j.data.serializers.json.loads(get_user_info()) tname = user_info["username"] user_entry = user_factory.get(f"{tname.replace('.3bot', '')}") if user_entry.has_agreed: return HTTPResponse(j.data.serializers.json.dumps({"allowed": True}), status=200, headers={"Content-Type": "application/json"}) else: user_entry.has_agreed = True user_entry.tname = tname user_entry.save() return HTTPResponse(j.data.serializers.json.dumps({"allowed": True}), status=201, headers={"Content-Type": "application/json"})
def get_all_deployments(solution_types: list = None) -> list: """List all deployments from kubectl and corresponding helm list info""" if not solution_types: return [] all_deployments = [] username = j.data.serializers.json.loads(get_user_info()).get("username") def get_deployment(solution_type): solution_type_deployments = get_deployments(solution_type, username) all_deployments.extend(solution_type_deployments) threads = [] for solution_type in solution_types: thread = gevent.spawn(get_deployment, solution_type) threads.append(thread) gevent.joinall(threads) return all_deployments
def install_deployment(): # DEBUGGING PURPOSES data = j.data.serializers.json.loads(request.body.read()) user_info = j.data.serializers.json.loads(get_user_info()) username = user_info["username"] vdc_name = data.get("vdc_name") if not vdc_name: abort(400, "Error: Not all required params was passed.") config_path = f"{j.core.dirs.CFGDIR}/vdc/kube/{j.data.text.removesuffix(username, '.3bot')}/{vdc_name}.yaml" k8s_client = j.sals.kubernetes.Manager(config_path=config_path) k8s_client.add_helm_repo(data["repo_name"], data["repo_url"]) k8s_client.update_repos() k8s_client.install_chart( release=data["release"], chart_name=data["chart_name"], namespace=f'{data["solution_type"]}-{data["release"]}', extra_config=data["config"], ) return j.data.serializers.json.dumps({"result": True})
def cancel_deployment(): data = j.data.serializers.json.loads(request.body.read()) user_info = j.data.serializers.json.loads(get_user_info()) username = user_info["username"] vdc_name = data.get("vdc_name") namespace = data.get("namespace", "default") if not vdc_name: abort(400, "Error: Not all required params was passed.") config_path = j.sals.fs.expanduser("~/.kube/config") k8s_client = j.sals.kubernetes.Manager(config_path=config_path) vdc = get_vdc() if namespace == "default": k8s_client.delete_deployed_release(release=data["release"], vdc_instance=vdc, namespace=namespace) else: k8s_client.execute_native_cmd(f"kubectl delete ns {namespace}") j.logger.info(f"Cancelling deployment for {data['solution_id']}") j.sals.marketplace.solutions.cancel_solution_by_uuid(data["solution_id"]) return j.data.serializers.json.dumps({"result": True})
def destroy_threebot() -> str: data = j.data.serializers.json.loads(request.body.read()) user_info = j.data.serializers.json.loads(get_user_info()) if "password" not in data or "uuid" not in data: return HTTPResponse( j.data.serializers.json.dumps( {"error": "invalid body. missing keys"}), status=400, headers={"Content-Type": "application/json"}, ) try: delete_threebot_solution(owner=user_info["username"], solution_uuid=data["uuid"], password=data["password"]) except (exceptions.Permission, exceptions.Validation): return HTTPResponse( j.data.serializers.json.dumps({"error": "invalid secret"}), status=401, headers={"Content-Type": "application/json"}, ) return j.data.serializers.json.dumps({"data": True})
def delete_vdc(): data = j.data.serializers.json.loads(request.body.read()) name = data.get("name") if not name: abort(400, "Error: Not all required params was passed.") user_info = j.data.serializers.json.loads(get_user_info()) username = user_info["username"] vdc = VDCFACTORY.find(vdc_name=name, owner_tname=username, load_info=True) if not vdc: return HTTPResponse(status=404, headers={"Content-Type": "application/json"}) try: j.logger.info(f"Attemting deleting vdc: {name}") vdc = VDCFACTORY.find(f"vdc_{vdc.vdc_name}_{vdc.owner_tname}") VDCFACTORY.cleanup_vdc(vdc) vdc.state = VDCSTATE.EMPTY vdc.save() except Exception as e: j.logger.error(f"Error deleting VDC {name} due to {str(e)}") return HTTPResponse(f"Error deleteing VDC {name}", status=400, headers={"Content-Type": "application/json"}) return HTTPResponse("Sucess", status=200, headers={"Content-Type": "application/json"})
def count_solutions(): user_info = j.data.serializers.json.loads(get_user_info()) res = j.sals.marketplace.solutions.count_solutions(user_info["username"]) return j.data.serializers.json.dumps({"data": res})
def list_solutions(solution_type: str) -> str: solutions = [] user_info = j.data.serializers.json.loads(get_user_info()) solutions = j.sals.marketplace.solutions.list_solutions(user_info["username"], solution_type.lower()) return j.data.serializers.json.dumps({"data": solutions})
def list_threebots() -> str: user_info = j.data.serializers.json.loads(get_user_info()) threebots = list_threebot_solutions(user_info["username"]) return j.data.serializers.json.dumps({"data": threebots})
def list_threebots() -> str: solutions = [] user_info = j.data.serializers.json.loads(get_user_info()) solutions = j.sals.marketplace.solutions.list_solutions( user_info["username"], "threebot") return j.data.serializers.json.dumps({"data": solutions})