Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions agent/app/service/app_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"maps"
"math"
"net/http"
Expand Down Expand Up @@ -45,6 +44,7 @@ import (
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/docker/api/types/container"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/subosito/gotenv"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -959,7 +959,7 @@ func handleMap(params map[string]interface{}, envParams map[string]string) {
}
}

func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.AppInstall, logger *log.Logger) (err error) {
func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.AppInstall, logger *logrus.Logger) (err error) {
if app.IsLocalApp() || app.IsCustomApp() {
return nil
}
Expand Down
33 changes: 21 additions & 12 deletions agent/app/task/task.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package task

import (
"bufio"
"context"
"errors"
"fmt"
"log"
"os"
"path"
"strconv"
"time"

"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/sirupsen/logrus"

"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/app/repo"
Expand All @@ -29,8 +28,7 @@ type Task struct {

Name string
TaskID string
Logger *log.Logger
Writer *bufio.Writer
Logger *logrus.Logger
SubTasks []*SubTask
Rollbacks []RollbackFunc
logFile *os.File
Expand Down Expand Up @@ -138,12 +136,13 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
}
}
logPath := path.Join(global.Dir.TaskDir, taskScope, taskID+".log")
file, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
logger := logrus.New()
logger.SetFormatter(&SimpleFormatter{})
logFile, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
if err != nil {
return nil, fmt.Errorf("failed to open log file: %w", err)
}
writer := bufio.NewWriter(file)
logger := log.New(file, "", log.LstdFlags)
logger.SetOutput(logFile)
taskModel := &model.Task{
ID: taskID,
Name: name,
Expand All @@ -156,7 +155,7 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
taskRepo := repo.NewITaskRepo()
ctx, cancle := context.WithCancel(context.Background())
global.TaskCtxMap[taskID] = cancle
task := &Task{TaskCtx: ctx, Name: name, logFile: file, Logger: logger, taskRepo: taskRepo, Task: taskModel, Writer: writer}
task := &Task{TaskCtx: ctx, Name: name, logFile: logFile, Logger: logger, taskRepo: taskRepo, Task: taskModel}
return task, nil
}

Expand All @@ -175,16 +174,18 @@ func ReNewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task,
return nil, fmt.Errorf("failed to create log directory: %w", err)
}
}

