예제 #1
0
def list_service(request):
    """
    서비스 목록 페이지
    :param request:
    :return:
    """
    if request.method == 'POST':
        logger.info("listService_ajax")
        control = ControlEngine(request.session.get("ctrl_header"))
        # TODO: 알람을 위해 가데이터 받아오기
        # control = ControllerEngine()  # TODO: debugging 용
        service_list = control.get_service_list()
        logger.info("listService_ajax_end")
        return JsonResponse(service_list)
    else:
        token = request.session.get('passToken')
        if not token:
            return redirect("/dashboard/domains/?next=/dashboard/service")
        logger.info("listService_end")
        if request.GET.get("error_msg_list"):
            error_msg_list = request.GET.get("error_msg_list")
            return render(request, 'service/index.html',
                          {"error_msg_list": error_msg_list})
        return render(request, 'service/index.html', {})
예제 #2
0
def synchronize_floating_server_host(request):
    if request.method == 'POST' and request.is_ajax():
        token = request.session.get("passToken")
        auth_url = request.session.get("auth_url")
        project_id = request.session.get("project_id")
        auth = ZabbixRestAPI.get_auth("Admin", "zabbix")
        zabbix = ZabbixRestAPI(auth)
        neutron = NeutronRestAPI(auth_url, token)
        err_msg_list = []
        # openstack의 floating_ip 목록 가져오기
        q = {"project_id": project_id}
        result_floating_ips = neutron.get_floating_ips(None, q)
        create_host_flag = False
        # host목록 가져오기
        result_hosts = zabbix.get_hosts(output=["hostid", "host", "status", "name"])
        if result_hosts.get("result") and result_floating_ips.get("success"):
            floating_ips = result_floating_ips["success"].get("floatingips")  # 중요1
            hosts = result_hosts.get("result")
            # floating_ips 목록이 Zabbix에 전부 등록되있는지 확인
            # TODO: zabbix에 등록된 host중 floatingip연결을 해제시키거나 삭제된 server가 있으면 제거
            recovery_list = []  # 중요3
            for floating_ip in floating_ips:
                if floating_ip.get("port_id"):
                    # floatingip - port 조회
                    result_port = neutron.getPort(floating_ip["port_id"])
                    if result_port.get("success"):
                        port = result_port["success"].get("port")
                        floating_ip["port_details"] = port
                        server_id = port.get("device_id")
                        host_names = [temp_host.get("host") for temp_host in hosts]
                        # 각 가상머신의 호스트가 등록되있지 않다면 등록 중요3
                        if server_id and "compute" in port.get("device_owner") and server_id not in host_names:
                            # floatingip - port - device(서버) 조회
                            recovery_info = {
                                "server": {
                                    "vm_id": server_id,
                                    "floating_ip_id": floating_ip.get("id"),
                                    "floating_ip_address": floating_ip.get("floating_ip_address"),
                                    "fixed_ips": port.get("fixed_ips")
                                },
                                "auth_url": auth_url,
                                "project_name": request.session.get("project_name")
                            }
                            recovery_list.append(recovery_info)
                    if result_port.get("error"):
                        err_msg_list.append(result_port["error"])

            if len(recovery_list) > 0:
                # 서비스 리스트 조회 -> 서비스 조회 -> vm_list에서 같은아이디 있는지 확인
                ctrl_header = request.session.get("ctrl_header")
                control = ControlEngine(ctrl_header)
                result_service_list = control.get_service_list()
                if result_service_list.get("success"):
                    for service in result_service_list["success"].get("service_list"):
                        service_id = service.get("service_id")
                        result_service = control.get_service(service_id)
                        if result_service.get("success"):
                            for recovery_info in recovery_list:
                                for vm in result_service["success"]["service_detail"].get("vm_list"):
                                    # 서비스내에 해당 server가 있는지 확인
                                    if vm.get("vm_id") == recovery_info["server"].get("vm_id"):
                                        recovery_info["server"]["vm_name"] = vm.get("vm_name")
                                        recovery_info["server"]["service_id"] = service_id
                        if result_service.get("error"):
                            err_msg_list.append(result_service["error"])
                if result_service_list.get("error"):
                    err_msg_list.append(result_service_list["error"])
                            # service_list 조회끝
            for recovery_info in recovery_list:
                logger.debug("\n\n\n\n" + json.dumps(recovery_info) + "\n\n\n\n")
                # hostgroup 조회
                result_hostgroup = zabbix.get_hostgroups({"name": "Linux servers"}, ["groupid"])
                if result_hostgroup.get("result"):
                    hostgroup_id = result_hostgroup["result"][0].get("groupid")
                    result_create_host = create_host(request, zabbix, recovery_info, hostgroup_id)
                    if len(result_create_host) < 1:
                        create_host_flag = True
                    else:
                        err_msg_list.append(result_create_host)
                if not result_hostgroup.get("result"):
                    err_msg_list.append(result_hostgroup)

            # 호스트 생성시 호스트 목록 다시조회
            if create_host_flag:
                result_hosts = zabbix.get_hosts(output=["hostid", "host", "status", "name"])
                if result_hosts.get("result"):
                    hosts = result_hosts["result"]
        if not result_hosts.get("result"):
            err_msg_list.append(result_hosts)
        if result_floating_ips.get("error"):
            err_msg_list.append(result_floating_ips["error"])
        return JsonResponse({"success": {"err_msg_list": err_msg_list}})