def delete(self, request): form, error = JsonParser(Argument('id', type=int, help='参数错误'), Argument('file', help='请输入文件路径')).parse(request.GET) if error is None: if not has_host_perm(request.user, form.id): return json_response(error='无权访问主机,请联系管理员') host = Host.objects.get(pk=form.id) if not host: return json_response(error='未找到指定主机') with host.get_ssh() as ssh: ssh.remove_file(form.file) return json_response(error=error)
def get(self, request): form, error = JsonParser(Argument('id', type=int, help='参数错误'), Argument('path', help='参数错误')).parse(request.GET) if error is None: if not has_host_perm(request.user, form.id): return json_response(error='无权访问主机,请联系管理员') host = Host.objects.get(pk=form.id) if not host: return json_response(error='未找到指定主机') objects = fetch_dir_list(host, form.path) return json_response(objects) return json_response(error=error)
def do_task(request): form, error = JsonParser( Argument('host_ids', type=list, filter=lambda x: len(x), help='请选择执行主机'), Argument('command', help='请输入执行命令内容'), Argument('interpreter', default='sh'), Argument('template_id', type=int, required=False) ).parse(request.body) if error is None: if not has_host_perm(request.user, form.host_ids): return json_response(error='无权访问主机,请联系管理员') token, rds = uuid.uuid4().hex, get_redis_connection() for host in Host.objects.filter(id__in=form.host_ids): data = dict( key=host.id, name=host.name, token=token, interpreter=form.interpreter, hostname=host.hostname, port=host.port, username=host.username, command=form.command, pkey=host.private_key, ) rds.rpush(settings.EXEC_WORKER_KEY, json.dumps(data)) form.host_ids.sort() host_ids = json.dumps(form.host_ids) tmp_str = f'{form.interpreter},{host_ids},{form.command}' digest = hashlib.md5(tmp_str.encode()).hexdigest() record = ExecHistory.objects.filter(user=request.user, digest=digest).first() if form.template_id: template = ExecTemplate.objects.filter(pk=form.template_id).first() if not template or template.body != form.command: form.template_id = None if record: record.template_id = form.template_id record.updated_at = human_datetime() record.save() else: ExecHistory.objects.create( user=request.user, digest=digest, interpreter=form.interpreter, template_id=form.template_id, command=form.command, host_ids=json.dumps(form.host_ids), ) return json_response(token) return json_response(error=error)
def get(self, request): form, error = JsonParser(Argument('id', type=int, help='参数错误'), Argument('file', help='请输入文件路径')).parse(request.GET) if error is None: if not has_host_perm(request.user, form.id): return json_response(error='无权访问主机,请联系管理员') host = Host.objects.filter(pk=form.id).first() if not host: return json_response(error='未找到指定主机') filename = os.path.basename(form.file) ssh_cli = host.get_ssh().get_client() sftp = ssh_cli.open_sftp() f = sftp.open(form.file) return FileResponseAfter(ssh_cli.close, f, as_attachment=True, filename=filename) return json_response(error=error)
def post(self, request): form, error = JsonParser( Argument('id', type=int, help='参数错误'), Argument('token', help='参数错误'), Argument('path', help='参数错误'), ).parse(request.POST) if error is None: if not has_host_perm(request.user, form.id): return json_response(error='无权访问主机,请联系管理员') file = request.FILES.get('file') if not file: return json_response(error='请选择要上传的文件') host = Host.objects.get(pk=form.id) if not host: return json_response(error='未找到指定主机') rds_cli = get_redis_connection() callback = partial(self._compute_progress, rds_cli, form.token, file.size) with host.get_ssh() as ssh: ssh.put_file_by_fl(file, f'{form.path}/{file.name}', callback=callback) return json_response(error=error)