Skip to content

Commit

Permalink
fix: log can not look (#2105)
Browse files Browse the repository at this point in the history
Signed-off-by: 张启航 <[email protected]>
  • Loading branch information
ZhangSetSail authored Dec 18, 2024
1 parent 75e5b0f commit 325fb30
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 8 deletions.
10 changes: 8 additions & 2 deletions api/eventlog/db/eventFilePlugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bufio"
"fmt"
eventutil "github.com/goodrain/rainbond/api/eventlog/util"
"github.com/goodrain/rainbond/pkg/component/storage"
"io"
"os"
"path"
Expand All @@ -43,6 +44,7 @@ func (m *EventFilePlugin) SaveMessage(events []*EventLogMessage) error {
if len(events) == 0 {
return nil
}
logrus.Info("init event file plugin save message")
filePath := eventutil.EventLogFilePath(m.HomePath)
if err := util.CheckAndCreateDir(filePath); err != nil {
return err
Expand All @@ -67,7 +69,7 @@ func (m *EventFilePlugin) SaveMessage(events []*EventLogMessage) error {
writeFile.Write([]byte(e.Message))
writeFile.Write([]byte("\n"))
}
return nil
return storage.Default().StorageCli.UploadFileToFile(filename, filename, nil)
}

// MessageData message data 获取指定操作的操作日志
Expand All @@ -92,7 +94,11 @@ func (m *EventFilePlugin) GetMessages(eventID, level string, length int) (interf
if err != nil {
logrus.Errorf("check file exist error %s", err.Error())
}
return message, nil
err = storage.Default().StorageCli.DownloadFileToDir(apath, path.Join(m.HomePath, "eventlog"))
if err != nil {
logrus.Errorf("download file to dir failure:%v", err)
return message, nil
}
}
eventFile, err := os.Open(apath)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions api/eventlog/db/filePlugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (m *filePlugin) getStdFilePath(serviceID string) (string, error) {
}

func (m *filePlugin) SaveMessage(events []*EventLogMessage) error {
logrus.Info("init file plugin save message")
if len(events) == 0 {
return nil
}
Expand Down
50 changes: 44 additions & 6 deletions config/configs/rbdcomponent/api_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package rbdcomponent
import (
"github.com/goodrain/rainbond-operator/util/constants"
"github.com/goodrain/rainbond/api/eventlog/conf"
"github.com/goodrain/rainbond/api/eventlog/entry"
"github.com/goodrain/rainbond/api/eventlog/exit/web"
utils "github.com/goodrain/rainbond/util"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/thejerf/suture"
)

// APIConfig config
Expand Down Expand Up @@ -62,10 +61,49 @@ func AddAPIFlags(fs *pflag.FlagSet, apic *APIConfig) {
}

type EventLogConfig struct {
Conf conf.Conf
Entry *entry.Entry
Logger *logrus.Logger
SocketServer *web.SocketServer
Conf conf.Conf
Entry *Entry
Logger *logrus.Logger
}

type Entry struct {
supervisor *suture.Supervisor
log *logrus.Entry
conf EntryConf
}

type EntryConf struct {
EventLogServer EventLogServerConf
DockerLogServer DockerLogServerConf
MonitorMessageServer MonitorMessageServerConf
NewMonitorMessageServerConf NewMonitorMessageServerConf
}

type EventLogServerConf struct {
BindIP string
BindPort int
CacheMessageSize int
}

// MonitorMessageServerConf monitor message server conf
type MonitorMessageServerConf struct {
SubAddress []string
SubSubscribe string
CacheMessageSize int
}

// NewMonitorMessageServerConf new monitor message server conf
type NewMonitorMessageServerConf struct {
ListenerHost string
ListenerPort int
}

// DockerLogServerConf docker log server conf
type DockerLogServerConf struct {
BindIP string
BindPort int
CacheMessageSize int
Mode string
}

func AddEventLogFlags(fs *pflag.FlagSet, elc *EventLogConfig) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/component/storage/local_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,7 @@ func CopyWithProgress(srcFile SrcFile, dstFile DstFile, allSize int64, logger ev
func (l *LocalStorage) DownloadDirToDir(srcDir, dstDir string) error {
return nil
}

func (l *LocalStorage) DownloadFileToDir(srcFile, dstDir string) error {
return nil
}
40 changes: 40 additions & 0 deletions pkg/component/storage/s3_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,43 @@ func (s3s *S3Storage) DownloadDirToDir(srcDir, dstDir string) error {

return nil
}

func (s3s *S3Storage) DownloadFileToDir(srcFile, dstDir string) error {
// 解析 S3 路径
bucketName, key, err := s3s.ParseDirPath(srcFile, false)
if err != nil {
return fmt.Errorf("解析源路径失败: %v", err)
}
// 检查目标目录是否存在,如果不存在则创建
if err := os.MkdirAll(dstDir, 0755); err != nil {
return fmt.Errorf("无法创建目录 %s: %v", dstDir, err)
}

// 构造目标文件路径
dstFilePath := fmt.Sprintf("%s/%s", dstDir, filepath.Base(key))

// 从 S3 下载文件
input := &s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(key),
}

result, err := s3s.s3Client.GetObject(input)
if err != nil {
return fmt.Errorf("无法从 S3 下载文件 %s: %v", key, err)
}
defer result.Body.Close()

// 创建目标文件
dstFile, err := os.OpenFile(dstFilePath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("无法打开文件 %s: %v", dstFilePath, err)
}
defer dstFile.Close()

// 将 S3 文件内容写入目标文件
if _, err := io.Copy(dstFile, result.Body); err != nil {
return fmt.Errorf("无法写入文件 %s: %v", dstFilePath, err)
}
return nil
}
1 change: 1 addition & 0 deletions pkg/component/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type InterfaceStorage interface {
SaveFile(fileName string, reader multipart.File) error
UploadFileToFile(src string, dst string, logger event.Logger) error
DownloadDirToDir(srcDir, dstDir string) error
DownloadFileToDir(srcFile, dstDir string) error
}

type SrcFile interface {
Expand Down

0 comments on commit 325fb30

Please sign in to comment.