-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.go
127 lines (113 loc) · 2.88 KB
/
build.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"github.com/fsouza/go-dockerclient"
"github.com/progrium/go-shell"
"github.com/spf13/cobra"
)
func init() {
Glu.AddCommand(buildCmd)
}
var buildCmd = &cobra.Command{
Use: "build <os-list> [<pkgs>] [<name>]",
Short: "Builds a Go project of Glider Labs",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Usage()
os.Exit(1)
}
defer shell.ErrExit()
shell.Trace = true
shell.Tee = os.Stdout
if tryContainer(cmd, args) {
return
}
var (
info = NewProjectInfo()
osList = strings.Split(args[0], ",")
pkgs = optArg(args, 1, ".")
name = optArg(args, 2, info.Name)
ldFlag string
)
if info.Version != "" {
ldFlag = fmt.Sprintf("-ldflags \"-X main.Version=%s\"", info.Version)
}
if insideContainer() {
os.Setenv("GOPATH", "/go")
path := fmt.Sprintf("/go/src/%s", info.Repo)
sh("mkdir -p", filepath.Dir(path))
sh("cp -r /project", path)
sh("cd", path) // for show
os.Chdir(path)
}
sh("go get -d", pkgs)
os.Setenv("CGO_ENABLED", "0")
for i := range osList {
os.Setenv("GOOS", strings.ToLower(osList[i]))
path := shell.Path("build", strings.Title(osList[i]))
sh("mkdir -p", path)
sh("go build -a -installsuffix cgo", ldFlag, "-o", shell.Path(path, name), pkgs)
}
if insideContainer() {
sh("rm -rf /project/build")
sh("mv build /project")
for i := range osList {
sh(fmt.Sprintf("tar -czvf /artifacts/%s-%s.tgz -C /project/build/%s %s",
name, strings.ToLower(osList[i]), strings.Title(osList[i]), name))
}
sh("tar -czf /artifacts/go-workspace.tgz -C /go .")
sh("rm -rf /go")
}
},
}
func tryContainer(cmd *cobra.Command, args []string) bool {
if insideContainer() {
return false
}
if !dockerExistsByName("glu") {
return false
}
fmt.Fprintln(os.Stderr, "* Using glu container")
args = append(strings.Split(cmd.CommandPath(), " "), args...)
var newCmd []string
var binary string
var err error
if os.Getenv("CIRCLECI") == "true" {
if binary, err = exec.LookPath("sudo"); err != nil {
return false
}
os.Setenv("GLU_CONTAINER", "true")
newCmd = []string{"sudo", "-E", "lxc-attach", "-n", dockerID("glu"), "--", "/bin/glu"}
newCmd = append(newCmd, args[1:]...)
} else {
if binary, err = exec.LookPath("docker"); err != nil {
return false
}
newCmd = []string{"docker", "exec", "glu"}
newCmd = append(newCmd, args...)
}
syscall.Exec(binary, newCmd, os.Environ())
return true
}
func insideContainer() bool {
return os.Getenv("GLU_CONTAINER") == "true"
}
func dockerID(container string) string {
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 cntr.ID
}
}
}
return ""
}