Skip to content

Commit b25b1f7

Browse files
committed
fix: fix docker log
1 parent 7120faa commit b25b1f7

16 files changed

Lines changed: 85 additions & 8 deletions

File tree

‎agent/app/service/container.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,10 @@ func writeContainerLogMessage(w io.Writer, done <-chan struct{}, messages <-chan
880880
// The collector closes both channels when it finishes. Drain all buffered
881881
// logs before handling its result, or a ready error channel can drop the tail.
882882
if err := <-errors; err != nil {
883+
if err == errLogContainerMissing {
884+
_, _ = fmt.Fprint(w, "event: container-missing\ndata: {}\n\n")
885+
return false
886+
}
883887
_, _ = fmt.Fprintf(w, "event: error\ndata: %v\n\n", err)
884888
}
885889
return false
@@ -889,6 +893,10 @@ func writeContainerLogMessage(w io.Writer, done <-chan struct{}, messages <-chan
889893
func collectLogs(done <-chan struct{}, params dto.StreamLog, messageChan chan<- string, errorChan chan<- error) {
890894
defer close(messageChan)
891895
defer close(errorChan)
896+
if err := checkLogContainers(params); err != nil {
897+
errorChan <- err
898+
return
899+
}
892900
var cmdArgs []string
893901
cmdArgs = append(cmdArgs, "logs")
894902
if params.Follow {

‎agent/app/service/logs.go‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package service
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/base64"
67
"encoding/json"
8+
"errors"
79
"fmt"
810
"os"
911
"os/exec"
@@ -16,6 +18,7 @@ import (
1618
"github.com/1Panel-dev/1Panel/agent/app/dto"
1719
"github.com/1Panel-dev/1Panel/agent/global"
1820
"github.com/1Panel-dev/1Panel/agent/i18n"
21+
"github.com/1Panel-dev/1Panel/agent/utils/common"
1922
"github.com/1Panel-dev/1Panel/agent/utils/re"
2023
)
2124

@@ -567,3 +570,47 @@ func (u *LogService) ListSystemLogFile() ([]string, error) {
567570

568571
return listFile, nil
569572
}
573+
574+
var errLogContainerMissing = errors.New("log container missing")
575+
576+
func checkLogContainers(params dto.StreamLog) error {
577+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
578+
defer cancel()
579+
command := "docker"
580+
args := []string{"container", "inspect", "--format", "{{.Id}}", params.Container}
581+
if params.Type == "compose" {
582+
command = common.GetDockerComposeCommand()
583+
args = nil
584+
if command != "docker-compose" {
585+
command = "docker"
586+
args = append(args, "compose")
587+
}
588+
for _, file := range strings.Split(params.Compose, ",") {
589+
if file != "" {
590+
args = append(args, "-f", file)
591+
}
592+
}
593+
args = append(args, "ps", "--all", "--quiet")
594+
}
595+
cmd := exec.CommandContext(ctx, command, args...)
596+
var stderr bytes.Buffer
597+
cmd.Stderr = &stderr
598+
output, err := cmd.Output()
599+
if err != nil {
600+
return logContainerPresenceResult(params.Type, stderr.String(), err)
601+
}
602+
return logContainerPresenceResult(params.Type, string(output), nil)
603+
}
604+
605+
func logContainerPresenceResult(kind, output string, err error) error {
606+
if err != nil {
607+
if kind != "compose" && (strings.Contains(output, "No such container:") || strings.Contains(output, "No such object:")) {
608+
return errLogContainerMissing
609+
}
610+
return fmt.Errorf("check log containers: %w: %s", err, strings.TrimSpace(output))
611+
}
612+
if strings.TrimSpace(output) == "" {
613+
return errLogContainerMissing
614+
}
615+
return nil
616+
}

‎frontend/src/components/log/container/index.vue‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ const searchLogs = async () => {
258258
}
259259
const source = new EventSource(url);
260260
eventSource = source;
261+
source.addEventListener('container-missing', () => {
262+
if (version !== searchVersion) return;
263+
stopListening();
264+
const message = i18n.global.t('container.logsUnavailable');
265+
term?.writeln(message);
266+
MsgError(message);
267+
});
261268
source.onmessage = (event: MessageEvent) => {
262269
if (version !== searchVersion) return;
263270
term?.write(Base64.toUint8Array(event.data));

‎frontend/src/lang/modules/en.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,6 +2566,8 @@ const message = {
25662566
missing: 'Missing',
25672567
},
25682568
container: {
2569+
logsUnavailable:
2570+
'The container does not exist or has been deleted. Container logs are unavailable. Recreate the container and try again.',
25692571
nvidiaSupport: 'NVIDIA GPU support',
25702572
nvidiaHelper:
25712573
'Register the NVIDIA container runtime. Install NVIDIA Container Toolkit first. Assign GPUs to each container separately.',

‎frontend/src/lang/modules/es-es.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,8 @@ const message = {
26012601
missing: 'No existe',
26022602
},
26032603
container: {
2604+
logsUnavailable:
2605+
'El contenedor no existe o se ha eliminado. Los registros del contenedor no están disponibles. Vuelva a crear el contenedor e inténtelo de nuevo.',
26042606
nvidiaSupport: 'Compatibilidad con GPU NVIDIA',
26052607
nvidiaHelper:
26062608
'Registra el entorno de ejecución de NVIDIA. Instala primero NVIDIA Container Toolkit. Asigna las GPU a cada contenedor por separado.',

‎frontend/src/lang/modules/fa.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,6 +2543,8 @@ const message = {
25432543
missing: 'ناموجود',
25442544
},
25452545
container: {
2546+
logsUnavailable:
2547+
'کانتینر وجود ندارد یا حذف شده است. لاگ‌های کانتینر در دسترس نیستند. کانتینر را دوباره ایجاد کنید و مجدداً تلاش کنید.',
25462548
nvidiaSupport: 'پشتیبانی از GPU انویدیا',
25472549
nvidiaHelper:
25482550
'محیط اجرای کانتینر NVIDIA را ثبت می‌کند. ابتدا NVIDIA Container Toolkit را نصب کنید. GPU را جداگانه به هر کانتینر اختصاص دهید.',

‎frontend/src/lang/modules/ja.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,6 +2549,8 @@ const message = {
25492549
missing: '存在しない',
25502550
},
25512551
container: {
2552+
logsUnavailable:
2553+
'コンテナが存在しないか、削除されています。コンテナのログは表示できません。コンテナを再作成してから、もう一度お試しください。',
25522554
nvidiaSupport: 'NVIDIA GPU サポート',
25532555
nvidiaHelper:
25542556
'NVIDIA コンテナーランタイムを登録します。先に NVIDIA Container Toolkit をインストールしてください。GPU はコンテナーごとに割り当てる必要があります。',

‎frontend/src/lang/modules/ko.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,8 @@ const message = {
25192519
missing: '없음',
25202520
},
25212521
container: {
2522+
logsUnavailable:
2523+
'컨테이너가 존재하지 않거나 삭제되었습니다. 컨테이너 로그를 확인할 수 없습니다. 컨테이너를 다시 생성한 후 재시도하세요.',
25222524
nvidiaSupport: 'NVIDIA GPU 지원',
25232525
nvidiaHelper:
25242526
'NVIDIA 컨테이너 런타임을 등록합니다. 먼저 NVIDIA Container Toolkit을 설치하세요. GPU는 각 컨테이너에 별도로 할당해야 합니다.',

‎frontend/src/lang/modules/lo.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,8 @@ const message = {
24922492
missing: 'ບໍ່ມີ',
24932493
},
24942494
container: {
2495+
logsUnavailable:
2496+
'ຄອນເທນເນີບໍ່ມີຢູ່ ຫຼື ຖືກລຶບແລ້ວ. ບໍ່ສາມາດເບິ່ງບັນທຶກຂອງຄອນເທນເນີໄດ້. ກະລຸນາສ້າງຄອນເທນເນີໃໝ່ ແລ້ວລອງອີກຄັ້ງ.',
24952497
nvidiaSupport: 'ຮອງຮັບ NVIDIA GPU',
24962498
nvidiaHelper:
24972499
'ລົງທະບຽນ NVIDIA runtime ສຳລັບຄອນເທນເນີ. ຕິດຕັ້ງ NVIDIA Container Toolkit ກ່ອນ ແລະ ຈັດສັນ GPU ໃຫ້ແຕ່ລະຄອນເທນເນີແຍກກັນ.',

‎frontend/src/lang/modules/ms.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,8 @@ const message = {
25932593
missing: 'Tiada',
25942594
},
25952595
container: {
2596+
logsUnavailable:
2597+
'Kontena tidak wujud atau telah dipadamkan. Log kontena tidak tersedia. Cipta semula kontena dan cuba lagi.',
25962598
nvidiaSupport: 'Sokongan GPU NVIDIA',
25972599
nvidiaHelper:
25982600
'Daftarkan runtime kontena NVIDIA. Pasang NVIDIA Container Toolkit dahulu. Peruntukkan GPU kepada setiap kontena secara berasingan.',

0 commit comments

Comments
 (0)