-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
299 lines (252 loc) · 7.13 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// +build ignore
package main
import (
"bytes"
"crypto/md5"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
)
var (
versionRe = regexp.MustCompile(`-[0-9]{1,3}-g[0-9a-f]{5,10}`)
goarch string
goos string
version string = "v1"
linuxPackageIteration string = ""
race bool
workingDir string
serverBinaryName string = "armada-stats"
)
func main() {
log.SetOutput(os.Stdout)
log.SetFlags(0)
ensureGoPath()
workingDir, _ = os.Getwd()
version = readVersion()
log.Printf("Version: %s, Package Iteration: %s\n", version, linuxPackageIteration)
flag.StringVar(&goarch, "goarch", runtime.GOARCH, "GOARCH")
flag.StringVar(&goos, "goos", runtime.GOOS, "GOOS")
flag.BoolVar(&race, "race", race, "Use race detector")
flag.Parse()
if flag.NArg() == 0 {
log.Println("Usage: go run build.go build")
return
}
for _, cmd := range flag.Args() {
switch cmd {
case "build":
pkg := "."
clean()
build(pkg, []string{})
case "package":
createLinuxPackages()
case "pkg-deb":
createDebPackages()
case "pkg-rpm":
createRpmPackages()
case "clean":
clean()
default:
log.Fatalf("Unknown command %q", cmd)
}
}
}
func createLinuxPackages() {
createDebPackages()
createRpmPackages()
}
func createDebPackages() {
createPackage(linuxPackageOptions{
packageType: "deb",
binPath: "/usr/local/bin/armada-stats",
configDir: "/etc/armada-stats",
configFilePath: "/etc/armada-stats/armada-stats.yml",
etcDefaultPath: "/etc/default",
etcDefaultFilePath: "/etc/default/armada-stats.yml",
systemdServiceFilePath: "/usr/lib/systemd/system/armada-stats.service",
systemdFileSrc: "packaging/systemd/armada-stats.service",
postinstSrc: "packaging/deb/control/postinst",
defaultFileSrc: "conf/defaults.yml",
})
}
func createRpmPackages() {
createPackage(linuxPackageOptions{
packageType: "rpm",
binPath: "/usr/local/bin/armada-stats",
configDir: "/etc/armada-stats",
configFilePath: "/etc/armada-stats/armada-stats.yml",
etcDefaultPath: "/etc/default",
etcDefaultFilePath: "/etc/default/armada-stats.yml",
systemdServiceFilePath: "/usr/lib/systemd/system/armada-stats.service",
systemdFileSrc: "packaging/systemd/armada-stats.service",
postinstSrc: "packaging/rpm/control/postinst",
defaultFileSrc: "conf/defaults.yml",
})
}
func createPackage(options linuxPackageOptions) {
packageRoot, _ := ioutil.TempDir("", "armada-stats-linux-pack")
// create directories
runPrint("mkdir", "-p", filepath.Join(packageRoot, options.configDir))
runPrint("mkdir", "-p", filepath.Join(packageRoot, options.etcDefaultPath))
runPrint("mkdir", "-p", filepath.Join(packageRoot, "/usr/local/bin"))
runPrint("mkdir", "-p", filepath.Join(packageRoot, "/usr/lib/systemd/system"))
//create package directory
runPrint("mkdir", "-p", filepath.Join(workingDir, "/dist"))
// copy binary
runPrint("cp", "-p", filepath.Join(workingDir, "tmp/bin/"+serverBinaryName), filepath.Join(packageRoot, options.binPath))
// copy environment var file
runPrint("cp", "-p", options.defaultFileSrc, filepath.Join(packageRoot, options.etcDefaultFilePath))
// copy config file
runPrint("cp", "conf/armada-stats.yml", filepath.Join(packageRoot, options.configFilePath))
// copy systemd file
runPrint("cp", "-p", options.systemdFileSrc, filepath.Join(packageRoot, options.systemdServiceFilePath))
args := []string{
"-s", "dir",
"--description", "armada-stats",
"-C", packageRoot,
"--license", "\"Apache 2.0\"",
"--maintainer", "krise3k@github.com",
"--url", "https://github.com/krise3k/armada-stats",
"--config-files", options.configFilePath,
"--config-files", options.etcDefaultFilePath,
"--config-files", options.systemdServiceFilePath,
"--after-install", options.postinstSrc,
"--name", "armada-stats",
"--version", version,
"-p", "./dist",
}
if linuxPackageIteration != "" {
args = append(args, "--iteration", linuxPackageIteration)
}
args = append(args, ".")
fmt.Println("Creating package: ", options.packageType)
runPrint("fpm", append([]string{"-t", options.packageType}, args...)...)
}
type linuxPackageOptions struct {
packageType string
binPath string
configDir string
configFilePath string
etcDefaultPath string
etcDefaultFilePath string
systemdServiceFilePath string
systemdFileSrc string
postinstSrc string
defaultFileSrc string
}
func runPrint(cmd string, args ...string) {
log.Println(cmd, strings.Join(args, " "))
ecmd := exec.Command(cmd, args...)
ecmd.Stdout = os.Stdout
ecmd.Stderr = os.Stderr
err := ecmd.Run()
if err != nil {
log.Fatal(err)
}
}
func ensureGoPath() {
if os.Getenv("GOPATH") == "" {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
gopath := filepath.Clean(filepath.Join(cwd, "../../../../"))
log.Println("GOPATH is", gopath)
os.Setenv("GOPATH", gopath)
}
}
func build(pkg string, tags []string) {
binary := "./tmp/bin/" + serverBinaryName
rmr(binary, binary+".md5")
args := []string{"build", "-ldflags", ldflags()}
if len(tags) > 0 {
args = append(args, "-tags", strings.Join(tags, ","))
}
if race {
args = append(args, "-race")
}
args = append(args, "-o", binary)
args = append(args, pkg)
setBuildEnv()
runPrint("go", "version")
runPrint("go", args...)
// Create an md5 checksum of the binary, to be included in the archive for
// automatic upgrades.
err := md5File(binary)
if err != nil {
log.Fatal(err)
}
}
func setBuildEnv() {
os.Setenv("GOOS", goos)
if strings.HasPrefix(goarch, "armv") {
os.Setenv("GOARCH", "arm")
os.Setenv("GOARM", goarch[4:])
} else {
os.Setenv("GOARCH", goarch)
}
if goarch == "386" {
os.Setenv("GO386", "387")
}
wd, err := os.Getwd()
if err != nil {
log.Println("Warning: can't determine current dir:", err)
log.Println("Build might not work as expected")
}
os.Setenv("GOPATH", fmt.Sprintf("%s%c%s", filepath.Join(wd, "Godeps", "_workspace"), os.PathListSeparator, os.Getenv("GOPATH")))
log.Println("GOPATH=" + os.Getenv("GOPATH"))
}
func readVersion() string {
version, err := ioutil.ReadFile(filepath.Join(workingDir, "VERSION"))
if err != nil {
log.Println("Warning: can't determine current version:", err)
}
return string(version)
}
func md5File(file string) error {
fd, err := os.Open(file)
if err != nil {
return err
}
defer fd.Close()
h := md5.New()
_, err = io.Copy(h, fd)
if err != nil {
return err
}
out, err := os.Create(file + ".md5")
if err != nil {
return err
}
_, err = fmt.Fprintf(out, "%x\n", h.Sum(nil))
if err != nil {
return err
}
return out.Close()
}
func ldflags() string {
var b bytes.Buffer
b.WriteString("-w")
b.WriteString(fmt.Sprintf(" -X main.version=%s", version))
return b.String()
}
func rmr(paths ...string) {
for _, path := range paths {
log.Println("rm -r", path)
os.RemoveAll(path)
}
}
func clean() {
rmr("bin", "Godeps/_workspace/pkg", "Godeps/_workspace/bin")
rmr("dist")
rmr("tmp")
rmr(filepath.Join(os.Getenv("GOPATH"), fmt.Sprintf("pkg/%s_%s/github.com/krise3k", goos, goarch)))
}