-
Notifications
You must be signed in to change notification settings - Fork 48
/
docker.go
113 lines (94 loc) · 2.69 KB
/
docker.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package captain // import "github.com/harbur/captain"
import (
"fmt"
"os"
"path/filepath"
"github.com/fsouza/go-dockerclient"
)
const defaultEndpoint = "unix:///var/run/docker.sock"
var client *docker.Client
func init() {
var err error
client, err = docker.NewClientFromEnv()
if err != nil {
panic(err)
}
}
type BuildArgSet struct {
slice []docker.BuildArg
}
func buildImage(app App, tag string, force bool) error {
info("Building image %s:%s", app.Image, tag)
// Nasty issue with CircleCI https://github.com/docker/docker/issues/4897
if os.Getenv("CIRCLECI") == "true" {
info("Running at %s environment...", "CIRCLECI")
execute("docker", "build", "-t", app.Image+":"+tag, filepath.Dir(app.Build))
return nil
}
// Create BuildArg set
buildArgSet := BuildArgSet{(make([]docker.BuildArg, 0, 10))}
if len(app.Build_arg) > 0 {
for k, arg := range app.Build_arg {
buildArgSet.slice = append(buildArgSet.slice, docker.BuildArg{Name: k, Value: arg})
}
}
opts := docker.BuildImageOptions{
Name: app.Image + ":" + tag,
Dockerfile: filepath.Base(app.Build),
NoCache: force,
SuppressOutput: false,
RmTmpContainer: true,
ForceRmTmpContainer: true,
OutputStream: os.Stdout,
ContextDir: filepath.Dir(app.Build),
BuildArgs: buildArgSet.slice,
}
// Use ~/.docker/ auth configuration if exists
dockercfg, _ := docker.NewAuthConfigurationsFromDockerCfg()
if dockercfg != nil {
opts.AuthConfigs = *dockercfg
}
err := client.BuildImage(opts)
if err != nil {
fmt.Printf("%s", err)
}
return err
}
func pushImage(image string, version string) error {
return execute("docker", "push", image+":"+version)
}
func pullImage(image string, version string) error {
return execute("docker", "pull", image+":"+version)
}
func tagImage(app App, origin string, tag string) error {
if tag != "" {
info("Tagging image %s:%s as %s:%s", app.Image, origin, app.Image, tag)
opts := docker.TagImageOptions{Repo: app.Image, Tag: tag, Force: true}
err := client.TagImage(app.Image+":"+origin, opts)
if err != nil {
fmt.Printf("%s", err)
}
return err
}
debug("Skipping tag of %s - no git repository", app.Image)
return nil
}
func removeImage(name string) error {
return client.RemoveImage(name)
}
/**
* Retrieves a list of existing Images for the specific App.
*/
func getImages(app App) []docker.APIImages {
debug("Getting images %s", app.Image)
imgs, _ := client.ListImages(docker.ListImagesOptions{All: false, Filter: app.Image})
return imgs
}
func imageExist(app App, tag string) bool {
repo := app.Image + ":" + tag
image, _ := client.InspectImage(repo)
if image != nil {
return true
}
return false
}