(req dto.ImagePull)
| 264 | } |
| 265 | |
| 266 | func (u *ImageService) ImagePull(req dto.ImagePull) error { |
| 267 | client, err := docker.NewDockerClient() |
| 268 | if err != nil { |
| 269 | return err |
| 270 | } |
| 271 | defer client.Close() |
| 272 | taskItem, err := task.NewTaskWithOps(strings.Join(req.ImageName, ","), task.TaskPull, task.TaskScopeImage, req.TaskID, 1) |
| 273 | if err != nil { |
| 274 | return fmt.Errorf("new task for image pull failed, err: %v", err) |
| 275 | } |
| 276 | |
| 277 | for _, item := range req.ImageName { |
| 278 | itemName := strings.ReplaceAll(path.Base(item), ":", "_") |
| 279 | taskItem.AddSubTask(i18n.GetWithName("ImagePull", itemName), func(t *task.Task) error { |
| 280 | taskItem.Logf("----------------- %s -----------------", itemName) |
| 281 | options := image.PullOptions{} |
| 282 | imageName := item |
| 283 | if req.RepoID == 0 { |
| 284 | hasAuth, authStr := loadAuthInfo(item) |
| 285 | if hasAuth { |
| 286 | options.RegistryAuth = authStr |
| 287 | } |
| 288 | } else { |
| 289 | repo, err := imageRepoRepo.Get(repo.WithByID(req.RepoID)) |
| 290 | taskItem.LogWithStatus(i18n.GetMsgByKey("ImageRepoAuthFromDB"), err) |
| 291 | if err != nil { |
| 292 | return err |
| 293 | } |
| 294 | if repo.Auth { |
| 295 | authConfig := registry.AuthConfig{ |
| 296 | Username: repo.Username, |
| 297 | Password: repo.Password, |
| 298 | } |
| 299 | encodedJSON, err := json.Marshal(authConfig) |
| 300 | if err != nil { |
| 301 | return err |
| 302 | } |
| 303 | authStr := base64.URLEncoding.EncodeToString(encodedJSON) |
| 304 | options.RegistryAuth = authStr |
| 305 | } |
| 306 | imageName = repo.DownloadUrl + "/" + item |
| 307 | } |
| 308 | dockerCli := docker.NewClientWithExist(client) |
| 309 | err = dockerCli.PullImageWithProcessAndOptions(taskItem, imageName, options) |
| 310 | taskItem.LogWithStatus(i18n.GetMsgByKey("TaskPull"), err) |
| 311 | if err != nil { |
| 312 | return err |
| 313 | } |
| 314 | return nil |
| 315 | }, nil) |
| 316 | } |
| 317 | go func() { |
| 318 | _ = taskItem.Execute() |
| 319 | }() |
| 320 | return nil |
| 321 | } |
| 322 | |
| 323 | func (u *ImageService) ImageLoad(req dto.ImageLoad) error { |
nothing calls this directly
no test coverage detected