Beispiel #1
0
def bind_rule_svc(lb_name, port, rule_id, service_list):
    url = consts.URLS["lb_bind_svc_rule"].format(lb_name_or_id=lb_name,
                                                 port=port,
                                                 rule_id=rule_id)
    data = []
    for service in service_list:
        if service["service_id"] and service["service_name"]:
            service_name = service["service_name"].lower()
            new_services = services.get_new_svc_by_name(service_name)
            if len(new_services) == 0:
                print "find new service failed because no service named {}, will skipped for lb {} and port {}".format(
                    service_name, lb_name, port)
                continue
            print "find new service for {}, {}".format(
                service_name, json.dumps(new_services))
            data.append({
                "service_id": new_services[0]["resource"]["uuid"],
                "container_ports": [service["container_port"]],
                "container_port": service["container_port"],
                "weight": service["weight"]
            })
        else:
            print "find service with no id and name when bind for lb {} and port {},will be skipped".format(
                lb_name, port)

    print "begin bind new service for {} and port {} and rule {}, service_list is {}".format(
        lb_name, port, rule_id, json.dumps(service_list))
    utils.send_request("POST", url, data)
Beispiel #2
0
def sync_ns():
    sqls = ""
    sql_tpl = "insert into resources_resource values(nextval('resources_resource_id_seq'::regclass),'NAMESPACE'," \
              "'{uuid}','{namespace}','{created_by}','2018-06-20 11:50:15','{region_id}:{k8s_ns_name}','{region_id}'," \
              "'','{project_uuid}','');\n"
    if consts.Configs["db_engine"] == "mysql":
        sql_tpl = "insert into resources_resource(`type`,`uuid`,`namespace`,`created_by`,`created_at`,`name`,`region_id`," \
                  "`space_uuid`,`project_uuid`,`namespace_uuid`) values('NAMESPACE','{uuid}','{namespace}','{created_by}'," \
                  "'2018-06-20 11:50:15','{region_id}:{k8s_ns_name}','{region_id}','','{project_uuid}','');\n"

    projects = utils.get_projects()
    for pro in projects:
        name = pro["name"]
        print "begin sync namespace for project {} \n".format(name or "default")
        resource_ns = utils.send_request("GET", consts.URLS["get_resource_ns"], specific_project=name)
        for ns in resource_ns:
            # k8s_ns_name = "default--" + ns["name"]
            k8s_ns_name = ns["name"]
            print "begin build sync sql of namespace for project {} and resource_namespace {}".format(
                name or "default", k8s_ns_name)
            k8s_ns = get_k8s_ns_by_name(k8s_ns_name)
            if k8s_ns:
                sql = sql_tpl.format(uuid=k8s_ns["uid"], namespace=consts.Configs["namespace"],
                                     region_id=utils.get_region_info("id"), created_by=consts.Configs["namespace"],
                                     k8s_ns_name=k8s_ns_name, project_uuid=pro["uuid"])
                sqls += sql
    print sqls
Beispiel #3
0
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)
Beispiel #4
0
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)
Beispiel #5
0
def create_rule(lb_name, frontend_port, rule):
    unused_keys = ["priority", "services", "type", "rule_id"]
    for key in unused_keys:
        if key in rule:
            del rule[key]
    return utils.send_request(
        "POST", consts.URLS["lb_create_rule"].format(lb_name_or_id=lb_name,
                                                     port=frontend_port), rule)
Beispiel #6
0
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)
Beispiel #7
0
def get_all_pipelines_for_current_project(results=None):
    if results is None:
        results = []
    pipeline_list = utils.send_request('GET', consts.URLS['get_all_pipelines'])
    results.extend(pipeline_list['results'])
    # has to be len(svc_list['results']), not svc_list['count']
    if len(pipeline_list['results']) >= 100:
        get_all_pipelines_for_current_project(results)
    return results
