def execute_job(**kwargs): logger_celery.info("kwargs:{}".format(kwargs)) username = kwargs.get('username') bk_service = BkService(username=username) info = bk_service.execute_job(**kwargs) logger_celery.info("execute_job=>{}".format(info)) job_instance_id = info.get('data').get('job_instance_id') job_kwargs = { 'bk_biz_id': kwargs.get('bk_biz_id'), 'job_instance_id': job_instance_id } job_status = bk_service.get_job_instance_status(**job_kwargs) logger_celery.info("get_job_instance_status=>{}".format(job_status)) if job_status.get('result'): log_info = bk_service.get_job_instance_log(**job_kwargs) logger_celery.info("get_job_instance_log=>{}".format(log_info)) if log_info.get('result'): log_datas = log_info.get('data') for log_data in log_datas: if log_data.get('is_finished'): log_step_results = log_data.get('step_results') for log_step_result in log_step_results: ip_logs = log_step_result.get('ip_logs') for ip_log in ip_logs: opt = Operation() opt.user = username opt.biz = kwargs.get('bk_biz_id') opt.machine_list = ip_log.get('ip') opt.result = log_step_result.get('ip_status') opt.log = ip_log.get('log_content') opt.save()
def create_task(request): bk_service = BkService(request=request) kwargs = { 'bk_biz_id': 2 } info = bk_service.create_task(**kwargs) return JsonResponse(info)
def search_business(request): bk_service = BkService(request=request) page = int(request.GET.get('page', 1)) page_size = int(request.GET.get('page_size', 10)) no_page = bool(request.GET.get('no_page', False)) bk_page = BkPageManage(func=bk_service.search_business, page=page, limit=page_size, no_page=no_page)() result, count = bk_page.get_result() data = { 'total': count, 'data': result } return JsonResponse(data)
def execute_job(request): bk_service = BkService(request=request) body = request.body body = json.loads(request.body) if body else {} job_form = JobForm(body) if not job_form.is_valid(): return JsonResponse(job_form.get_errors()) bk_biz_id = job_form.cleaned_data.get('bk_biz_id') bk_job_id = job_form.cleaned_data.get('bk_job_id') kwargs = { 'username': request.user.username, 'bk_biz_id': bk_biz_id, 'bk_job_id': bk_job_id } job_detail = bk_service.get_job_detail(**kwargs) steps = job_detail.get('data').get('steps') for step in steps: step['ip_list'] = body.get('ip_list') kwargs.update({ 'steps': steps }) celery_task.execute_job.delay(**kwargs) return JsonResponse({'result': True})
def search_set(request): bk_service = BkService(request=request) bk_biz_id = request.GET.get('bk_biz_id', 0) page = int(request.GET.get('page', 1)) page_size = int(request.GET.get('page_size', 10)) no_page = bool(request.GET.get('no_page', False)) kwargs = { 'bk_biz_id': bk_biz_id } bk_page = BkPageManage(func=bk_service.search_set, page=page, limit=page_size, no_page=no_page, **kwargs)() result, count = bk_page.get_result() data = { 'total': count, 'data': result } return JsonResponse(data)
def search_host(request): bk_service = BkService(request=request) body = request.body body = json.loads(request.body) if body else {} page = int(body.get('page', 1)) page_size = int(body.get('page_size', 10)) no_page = bool(body.get('no_page', False)) bk_biz_id = body.get('bk_biz_id', 0) bk_set_id = body.get('bk_set_id', 0) bk_module_id = body.get('bk_module_id', 0) ip = list(body.get('ip', [])) kwargs = { 'condition': [ { "bk_obj_id": "host", "fields": ['bk_host_name', 'bk_host_innerip', 'bk_host_outerip'], "condition": [] }, { "bk_obj_id": "biz", "fields": ['bk_biz_id', 'bk_biz_name'], "condition": [] }, { "bk_obj_id": "set", "fields": ['bk_set_id', 'bk_set_name'], "condition": [] }, { "bk_obj_id": "module", "fields": ["bk_module_id", 'bk_module_name'], "condition": [] } ] } if bk_biz_id: kwargs.update({ 'bk_biz_id': bk_biz_id, }) if bk_set_id: kwargs.get('condition')[2] = { "bk_obj_id": "set", "fields": ['bk_set_id', 'bk_set_name'], "condition": [ { "field": "bk_set_id", "operator": "$eq", "value": bk_set_id } ] } if bk_module_id: kwargs.get('condition')[3] = { "bk_obj_id": "module", "fields": ['bk_module_id', 'bk_module_name'], "condition": [ { "field": "bk_module_id", "operator": "$eq", "value": bk_module_id } ] } if ip: kwargs.update( { "ip": { "data": ip, "exact": 1, "flag": "bk_host_innerip|bk_host_outerip" }, } ) bk_page = BkPageManage(func=bk_service.search_host, page=page, limit=page_size, no_page=no_page, **kwargs)() result, count = bk_page.get_result() data = { 'total': count, 'data': result } return JsonResponse(data)