def create_image(task_id): """ 创建镜像名称 """ task_info = TaskInfo.objects.filter(task_id=task_id, task_status=1).first() if not task_info: return operation_args = task_info.operation_args args = json.loads(operation_args) image_name = args["image_name"].strip() image_desc = args["image_desc"].strip() image_rank = args["rank"] image_vul_name = args["image_vul_name"].strip() image_info = ImageInfo.objects.filter(image_name=image_name).first() if not image_info: image_info = ImageInfo(image_name=image_name, image_desc=image_desc, rank=image_rank, image_vul_name=image_vul_name) image = None msg = {} try: image = client.images.get(image_name) except Exception as e: image_info.is_ok = False image_info.save() try: images = client.images.pull(image_name) if Image == type(images): image = images else: if len(images) > 0: image = images[0] except ImageNotFound: msg = R.build(msg="%s 不存在") except Exception: traceback.print_exc() msg = R.err(msg="%s 添加失败" % (image_name, )) if image: config = image.attrs["ContainerConfig"] port_list = [] if "ExposedPorts" in config: port_list = config["ExposedPorts"] ports = [] for port in port_list: port = port.replace("/", "").replace("tcp", "").replace("udp", "") ports.append(port) image_port = ",".join(ports) image_info.image_port = image_port image_info.is_ok = True image_info.save() msg = R.ok(msg="%s 添加成功" % (image_name, ), data=json.dumps({"image_port": image_port})) task_info.task_status = 3 else: task_info.task_status = 4 task_info.task_msg = json.dumps(msg) task_info.save()
def create_image(task_id): """ 创建镜像名称 """ task_info = TaskInfo.objects.filter(task_id=task_id, task_status=1).first() if not task_info: return operation_args = task_info.operation_args args = json.loads(operation_args) image_name = args["image_name"].strip() image_info = ImageInfo.objects.filter(image_name=image_name).first() if not image_info: image_desc = image_name image_rank = 2.5 image_vul_name = image_desc image_info = ImageInfo(image_name=image_name, image_desc=image_desc, rank=image_rank, image_vul_name=image_vul_name) image = None msg = {} try: image = client.images.get(image_name) except Exception as e: image_info.is_ok = False image_info.save() try: last_info = {} progress_info = { "total": 0, "progress_count": 0, "progress": round(0.0, 2), } black_list = ["total", "progress_count", "progress"] for line in api_docker_client.pull(image_name, stream=True, decode=True): if "status" in line and "progressDetail" in line and "id" in line: id = line["id"] status = line["status"] if len(line["progressDetail"]) > 0: try: current = line["progressDetail"]["current"] total = line["progressDetail"]["total"] line["progress"] = round((current / total) * 100, 2) if (current / total) > 1: line["progress"] = round(0.99 * 100, 2) except: line["progress"] = round(1 * 100, 2) else: if (("Download" in status or "Pull" in status) and ("complete" in status)) or ("Verifying" in status) or \ ("Layer" in status and "already" in status and "exists" in status): line["progress"] = round(100.00, 2) else: line["progress"] = round(0.00, 2) progress_info[id] = line progress_info["total"] = len(progress_info) - len(black_list) progress_count = 0 for key in progress_info: if key in black_list: continue if 100.00 != progress_info[key]["progress"]: continue progress_count += 1 progress_info["progress_count"] = progress_count progress_info["progress"] = round((progress_count/progress_info["total"])*100, 2) r.set(str(task_id), json.dumps(progress_info,ensure_ascii=False)) print(json.dumps(progress_info, ensure_ascii=False)) last_info = line if "status" in last_info and ("Downloaded newer image for" in last_info["status"] or "Image is up to date for" in last_info["status"]): image = client.images.get(image_name) else: raise Exception except ImageNotFound: msg = R.build(msg="%s 不存在" % (image_name,)) except Exception: traceback.print_exc() msg = R.err(msg="%s 添加失败" % (image_name,)) if image: config = image.attrs["ContainerConfig"] port_list = [] if "ExposedPorts" in config: port_list = config["ExposedPorts"] ports = [] for port in port_list: port = port.replace("/", "").replace("tcp", "").replace("udp", "") ports.append(port) image_port = ",".join(ports) image_info.image_port = image_port image_info.is_ok = True image_info.save() msg = R.ok(msg="%s 添加成功" % (image_name,), data=json.dumps({"image_port": image_port})) task_info.task_status = 3 else: task_info.task_status = 4 task_info.task_msg = json.dumps(msg) task_info.save()
def create_image_task(image_info, user_info, request_ip, image_file=None): """ 创建镜像任务 """ user_id = user_info.id task_id = create_create_image_task(image_info=image_info, user_info=user_info) if user_info.is_superuser: image_name = image_info.image_name image_desc = image_info.image_desc image_vul_name = image_info.image_vul_name image_rank = image_info.rank task_info = TaskInfo.objects.filter(task_id=task_id).first() if image_file: task_msg = {} try: file_info = image_file.read() images = client.images.load(file_info) image = images[0] repo_tags = image.attrs["RepoTags"] if len(repo_tags) == 0: # 移除本地镜像 try: client.images.remove(image.id) except Exception as e: pass task_msg = R.build(msg="文件镜像 Tag 不能为空") else: config = image.attrs["ContainerConfig"] port_list = [] if "ExposedPorts" in config: port_list = config["ExposedPorts"] ports = [] for port in port_list: port = port.replace("/", "").replace("tcp", "").replace("udp", "") ports.append(port) image_name = repo_tags[0] image_port = ",".join(ports) image_info = ImageInfo.objects.filter(image_name=image_name).first() if not image_info: image_info = ImageInfo() image_info.image_name = image_name image_info.image_port = image_port # image_vul_name image_info.image_vul_name = image_name.replace("vulfocus/","") if not image_vul_name else image_vul_name # image_desc image_info.image_desc = image_name.replace("vulfocus/","") if not image_desc else image_desc # rank image_info.rank = 2.5 if image_rank > 5 or image_rank < 0.5 else image_rank image_info.is_ok = True image_info.save() task_info.task_name = "拉取镜像:"+image_name task_info.task_status = 3 task_msg = R.ok(data="%s 添加成功" % (image_name, )) except Exception as e: traceback.print_exc() task_msg = R.err() try: image_info.delete() except: pass task_info.task_status = 4 finally: task_info.task_msg = json.dumps(task_msg) task_info.update_date = timezone.now() task_info.save() elif image_name: # 创建任务 # create_image(task_id=task_id) create_image.delay(task_id) else: R.build(msg="镜像文件或镜像名称不能为空") operation_args = ImageInfoSerializer(image_info).data sys_log = SysLog(user_id=user_id, operation_type="镜像", operation_name="创建", ip=request_ip, operation_value=operation_args["image_vul_name"], operation_args=json.dumps(operation_args)) sys_log.save() else: task_info = TaskInfo.objects.filter(task_id=task_id).first() task_info.task_msg = json.dumps(R.build(msg="权限不足")) task_info.task_status = 3 task_info.update_date = timezone.now() task_info.save() return task_id