build.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // +build ignore
  2. package main
  3. import (
  4. "bytes"
  5. "crypto/md5"
  6. "encoding/json"
  7. "flag"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "log"
  12. "os"
  13. "os/exec"
  14. "path/filepath"
  15. "regexp"
  16. "runtime"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. var (
  22. versionRe = regexp.MustCompile(`-[0-9]{1,3}-g[0-9a-f]{5,10}`)
  23. goarch string
  24. goos string
  25. version string = "v1"
  26. race bool
  27. workingDir string
  28. serverBinaryName string = "grafana-server"
  29. )
  30. const minGoVersion = 1.3
  31. func main() {
  32. log.SetOutput(os.Stdout)
  33. log.SetFlags(0)
  34. ensureGoPath()
  35. readVersionFromPackageJson()
  36. log.Printf("Version: %s\n", version)
  37. flag.StringVar(&goarch, "goarch", runtime.GOARCH, "GOARCH")
  38. flag.StringVar(&goos, "goos", runtime.GOOS, "GOOS")
  39. flag.BoolVar(&race, "race", race, "Use race detector")
  40. flag.Parse()
  41. if flag.NArg() == 0 {
  42. log.Println("Usage: go run build.go build")
  43. return
  44. }
  45. workingDir, _ = os.Getwd()
  46. for _, cmd := range flag.Args() {
  47. switch cmd {
  48. case "setup":
  49. setup()
  50. case "build":
  51. pkg := "."
  52. clean()
  53. build(pkg, []string{})
  54. case "test":
  55. test("./pkg/...")
  56. grunt("test")
  57. case "package":
  58. //verifyGitRepoIsClean()
  59. //grunt("release", "--pkgVer="+version)
  60. createPackage("deb", "default")
  61. //createPackage("rpm", "sysconfig")
  62. case "latest":
  63. makeLatestDistCopies()
  64. case "clean":
  65. clean()
  66. default:
  67. log.Fatalf("Unknown command %q", cmd)
  68. }
  69. }
  70. }
  71. func makeLatestDistCopies() {
  72. runError("cp", "dist/grafana_"+version+"_amd64.deb", "dist/grafana_latest_amd64.deb")
  73. runError("cp", "dist/grafana-"+strings.Replace(version, "-", "_", 5)+"-1.x86_64.rpm", "dist/grafana-latest-1.x86_64.rpm")
  74. runError("cp", "dist/grafana-"+version+".x86_64.tar.gz", "dist/grafana-latest.x86_64.tar.gz")
  75. }
  76. func readVersionFromPackageJson() {
  77. reader, err := os.Open("package.json")
  78. if err != nil {
  79. log.Fatal("Failed to open package.json")
  80. return
  81. }
  82. defer reader.Close()
  83. jsonObj := map[string]interface{}{}
  84. jsonParser := json.NewDecoder(reader)
  85. if err := jsonParser.Decode(&jsonObj); err != nil {
  86. log.Fatal("Failed to decode package.json")
  87. }
  88. version = jsonObj["version"].(string)
  89. }
  90. func createPackage(packageType string, defaultPath string) {
  91. homeDir := "/usr/share/grafana"
  92. configDir := "/etc/grafana"
  93. configFilePath := "/etc/grafana/grafana.ini"
  94. defaultFilePath := filepath.Join("/etc/", defaultPath, "grafana-server")
  95. grafanaServerBinPath := "/usr/sbin/" + serverBinaryName
  96. initdScriptPath := "/etc/init.d/grafana-server"
  97. systemdServiceFilePath := "/usr/lib/systemd/system/grafana-server.service"
  98. packageRoot, _ := ioutil.TempDir("", "grafana-linux-pack")
  99. packageConfDir := filepath.Join("packaging", packageType)
  100. postintSrc := filepath.Join(packageConfDir, "control/postinst")
  101. initdScriptSrc := filepath.Join(packageConfDir, "init.d/grafana-server")
  102. defaultFileSrc := filepath.Join(packageConfDir, defaultPath, "grafana-server")
  103. systemdFileSrc := filepath.Join(packageConfDir, "systemd/grafana-server.service")
  104. // create directories
  105. runPrint("mkdir", "-p", filepath.Join(packageRoot, homeDir))
  106. runPrint("mkdir", "-p", filepath.Join(packageRoot, configDir))
  107. runPrint("mkdir", "-p", filepath.Join(packageRoot, "/etc/init.d"))
  108. runPrint("mkdir", "-p", filepath.Join(packageRoot, "/etc/", defaultPath))
  109. runPrint("mkdir", "-p", filepath.Join(packageRoot, "/usr/lib/systemd/system"))
  110. runPrint("mkdir", "-p", filepath.Join(packageRoot, "/usr/sbin"))
  111. // copy binary
  112. runPrint("cp", "-p", filepath.Join(workingDir, "tmp/bin/"+serverBinaryName), filepath.Join(packageRoot, grafanaServerBinPath))
  113. // copy init.d script
  114. runPrint("cp", "-p", initdScriptSrc, filepath.Join(packageRoot, initdScriptPath))
  115. // copy environment var file
  116. runPrint("cp", "-p", defaultFileSrc, filepath.Join(packageRoot, defaultFilePath))
  117. // copy systemd file
  118. runPrint("cp", "-p", systemdFileSrc, filepath.Join(packageRoot, systemdServiceFilePath))
  119. // copy release files
  120. runPrint("cp", "-a", filepath.Join(workingDir, "tmp")+"/.", filepath.Join(packageRoot, homeDir))
  121. // copy sample ini file to /etc/opt/grafana
  122. runPrint("cp", "conf/sample.ini", filepath.Join(packageRoot, configFilePath))
  123. args := []string{
  124. "-s", "dir",
  125. "--description", "Grafana",
  126. "-C", packageRoot,
  127. "--vendor", "Grafana",
  128. "--depends", "adduser",
  129. "--url", "http://grafana.org",
  130. "--license", "Apache 2.0",
  131. "--maintainer", "contact@grafana.org",
  132. "--config-files", configFilePath,
  133. "--config-files", initdScriptPath,
  134. "--config-files", defaultFilePath,
  135. "--config-files", systemdServiceFilePath,
  136. "--after-install", postintSrc,
  137. "--name", "grafana",
  138. "--version", version,
  139. "-p", "./dist",
  140. ".",
  141. }
  142. fmt.Println("Creating package: ", packageType)
  143. runPrint("fpm", append([]string{"-t", packageType}, args...)...)
  144. }
  145. func verifyGitRepoIsClean() {
  146. rs, err := runError("git", "ls-files", "--modified")
  147. if err != nil {
  148. log.Fatalf("Failed to check if git tree was clean, %v, %v\n", string(rs), err)
  149. return
  150. }
  151. count := len(string(rs))
  152. if count > 0 {
  153. log.Fatalf("Git repository has modified files, aborting")
  154. }
  155. log.Println("Git repository is clean")
  156. }
  157. func ensureGoPath() {
  158. if os.Getenv("GOPATH") == "" {
  159. cwd, err := os.Getwd()
  160. if err != nil {
  161. log.Fatal(err)
  162. }
  163. gopath := filepath.Clean(filepath.Join(cwd, "../../../../"))
  164. log.Println("GOPATH is", gopath)
  165. os.Setenv("GOPATH", gopath)
  166. }
  167. }
  168. func ChangeWorkingDir(dir string) {
  169. os.Chdir(dir)
  170. }
  171. func grunt(params ...string) {
  172. runPrint("./node_modules/grunt-cli/bin/grunt", params...)
  173. }
  174. func setup() {
  175. runPrint("go", "get", "-v", "github.com/tools/godep")
  176. runPrint("go", "get", "-v", "github.com/mattn/go-sqlite3")
  177. runPrint("go", "install", "-v", "github.com/mattn/go-sqlite3")
  178. }
  179. func test(pkg string) {
  180. setBuildEnv()
  181. runPrint("go", "test", "-short", "-timeout", "60s", pkg)
  182. }
  183. func build(pkg string, tags []string) {
  184. binary := "./bin/" + serverBinaryName
  185. if goos == "windows" {
  186. binary += ".exe"
  187. }
  188. rmr(binary, binary+".md5")
  189. args := []string{"build", "-ldflags", ldflags()}
  190. if len(tags) > 0 {
  191. args = append(args, "-tags", strings.Join(tags, ","))
  192. }
  193. if race {
  194. args = append(args, "-race")
  195. }
  196. args = append(args, "-o", binary)
  197. args = append(args, pkg)
  198. setBuildEnv()
  199. runPrint("go", args...)
  200. // Create an md5 checksum of the binary, to be included in the archive for
  201. // automatic upgrades.
  202. err := md5File(binary)
  203. if err != nil {
  204. log.Fatal(err)
  205. }
  206. }
  207. func ldflags() string {
  208. var b bytes.Buffer
  209. b.WriteString("-w")
  210. b.WriteString(fmt.Sprintf(" -X main.version '%s'", version))
  211. b.WriteString(fmt.Sprintf(" -X main.commit '%s'", getGitSha()))
  212. b.WriteString(fmt.Sprintf(" -X main.buildstamp %d", buildStamp()))
  213. return b.String()
  214. }
  215. func rmr(paths ...string) {
  216. for _, path := range paths {
  217. log.Println("rm -r", path)
  218. os.RemoveAll(path)
  219. }
  220. }
  221. func clean() {
  222. rmr("bin", "Godeps/_workspace/pkg", "Godeps/_workspace/bin")
  223. rmr("dist")
  224. rmr("tmp")
  225. rmr(filepath.Join(os.Getenv("GOPATH"), fmt.Sprintf("pkg/%s_%s/github.com/grafana", goos, goarch)))
  226. }
  227. func setBuildEnv() {
  228. os.Setenv("GOOS", goos)
  229. if strings.HasPrefix(goarch, "armv") {
  230. os.Setenv("GOARCH", "arm")
  231. os.Setenv("GOARM", goarch[4:])
  232. } else {
  233. os.Setenv("GOARCH", goarch)
  234. }
  235. if goarch == "386" {
  236. os.Setenv("GO386", "387")
  237. }
  238. wd, err := os.Getwd()
  239. if err != nil {
  240. log.Println("Warning: can't determine current dir:", err)
  241. log.Println("Build might not work as expected")
  242. }
  243. os.Setenv("GOPATH", fmt.Sprintf("%s%c%s", filepath.Join(wd, "Godeps", "_workspace"), os.PathListSeparator, os.Getenv("GOPATH")))
  244. log.Println("GOPATH=" + os.Getenv("GOPATH"))
  245. }
  246. func getGitSha() string {
  247. v, err := runError("git", "describe", "--always", "--dirty")
  248. if err != nil {
  249. return "unknown-dev"
  250. }
  251. v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte {
  252. s[0] = '+'
  253. return s
  254. })
  255. return string(v)
  256. }
  257. func buildStamp() int64 {
  258. bs, err := runError("git", "show", "-s", "--format=%ct")
  259. if err != nil {
  260. return time.Now().Unix()
  261. }
  262. s, _ := strconv.ParseInt(string(bs), 10, 64)
  263. return s
  264. }
  265. func buildArch() string {
  266. os := goos
  267. if os == "darwin" {
  268. os = "macosx"
  269. }
  270. return fmt.Sprintf("%s-%s", os, goarch)
  271. }
  272. func run(cmd string, args ...string) []byte {
  273. bs, err := runError(cmd, args...)
  274. if err != nil {
  275. log.Println(cmd, strings.Join(args, " "))
  276. log.Println(string(bs))
  277. log.Fatal(err)
  278. }
  279. return bytes.TrimSpace(bs)
  280. }
  281. func runError(cmd string, args ...string) ([]byte, error) {
  282. ecmd := exec.Command(cmd, args...)
  283. bs, err := ecmd.CombinedOutput()
  284. if err != nil {
  285. return nil, err
  286. }
  287. return bytes.TrimSpace(bs), nil
  288. }
  289. func runPrint(cmd string, args ...string) {
  290. log.Println(cmd, strings.Join(args, " "))
  291. ecmd := exec.Command(cmd, args...)
  292. ecmd.Stdout = os.Stdout
  293. ecmd.Stderr = os.Stderr
  294. err := ecmd.Run()
  295. if err != nil {
  296. log.Fatal(err)
  297. }
  298. }
  299. func md5File(file string) error {
  300. fd, err := os.Open(file)
  301. if err != nil {
  302. return err
  303. }
  304. defer fd.Close()
  305. h := md5.New()
  306. _, err = io.Copy(h, fd)
  307. if err != nil {
  308. return err
  309. }
  310. out, err := os.Create(file + ".md5")
  311. if err != nil {
  312. return err
  313. }
  314. _, err = fmt.Fprintf(out, "%x\n", h.Sum(nil))
  315. if err != nil {
  316. return err
  317. }
  318. return out.Close()
  319. }