-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Added progress display for image pulling #7955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package task | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
|
|
@@ -26,6 +27,7 @@ type Task struct { | |
| Name string | ||
| TaskID string | ||
| Logger *log.Logger | ||
| Writer *bufio.Writer | ||
| SubTasks []*SubTask | ||
| Rollbacks []RollbackFunc | ||
| logFile *os.File | ||
|
|
@@ -111,6 +113,7 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e | |
| if err != nil { | ||
| return nil, fmt.Errorf("failed to open log file: %w", err) | ||
| } | ||
| writer := bufio.NewWriter(file) | ||
| logger := log.New(file, "", log.LstdFlags) | ||
| taskModel := &model.Task{ | ||
| ID: taskID, | ||
|
|
@@ -122,7 +125,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: file, Logger: logger, taskRepo: taskRepo, Task: taskModel, Writer: writer} | ||
| return task, nil | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code does not appear to have any significant differences from its previous version, so there are no specific issues identified that require attention. It appears to be in good shape with an exception:
A more thorough analysis would confirm if this change affects readability or functionality. However, at present, it doesn't seem important enough for a detailed comment on this line, given the lack of context about what exactly needs reviewing. Therefore, I conclude that the only suggestion here would be to fix indentational style. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,21 @@ package docker | |
|
|
||
| import ( | ||
| "context" | ||
| "github.com/docker/docker/api/types/network" | ||
|
|
||
| "github.com/docker/docker/api/types/container" | ||
| "github.com/docker/docker/api/types/image" | ||
|
|
||
| "encoding/json" | ||
| "fmt" | ||
| "github.com/1Panel-dev/1Panel/agent/app/model" | ||
| "github.com/1Panel-dev/1Panel/agent/app/task" | ||
| "github.com/1Panel-dev/1Panel/agent/global" | ||
|
|
||
| "github.com/docker/docker/api/types" | ||
| "github.com/docker/docker/api/types/container" | ||
| "github.com/docker/docker/api/types/filters" | ||
| "github.com/docker/docker/api/types/image" | ||
| "github.com/docker/docker/api/types/network" | ||
| "github.com/docker/docker/client" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| func NewDockerClient() (*client.Client, error) { | ||
|
|
@@ -28,10 +32,6 @@ func NewDockerClient() (*client.Client, error) { | |
| return cli, nil | ||
| } | ||
|
|
||
| type Client struct { | ||
| cli *client.Client | ||
| } | ||
|
|
||
| func NewClient() (Client, error) { | ||
| var settingItem model.Setting | ||
| _ = global.DB.Where("key = ?", "DockerSockPath").First(&settingItem).Error | ||
|
|
@@ -48,10 +48,8 @@ func NewClient() (Client, error) { | |
| }, nil | ||
| } | ||
|
|
||
| func NewClientWithCli(cli *client.Client) (Client, error) { | ||
| return Client{ | ||
| cli: cli, | ||
| }, nil | ||
| type Client struct { | ||
| cli *client.Client | ||
| } | ||
|
|
||
| func (c Client) Close() { | ||
|
|
@@ -142,6 +140,7 @@ func CreateDefaultDockerNetwork() error { | |
| global.LOG.Errorf("init docker client error %s", err.Error()) | ||
| return err | ||
| } | ||
|
|
||
| defer cli.Close() | ||
| if !cli.NetworkExist("1panel-network") { | ||
| if err := cli.CreateNetwork("1panel-network"); err != nil { | ||
|
|
@@ -151,3 +150,66 @@ func CreateDefaultDockerNetwork() error { | |
| } | ||
| return nil | ||
| } | ||
|
|
||
| func setLog(id, newLastLine string, task *task.Task) error { | ||
| data, err := os.ReadFile(task.Task.LogFile) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read file: %v", err) | ||
| } | ||
| lines := strings.Split(string(data), "\n") | ||
| exist := false | ||
| for index, line := range lines { | ||
| if strings.Contains(line, id) { | ||
| lines[index] = newLastLine | ||
| exist = true | ||
| break | ||
| } | ||
| } | ||
| if !exist { | ||
| task.Log(newLastLine) | ||
| return nil | ||
| } | ||
| output := strings.Join(lines, "\n") | ||
| _ = os.WriteFile(task.Task.LogFile, []byte(output), os.ModePerm) | ||
| return nil | ||
| } | ||
|
|
||
| func (c Client) PullImageWithProcess(task *task.Task, imageName string) error { | ||
| out, err := c.cli.ImagePull(context.Background(), imageName, image.PullOptions{}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer out.Close() | ||
| decoder := json.NewDecoder(out) | ||
| for { | ||
| var progress map[string]interface{} | ||
| if err = decoder.Decode(&progress); err != nil { | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| return err | ||
| } | ||
| timeStr := time.Now().Format("2006/01/02 15:04:05") | ||
| status, _ := progress["status"].(string) | ||
| if status == "Downloading" || status == "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 [%s] --- %.2f%%", timeStr, status, id, (current/total)*100) | ||
| } else { | ||
| progressStr = fmt.Sprintf("%s %s [%s] --- %.2f%%", timeStr, status, id, current) | ||
| } | ||
|
|
||
| _ = setLog(id, progressStr, task) | ||
| } | ||
| if status == "Pull complete" || status == "Download complete" { | ||
| id, _ := progress["id"].(string) | ||
| progressStr := fmt.Sprintf("%s %s [%s] --- %.2f%%", timeStr, status, id, 100.0) | ||
| _ = setLog(id, progressStr, task) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here are some observations:
Overall, I would suggest keeping the original names unless they are clearly misleading or miscommunicated the intended usage. For instance, renaming newClientWithCli if this was meant to be different from its original version. Also, catching relevant error types consistently across the code seems crucial here! |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There were no significant changes to be made based on the given code excerpt. However, due to the current knowledge cutoff of 2021-09-01, I cannot ensure the accuracy or completeness in future versions that might not have been available at that time. Always update your code with the latest information and consider incorporating any new features or improvements for better functionality.