def init_lb_list(): lb_list = utils.send_request("GET", consts.URLS["get_lb"]) for lb in lb_list: # lb_ft = get_lb_frontends(lb["name"]) filename = utils.get_current_folder( ) + consts.Prefix["lb_name_prefix"] + lb["name"] utils.file_writer(filename, lb)
def init_cm_handler(svc_detail): svc_name = svc_detail["service_name"].lower() svc_uuid = svc_detail["uuid"] mount_points = svc_detail["mount_points"] cm_data = {} cm_name_pre = utils.get_current_folder() + consts.Prefix["cm_name_prefix"] if svc_detail["app_name"]: cm_name_pre = cm_name_pre + consts.Prefix["app_cm_flag_file"] for mp in mount_points: print "init configMap for service {}".format(svc_name) cm_name = cm_name_pre + svc_uuid if mp["type"] == "config": value = mp["value"] key = value["key"] conf_file_name = value["name"] data = utils.send_request( "GET", consts.URLS["get_config_content"].format( filename=conf_file_name)) print "config file {} response is ".format(conf_file_name) print data if "content" in data: for item in data["content"]: if item["key"] == key: content = item["value"] cm_data[key] = content elif mp["type"] == "raw" or mp["type"] == "text": key = "key" + mp["path"].replace("/", "-") content = mp["value"] cm_data[key] = content else: raise "Unknown config type " + mp["type"] utils.file_writer(cm_name, cm_data)
def trans_app_data(app): app_data = { "resource": { "create_method": "UI" }, "kubernetes": [] } app_data["resource"]["name"] = consts.Prefix["app_name_prefix"] + app["app_name"].lower() app_namespace_name = app["app_name"] + "-" + app["space_name"] if app["space_name"] else "default" app_data["namespace"] = { "name": app_namespace_name, "uuid": namespaces.get_alauda_ns_by_name(app_namespace_name)["uid"] } app_data["cluster"] = { "name": app["region_name"], "uuid": app["region_uuid"] } for app_service in app["services"]: app_service_detail = get_app_service_detail(app_service["uuid"].lower()) app_data["kubernetes"].extend(services.trans_pod_controller(app_service_detail)) if len(app_service_detail["mount_points"]) > 0: app_data["resource"]["create_method"] = "yaml" app_create_data_file = utils.get_current_folder() + consts.Prefix["app_create_data_prefix"] + app["app_name"] utils.file_writer(app_create_data_file, app_data) return app_data
def get_app_list(): results = [] file_name = utils.get_current_folder() + consts.Prefix["app_list_file"] if path.exists(file_name): results = utils.file_reader(file_name) else: raise Exception("app list file doesn't exists!") return results
def get_app_service_detail(svc_id): svc_detail = {} file_name = utils.get_current_folder() + consts.Prefix["app_service_detail_file"] + svc_id if path.exists(file_name): svc_detail = utils.file_reader(file_name) else: raise Exception("app service detail file {} doesn't exists!".format(file_name)) return svc_detail
def init_svc_lb(): svc_list = services.get_service_list() get_lb_url = consts.URLS["get_lb"] for svc in svc_list: url = get_lb_url + "&service_id=" + svc["uuid"] svc_lb_data = utils.send_request("GET", url) filename = utils.get_current_folder( ) + consts.Prefix["lb_name_prefix"] + svc["service_name"].lower() utils.file_writer(filename, svc_lb_data)
def init_app_svc_lb(): app_list = applications.get_app_list() get_lb_url = consts.URLS["get_lb"] for app in app_list: for app_svc in app["services"]: url = get_lb_url + "&service_id=" + app_svc["uuid"] svc_lb_data = utils.send_request("GET", url) filename = utils.get_current_folder() + consts.Prefix[ "lb_name_prefix"] + app_svc["service_name"].lower() utils.file_writer(filename, svc_lb_data)
def create_cm(): current_folder = utils.get_current_folder() print "begin create configMap " for filename in os.listdir(current_folder): if filename.startswith(consts.Prefix["cm_name_prefix"]): cm_file = filename print "find configMap file " + filename if not os.path.exists(current_folder + cm_file): raise "configMap file for {} not exists! please check task of init cm".format( current_folder + cm_file) cm_data = utils.file_reader(current_folder + cm_file) print "config map data for file {},{}".format( filename, json.dumps(cm_data)) is_app = False if filename.startswith(consts.Prefix["cm_name_prefix"] + consts.Prefix["app_cm_flag_file"]): is_app = True filename_pre = consts.Prefix["cm_name_prefix"] + consts.Prefix["app_cm_flag_file"] if is_app\ else consts.Prefix["cm_name_prefix"] service_uuid = filename.replace(filename_pre, "") print "find cm service uuid {}, type is {}".format( service_uuid, "app" if is_app else "service") svc_detail = applications.get_app_service_detail(service_uuid) if is_app \ else services.get_service_detail(service_uuid) data = { "apiVersion": "v1", "kind": "ConfigMap", "metadata": { "annotations": {}, "namespace": applications.get_app_svc_namespace(svc_detail) if is_app else svc_detail["space_name"], "name": filename.replace("_", "-") }, "data": cm_data } print "begin create configMap for {}".format(current_folder + filename) res = utils.send_request("POST", consts.URLS["create_cm"], data) if isinstance(res, list) and len(res) > 0: print "configMap {} create success,list".format( current_folder + filename) elif isinstance( res, dict) and "result" in res and len(res["result"]) > 0: print "create namespace for {} success".format(current_folder + filename) elif isinstance(res, dict) and "errors" in res and \ res["errors"][0]["message"].find("already exists") >= 0: print res["errors"][0]["message"] + ", will be skipped" else: print "configMap {} create ERROR!!!".format(current_folder + filename) exit(1)
def svc_detail_handler(svc): current_folder = utils.get_current_folder() prefix = consts.Prefix["app_service_detail_file"] if svc[ "app_name"] else consts.Prefix["service_detail_file"] file_name_id = current_folder + prefix + svc["uuid"] file_name_svc_name = current_folder + prefix + svc["service_name"].lower() svc_detail = utils.send_request( "GET", consts.URLS["get_or_delete_svc_detail"].format(service_id=svc["uuid"])) utils.file_writer(file_name_id, svc_detail) # detail for svc_name utils.file_writer(file_name_svc_name, svc_detail)
def init_pipeline(): all_pips = get_all_pipelines_for_current_project() cache_task_types = ["update-service", "exec", "manual-control", ""] for pl in all_pips: pipeline = get_pipeline_detail(pl["uuid"]) if "tasks" in pipeline["stages"][0]: tasks = pipeline["stages"][0]["tasks"] for task in tasks: if task["type"] in cache_task_types: cache_pl_file = utils.get_current_folder() + consts.Prefix[ "pipeline_name_prefix"] + pipeline["name"] utils.file_writer(cache_pl_file, pipeline)
def main(): print "\nbegin handle lb bindings\n" current_folder = utils.get_current_folder() for filename in os.listdir(current_folder): if filename.startswith(consts.Prefix["lb_name_prefix"]): lb_file = filename print "\nfind lb file " + filename if not os.path.exists(current_folder + lb_file): raise "lb file for {} not exists! please check task of init lb".format( lb_file) lb_datas = get_lb(lb_file) lb_name = lb_datas["name"] print "\nbegin handle lb {} \n".format(lb_name) for frontend in lb_datas["frontends"]: port = frontend["port"] if utils.no_task_record(lb_file + "_frontend_" + str(port)): if frontend["protocol"] == "tcp": print "begin check if frontend had been used " check_frontend_used(lb_name, port) print "begin create frontend for lb {} and port {}".format( lb_name, port) create_frontend(lb_name, frontend) utils.task_record(lb_file + "_frontend_" + str(port)) if frontend["protocol"] == "tcp": print "\nskip tcp frontend {} rules create\n".format(port) continue rules = frontend["rules"] for rule in rules: if utils.no_task_record(lb_file + "_frontend_" + str(port) + "_rule_" + rule["rule_id"]): # if rule["type"] == "system": # continue print "\nbegin create rule for domain {} and port {}".format( rule["domain"], port) rule_info = create_rule(lb_name, port, copy.deepcopy(rule)) # record rule create task utils.task_record(lb_file + "_frontend_" + str(port) + "_rule_" + rule["rule_id"]) print "\nnew rule info is: {}\n".format( json.dumps(rule_info)) if "services" in rule and len(rule["services"]) > 0: bind_rule_svc(lb_name, frontend["port"], rule_info["data"]["rule_id"], rule["services"])
def handle_pipeline(): print "\nbegin update pipeline data\n" current_folder = utils.get_current_folder() for filename in os.listdir(current_folder): if filename.startswith(consts.Prefix["pipeline_name_prefix"]): pipeline_file = filename if utils.no_task_record(pipeline_file): print "\nfind pipeline file " + filename if not os.path.exists(current_folder + pipeline_file): raise "pipeline file for {} not exists! please check task of init lb".format( pipeline_file) pipeline_data = utils.file_reader(current_folder + pipeline_file) pipeline_name = pipeline_data["name"] print "\nbegin handle pipeline {} \n".format(pipeline_name) transed_pipeline = trans_pipeline(pipeline_data) print "transed pipeline data is {}".format( json.dumps(transed_pipeline)) print "\n begin update pipeline {}".format( transed_pipeline["name"]) update_pipeline(transed_pipeline) utils.task_record(pipeline_file)
def init_svc_list(): svc_list = get_svc_list_for_current_project(results=[]) file_name = utils.get_current_folder() + consts.Prefix["service_list_file"] utils.file_writer(file_name, svc_list)
def create_app(data): url = consts.URLS["create_app"] app_create_data_file = utils.get_current_folder( ) + consts.Prefix["app_create_data_prefix"] + data["resource"]["name"] utils.file_writer(app_create_data_file, data) return utils.send_request("POST", url, data)
def init_app_list(): app_list = get_app_list_for_current_project(results=[]) file_name = utils.get_current_folder() + consts.Prefix["app_list_file"] utils.file_writer(file_name, app_list)
def get_svc_lb(svc_name): return utils.file_reader(utils.get_current_folder() + consts.Prefix["lb_name_prefix"] + svc_name)
def get_lb(lb_file): return utils.file_reader(utils.get_current_folder() + lb_file)