logPath := path.Join(global.Dir.TaskDir, taskScope, taskID+".log")
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
logger := logrus.New()
logger.SetFormatter(&SimpleFormatter{})
logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
if err != nil {
return nil, fmt.Errorf("failed to open log file: %w", err)
}
writer := bufio.NewWriter(file)
logger := log.New(file, "", log.LstdFlags)
logger.SetOutput(logFile)
logger.Print("\n --------------------------------------------------- \n")
taskItem.Status = constant.StatusExecuting
task := &Task{Name: name, logFile: file, Logger: logger, taskRepo: taskRepo, Task: &taskItem, Writer: writer}
task := &Task{Name: name, logFile: logFile, Logger: logger, taskRepo: taskRepo, Task: &taskItem}
task.updateTask(&taskItem)
return task, nil
}
Expand Down Expand Up @@ -353,3 +354,11 @@ func (t *Task) LogSuccessWithOps(operate, msg string) {
func (t *Task) LogFailedWithOps(operate, msg string, err error) {
t.Logger.Printf("%s%s%s : %s ", i18n.GetMsgByKey(operate), msg, i18n.GetMsgByKey("Failed"), err.Error())
}

type SimpleFormatter struct{}

func (f *SimpleFormatter) Format(entry *logrus.Entry) ([]byte, error) {
timestamp := entry.Time.Format("2006/01/02 15:04:05")
message := fmt.Sprintf("%s %s\n", timestamp, entry.Message)
return []byte(message), nil
}
25 changes: 2 additions & 23 deletions agent/utils/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,7 @@ func (c Client) PushImageWithProcessAndOptions(task *task.Task, imageName string
status, _ := progress["status"].(string)
switch status {
case "Pushing":
id, _ := progress["id"].(string)
progressDetail, _ := progress["progressDetail"].(map[string]interface{})
current, _ := progressDetail["current"].(float64)
progressStr := ""
total, ok := progressDetail["total"].(float64)
if ok {
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, (current/total)*100)
} else {
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, current)
}

_ = setLog(id, progressStr, task)
logProcess(progress, task)
case "Pushed":
id, _ := progress["id"].(string)
progressStr := fmt.Sprintf("%s [%s] --- %.2f%%", status, id, 100.0)
Expand Down Expand Up @@ -298,17 +287,7 @@ func (c Client) BuildImageWithProcessAndOptions(task *task.Task, tar io.ReadClos
}
switch status {
case "Downloading", "Extracting":
id, _ := progress["id"].(string)
progressDetail, _ := progress["progressDetail"].(map[string]interface{})
current, _ := progressDetail["current"].(float64)
progressStr := ""
total, ok := progressDetail["total"].(float64)
if ok {
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, (current/total)*100)
} else {
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, current)
}
_ = setLog(id, progressStr, task)
logProcess(progress, task)
case "Pull complete", "Download complete", "Verifying Checksum":
id, _ := progress["id"].(string)
progressStr := fmt.Sprintf("%s [%s] --- %.2f%%", status, id, 100.0)
Expand Down
22 changes: 16 additions & 6 deletions core/app/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package task
import (
"context"
"fmt"
"log"
"os"
"path"
"strconv"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/1Panel-dev/1Panel/core/global"
"github.com/1Panel-dev/1Panel/core/i18n"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)

type ActionFunc func(*Task) error
Expand All @@ -24,7 +24,7 @@ type RollbackFunc func(*Task)
type Task struct {
Name string
TaskID string
Logger *log.Logger
Logger *logrus.Logger
SubTasks []*SubTask
Rollbacks []RollbackFunc
logFile *os.File
Expand Down Expand Up @@ -59,7 +59,7 @@ const (

const (
TaskScopeSystem = "System"
TaskScopeScript = "Script"
TaskScopeScript = "ScriptLibrary"
TaskScopeNodeFile = "NodeFile"
TaskScopeAppBackup = "AppBackup"
TaskScopeCluster = "Cluster"
Expand All @@ -85,11 +85,13 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
}
}
logPath := path.Join(logItem, taskScope, taskID+".log")
file, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
logger := logrus.New()
logger.SetFormatter(&SimpleFormatter{})
logFile, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
if err != nil {
return nil, fmt.Errorf("failed to open log file: %w", err)
}
logger := log.New(file, "", log.LstdFlags)
logger.SetOutput(logFile)
taskModel := &model.Task{
ID: taskID,
Name: name,
Expand All @@ -100,7 +102,7 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
Operate: operate,
}
taskRepo := repo.NewITaskRepo()
task := &Task{Name: name, logFile: file, Logger: logger, taskRepo: taskRepo, Task: taskModel}
task := &Task{Name: name, logFile: logFile, Logger: logger, taskRepo: taskRepo, Task: taskModel}
return task, nil
}

Expand Down Expand Up @@ -263,3 +265,11 @@ func (t *Task) LogSuccessWithOps(operate, msg string) {
func (t *Task) LogFailedWithOps(operate, msg string, err error) {
t.Logger.Printf("%s%s%s : %s ", i18n.GetMsgByKey(operate), msg, i18n.GetMsgByKey("Failed"), err.Error())
}

type SimpleFormatter struct{}

func (f *SimpleFormatter) Format(entry *logrus.Entry) ([]byte, error) {
timestamp := entry.Time.Format("2006/01/02 15:04:05")
message := fmt.Sprintf("%s %s\n", timestamp, entry.Message)
return []byte(message), nil
}
Loading