build.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. // deb & rpm does not support semver so have to handle their version a little differently
  27. linuxPackageVersion string = "v1"
  28. linuxPackageIteration string = ""
  29. race bool
  30. workingDir string
  31. binaries []string = []string{"grafana-server", "grafana-cli"}
  32. )
  33. const minGoVersion = 1.3
  34. func main() {
  35. log.SetOutput(os.Stdout)
  36. log.SetFlags(0)
  37. ensureGoPath()
  38. readVersionFromPackageJson()
  39. log.Printf("Version: %s, Linux Version: %s, Package Iteration: %s\n", version, linuxPackageVersion, linuxPackageIteration)
  40. flag.StringVar(&goarch, "goarch", runtime.GOARCH, "GOARCH")
  41. flag.StringVar(&goos, "goos", runtime.GOOS, "GOOS")
  42. flag.BoolVar(&race, "race", race, "Use race detector")
  43. flag.Parse()
  44. if flag.NArg() == 0 {
  45. log.Println("Usage: go run build.go build")
  46. return
  47. }
  48. workingDir, _ = os.Getwd()
  49. for _, cmd := range flag.Args() {
  50. switch cmd {
  51. case "setup":
  52. setup()
  53. case "build":
  54. clean()
  55. for _, binary := range binaries {
  56. build(binary, "./pkg/cmd/"+binary, []string{})
  57. }
  58. case "test":
  59. test("./pkg/...")
  60. grunt("test")
  61. case "package":
  62. grunt("release", fmt.Sprintf("--pkgVer=%v-%v", linuxPackageVersion, linuxPackageIteration))
  63. createLinuxPackages()
  64. case "pkg-rpm":
  65. grunt("release")
  66. createRpmPackages()
  67. case "pkg-deb":
  68. grunt("release")
  69. createDebPackages()
  70. case "latest":
  71. makeLatestDistCopies()
  72. case "clean":
  73. clean()
  74. default:
  75. log.Fatalf("Unknown command %q", cmd)
  76. }
  77. }
  78. }
  79. func makeLatestDistCopies() {
  80. rpmIteration := "-1"
  81. if linuxPackageIteration != "" {
  82. rpmIteration = linuxPackageIteration
  83. }
  84. runError("cp", fmt.Sprintf("dist/grafana_%v-%v_amd64.deb", linuxPackageVersion, linuxPackageIteration), "dist/grafana_latest_amd64.deb")
  85. runError("cp", fmt.Sprintf("dist/grafana-%v-%v.x86_64.rpm", linuxPackageVersion, rpmIteration), "dist/grafana-latest-1.x86_64.rpm")
  86. runError("cp", fmt.Sprintf("dist/grafana-%v-%v.linux-x64.tar.gz", linuxPackageVersion, linuxPackageIteration), "dist/grafana-latest.linux-x64.tar.gz")
  87. }
  88. func readVersionFromPackageJson() {
  89. reader, err := os.Open("package.json")
  90. if err != nil {
  91. log.Fatal("Failed to open package.json")
  92. return
  93. }
  94. defer reader.Close()
  95. jsonObj := map[string]interface{}{}
  96. jsonParser := json.NewDecoder(reader)
  97. if err := jsonParser.Decode(&jsonObj); err != nil {
  98. log.Fatal("Failed to decode package.json")
  99. }
  100. version = jsonObj["version"].(string)
  101. linuxPackageVersion = version
  102. linuxPackageIteration = ""
  103. // handle pre version stuff (deb / rpm does not support semver)
  104. parts := strings.Split(version, "-")
  105. if len(parts) > 1 {
  106. linuxPackageVersion = parts[0]
  107. linuxPackageIteration = parts[1]
  108. }
  109. // add timestamp to iteration
  110. linuxPackageIteration = fmt.Sprintf("%d%s", time.Now().Unix(), linuxPackageIteration)
  111. }
  112. type linuxPackageOptions struct {
  113. packageType string
  114. homeDir string
  115. binPath string
  116. serverBinPath string
  117. cliBinPath string
  118. configDir string
  119. configFilePath string
  120. ldapFilePath string
  121. etcDefaultPath string
  122. etcDefaultFilePath string
  123. initdScriptFilePath string
  124. systemdServiceFilePath string
  125. postinstSrc string
  126. initdScriptSrc string
  127. defaultFileSrc string
  128. systemdFileSrc string
  129. depends []string
  130. }
  131. func createDebPackages() {
  132. createPackage(linuxPackageOptions{
  133. packageType: "deb",
  134. homeDir: "/usr/share/grafana",
  135. binPath: "/usr/sbin",
  136. configDir: "/etc/grafana",
  137. configFilePath: "/etc/grafana/grafana.ini",
  138. ldapFilePath: "/etc/grafana/ldap.toml",
  139. etcDefaultPath: "/etc/default",
  140. etcDefaultFilePath: "/etc/default/grafana-server",
  141. initdScriptFilePath: "/etc/init.d/grafana-server",
  142. systemdServiceFilePath: "/usr/lib/systemd/system/grafana-server.service",
  143. postinstSrc: "packaging/deb/control/postinst",
  144. initdScriptSrc: "packaging/deb/init.d/grafana-server",
  145. defaultFileSrc: "packaging/deb/default/grafana-server",
  146. systemdFileSrc: "packaging/deb/systemd/grafana-server.service",
  147. depends: []string{"adduser", "libfontconfig"},
  148. })
  149. }
  150. func createRpmPackages() {
  151. createPackage(linuxPackageOptions{
  152. packageType: "rpm",
  153. homeDir: "/usr/share/grafana",
  154. binPath: "/usr/sbin",
  155. configDir: "/etc/grafana",
  156. configFilePath: "/etc/grafana/grafana.ini",
  157. ldapFilePath: "/etc/grafana/ldap.toml",
  158. etcDefaultPath: "/etc/sysconfig",
  159. etcDefaultFilePath: "/etc/sysconfig/grafana-server",
  160. initdScriptFilePath: "/etc/init.d/grafana-server",
  161. systemdServiceFilePath: "/usr/lib/systemd/system/grafana-server.service",
  162. postinstSrc: "packaging/rpm/control/postinst",
  163. initdScriptSrc: "packaging/rpm/init.d/grafana-server",
  164. defaultFileSrc: "packaging/rpm/sysconfig/grafana-server",
  165. systemdFileSrc: "packaging/rpm/systemd/grafana-server.service",
  166. depends: []string{"initscripts", "fontconfig"},
  167. })
  168. }
  169. func createLinuxPackages() {
  170. createDebPackages()
  171. createRpmPackages()
  172. }
  173. func createPackage(options linuxPackageOptions) {
  174. packageRoot, _ := ioutil.TempDir("", "grafana-linux-pack")
  175. // create directories
  176. runPrint("mkdir", "-p", filepath.Join(packageRoot, options.homeDir))
  177. runPrint("mkdir", "-p", filepath.Join(packageRoot, options.configDir))
  178. runPrint("mkdir", "-p", filepath.Join(packageRoot, "/etc/init.d"))
  179. runPrint("mkdir", "-p", filepath.Join(packageRoot, options.etcDefaultPath))
  180. runPrint("mkdir", "-p", filepath.Join(packageRoot, "/usr/lib/systemd/system"))
  181. runPrint("mkdir", "-p", filepath.Join(packageRoot, "/usr/sbin"))
  182. // copy binary
  183. for _, binary := range binaries {
  184. runPrint("cp", "-p", filepath.Join(workingDir, "tmp/bin/"+binary), filepath.Join(packageRoot, "/usr/sbin/"+binary))
  185. }
  186. // copy init.d script
  187. runPrint("cp", "-p", options.initdScriptSrc, filepath.Join(packageRoot, options.initdScriptFilePath))
  188. // copy environment var file
  189. runPrint("cp", "-p", options.defaultFileSrc, filepath.Join(packageRoot, options.etcDefaultFilePath))
  190. // copy systemd file
  191. runPrint("cp", "-p", options.systemdFileSrc, filepath.Join(packageRoot, options.systemdServiceFilePath))
  192. // copy release files
  193. runPrint("cp", "-a", filepath.Join(workingDir, "tmp")+"/.", filepath.Join(packageRoot, options.homeDir))
  194. // remove bin path
  195. runPrint("rm", "-rf", filepath.Join(packageRoot, options.homeDir, "bin"))
  196. // copy sample ini file to /etc/grafana
  197. runPrint("cp", "conf/sample.ini", filepath.Join(packageRoot, options.configFilePath))
  198. // copy sample ldap toml config file to /etc/grafana/ldap.toml
  199. runPrint("cp", "conf/ldap.toml", filepath.Join(packageRoot, options.ldapFilePath))
  200. args := []string{
  201. "-s", "dir",
  202. "--description", "Grafana",
  203. "-C", packageRoot,
  204. "--vendor", "Grafana",
  205. "--url", "http://grafana.org",
  206. "--license", "\"Apache 2.0\"",
  207. "--maintainer", "contact@grafana.org",
  208. "--config-files", options.configFilePath,
  209. "--config-files", options.ldapFilePath,
  210. "--config-files", options.initdScriptFilePath,
  211. "--config-files", options.etcDefaultFilePath,
  212. "--config-files", options.systemdServiceFilePath,
  213. "--after-install", options.postinstSrc,
  214. "--name", "grafana",
  215. "--version", linuxPackageVersion,
  216. "-p", "./dist",
  217. }
  218. if linuxPackageIteration != "" {
  219. args = append(args, "--iteration", linuxPackageIteration)
  220. }
  221. // add dependenciesj
  222. for _, dep := range options.depends {
  223. args = append(args, "--depends", dep)
  224. }
  225. args = append(args, ".")
  226. fmt.Println("Creating package: ", options.packageType)
  227. runPrint("fpm", append([]string{"-t", options.packageType}, args...)...)
  228. }
  229. func verifyGitRepoIsClean() {
  230. rs, err := runError("git", "ls-files", "--modified")
  231. if err != nil {
  232. log.Fatalf("Failed to check if git tree was clean, %v, %v\n", string(rs), err)
  233. return
  234. }
  235. count := len(string(rs))
  236. if count > 0 {
  237. log.Fatalf("Git repository has modified files, aborting")
  238. }
  239. log.Println("Git repository is clean")
  240. }
  241. func ensureGoPath() {
  242. if os.Getenv("GOPATH") == "" {
  243. cwd, err := os.Getwd()
  244. if err != nil {
  245. log.Fatal(err)
  246. }
  247. gopath := filepath.Clean(filepath.Join(cwd, "../../../../"))
  248. log.Println("GOPATH is", gopath)
  249. os.Setenv("GOPATH", gopath)
  250. }
  251. }
  252. func ChangeWorkingDir(dir string) {
  253. os.Chdir(dir)
  254. }
  255. func grunt(params ...string) {
  256. runPrint("./node_modules/.bin/grunt", params...)
  257. }
  258. func setup() {
  259. runPrint("go", "get", "-v", "github.com/tools/godep")
  260. runPrint("go", "get", "-v", "github.com/blang/semver")
  261. runPrint("go", "get", "-v", "github.com/mattn/go-sqlite3")
  262. runPrint("go", "install", "-v", "github.com/mattn/go-sqlite3")
  263. }
  264. func test(pkg string) {
  265. setBuildEnv()
  266. runPrint("go", "test", "-short", "-timeout", "60s", pkg)
  267. }
  268. func build(binaryName, pkg string, tags []string) {
  269. binary := "./bin/" + binaryName
  270. if goos == "windows" {
  271. binary += ".exe"
  272. }
  273. rmr(binary, binary+".md5")
  274. args := []string{"build", "-ldflags", ldflags()}
  275. if len(tags) > 0 {
  276. args = append(args, "-tags", strings.Join(tags, ","))
  277. }
  278. if race {
  279. args = append(args, "-race")
  280. }
  281. args = append(args, "-o", binary)
  282. args = append(args, pkg)
  283. setBuildEnv()
  284. runPrint("go", "version")
  285. runPrint("go", args...)
  286. // Create an md5 checksum of the binary, to be included in the archive for
  287. // automatic upgrades.
  288. err := md5File(binary)
  289. if err != nil {
  290. log.Fatal(err)
  291. }
  292. }
  293. func ldflags() string {
  294. var b bytes.Buffer
  295. b.WriteString("-w")
  296. b.WriteString(fmt.Sprintf(" -X main.version=%s", version))
  297. b.WriteString(fmt.Sprintf(" -X main.commit=%s", getGitSha()))
  298. b.WriteString(fmt.Sprintf(" -X main.buildstamp=%d", buildStamp()))
  299. return b.String()
  300. }
  301. func rmr(paths ...string) {
  302. for _, path := range paths {
  303. log.Println("rm -r", path)
  304. os.RemoveAll(path)
  305. }
  306. }
  307. func clean() {
  308. rmr("bin", "Godeps/_workspace/pkg", "Godeps/_workspace/bin")
  309. rmr("dist")
  310. rmr("tmp")
  311. rmr(filepath.Join(os.Getenv("GOPATH"), fmt.Sprintf("pkg/%s_%s/github.com/grafana", goos, goarch)))
  312. }
  313. func setBuildEnv() {
  314. os.Setenv("GOOS", goos)
  315. if strings.HasPrefix(goarch, "armv") {
  316. os.Setenv("GOARCH", "arm")
  317. os.Setenv("GOARM", goarch[4:])
  318. } else {
  319. os.Setenv("GOARCH", goarch)
  320. }
  321. if goarch == "386" {
  322. os.Setenv("GO386", "387")
  323. }
  324. wd, err := os.Getwd()
  325. if err != nil {
  326. log.Println("Warning: can't determine current dir:", err)
  327. log.Println("Build might not work as expected")
  328. }
  329. os.Setenv("GOPATH", fmt.Sprintf("%s%c%s", filepath.Join(wd, "Godeps", "_workspace"), os.PathListSeparator, os.Getenv("GOPATH")))
  330. log.Println("GOPATH=" + os.Getenv("GOPATH"))
  331. }
  332. func getGitSha() string {
  333. v, err := runError("git", "describe", "--always", "--dirty")
  334. if err != nil {
  335. return "unknown-dev"
  336. }
  337. v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte {
  338. s[0] = '+'
  339. return s
  340. })
  341. return string(v)
  342. }
  343. func buildStamp() int64 {
  344. bs, err := runError("git", "show", "-s", "--format=%ct")
  345. if err != nil {
  346. return time.Now().Unix()
  347. }
  348. s, _ := strconv.ParseInt(string(bs), 10, 64)
  349. return s
  350. }
  351. func buildArch() string {
  352. os := goos
  353. if os == "darwin" {
  354. os = "macosx"
  355. }
  356. return fmt.Sprintf("%s-%s", os, goarch)
  357. }
  358. func run(cmd string, args ...string) []byte {
  359. bs, err := runError(cmd, args...)
  360. if err != nil {
  361. log.Println(cmd, strings.Join(args, " "))
  362. log.Println(string(bs))
  363. log.Fatal(err)
  364. }
  365. return bytes.TrimSpace(bs)
  366. }
  367. func runError(cmd string, args ...string) ([]byte, error) {
  368. ecmd := exec.Command(cmd, args...)
  369. bs, err := ecmd.CombinedOutput()
  370. if err != nil {
  371. return nil, err
  372. }
  373. return bytes.TrimSpace(bs), nil
  374. }
  375. func runPrint(cmd string, args ...string) {
  376. log.Println(cmd, strings.Join(args, " "))
  377. ecmd := exec.Command(cmd, args...)
  378. ecmd.Stdout = os.Stdout
  379. ecmd.Stderr = os.Stderr
  380. err := ecmd.Run()
  381. if err != nil {
  382. log.Fatal(err)
  383. }
  384. }
  385. func md5File(file string) error {
  386. fd, err := os.Open(file)
  387. if err != nil {
  388. return err
  389. }
  390. defer fd.Close()
  391. h := md5.New()
  392. _, err = io.Copy(h, fd)
  393. if err != nil {
  394. return err
  395. }
  396. out, err := os.Create(file + ".md5")
  397. if err != nil {
  398. return err
  399. }
  400. _, err = fmt.Fprintf(out, "%x\n", h.Sum(nil))
  401. if err != nil {
  402. return err
  403. }
  404. return out.Close()
  405. }