Beispiel #8
0
def get_app_list_for_current_project(results=None):
    if results is None:
        results = []
    app_list = utils.send_request('GET', consts.URLS['get_application_list'])
    results.extend(app_list)
    # has to be len(app_list)
    if len(app_list) >= 100:
        get_app_list_for_current_project(results)
    return results
Beispiel #9
0
def sync_ns_v2():
    # resource namespace and app_name+space_name
    resource_ns = utils.send_request("GET", consts.URLS["get_resource_ns"])
    add_default = True
    for ns in resource_ns:
        if ns["name"] == "default":
            add_default = False
    if add_default:
        resource_ns.append({"name": "default"})
    ns_handler(resource_ns)
Beispiel #10
0
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 is_healthy(self, es_host_name):
     """Check whether elasticsearch is healthy"""
     try:
         url = utils.build_url(self.host, ["_cluster/health"])
         res = utils.send_request(url, "GET")
         return res["status"] in ["green", "yellow"]
     except Exception as err:
         logger.error("Elasticsearch is not healthy")
         logger.error(err)
         return False
Beispiel #12
0
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)
Beispiel #13
0
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)
Beispiel #14
0
def create_frontend(lb_name, frontend):
    protocol = frontend["protocol"]
    data = {
        "certificate_id": frontend["certificate_id"],
        "certificate_name": frontend["certificate_name"],
        "container_port": frontend["container_port"],
        "port": frontend["port"],
        "service_id": "",
        "protocol": frontend["protocol"],
        "container_ports": []
        # "space_id": frontend["space_id"],
        # "space_name": frontend["space_name"]
    }
    if frontend["service_id"] and frontend["service_name"]:
        new_svcs = services.get_new_svc_by_name(
            frontend["service_name"].lower())
        data["service_id"] = new_svcs[0]["resource"]["uuid"]

    utils.send_request("POST",
                       consts.URLS["lb_frontends"].format(lb_name=lb_name),
                       data)
Beispiel #15
0
def get_app_by_name(app_name):
    apps = utils.send_request(
        "GET", consts.URLS["search_app"].format(app_name=app_name))
    result = []
    if apps["count"] > 1:
        for app in apps["results"]:
            if app["resource"]["name"] == app_name:
                result.append(app)
                break
    if apps["count"] == 0:
        print "Attention!!! not found new app named {}".format(app_name)
    # raise Exception("find {count} instances for {svc_name} ".format(count=data["count"], svc_name=svc_name))
    return result or apps["results"]
Beispiel #16
0
def update_app(app):
    kubernetes = app["kubernetes"]
    for control in kubernetes:
        if control["apiVersion"] == "extensions/v1beta1":
            service_id = control["metadata"]["labels"][
                "service.alauda.io/uuid"]
            service_name = control["metadata"]["name"].lower()
            control["metadata"]["labels"]["alauda_service_id"] = service_id
            control["metadata"]["labels"]["service_name"] = service_name
            control["spec"]["template"]["metadata"]["labels"][
                "alauda_service_id"] = service_id
            control["spec"]["template"]["metadata"]["labels"][
                "service_name"] = service_name
            # LabelSelector
            control["spec"]["selector"] = {
                "matchLabels": {
                    "service.alauda.io/uuid": service_id
                }
            }
    data = {"namespace": app["cluster"]["name"], "kubernetes": kubernetes}
    utils.send_request(
        "PATCH",
        consts.URLS["get_app_by_id"].format(app_id=app["resource"]["uuid"]),
        data)
Beispiel #17
0
def mock_sync_ns():
    resource_ns = utils.send_request("GET", consts.URLS["get_resource_ns"])
    add_default = True
    for ns in resource_ns:
        if ns["name"] == "default":
            add_default = False
    if add_default:
        resource_ns.append({"name": "default"})
    for ns in resource_ns:
        req_params = {
            "apiVersion": "v1",
            "kind": "Namespace",
            "metadata": {
                "name": ns["name"]
            }
        }
        print "begin sync namespace {} with params ".format(ns["name"], json.dumps(req_params))
