From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 5DD9DB2F9 for ; Wed, 23 Nov 2022 16:01:59 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 33D7223F0B for ; Wed, 23 Nov 2022 16:01:29 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Wed, 23 Nov 2022 16:01:26 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id D957E44DA8; Wed, 23 Nov 2022 15:53:02 +0100 (CET) From: Daniel Tschlatscher To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com, pmg-devel@lists.proxmox.com Date: Wed, 23 Nov 2022 15:52:08 +0100 Message-Id: <20221123145210.592981-3-d.tschlatscher@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221123145210.592981-1-d.tschlatscher@proxmox.com> References: <20221123145210.592981-1-d.tschlatscher@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.130 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pmg-devel] [PATCH pmg-api v4] make tasklog downloadable in the PMG backend X-BeenThere: pmg-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Mail Gateway development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Nov 2022 15:01:59 -0000 The read_tasklog API call now stream the whole log file if the query parameter 'download' is set to true. This is done in preparation for the task log download button to be added in the TaskViewer. I saw an opportunity here to clear some redundant code for displaying the tasklog and replaced it with a call to dump_logfile(), akin to how this is handled in pve-manager. Signed-off-by: Daniel Tschlatscher --- Changes from v3: * API parameter 'download' for directly streaming the file * Added missing API parameter descriptions src/PMG/API2/Tasks.pm | 60 ++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/src/PMG/API2/Tasks.pm b/src/PMG/API2/Tasks.pm index 598fb4d..5a6f002 100644 --- a/src/PMG/API2/Tasks.pm +++ b/src/PMG/API2/Tasks.pm @@ -238,12 +238,19 @@ __PACKAGE__->register_method({ type => 'integer', minimum => 0, optional => 1, + description => "Start at this line when reading the tasklog", }, limit => { type => 'integer', minimum => 0, optional => 1, + description => "The amount of lines to read from the tasklog.", }, + download => { + type => 'boolean', + optional => 1, + description => "Whether the tasklog file should be downloaded. This parameter can't be used in conjunction with other parameters", + } }, }, returns => { @@ -268,37 +275,42 @@ __PACKAGE__->register_method({ my ($task, $filename) = PVE::Tools::upid_decode($param->{upid}, 1); raise_param_exc({ upid => "unable to parse worker upid" }) if !$task; - my $lines = []; + my $download = $param->{download} // 0; - my $restenv = PMG::RESTEnvironment->get(); + if ($download) { + die "Parameter 'download' can't be used with other parameters\n" + if (defined($param->{start}) || defined($param->{limit})); - my $fh = IO::File->new($filename, "r"); - raise_param_exc({ upid => "no such task - unable to open file - $!" }) if !$fh; + my $fh; + my $use_compression = ( -s $filename ) > 1024; - my $start = $param->{start} || 0; - my $limit = $param->{limit} || 50; - my $count = 0; - my $line; - while (defined ($line = <$fh>)) { - next if $count++ < $start; - next if $limit <= 0; - chomp $line; - push @$lines, { n => $count, t => $line}; - $limit--; - } + # 1024 is a practical cutoff for the size distribution of our log files. + if ($use_compression) { + open($fh, "-|", "/usr/bin/gzip", "-c", "$filename") + or die "Could not create compressed file stream for file '$filename' - $!\n"; + } else { + open($fh, '<', $filename) or die "Could not open file '$filename' - $!\n"; + } - close($fh); + return { + download => { + fh => $fh, + stream => 1, + 'content-encoding' => $use_compression ? 'gzip' : undef, + 'content-type' => "text/plain", + 'content-disposition' => "attachment; filename=\"".$param->{upid}."\"", + }, + }, + } else { + my $start = $param->{start} // 0; + my $limit = $param->{limit} // 50; - # HACK: ExtJS store.guaranteeRange() does not like empty array - # so we add a line - if (!$count) { - $count++; - push @$lines, { n => $count, t => "no content"}; - } + my ($count, $lines) = PVE::Tools::dump_logfile($filename, $start, $limit); - $restenv->set_result_attrib('total', $count); + PMG::RESTEnvironment->get()->set_result_attrib('total', $count); - return $lines; + return $lines; + } }}); -- 2.30.2