forked from docker/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
64 lines (59 loc) · 1.46 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package image
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/docker/app/internal/renderer"
)
func contains(list []string, needle string) bool {
for _, e := range list {
if e == needle {
return true
}
}
return false
}
// Add add service images to the app package
func Add(appname string, services []string, composeFiles []string, settingsFile []string, env map[string]string) error {
config, err := renderer.Render(appname, composeFiles, settingsFile, env)
if err != nil {
return err
}
os.Mkdir(filepath.Join(appname, "images"), 0755)
for _, s := range config.Services {
if len(services) != 0 && !contains(services, s.Name) {
continue
}
cmd := exec.Command("docker", "save", "-o", filepath.Join(appname, "images", s.Name), s.Image)
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(output)
return err
}
}
return nil
}
// Load loads app packed images to the docker daemon
func Load(appname string, services []string) error {
imageDir, err := os.Open(filepath.Join(appname, "images"))
if err != nil {
return fmt.Errorf("no images found in app")
}
images, err := imageDir.Readdirnames(0)
if err != nil {
return err
}
for _, i := range images {
if len(services) != 0 && !contains(services, i) {
continue
}
cmd := exec.Command("docker", "load", "-i", filepath.Join(appname, "images", i))
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(output)
return err
}
}
return nil
}