| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | // +build ignore
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"crypto/md5" | 
					
						
							| 
									
										
										
										
											2015-03-04 23:23:21 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 	"flag" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2015-03-02 19:56:40 +08:00
										 |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 	"log" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"os/exec" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"regexp" | 
					
						
							|  |  |  | 	"runtime" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ( | 
					
						
							| 
									
										
										
										
											2015-04-22 00:46:44 +08:00
										 |  |  | 	versionRe = regexp.MustCompile(`-[0-9]{1,3}-g[0-9a-f]{5,10}`) | 
					
						
							|  |  |  | 	goarch    string | 
					
						
							|  |  |  | 	goos      string | 
					
						
							|  |  |  | 	version   string = "v1" | 
					
						
							|  |  |  | 	// deb & rpm does not support semver so have to handle their version a little differently
 | 
					
						
							|  |  |  | 	linuxPackageVersion   string = "v1" | 
					
						
							|  |  |  | 	linuxPackageIteration string = "" | 
					
						
							|  |  |  | 	race                  bool | 
					
						
							|  |  |  | 	workingDir            string | 
					
						
							| 
									
										
										
										
											2016-02-15 22:22:03 +08:00
										 |  |  | 	binaries              []string = []string{"grafana-server", "grafana-cli"} | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const minGoVersion = 1.3 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							|  |  |  | 	log.SetOutput(os.Stdout) | 
					
						
							|  |  |  | 	log.SetFlags(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-02 19:56:40 +08:00
										 |  |  | 	ensureGoPath() | 
					
						
							| 
									
										
										
										
											2015-03-03 00:21:52 +08:00
										 |  |  | 	readVersionFromPackageJson() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 00:46:44 +08:00
										 |  |  | 	log.Printf("Version: %s, Linux Version: %s, Package Iteration: %s\n", version, linuxPackageVersion, linuxPackageIteration) | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-28 20:10:09 +08:00
										 |  |  | 	workingDir, _ = os.Getwd() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 	for _, cmd := range flag.Args() { | 
					
						
							|  |  |  | 		switch cmd { | 
					
						
							|  |  |  | 		case "setup": | 
					
						
							|  |  |  | 			setup() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case "build": | 
					
						
							| 
									
										
										
										
											2015-03-04 01:11:16 +08:00
										 |  |  | 			clean() | 
					
						
							| 
									
										
										
										
											2016-02-15 22:22:03 +08:00
										 |  |  | 			for _, binary := range binaries { | 
					
						
							|  |  |  | 				build(binary, "./pkg/cmd/"+binary, []string{}) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		case "test": | 
					
						
							| 
									
										
										
										
											2015-01-28 20:10:09 +08:00
										 |  |  | 			test("./pkg/...") | 
					
						
							| 
									
										
										
										
											2015-03-04 21:48:21 +08:00
										 |  |  | 			grunt("test") | 
					
						
							| 
									
										
										
										
											2015-01-28 20:10:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		case "package": | 
					
						
							| 
									
										
										
										
											2016-03-31 03:02:44 +08:00
										 |  |  | 			grunt("release", fmt.Sprintf("--pkgVer=%v-%v", linuxPackageVersion, linuxPackageIteration)) | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 			createLinuxPackages() | 
					
						
							| 
									
										
										
										
											2015-01-28 20:10:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-05 05:36:38 +08:00
										 |  |  | 		case "pkg-rpm": | 
					
						
							|  |  |  | 			grunt("release") | 
					
						
							|  |  |  | 			createRpmPackages() | 
					
						
							| 
									
										
										
										
											2015-12-05 05:55:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-05 05:36:38 +08:00
										 |  |  | 		case "pkg-deb": | 
					
						
							|  |  |  | 			grunt("release") | 
					
						
							|  |  |  | 			createDebPackages() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-04 23:23:21 +08:00
										 |  |  | 		case "latest": | 
					
						
							|  |  |  | 			makeLatestDistCopies() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 		case "clean": | 
					
						
							| 
									
										
										
										
											2015-03-03 01:26:43 +08:00
										 |  |  | 			clean() | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			log.Fatalf("Unknown command %q", cmd) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-04 23:23:21 +08:00
										 |  |  | func makeLatestDistCopies() { | 
					
						
							| 
									
										
										
										
											2015-10-17 00:26:30 +08:00
										 |  |  | 	rpmIteration := "-1" | 
					
						
							|  |  |  | 	if linuxPackageIteration != "" { | 
					
						
							| 
									
										
										
										
											2016-03-31 03:02:44 +08:00
										 |  |  | 		rpmIteration = linuxPackageIteration | 
					
						
							| 
									
										
										
										
											2015-10-17 00:26:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-31 03:02:44 +08:00
										 |  |  | 	runError("cp", fmt.Sprintf("dist/grafana_%v-%v_amd64.deb", linuxPackageVersion, linuxPackageIteration), "dist/grafana_latest_amd64.deb") | 
					
						
							|  |  |  | 	runError("cp", fmt.Sprintf("dist/grafana-%v-%v.x86_64.rpm", linuxPackageVersion, rpmIteration), "dist/grafana-latest-1.x86_64.rpm") | 
					
						
							|  |  |  | 	runError("cp", fmt.Sprintf("dist/grafana-%v-%v.linux-x64.tar.gz", linuxPackageVersion, linuxPackageIteration), "dist/grafana-latest.linux-x64.tar.gz") | 
					
						
							| 
									
										
										
										
											2015-03-04 23:23:21 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-03 00:21:52 +08:00
										 |  |  | func readVersionFromPackageJson() { | 
					
						
							| 
									
										
										
										
											2015-03-04 23:23:21 +08:00
										 |  |  | 	reader, err := os.Open("package.json") | 
					
						
							| 
									
										
										
										
											2015-03-03 00:21:52 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-03-04 23:23:21 +08:00
										 |  |  | 		log.Fatal("Failed to open package.json") | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer reader.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	jsonObj := map[string]interface{}{} | 
					
						
							|  |  |  | 	jsonParser := json.NewDecoder(reader) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := jsonParser.Decode(&jsonObj); err != nil { | 
					
						
							|  |  |  | 		log.Fatal("Failed to decode package.json") | 
					
						
							| 
									
										
										
										
											2015-03-03 00:21:52 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-04 23:23:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	version = jsonObj["version"].(string) | 
					
						
							| 
									
										
										
										
											2015-04-22 00:46:44 +08:00
										 |  |  | 	linuxPackageVersion = version | 
					
						
							|  |  |  | 	linuxPackageIteration = "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// handle pre version stuff (deb / rpm does not support semver)
 | 
					
						
							| 
									
										
										
										
											2015-04-22 14:56:14 +08:00
										 |  |  | 	parts := strings.Split(version, "-") | 
					
						
							| 
									
										
										
										
											2015-04-22 00:46:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 14:56:14 +08:00
										 |  |  | 	if len(parts) > 1 { | 
					
						
							|  |  |  | 		linuxPackageVersion = parts[0] | 
					
						
							|  |  |  | 		linuxPackageIteration = parts[1] | 
					
						
							| 
									
										
										
										
											2016-03-31 01:05:30 +08:00
										 |  |  | 		if linuxPackageIteration != "" { | 
					
						
							|  |  |  | 			// add timestamp to iteration
 | 
					
						
							|  |  |  | 			linuxPackageIteration = fmt.Sprintf("%s%v", linuxPackageIteration, time.Now().Unix()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		log.Println(fmt.Sprintf("teration %v", linuxPackageIteration)) | 
					
						
							| 
									
										
										
										
											2015-04-22 00:46:44 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-03 00:21:52 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | type linuxPackageOptions struct { | 
					
						
							|  |  |  | 	packageType            string | 
					
						
							|  |  |  | 	homeDir                string | 
					
						
							| 
									
										
										
										
											2016-02-15 22:22:03 +08:00
										 |  |  | 	binPath                string | 
					
						
							| 
									
										
										
										
											2016-02-15 21:58:16 +08:00
										 |  |  | 	serverBinPath          string | 
					
						
							|  |  |  | 	cliBinPath             string | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	configDir              string | 
					
						
							|  |  |  | 	configFilePath         string | 
					
						
							| 
									
										
										
										
											2015-07-15 16:08:23 +08:00
										 |  |  | 	ldapFilePath           string | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	etcDefaultPath         string | 
					
						
							|  |  |  | 	etcDefaultFilePath     string | 
					
						
							|  |  |  | 	initdScriptFilePath    string | 
					
						
							|  |  |  | 	systemdServiceFilePath string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	postinstSrc    string | 
					
						
							|  |  |  | 	initdScriptSrc string | 
					
						
							|  |  |  | 	defaultFileSrc string | 
					
						
							|  |  |  | 	systemdFileSrc string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	depends []string | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-04-08 22:58:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-05 05:36:38 +08:00
										 |  |  | func createDebPackages() { | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	createPackage(linuxPackageOptions{ | 
					
						
							|  |  |  | 		packageType:            "deb", | 
					
						
							|  |  |  | 		homeDir:                "/usr/share/grafana", | 
					
						
							| 
									
										
										
										
											2016-02-15 22:22:03 +08:00
										 |  |  | 		binPath:                "/usr/sbin", | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 		configDir:              "/etc/grafana", | 
					
						
							|  |  |  | 		configFilePath:         "/etc/grafana/grafana.ini", | 
					
						
							| 
									
										
										
										
											2015-07-15 16:08:23 +08:00
										 |  |  | 		ldapFilePath:           "/etc/grafana/ldap.toml", | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 		etcDefaultPath:         "/etc/default", | 
					
						
							|  |  |  | 		etcDefaultFilePath:     "/etc/default/grafana-server", | 
					
						
							|  |  |  | 		initdScriptFilePath:    "/etc/init.d/grafana-server", | 
					
						
							|  |  |  | 		systemdServiceFilePath: "/usr/lib/systemd/system/grafana-server.service", | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		postinstSrc:    "packaging/deb/control/postinst", | 
					
						
							|  |  |  | 		initdScriptSrc: "packaging/deb/init.d/grafana-server", | 
					
						
							|  |  |  | 		defaultFileSrc: "packaging/deb/default/grafana-server", | 
					
						
							|  |  |  | 		systemdFileSrc: "packaging/deb/systemd/grafana-server.service", | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		depends: []string{"adduser", "libfontconfig"}, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-12-05 05:36:38 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-05 05:36:38 +08:00
										 |  |  | func createRpmPackages() { | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	createPackage(linuxPackageOptions{ | 
					
						
							|  |  |  | 		packageType:            "rpm", | 
					
						
							|  |  |  | 		homeDir:                "/usr/share/grafana", | 
					
						
							| 
									
										
										
										
											2016-02-15 22:22:03 +08:00
										 |  |  | 		binPath:                "/usr/sbin", | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 		configDir:              "/etc/grafana", | 
					
						
							|  |  |  | 		configFilePath:         "/etc/grafana/grafana.ini", | 
					
						
							| 
									
										
										
										
											2015-07-15 16:08:23 +08:00
										 |  |  | 		ldapFilePath:           "/etc/grafana/ldap.toml", | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 		etcDefaultPath:         "/etc/sysconfig", | 
					
						
							|  |  |  | 		etcDefaultFilePath:     "/etc/sysconfig/grafana-server", | 
					
						
							|  |  |  | 		initdScriptFilePath:    "/etc/init.d/grafana-server", | 
					
						
							|  |  |  | 		systemdServiceFilePath: "/usr/lib/systemd/system/grafana-server.service", | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		postinstSrc:    "packaging/rpm/control/postinst", | 
					
						
							|  |  |  | 		initdScriptSrc: "packaging/rpm/init.d/grafana-server", | 
					
						
							|  |  |  | 		defaultFileSrc: "packaging/rpm/sysconfig/grafana-server", | 
					
						
							|  |  |  | 		systemdFileSrc: "packaging/rpm/systemd/grafana-server.service", | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		depends: []string{"initscripts", "fontconfig"}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-04-08 22:58:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-05 05:36:38 +08:00
										 |  |  | func createLinuxPackages() { | 
					
						
							|  |  |  | 	createDebPackages() | 
					
						
							|  |  |  | 	createRpmPackages() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | func createPackage(options linuxPackageOptions) { | 
					
						
							|  |  |  | 	packageRoot, _ := ioutil.TempDir("", "grafana-linux-pack") | 
					
						
							| 
									
										
										
										
											2015-03-02 19:56:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-09 23:18:04 +08:00
										 |  |  | 	// create directories
 | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	runPrint("mkdir", "-p", filepath.Join(packageRoot, options.homeDir)) | 
					
						
							|  |  |  | 	runPrint("mkdir", "-p", filepath.Join(packageRoot, options.configDir)) | 
					
						
							| 
									
										
										
										
											2015-04-10 16:58:32 +08:00
										 |  |  | 	runPrint("mkdir", "-p", filepath.Join(packageRoot, "/etc/init.d")) | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	runPrint("mkdir", "-p", filepath.Join(packageRoot, options.etcDefaultPath)) | 
					
						
							| 
									
										
										
										
											2015-04-10 16:58:32 +08:00
										 |  |  | 	runPrint("mkdir", "-p", filepath.Join(packageRoot, "/usr/lib/systemd/system")) | 
					
						
							|  |  |  | 	runPrint("mkdir", "-p", filepath.Join(packageRoot, "/usr/sbin")) | 
					
						
							| 
									
										
										
										
											2015-03-02 19:56:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-09 23:18:04 +08:00
										 |  |  | 	// copy binary
 | 
					
						
							| 
									
										
										
										
											2016-02-15 22:22:03 +08:00
										 |  |  | 	for _, binary := range binaries { | 
					
						
							|  |  |  | 		runPrint("cp", "-p", filepath.Join(workingDir, "tmp/bin/"+binary), filepath.Join(packageRoot, "/usr/sbin/"+binary)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-08 20:10:04 +08:00
										 |  |  | 	// copy init.d script
 | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	runPrint("cp", "-p", options.initdScriptSrc, filepath.Join(packageRoot, options.initdScriptFilePath)) | 
					
						
							| 
									
										
										
										
											2015-04-09 23:18:04 +08:00
										 |  |  | 	// copy environment var file
 | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	runPrint("cp", "-p", options.defaultFileSrc, filepath.Join(packageRoot, options.etcDefaultFilePath)) | 
					
						
							| 
									
										
										
										
											2015-04-08 22:58:05 +08:00
										 |  |  | 	// copy systemd file
 | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	runPrint("cp", "-p", options.systemdFileSrc, filepath.Join(packageRoot, options.systemdServiceFilePath)) | 
					
						
							| 
									
										
										
										
											2015-04-08 20:10:04 +08:00
										 |  |  | 	// copy release files
 | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	runPrint("cp", "-a", filepath.Join(workingDir, "tmp")+"/.", filepath.Join(packageRoot, options.homeDir)) | 
					
						
							| 
									
										
										
										
											2015-04-15 21:41:56 +08:00
										 |  |  | 	// remove bin path
 | 
					
						
							|  |  |  | 	runPrint("rm", "-rf", filepath.Join(packageRoot, options.homeDir, "bin")) | 
					
						
							| 
									
										
										
										
											2015-07-15 16:08:23 +08:00
										 |  |  | 	// copy sample ini file to /etc/grafana
 | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	runPrint("cp", "conf/sample.ini", filepath.Join(packageRoot, options.configFilePath)) | 
					
						
							| 
									
										
										
										
											2015-07-15 16:08:23 +08:00
										 |  |  | 	// copy sample ldap toml config file to /etc/grafana/ldap.toml
 | 
					
						
							| 
									
										
										
										
											2015-07-15 19:52:30 +08:00
										 |  |  | 	runPrint("cp", "conf/ldap.toml", filepath.Join(packageRoot, options.ldapFilePath)) | 
					
						
							| 
									
										
										
										
											2015-03-02 19:56:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	args := []string{ | 
					
						
							|  |  |  | 		"-s", "dir", | 
					
						
							|  |  |  | 		"--description", "Grafana", | 
					
						
							|  |  |  | 		"-C", packageRoot, | 
					
						
							|  |  |  | 		"--vendor", "Grafana", | 
					
						
							|  |  |  | 		"--url", "http://grafana.org", | 
					
						
							| 
									
										
										
										
											2016-01-17 02:16:49 +08:00
										 |  |  | 		"--license", "\"Apache 2.0\"", | 
					
						
							| 
									
										
										
										
											2015-03-02 19:56:40 +08:00
										 |  |  | 		"--maintainer", "contact@grafana.org", | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 		"--config-files", options.configFilePath, | 
					
						
							| 
									
										
										
										
											2015-08-22 14:34:20 +08:00
										 |  |  | 		"--config-files", options.ldapFilePath, | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 		"--config-files", options.initdScriptFilePath, | 
					
						
							|  |  |  | 		"--config-files", options.etcDefaultFilePath, | 
					
						
							|  |  |  | 		"--config-files", options.systemdServiceFilePath, | 
					
						
							|  |  |  | 		"--after-install", options.postinstSrc, | 
					
						
							| 
									
										
										
										
											2015-03-02 19:56:40 +08:00
										 |  |  | 		"--name", "grafana", | 
					
						
							| 
									
										
										
										
											2015-04-22 00:46:44 +08:00
										 |  |  | 		"--version", linuxPackageVersion, | 
					
						
							| 
									
										
										
										
											2015-03-02 19:56:40 +08:00
										 |  |  | 		"-p", "./dist", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 00:46:44 +08:00
										 |  |  | 	if linuxPackageIteration != "" { | 
					
						
							|  |  |  | 		args = append(args, "--iteration", linuxPackageIteration) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-10 22:20:01 +08:00
										 |  |  | 	// add dependenciesj
 | 
					
						
							|  |  |  | 	for _, dep := range options.depends { | 
					
						
							|  |  |  | 		args = append(args, "--depends", dep) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	args = append(args, ".") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Println("Creating package: ", options.packageType) | 
					
						
							|  |  |  | 	runPrint("fpm", append([]string{"-t", options.packageType}, args...)...) | 
					
						
							| 
									
										
										
										
											2015-03-02 19:56:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-03 01:26:43 +08:00
										 |  |  | func verifyGitRepoIsClean() { | 
					
						
							| 
									
										
										
										
											2015-03-02 19:56:40 +08:00
										 |  |  | 	rs, err := runError("git", "ls-files", "--modified") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Fatalf("Failed to check if git tree was clean, %v, %v\n", string(rs), err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	count := len(string(rs)) | 
					
						
							|  |  |  | 	if count > 0 { | 
					
						
							|  |  |  | 		log.Fatalf("Git repository has modified files, aborting") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-03 01:26:43 +08:00
										 |  |  | 	log.Println("Git repository is clean") | 
					
						
							| 
									
										
										
										
											2015-03-02 19:56:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-28 20:10:09 +08:00
										 |  |  | func ChangeWorkingDir(dir string) { | 
					
						
							|  |  |  | 	os.Chdir(dir) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-04 14:09:59 +08:00
										 |  |  | func grunt(params ...string) { | 
					
						
							|  |  |  | 	runPrint("./node_modules/grunt-cli/bin/grunt", params...) | 
					
						
							| 
									
										
										
										
											2015-01-28 20:10:09 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | func setup() { | 
					
						
							|  |  |  | 	runPrint("go", "get", "-v", "github.com/tools/godep") | 
					
						
							| 
									
										
										
										
											2015-04-22 14:26:49 +08:00
										 |  |  | 	runPrint("go", "get", "-v", "github.com/blang/semver") | 
					
						
							| 
									
										
										
										
											2015-03-11 00:34:28 +08:00
										 |  |  | 	runPrint("go", "get", "-v", "github.com/mattn/go-sqlite3") | 
					
						
							|  |  |  | 	runPrint("go", "install", "-v", "github.com/mattn/go-sqlite3") | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func test(pkg string) { | 
					
						
							|  |  |  | 	setBuildEnv() | 
					
						
							|  |  |  | 	runPrint("go", "test", "-short", "-timeout", "60s", pkg) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-15 21:09:34 +08:00
										 |  |  | func build(binaryName, pkg string, tags []string) { | 
					
						
							|  |  |  | 	binary := "./bin/" + binaryName | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 	if goos == "windows" { | 
					
						
							|  |  |  | 		binary += ".exe" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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() | 
					
						
							| 
									
										
										
										
											2015-12-04 18:21:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	runPrint("go", "version") | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 	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 ldflags() string { | 
					
						
							|  |  |  | 	var b bytes.Buffer | 
					
						
							|  |  |  | 	b.WriteString("-w") | 
					
						
							| 
									
										
										
										
											2015-12-01 16:23:30 +08:00
										 |  |  | 	b.WriteString(fmt.Sprintf(" -X main.version=%s", version)) | 
					
						
							|  |  |  | 	b.WriteString(fmt.Sprintf(" -X main.commit=%s", getGitSha())) | 
					
						
							|  |  |  | 	b.WriteString(fmt.Sprintf(" -X main.buildstamp=%d", buildStamp())) | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 	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") | 
					
						
							| 
									
										
										
										
											2015-03-03 01:26:43 +08:00
										 |  |  | 	rmr("dist") | 
					
						
							|  |  |  | 	rmr("tmp") | 
					
						
							| 
									
										
										
										
											2015-03-03 17:18:24 +08:00
										 |  |  | 	rmr(filepath.Join(os.Getenv("GOPATH"), fmt.Sprintf("pkg/%s_%s/github.com/grafana", goos, goarch))) | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 getGitSha() string { | 
					
						
							|  |  |  | 	v, err := runError("git", "describe", "--always", "--dirty") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "unknown-dev" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte { | 
					
						
							|  |  |  | 		s[0] = '+' | 
					
						
							|  |  |  | 		return s | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	return string(v) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func buildStamp() int64 { | 
					
						
							|  |  |  | 	bs, err := runError("git", "show", "-s", "--format=%ct") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return time.Now().Unix() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	s, _ := strconv.ParseInt(string(bs), 10, 64) | 
					
						
							|  |  |  | 	return s | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func buildArch() string { | 
					
						
							|  |  |  | 	os := goos | 
					
						
							|  |  |  | 	if os == "darwin" { | 
					
						
							|  |  |  | 		os = "macosx" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return fmt.Sprintf("%s-%s", os, goarch) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func run(cmd string, args ...string) []byte { | 
					
						
							|  |  |  | 	bs, err := runError(cmd, args...) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Println(cmd, strings.Join(args, " ")) | 
					
						
							|  |  |  | 		log.Println(string(bs)) | 
					
						
							|  |  |  | 		log.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return bytes.TrimSpace(bs) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func runError(cmd string, args ...string) ([]byte, error) { | 
					
						
							|  |  |  | 	ecmd := exec.Command(cmd, args...) | 
					
						
							|  |  |  | 	bs, err := ecmd.CombinedOutput() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-04 23:23:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-05 17:46:58 +08:00
										 |  |  | 	return bytes.TrimSpace(bs), nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 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() | 
					
						
							|  |  |  | } |