(dclient *dockerapi.Client, imageRef, local string, extract, removeOrig bool)
| 264 | } |
| 265 | |
| 266 | func SaveImage(dclient *dockerapi.Client, imageRef, local string, extract, removeOrig bool) error { |
| 267 | if local == "" { |
| 268 | return ErrBadParam |
| 269 | } |
| 270 | |
| 271 | var err error |
| 272 | if dclient == nil { |
| 273 | socketInfo, err := dockerclient.GetUnixSocketAddr() |
| 274 | if err != nil { |
| 275 | return err |
| 276 | } |
| 277 | |
| 278 | if socketInfo == nil || socketInfo.Address == "" { |
| 279 | return fmt.Errorf("no unix socket found") |
| 280 | } |
| 281 | |
| 282 | dclient, err = dockerapi.NewClient(socketInfo.Address) |
| 283 | if err != nil { |
| 284 | log.Errorf("dockerutil.SaveImage: dockerapi.NewClient() error = %v", err) |
| 285 | return err |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | imageRef = CleanImageID(imageRef) |
| 290 | |
| 291 | //todo: 'pull' the image if it's not available locally yet |
| 292 | //note: HasImage() doesn't work with image IDs |
| 293 | |
| 294 | dir := fsutil.FileDir(local) |
| 295 | if !fsutil.DirExists(dir) { |
| 296 | err := os.MkdirAll(dir, 0755) |
| 297 | if err != nil { |
| 298 | return err |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | dfile, err := os.Create(local) |
| 303 | if err != nil { |
| 304 | return err |
| 305 | } |
| 306 | |
| 307 | options := dockerapi.ExportImageOptions{ |
| 308 | Name: imageRef, |
| 309 | OutputStream: dfile, |
| 310 | InactivityTimeout: 20 * time.Second, |
| 311 | } |
| 312 | |
| 313 | err = dclient.ExportImage(options) |
| 314 | if err != nil { |
| 315 | log.Errorf("dockerutil.SaveImage: dclient.ExportImage() error = %v", err) |
| 316 | dfile.Close() |
| 317 | return err |
| 318 | } |
| 319 | |
| 320 | dfile.Close() |
| 321 | |
| 322 | if extract { |
| 323 | dstDir := filepath.Dir(local) |
no test coverage detected