def create_inputjson(): with open(templatepath + "/inputoption.json", 'r') as f: optionfile = json.loads(f.read()) finalinput = {} for k, v in optionfile.items(): ip = {k: v["value"]} finalinput.update(ip) status, statusmessage, load_api_config = datafile.load_s3_file(apifilepath) if status == "error": message = { "statusCode": 400, "errorMessage": "load_requirement error", "Error": load_api_config } return (message) for version in load_api_config["versions"]: versionid = version["versionid"] versionname = version["versionname"] for resourceid in version["resources"]: resourcefilepath = datapath + "/servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/config.json" status, statusmessage, load_resource_config = datafile.load_s3_file( resourcefilepath) if status == "error": message = { "statusCode": 400, "errorMessage": "load_requirement error", "Error": load_resource_config } return (message) if os.path.isdir(templatepath + "/" + resourceid) == False: os.mkdir(templatepath + "/" + resourceid) finalinput.update({"resourceid": resourceid}) finalinput.update({"serviceid": serviceid}) resourcename = load_resource_config["resourcename"] generateatt = { "DeployName": "deloy" + resourceid, #form deploy_resourceid "HpaName": "hpa" + resourceid, #form hpa_resourceid "IngressName": "ingress" + resourceid, #form "LabelName": resourceid, #form "NameSpace": "default", #form "Protocol": "TCP", #form "ServiceName": "service" + resourceid, #form "ImageName": resourceid, #form "Containername": resourceid, #form "Resourceurl": "/" + versionname + "/" + resourcename + "/$2", #form "ResourceEndpoint": "/" + versionname + "/" + resourcename + "(/|$)(.*)", #form "Cpu": "250m", "memory": "500Mi" } finalinput.update(generateatt) prams = json.dumps(finalinput) with open(templatepath + "/" + resourceid + "/input.json", 'w') as f: inputfile = f.write(prams)
def getoption(serviceid,optionname): optionpath="deploy/"+serviceid+"/"+optionname+"/"+optionname+".json" status,statusmessage,optionjson=datafile.load_s3_file(optionpath) if status == "error": optionjson={} message={"statusCode":200,"body":optionjson} return(message)
def updateservicebyid(productid, serviceid): servicepath = "services/" + productid + "/services.json" status, statusmessage, services = datafile.load_s3_file(servicepath) if status == "success": serviceids = [] for svc in services: serviceids.append(svc["serviceid"]) if serviceid in serviceids: service = {} for svc in services: if svc["serviceid"] == serviceid: svc["status"] = "design" service = svc status, statusmessage, writeservice = datafile.write_s3_file( servicepath, services) if status == "error": message = {"statusCode": 400, "errorMessage": statusmessage} return (message) message = {"statusCode": 200, "body": service} else: message = { "statusCode": 400, "errorMessage": "serviceid does not exist" } else: message = {"statusCode": 400, "errorMessage": services} return (message)
def deleteservice(productid, serviceid): servicepath = "services/" + productid + "/services.json" status, statusmessage, services = datafile.load_s3_file(servicepath) if status == "success": serviceids = [] for svc in services: serviceids.append(svc["serviceid"]) if serviceid in serviceids: for svc in services: if svc["serviceid"] == serviceid: services.remove(svc) svcprefix = "servicedesigns/" + serviceid delstatus, delstatusmessage = datafile.delete_folders( svcprefix) status, statusmessage, response = datafile.write_s3_file( servicepath, services) message = {"statusCode": 200, 'message': 'Service Deleted'} else: message = { "statusCode": 400, "errorMessage": "serviceid does not exist" } else: message = {"errorMessage": statusmessage, "Error": services} return (message)
def getservice(productid): servicepath = "services/" + productid + "/services.json" status, statusmessage, services = datafile.load_s3_file(servicepath) if status != "success": services = [] message = {"statusCode": 200, "body": services} return (message)
def deleteversion(productid, serviceid, apiid, versionid): apipath = "servicedesigns/" + serviceid + "/api/config.json" status, statusmessage, api = datafile.load_s3_file(apipath) if status == "success": versionids = [] for version in api["versions"]: versionids.append(version["versionid"]) if versionid in versionids: for version in api["versions"]: if version["versionid"] == versionid: api["versions"].remove(version) status, statusmessage, response = datafile.write_s3_file( apipath, api) versionprefix = "servicedesigns/" + serviceid + "/api/" + versionid delstatus, delstatusmessage = datafile.delete_folders( versionprefix) message = {"statusCode": 200, "message": "Version Deleted"} else: message = { "statusCode": 400, "errorMessage": "versionid does not exist" } else: message = {"errorMessage": statusmessage, "Error": api} return (message)
def create_netrc(): try: coderepoconfigpath = "repoconfig/config.json" status, statusmessage, codecred = datafile.load_s3_file( coderepoconfigpath) if status == "error": message = { "statusCode": 400, "errorMessage": statusmessage, "Error": codecred } return (message) gitrepourl = codecred["gitrepourl"] gitusername = codecred["gitusername"] gitpassword = codecred["gitpassword"] secretpath = os.getenv("NETRC") machine = gitrepourl.split("/")[2] gitcred = "machine " + machine + " login " + gitusername + " password " + gitpassword + "\n" datawrite = writefile(secretpath, gitcred) destinationpath = "/home/app/.netrc" directory = os.path.dirname(destinationpath) if not os.path.exists(directory): os.makedirs(directory) copyfile(secretpath, destinationpath) return ("success", "netrc saved") except Exception as e: return ("error", str(e))
def deletetable(serviceid, dbid, tableid): dbpath = "servicedesigns/" + serviceid + "/data/" + dbid + "/config.json" status, statusmessage, data = datafile.load_s3_file(dbpath) if status == "success": if tableid in data["tables"]: tablepathprefix = "servicedesigns/" + serviceid + "/data/" + dbid + "/tables/" + tableid data["tables"].remove(tableid) status, statusmessage = datafile.delete_folders(tablepathprefix) datastatus, datamessage, datawrite = datafile.write_s3_file( dbpath, data) if status == "success": message = {"statusCode": 200, "message": "Table Deleted"} else: message = { "statusCode": 400, "errorMessage": "Table deletion failed" } else: message = { "statusCode": 400, "errorMessage": "tableid does not exist" } else: message = {"errorMessage": statusmessage, "Error": data} return (message)
def deletemethod(productid, serviceid, apiid, versionid, resourceid, methodid): resourcepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/config.json" status, statusmessage, resource = datafile.load_s3_file(resourcepath) if status == "success": methodids = [] for method in resource["methods"]: methodids.append(method["Ref"]) if methodid in methodids: for method in resource["methods"]: if method["Ref"] == methodid: resource["methods"].remove(method) resstatus, resstatusmessage, res = datafile.write_s3_file( resourcepath, resource) methodprefix = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid methodpath = methodprefix + ".json" status, statusmessage, delmeth = datafile.delete_s3_file( methodpath) delstatus, delstatusmessage = datafile.delete_folders(methodprefix) if status == "success" and delstatus == "success": message = {"statusCode": 200, "message": "method deleted"} else: message = {"statusCode": 400, "message": delstatusmessage} else: message = {"statusCode": 400, "message": "methodid does not exist"} else: message = {"errorMessage": statusmessage, "Error": resource} return (message)
def createdeploy(productid,serviceid): try: templatepath="/home/app/web/archeplay/template" servicepath= "services/"+productid+"/services.json" status,statusmessage,services=datafile.load_s3_file(servicepath) if status == "error": message={"statusCode":400,"errorMessage":statusmessage,"Error":services} return(message) for svc in services: if svc["serviceid"] == serviceid: servicetype=svc["servicetype"] svc["status"]="published" writestatus,writestatusmessage,srvswrite=datafile.write_s3_file(servicepath,services) if writestatus == "error": message={"statusCode":400,"errorMessage":writestatusmessage,"Error":srvswrite} return(message) svctemplatepath=templatepath+"/servicetemplates/"+servicetype+".json" with open(svctemplatepath,"r") as f: stepdata=f.read() stepjson=json.loads(stepdata) optionname=stepjson["optionname"] optionjson=getfiles(stepjson,serviceid,templatepath) optionpath="deploy/"+serviceid+"/"+optionname+"/"+optionname+".json" status,statusmessage,optionput=datafile.write_s3_file(optionpath,optionjson) if status == "success": message={"statusCode":200,"body":optionjson} else: message={"statusCode":400,"errorMessage":optionput} return(message) except Exception as e: message={"statusCode":400,"errorMessage":str(e)} return(message)
def get_branches(): try: coderepoconfigpath = "repoconfig/config.json" status, statusmessage, codecred = datafile.load_s3_file( coderepoconfigpath) if status == "error": message = {"statusCode": 400, "errorMessage": statusmessage} return (message) gitrepourl = codecred["gitrepourl"] gitusername = codecred["gitusername"] archepath = "/home/app/web/archeplay" localpath = "/home/app/web/archeplay/" + uniqid("localgit") repo = git.Repo.clone_from(gitrepourl, localpath) repo.config_writer().set_value("user", "name", gitusername).release() repo.config_writer().set_value("user", "email", gitusername).release() branch = get_all_branches(localpath) a = branch.decode('ascii') liststr = list(a.split("\n")) liststr.remove('') branchnames = [] for name in liststr: branchname = name.split("/") branchnames.append(branchname[1]) branchnames.pop(0) message = {"statusCode": 200, "body": branchnames} return (message) except Exception as e: message = {"statusCode": 400, "errorMessage": str(e)} return (message)
def gettable(serviceid, dbid): dbpath = "servicedesigns/" + serviceid + "/data/" + dbid + "/config.json" status, statusmessage, data = datafile.load_s3_file(dbpath) if status == "success": tables = [] if data["tables"] != []: for tableid in data["tables"]: tablepath = "servicedesigns/" + serviceid + "/data/" + dbid + "/tables/" + tableid + "/config.json" status, statusmessage, table = datafile.load_s3_file(tablepath) tables.append(table) message = {"statusCode": 200, "body": tables} else: message = {"statusCode": 200, "body": tables} else: message = {"errorMessage": statusmessage, "Error": data} return (message)
def getalltestcases(productid, serviceid, apiid, versionid, resourceid): testcasemethodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/testcases" methodstatus, methodstatusmessage, methodid = datafile.list_directory( testcasemethodpath) if methodstatus == "success": testcases = [] try: for item in methodid: testcasepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/testcases/" + item testcasestatus, testcasestatusmessage, files = datafile.list_directory( testcasepath) try: for filename in files: testcasefile = testcasepath + "/" + filename status, statusmessage, testcase = datafile.load_s3_file( testcasefile) testcases.append(testcase) except Exception as e: print(str(e)) except Exception as e: print(str(e)) message = {"statusCode": 200, "body": testcases} else: testcases = [] message = {"statusCode": 200, "body": testcases} return (message)
def getresource(productid, serviceid, apiid, versionid): apipath = "servicedesigns/" + serviceid + "/api/config.json" status, statusmessage, api = datafile.load_s3_file(apipath) if status == "success": resources = [] for ver in api['versions']: for resourceid in ver["resources"]: resourcepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/config.json" status, statusmessage, resource = datafile.load_s3_file( resourcepath) if status == "success": resources.append(resource) message = {"statusCode": 200, "body": resources} else: message = {"statusCode": 400, 'errorMessage': api} return (message)
def deleteresource(productid, serviceid, apiid, versionid, resourceid): apipath = "servicedesigns/" + serviceid + "/api/config.json" status, statusmessage, api = datafile.load_s3_file(apipath) if status == "success": for ver in api['versions']: if ver["versionid"] == versionid: if resourceid in ver["resources"]: resourceprefix = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid delstatus, statusmessage = datafile.delete_folders( resourceprefix) ver["resources"].remove(resourceid) status, statusmessage, response = datafile.write_s3_file( apipath, api) if delstatus == "success" and status == "success": message = { "statusCode": 200, "message": "resource deleted" } else: message = { "statusCode": 400, "errorMessage": "resource deletion failed" } else: message = { "statusCode": 400, "errorMessage": "resourceid does not exist" } else: message = {"statusCode": 400, 'errorMessage': api} return (message)
def gettablebyid(serviceid, dbid, tableid): tablepath = "servicedesigns/" + serviceid + "/data/" + dbid + "/tables/" + tableid + "/config.json" status, statusmessage, table = datafile.load_s3_file(tablepath) if status == "success": message = {"statusCode": 200, "body": table} else: message = {"errorMessage": statusmessage, "Error": table} return (message)
def getdatabyid(serviceid, versionid, resourceid, dbid): datapath = "servicedesigns/" + serviceid + "/data/" + dbid + "/config.json" status, statusmessage, data = datafile.load_s3_file(datapath) if status == "success": message = {"statusCode": 200, "body": data} else: message = {"errorMessage": statusmessage, "Error": data} return (message)
def getcoderepo(): coderepoconfigpath = "repoconfig/config.json" status, statusmessage, coderepoconf = datafile.load_s3_file( coderepoconfigpath) if status == "error": coderepoconf = {} message = {"statusCode": 200, "body": coderepoconf} return (message)
def gettemplaterepo(): templaterepoconfigpath = "templateconfig/config.json" status, statusmessage, templaterepoconf = datafile.load_s3_file( templaterepoconfigpath) if status == "error": templaterepoconf = {} message = {"statusCode": 200, "body": templaterepoconf} return (message)
def getapibyid(productid, serviceid, apiid): apipath = "servicedesigns/" + serviceid + "/api/config.json" status, statusmessage, api = datafile.load_s3_file(apipath) if status == "success": message = {"statusCode": 200, "body": api} else: message = {"errorMessgae": statusmessage, "Error": api} return (message)
def getmethodbyid(productid, serviceid, apiid, versionid, resourceid, methodid): methodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + ".json" status, statusmessage, method = datafile.load_s3_file(methodpath) if status == "success": message = {"statusCode": 200, "body": method} else: message = {"erroeMessage": statusmessage, "Error": method} return (message)
def getmethod(productid, serviceid, apiid, versionid, resourceid): resourcepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/config.json" status, statusmessage, resource = datafile.load_s3_file(resourcepath) if status == "success": methods = [] if resource["methods"] != []: for method in resource["methods"]: methodid = method["Ref"] methodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + ".json" methstatus, methstatusmessage, loadmethod = datafile.load_s3_file( methodpath) if methstatus == "success": methods.append(loadmethod) message = {"statusCode": 200, "body": methods} else: message = {"statusCode": 200, "body": methods} else: message = {"statusCode": 400, "errorMessage": resource} return (message)
def getdata(serviceid, versionid, resourceid): resourcepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/config.json" status, statusmessage, resource = datafile.load_s3_file(resourcepath) if status == "success": for data in resource["data"]: database = [] if data["db"] != []: for db in data["db"]: dbid = db["Ref"] datapath = "servicedesigns/" + serviceid + "/data/" + dbid + "/config.json" status, statusmessage, data = datafile.load_s3_file( datapath) database.append(data) message = {"statusCode": 200, "body": database} else: message = {"statusCode": 200, "body": database} else: message = {"errorMessage": statusmessage, "Error": resource} return (message)
def getlogs(dbid, serviceid): tenantid = "servicedesigns" db_path = tenantid + "/" + serviceid + "/data/" + dbid db_logfile = db_path + "/log.json" status, status_message, dblogs_config = datafile.load_s3_file(db_logfile) if status == "error": successmessage = { "error_message": "failed to load database logfile", "status": 400 } else: successmessage = {"db_output": dblogs_config, "status": 200} return (successmessage)
def getresourcebyid(productid, serviceid, apiid, versionid, resourceid): apipath = "servicedesigns/" + serviceid + "/api/config.json" status, statusmessage, api = datafile.load_s3_file(apipath) if status == "success": for ver in api['versions']: if ver["versionid"] == versionid: if resourceid in ver["resources"]: resourcepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/config.json" status, statusmessage, resource = datafile.load_s3_file( resourcepath) if status == "success": message = {"statusCode": 200, "body": resource} else: message = {"statusCode": 400, "errorMessage": resource} else: message = { "statusCode": 400, "errorMessage": "resourceid does not exist" } else: message = {"statusCode": 400, 'errorMessage': api} return (message)
def createtable(serviceid, dbid): iop = {"tablename": ""} tablename = request.json["tablename"] json_dict = request.get_json() dbpath = "servicedesigns/" + serviceid + "/data/" + dbid + "/config.json" status, statusmessage, database = datafile.load_s3_file(dbpath) if status == "success": if database["tables"] == []: tableid = uniqid("tb") elif database["tables"] != []: a = True while a is True: tableid = uniqid('tb') if tableid in database["tables"]: a = True else: a = False database["tables"].append(tableid) status, statusmessage, response = datafile.write_s3_file( dbpath, database) tablejson = { "tableid": tableid, "tablename": tablename, "dbtype": database["DBType"], "schema": [], "indexes": {} } if "description" in json_dict: tablejson["description"] = request.json["description"] tablepath = "servicedesigns/" + serviceid + "/data/" + dbid + "/tables/" + tableid + "/config.json" status, statusmessage, table = datafile.write_s3_file( tablepath, tablejson) if status == "success": message = { "statusCode": 200, "message": "Table Created", "body": tablejson } else: message = { "statusCode": 400, "errorMessage": statusmessage, "Error": table } else: message = { "statusCode": 400, "errorMessage": statusmessage, "Error": database } return (message)
def updatetable(serviceid, dbid, tableid): json_dict = request.get_json() tablepath = "servicedesigns/" + serviceid + "/data/" + dbid + "/tables/" + tableid + "/config.json" status, statusmessage, table = datafile.load_s3_file(tablepath) if status == "success": if "addschema" in json_dict: for sch in request.json['addschema']: if all(sch['attributename'] != tbh['attributename'] for tbh in table['schema']): table['schema'].append(sch) # table['schema']=table['schema']+request['addschema'] if "deleteschema" in json_dict: for sch in request.json['deleteschema']: for schema in table['schema']: if sch['attributename'] == schema['attributename']: if sch['attributename'] not in str(table['indexes']): table['schema'].remove(schema) if "editschema" in json_dict: for schema in table['schema']: for sch in request.json['editschema']: if sch['attributename'] == schema['attributename']: table['schema'][table['schema'].index(schema)] = sch if "indexes" in json_dict: table['indexes'] = request.json['indexes'] if "updatepath" in json_dict: request.json['updatevalue'] = json.loads( request.json['updatevalue']) path = request.json['updatepath'].split("/") myd = table for i in path[:-1]: myd = myd[i] if "[" in path[-1]: path[-1] = path[-1].replace("]", "") p = path[-1].split("[") if p[0] not in myd: myd[p[0]] = [] if int(p[1]) >= len(myd[p[0]]): myd[p[0]].append(request.json['updatevalue']) else: myd[p[0]][int(p[1])] = request.json['updatevalue'] else: myd[path[-1]] = request.json['updatevalue'] status, statusmessage, response = datafile.write_s3_file( tablepath, table) if status == "success": message = {"statusCode": 200, "body": table} else: message = {"statusCode": 400, "errorMessage": statusmessage} else: message = {"errorMessage": statusmessage, "Error": table} return (message)
def createdb(serviceid, versionid, resourceid): json_dict = request.get_json() resourcepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/config.json" status, statusmessage, resource = datafile.load_s3_file(resourcepath) if status == "success": for data in resource["data"]: if data["db"] == []: dbid = uniqid("db") db = {"Ref": dbid} data["db"].append(db) elif data["db"] != []: dbids = [] for db in data["db"]: dbids.append(db["Ref"]) a = True while a is True: dbid = uniqid('db') if dbid in dbids: a = True else: a = False db = {"Ref": dbid} data["db"].append(db) resstatus, resstatusmessage, res = datafile.write_s3_file( resourcepath, resource) dbjson = { "dbid": dbid, "CreateType": "IMPORT|NEW", "import": { "Ref": dbid }, "DBType": "DYNAMODB", "type": "dynamodb", "deploy": { "type": "mock" }, "tables": [] } if "description" in json_dict: dbjson["description"] = request.json["description"] datapath = "servicedesigns/" + serviceid + "/data/" + dbid + "/config.json" status, statusmessage, response = datafile.write_s3_file( datapath, dbjson) if status == "success": message = {"statusCode": 200, "body": dbjson} else: message = {"statusCode": 400, "errorMessage": statusmessage} else: message = {"errorMessage": statusmessage, "Error": resource} return (message)
def get_branchbytype(branchtype): try: coderepoconfigpath = "repoconfig/config.json" status, statusmessage, codecred = datafile.load_s3_file( coderepoconfigpath) if status == "error": message = {"statusCode": 400, "errorMessage": statusmessage} return (message) branches = codecred[branchtype] message = {"statusCode": 200, "body": branches} return (message) except Exception as e: message = {"statusCode": 400, "errorMessage": str(e)} return (message)
def getproduct(): iop = {"userid": ""} userid = request.headers["userid"] productpath = "products/products.json" status, statusmessage, products = datafile.load_s3_file(productpath) if status != "success": res = [] elif status == "success": res = [] for prod in products: if prod["userid"] == userid: res.append(prod) message = {"statusCode": 200, "body": res} return (message)