build.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // +build ignore
  2. package main
  3. import (
  4. "bytes"
  5. "crypto/md5"
  6. "crypto/sha256"
  7. "encoding/json"
  8. "flag"
  9. "fmt"
  10. "io"
  11. "io/ioutil"
  12. "log"
  13. "os"
  14. "os/exec"
  15. "path"
  16. "path/filepath"
  17. "runtime"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. const (
  23. windows = "windows"
  24. linux = "linux"
  25. )
  26. var (
  27. //versionRe = regexp.MustCompile(`-[0-9]{1,3}-g[0-9a-f]{5,10}`)
  28. goarch string
  29. goos string
  30. gocc string
  31. cgo bool
  32. pkgArch string
  33. version string = "v1"
  34. // deb & rpm does not support semver so have to handle their version a little differently
  35. linuxPackageVersion string = "v1"
  36. linuxPackageIteration string = ""
  37. race bool
  38. phjsToRelease string
  39. workingDir string
  40. includeBuildId bool = true
  41. buildId string = "0"
  42. binaries []string = []string{"grafana-server", "grafana-cli"}
  43. isDev bool = false
  44. enterprise bool = false
  45. )
  46. func main() {
  47. log.SetOutput(os.Stdout)
  48. log.SetFlags(0)
  49. ensureGoPath()
  50. var buildIdRaw string
  51. flag.StringVar(&goarch, "goarch", runtime.GOARCH, "GOARCH")
  52. flag.StringVar(&goos, "goos", runtime.GOOS, "GOOS")
  53. flag.StringVar(&gocc, "cc", "", "CC")
  54. flag.BoolVar(&cgo, "cgo-enabled", cgo, "Enable cgo")
  55. flag.StringVar(&pkgArch, "pkg-arch", "", "PKG ARCH")
  56. flag.StringVar(&phjsToRelease, "phjs", "", "PhantomJS binary")
  57. flag.BoolVar(&race, "race", race, "Use race detector")
  58. flag.BoolVar(&includeBuildId, "includeBuildId", includeBuildId, "IncludeBuildId in package name")
  59. flag.BoolVar(&enterprise, "enterprise", enterprise, "Build enterprise version of Grafana")
  60. flag.StringVar(&buildIdRaw, "buildId", "0", "Build ID from CI system")
  61. flag.BoolVar(&isDev, "dev", isDev, "optimal for development, skips certain steps")
  62. flag.Parse()
  63. buildId = shortenBuildId(buildIdRaw)
  64. readVersionFromPackageJson()
  65. if pkgArch == "" {
  66. pkgArch = goarch
  67. }
  68. log.Printf("Version: %s, Linux Version: %s, Package Iteration: %s\n", version, linuxPackageVersion, linuxPackageIteration)
  69. if flag.NArg() == 0 {
  70. log.Println("Usage: go run build.go build")
  71. return
  72. }
  73. workingDir, _ = os.Getwd()
  74. for _, cmd := range flag.Args() {
  75. switch cmd {
  76. case "setup":
  77. setup()
  78. case "build-srv":
  79. clean()
  80. build("grafana-server", "./pkg/cmd/grafana-server", []string{})
  81. case "build-cli":
  82. clean()
  83. build("grafana-cli", "./pkg/cmd/grafana-cli", []string{})
  84. case "build-server":
  85. clean()
  86. build("grafana-server", "./pkg/cmd/grafana-server", []string{})
  87. case "build":
  88. //clean()
  89. for _, binary := range binaries {
  90. build(binary, "./pkg/cmd/"+binary, []string{})
  91. }
  92. case "build-frontend":
  93. grunt(gruntBuildArg("build")...)
  94. case "test":
  95. test("./pkg/...")
  96. grunt("test")
  97. case "package":
  98. grunt(gruntBuildArg("build")...)
  99. grunt(gruntBuildArg("package")...)
  100. if goos == linux {
  101. createLinuxPackages()
  102. }
  103. case "package-only":
  104. grunt(gruntBuildArg("package")...)
  105. if goos == linux {
  106. createLinuxPackages()
  107. }
  108. case "pkg-archive":
  109. grunt(gruntBuildArg("package")...)
  110. case "pkg-rpm":
  111. grunt(gruntBuildArg("release")...)
  112. createRpmPackages()
  113. case "pkg-deb":
  114. grunt(gruntBuildArg("release")...)
  115. createDebPackages()
  116. case "sha-dist":
  117. shaFilesInDist()
  118. case "latest":
  119. makeLatestDistCopies()
  120. case "clean":
  121. clean()
  122. default:
  123. log.Fatalf("Unknown command %q", cmd)
  124. }
  125. }
  126. }
  127. func makeLatestDistCopies() {
  128. files, err := ioutil.ReadDir("dist")
  129. if err != nil {
  130. log.Fatalf("failed to create latest copies. Cannot read from /dist")
  131. }
  132. latestMapping := map[string]string{
  133. "_amd64.deb": "dist/grafana_latest_amd64.deb",
  134. ".x86_64.rpm": "dist/grafana-latest-1.x86_64.rpm",
  135. ".linux-amd64.tar.gz": "dist/grafana-latest.linux-x64.tar.gz",
  136. }
  137. for _, file := range files {
  138. for extension, fullName := range latestMapping {
  139. if strings.HasSuffix(file.Name(), extension) {
  140. runError("cp", path.Join("dist", file.Name()), fullName)
  141. }
  142. }
  143. }
  144. }
  145. func readVersionFromPackageJson() {
  146. reader, err := os.Open("package.json")
  147. if err != nil {
  148. log.Fatal("Failed to open package.json")
  149. return
  150. }
  151. defer reader.Close()
  152. jsonObj := map[string]interface{}{}
  153. jsonParser := json.NewDecoder(reader)
  154. if err := jsonParser.Decode(&jsonObj); err != nil {
  155. log.Fatal("Failed to decode package.json")
  156. }
  157. version = jsonObj["version"].(string)
  158. linuxPackageVersion = version
  159. linuxPackageIteration = ""
  160. // handle pre version stuff (deb / rpm does not support semver)
  161. parts := strings.Split(version, "-")
  162. if len(parts) > 1 {
  163. linuxPackageVersion = parts[0]
  164. linuxPackageIteration = parts[1]
  165. }
  166. // add timestamp to iteration
  167. if includeBuildId {
  168. if buildId != "0" {
  169. linuxPackageIteration = fmt.Sprintf("%s%s", buildId, linuxPackageIteration)
  170. } else {
  171. linuxPackageIteration = fmt.Sprintf("%d%s", time.Now().Unix(), linuxPackageIteration)
  172. }
  173. }
  174. }
  175. type linuxPackageOptions struct {
  176. packageType string
  177. homeDir string
  178. binPath string
  179. serverBinPath string
  180. cliBinPath string
  181. configDir string
  182. ldapFilePath string
  183. etcDefaultPath string
  184. etcDefaultFilePath string
  185. initdScriptFilePath string
  186. systemdServiceFilePath string
  187. postinstSrc string
  188. initdScriptSrc string
  189. defaultFileSrc string
  190. systemdFileSrc string
  191. depends []string
  192. }
  193. func createDebPackages() {
  194. previousPkgArch := pkgArch
  195. if pkgArch == "armv7" {
  196. pkgArch = "armhf"
  197. }
  198. createPackage(linuxPackageOptions{
  199. packageType: "deb",
  200. homeDir: "/usr/share/grafana",
  201. binPath: "/usr/sbin",
  202. configDir: "/etc/grafana",
  203. etcDefaultPath: "/etc/default",
  204. etcDefaultFilePath: "/etc/default/grafana-server",
  205. initdScriptFilePath: "/etc/init.d/grafana-server",
  206. systemdServiceFilePath: "/usr/lib/systemd/system/grafana-server.service",
  207. postinstSrc: "packaging/deb/control/postinst",
  208. initdScriptSrc: "packaging/deb/init.d/grafana-server",
  209. defaultFileSrc: "packaging/deb/default/grafana-server",
  210. systemdFileSrc: "packaging/deb/systemd/grafana-server.service",
  211. depends: []string{"adduser", "libfontconfig"},
  212. })
  213. pkgArch = previousPkgArch
  214. }
  215. func createRpmPackages() {
  216. previousPkgArch := pkgArch
  217. switch {
  218. case pkgArch == "armv7":
  219. pkgArch = "armhfp"
  220. case pkgArch == "arm64":
  221. pkgArch = "aarch64"
  222. }
  223. createPackage(linuxPackageOptions{
  224. packageType: "rpm",
  225. homeDir: "/usr/share/grafana",
  226. binPath: "/usr/sbin",
  227. configDir: "/etc/grafana",
  228. etcDefaultPath: "/etc/sysconfig",
  229. etcDefaultFilePath: "/etc/sysconfig/grafana-server",
  230. initdScriptFilePath: "/etc/init.d/grafana-server",
  231. systemdServiceFilePath: "/usr/lib/systemd/system/grafana-server.service",
  232. postinstSrc: "packaging/rpm/control/postinst",
  233. initdScriptSrc: "packaging/rpm/init.d/grafana-server",
  234. defaultFileSrc: "packaging/rpm/sysconfig/grafana-server",
  235. systemdFileSrc: "packaging/rpm/systemd/grafana-server.service",
  236. depends: []string{"/sbin/service", "fontconfig", "freetype", "urw-fonts"},
  237. })
  238. pkgArch = previousPkgArch
  239. }
  240. func createLinuxPackages() {
  241. createDebPackages()
  242. createRpmPackages()
  243. }
  244. func createPackage(options linuxPackageOptions) {
  245. packageRoot, _ := ioutil.TempDir("", "grafana-linux-pack")
  246. // create directories
  247. runPrint("mkdir", "-p", filepath.Join(packageRoot, options.homeDir))
  248. runPrint("mkdir", "-p", filepath.Join(packageRoot, options.configDir))
  249. runPrint("mkdir", "-p", filepath.Join(packageRoot, "/etc/init.d"))
  250. runPrint("mkdir", "-p", filepath.Join(packageRoot, options.etcDefaultPath))
  251. runPrint("mkdir", "-p", filepath.Join(packageRoot, "/usr/lib/systemd/system"))
  252. runPrint("mkdir", "-p", filepath.Join(packageRoot, "/usr/sbin"))
  253. // copy binary
  254. for _, binary := range binaries {
  255. runPrint("cp", "-p", filepath.Join(workingDir, "tmp/bin/"+binary), filepath.Join(packageRoot, "/usr/sbin/"+binary))
  256. }
  257. // copy init.d script
  258. runPrint("cp", "-p", options.initdScriptSrc, filepath.Join(packageRoot, options.initdScriptFilePath))
  259. // copy environment var file
  260. runPrint("cp", "-p", options.defaultFileSrc, filepath.Join(packageRoot, options.etcDefaultFilePath))
  261. // copy systemd file
  262. runPrint("cp", "-p", options.systemdFileSrc, filepath.Join(packageRoot, options.systemdServiceFilePath))
  263. // copy release files
  264. runPrint("cp", "-a", filepath.Join(workingDir, "tmp")+"/.", filepath.Join(packageRoot, options.homeDir))
  265. // remove bin path
  266. runPrint("rm", "-rf", filepath.Join(packageRoot, options.homeDir, "bin"))
  267. args := []string{
  268. "-s", "dir",
  269. "--description", "Grafana",
  270. "-C", packageRoot,
  271. "--url", "https://grafana.com",
  272. "--maintainer", "contact@grafana.com",
  273. "--config-files", options.initdScriptFilePath,
  274. "--config-files", options.etcDefaultFilePath,
  275. "--config-files", options.systemdServiceFilePath,
  276. "--after-install", options.postinstSrc,
  277. "--version", linuxPackageVersion,
  278. "-p", "./dist",
  279. }
  280. name := "grafana"
  281. if enterprise {
  282. name += "-enterprise"
  283. args = append(args, "--replaces", "grafana")
  284. }
  285. args = append(args, "--name", name)
  286. description := "Grafana"
  287. if enterprise {
  288. description += " Enterprise"
  289. }
  290. args = append(args, "--vendor", description)
  291. if !enterprise {
  292. args = append(args, "--license", "\"Apache 2.0\"")
  293. }
  294. if options.packageType == "rpm" {
  295. args = append(args, "--rpm-posttrans", "packaging/rpm/control/posttrans")
  296. }
  297. if options.packageType == "deb" {
  298. args = append(args, "--deb-no-default-config-files")
  299. }
  300. if pkgArch != "" {
  301. args = append(args, "-a", pkgArch)
  302. }
  303. if linuxPackageIteration != "" {
  304. args = append(args, "--iteration", linuxPackageIteration)
  305. }
  306. // add dependenciesj
  307. for _, dep := range options.depends {
  308. args = append(args, "--depends", dep)
  309. }
  310. args = append(args, ".")
  311. fmt.Println("Creating package: ", options.packageType)
  312. runPrint("fpm", append([]string{"-t", options.packageType}, args...)...)
  313. }
  314. func ensureGoPath() {
  315. if os.Getenv("GOPATH") == "" {
  316. cwd, err := os.Getwd()
  317. if err != nil {
  318. log.Fatal(err)
  319. }
  320. gopath := filepath.Clean(filepath.Join(cwd, "../../../../"))
  321. log.Println("GOPATH is", gopath)
  322. os.Setenv("GOPATH", gopath)
  323. }
  324. }
  325. func grunt(params ...string) {
  326. if runtime.GOOS == windows {
  327. runPrint(`.\node_modules\.bin\grunt`, params...)
  328. } else {
  329. runPrint("./node_modules/.bin/grunt", params...)
  330. }
  331. }
  332. func gruntBuildArg(task string) []string {
  333. args := []string{task}
  334. if includeBuildId {
  335. args = append(args, fmt.Sprintf("--pkgVer=%v-%v", linuxPackageVersion, linuxPackageIteration))
  336. } else {
  337. args = append(args, fmt.Sprintf("--pkgVer=%v", version))
  338. }
  339. if pkgArch != "" {
  340. args = append(args, fmt.Sprintf("--arch=%v", pkgArch))
  341. }
  342. if phjsToRelease != "" {
  343. args = append(args, fmt.Sprintf("--phjsToRelease=%v", phjsToRelease))
  344. }
  345. if enterprise {
  346. args = append(args, "--enterprise")
  347. }
  348. args = append(args, fmt.Sprintf("--platform=%v", goos))
  349. return args
  350. }
  351. func setup() {
  352. runPrint("go", "get", "-v", "github.com/golang/dep")
  353. runPrint("go", "install", "-v", "./pkg/cmd/grafana-server")
  354. }
  355. func test(pkg string) {
  356. setBuildEnv()
  357. runPrint("go", "test", "-short", "-timeout", "60s", pkg)
  358. }
  359. func build(binaryName, pkg string, tags []string) {
  360. binary := fmt.Sprintf("./bin/%s-%s/%s", goos, goarch, binaryName)
  361. if isDev {
  362. //don't include os and arch in output path in dev environment
  363. binary = fmt.Sprintf("./bin/%s", binaryName)
  364. }
  365. if goos == windows {
  366. binary += ".exe"
  367. }
  368. if !isDev {
  369. rmr(binary, binary+".md5")
  370. }
  371. args := []string{"build", "-ldflags", ldflags()}
  372. if len(tags) > 0 {
  373. args = append(args, "-tags", strings.Join(tags, ","))
  374. }
  375. if race {
  376. args = append(args, "-race")
  377. }
  378. args = append(args, "-o", binary)
  379. args = append(args, pkg)
  380. if !isDev {
  381. setBuildEnv()
  382. runPrint("go", "version")
  383. fmt.Printf("Targeting %s/%s\n", goos, goarch)
  384. }
  385. runPrint("go", args...)
  386. if !isDev {
  387. // Create an md5 checksum of the binary, to be included in the archive for
  388. // automatic upgrades.
  389. err := md5File(binary)
  390. if err != nil {
  391. log.Fatal(err)
  392. }
  393. }
  394. }
  395. func ldflags() string {
  396. var b bytes.Buffer
  397. b.WriteString("-w")
  398. b.WriteString(fmt.Sprintf(" -X main.version=%s", version))
  399. b.WriteString(fmt.Sprintf(" -X main.commit=%s", getGitSha()))
  400. b.WriteString(fmt.Sprintf(" -X main.buildstamp=%d", buildStamp()))
  401. b.WriteString(fmt.Sprintf(" -X main.buildBranch=%s", getGitBranch()))
  402. return b.String()
  403. }
  404. func rmr(paths ...string) {
  405. for _, path := range paths {
  406. log.Println("rm -r", path)
  407. os.RemoveAll(path)
  408. }
  409. }
  410. func clean() {
  411. if isDev {
  412. return
  413. }
  414. rmr("dist")
  415. rmr("tmp")
  416. rmr(filepath.Join(os.Getenv("GOPATH"), fmt.Sprintf("pkg/%s_%s/github.com/grafana", goos, goarch)))
  417. }
  418. func setBuildEnv() {
  419. os.Setenv("GOOS", goos)
  420. if goos == windows {
  421. // require windows >=7
  422. os.Setenv("CGO_CFLAGS", "-D_WIN32_WINNT=0x0601")
  423. }
  424. if goarch != "amd64" || goos != linux {
  425. // needed for all other archs
  426. cgo = true
  427. }
  428. if strings.HasPrefix(goarch, "armv") {
  429. os.Setenv("GOARCH", "arm")
  430. os.Setenv("GOARM", goarch[4:])
  431. } else {
  432. os.Setenv("GOARCH", goarch)
  433. }
  434. if goarch == "386" {
  435. os.Setenv("GO386", "387")
  436. }
  437. if cgo {
  438. os.Setenv("CGO_ENABLED", "1")
  439. }
  440. if gocc != "" {
  441. os.Setenv("CC", gocc)
  442. }
  443. }
  444. func getGitBranch() string {
  445. v, err := runError("git", "rev-parse", "--abbrev-ref", "HEAD")
  446. if err != nil {
  447. return "master"
  448. }
  449. return string(v)
  450. }
  451. func getGitSha() string {
  452. v, err := runError("git", "rev-parse", "--short", "HEAD")
  453. if err != nil {
  454. return "unknown-dev"
  455. }
  456. return string(v)
  457. }
  458. func buildStamp() int64 {
  459. bs, err := runError("git", "show", "-s", "--format=%ct")
  460. if err != nil {
  461. return time.Now().Unix()
  462. }
  463. s, _ := strconv.ParseInt(string(bs), 10, 64)
  464. return s
  465. }
  466. func runError(cmd string, args ...string) ([]byte, error) {
  467. ecmd := exec.Command(cmd, args...)
  468. bs, err := ecmd.CombinedOutput()
  469. if err != nil {
  470. return nil, err
  471. }
  472. return bytes.TrimSpace(bs), nil
  473. }
  474. func runPrint(cmd string, args ...string) {
  475. log.Println(cmd, strings.Join(args, " "))
  476. ecmd := exec.Command(cmd, args...)
  477. ecmd.Stdout = os.Stdout
  478. ecmd.Stderr = os.Stderr
  479. err := ecmd.Run()
  480. if err != nil {
  481. log.Fatal(err)
  482. }
  483. }
  484. func md5File(file string) error {
  485. fd, err := os.Open(file)
  486. if err != nil {
  487. return err
  488. }
  489. defer fd.Close()
  490. h := md5.New()
  491. _, err = io.Copy(h, fd)
  492. if err != nil {
  493. return err
  494. }
  495. out, err := os.Create(file + ".md5")
  496. if err != nil {
  497. return err
  498. }
  499. _, err = fmt.Fprintf(out, "%x\n", h.Sum(nil))
  500. if err != nil {
  501. return err
  502. }
  503. return out.Close()
  504. }
  505. func shaFilesInDist() {
  506. filepath.Walk("./dist", func(path string, f os.FileInfo, err error) error {
  507. if path == "./dist" {
  508. return nil
  509. }
  510. if !strings.Contains(path, ".sha256") {
  511. err := shaFile(path)
  512. if err != nil {
  513. log.Printf("Failed to create sha file. error: %v\n", err)
  514. }
  515. }
  516. return nil
  517. })
  518. }
  519. func shaFile(file string) error {
  520. fd, err := os.Open(file)
  521. if err != nil {
  522. return err
  523. }
  524. defer fd.Close()
  525. h := sha256.New()
  526. _, err = io.Copy(h, fd)
  527. if err != nil {
  528. return err
  529. }
  530. out, err := os.Create(file + ".sha256")
  531. if err != nil {
  532. return err
  533. }
  534. _, err = fmt.Fprintf(out, "%x\n", h.Sum(nil))
  535. if err != nil {
  536. return err
  537. }
  538. return out.Close()
  539. }
  540. func shortenBuildId(buildId string) string {
  541. buildId = strings.Replace(buildId, "-", "", -1)
  542. if len(buildId) < 9 {
  543. return buildId
  544. }
  545. return buildId[0:8]
  546. }