-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontainer.go
76 lines (67 loc) · 1.57 KB
/
container.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
package main
import (
"fmt"
"os"
"github.com/fsouza/go-dockerclient"
"github.com/progrium/go-shell"
"github.com/spf13/cobra"
)
func init() {
containerCmd.AddCommand(containerUpCmd)
containerCmd.AddCommand(containerDownCmd)
Glu.AddCommand(containerCmd)
}
var containerCmd = &cobra.Command{
Use: "container",
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}
var containerUpCmd = &cobra.Command{
Use: "up",
Short: "Creates a glu container",
Run: func(cmd *cobra.Command, args []string) {
defer shell.ErrExit()
shell.Trace = true
shell.Tee = os.Stdout
if dockerExistsByName("glu") {
sh("docker rm -f glu")
}
var artifactsMount string
if os.Getenv("CIRCLE_ARTIFACTS") != "" {
artifactsMount = os.Getenv("CIRCLE_ARTIFACTS") + ":"
}
sh("docker run -d --name glu",
"--label glu",
"--entrypoint /usr/bin/tail",
"--volume $PWD:/project",
fmt.Sprintf("--volume %s/artifacts", artifactsMount),
"gliderlabs/glu -f /dev/null")
},
}
var containerDownCmd = &cobra.Command{
Use: "down",
Short: "Destroys a glu container",
Run: func(cmd *cobra.Command, args []string) {
defer shell.ErrExit()
shell.Trace = true
shell.Tee = os.Stdout
if dockerExistsByName("glu") {
sh("docker rm -f glu")
}
},
}
func dockerExistsByName(container string) bool {
client, err := docker.NewClientFromEnv()
fatal(err)
containers, err := client.ListContainers(docker.ListContainersOptions{})
fatal(err)
for _, cntr := range containers {
for _, name := range cntr.Names {
if name[1:] == container {
return true
}
}
}
return false
}