Beispiel #18
0
def get_new_svc_by_name(svc_name):
    url = consts.URLS["get_svc_v2"].format(service_name=svc_name.lower())
    project_name = project.get_project_by_svc_name(svc_name)
    print "find svc {} was in project {}".format(svc_name, project_name)
    data = utils.send_request('GET', url, specific_project=project_name)
    result = []
    if "errors" in data:
        print "find new svc by name ERROR!!! not found new svc named {}".format(
            svc_name)
        print data
        return result
    if data["count"] > 1:
        for svc in data["results"]:
            if svc["resource"]["name"] == svc_name:
                result.append(svc)
                break
    if data["count"] == 0:
        print "Attention!!! not found new svc named {}".format(svc_name)
    #    raise Exception("find {count} instances for {svc_name} ".format(count=data["count"], svc_name=svc_name))
    return result or data["results"]
Beispiel #19
0
def ns_handler(resource_ns):
    for ns in resource_ns:
        req_params = {
            "apiVersion": "v1",
            "kind": "Namespace",
            "metadata": {
                "name": ns["name"]
            }
        }
        print "begin sync namespace {} ".format(ns["name"])
        res = utils.send_request("POST", consts.URLS["create_get_ns"], req_params)
        if isinstance(res, dict) and "result" in res and len(res["result"]) > 0:
            print "create namespace for {} success".format(ns["name"])
        elif isinstance(res, list) and len(res) > 0 and "kubernetes" in res[0]:
            print "create namespace for {} success , list".format(ns["name"])
        elif isinstance(res, dict) and "errors" in res and \
                res["errors"][0]["message"].find("duplicate key value violates unique constraint") >= 0:
            print res["errors"][0]["message"] + ", will be skipped"
        else:
            print "create namespace {} error!!!".format(ns["name"])
            print res
            exit(1)
Beispiel #20
0
def get_app_by_api(app_id):
    return utils.send_request(
        "GET", consts.URLS["get_app_by_id"].format(app_id=app_id))
Beispiel #21
0
def update_pipeline(pipeline):
    utils.send_request(
        "PUT", consts.URLS["get_or_update_pipeline"].format(
            pipeline_id=pipeline["uuid"]), pipeline)
Beispiel #22
0
def get_pipeline_detail(pl_id):
    pipeline = utils.send_request(
        "GET", consts.URLS["get_or_update_pipeline"].format(pipeline_id=pl_id))
    return pipeline
Beispiel #23
0
def delete_old_application(app_id):
    utils.send_request("DELETE", consts.URLS["get_or_delete_application_detail"].format(app_id=app_id))
Beispiel #24
0
def delete_tcp_frontend(lb_name, port):
    url = consts.URLS["lb_create_listener"].format(lb_name_or_id=lb_name,
                                                   port=port)
    utils.send_request("DELETE", url)
Beispiel #25
0
def get_lb_frontends(lb_name):
    url = consts.URLS["lb_frontends"].format(lb_name=lb_name)
    lb_ft = utils.send_request("GET", url)
    print "lb frontends for {}".format(lb_name)
    # print lb_ft
    return lb_ft
Beispiel #26
0
def get_alauda_namespaces():
    ns = utils.send_request("GET", consts.URLS["create_get_ns"] + "?page=1&page_size=100")
    return ns["results"]
Beispiel #27
0
def delete_old_svc(svc_id):
    utils.send_request(
        "DELETE",
        consts.URLS["get_or_delete_svc_detail"].format(service_id=svc_id))
Beispiel #28
0
def get_v1_svc_by_api(svc_id):
    return utils.send_request(
        "GET",
        consts.URLS["get_or_delete_svc_detail"].format(service_id=svc_id))
 def list_indices(self):
     """Get all indices from elasticsearch"""
     url = utils.build_url(self.host, ["_cat", "indices?format=json"])
     res = utils.send_request(url, "GET")
     return res
Beispiel #30
0
def get_v1_app_by_api(app_id):
    return utils.send_request("GET", consts.URLS["get_or_delete_application_detail"].format(app_id=app_id))