From 8bde4d4e34216ff98fb91dcf301df46b3ae0d472 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 16 Jun 2015 20:20:59 -0700 Subject: [PATCH] Migrating minio server build to do 'go run make.go' style like mc --- Godeps/Godeps.json | 4 +- .../src/github.com/minio/cli/app.go | 1 - Makefile | 26 ++- buildscripts/date.go | 26 --- hash-binary.go | 34 ---- main.go | 63 +++---- make.go | 168 ++++++++++++++++++ version.go | 29 +++ 8 files changed, 231 insertions(+), 120 deletions(-) delete mode 100644 buildscripts/date.go delete mode 100644 hash-binary.go create mode 100644 make.go create mode 100644 version.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index c5d217ac9..9af504957 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -23,8 +23,8 @@ }, { "ImportPath": "github.com/minio/cli", - "Comment": "1.2.0-111-g432a101", - "Rev": "432a1019ded42d1b34fb5f1aad60fb609245d5a1" + "Comment": "1.2.0-112-g823349c", + "Rev": "823349ce91e76834a4af0119d5bbc58fd4d2c6b0" }, { "ImportPath": "github.com/stretchr/objx", diff --git a/Godeps/_workspace/src/github.com/minio/cli/app.go b/Godeps/_workspace/src/github.com/minio/cli/app.go index 57f1a8872..acde02bbd 100644 --- a/Godeps/_workspace/src/github.com/minio/cli/app.go +++ b/Godeps/_workspace/src/github.com/minio/cli/app.go @@ -198,7 +198,6 @@ func (a *App) Run(arguments []string) (err error) { // RunAndExitOnError - Another entry point to the cli app, takes care of passing arguments and error handling func (a *App) RunAndExitOnError() { if err := a.Run(os.Args); err != nil { - fmt.Fprintln(os.Stderr, err) os.Exit(1) } } diff --git a/Makefile b/Makefile index 9514d2633..e79707b78 100644 --- a/Makefile +++ b/Makefile @@ -31,25 +31,20 @@ cyclo: @echo "Running $@:" @test -z "$$(gocyclo -over 19 . | grep -v Godeps/_workspace/src/ | tee /dev/stderr)" -pre-build: - @echo "Running pre-build:" - -build-all: verifiers - @echo "Building Libraries:" - @godep go generate ./... - @godep go build -a ./... # have no stale packages +gomake-all: getdeps verifiers + @echo "Installing minio:" + @go run make.go install -test-all: build-all - @echo "Running Test Suites:" - @godep go test -race ./... +release: getdeps verifiers + @echo "Installing minio:" + @go run make.go release + @go run make.go install -test: test-all +godepupdate: + @for i in $(grep ImportPath Godeps/Godeps.json | grep -v minio/minio | cut -f2 -d: | sed -e 's/,//' -e 's/^[ \t]*//' -e 's/[ \t]*$//' -e 's/\"//g'); do godep update $i; done -minio: pre-build build-all test-all -install: minio - @echo "Installing minio:" - @godep go install -a -ldflags "-X main.BuildDate `go run buildscripts/date.go`" github.com/minio/minio +install: gomake-all save: @godep save ./... @@ -65,3 +60,4 @@ clean: @rm -fv cover.out @rm -fv pkg/utils/split/TESTPREFIX.* @rm -fv minio + @find Godeps -name "*.a" -type f -exec rm -vf {} \+ diff --git a/buildscripts/date.go b/buildscripts/date.go deleted file mode 100644 index d10a5d19e..000000000 --- a/buildscripts/date.go +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Minio Client (C) 2014, 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "fmt" - "time" -) - -func main() { - fmt.Println(time.Now().UTC().Format(time.RFC3339Nano)) -} diff --git a/hash-binary.go b/hash-binary.go deleted file mode 100644 index 2344a4c4e..000000000 --- a/hash-binary.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "fmt" - "io" - "os" - "os/exec" - - "crypto/md5" -) - -// hashBinary computes MD5SUM of a binary file on disk -func hashBinary(progName string) (string, error) { - path, err := exec.LookPath(progName) - if err != nil { - return "", err - } - - m := md5.New() - - file, err := os.Open(path) // For read access. - if err != nil { - return "", err - } - - io.Copy(m, file) - return fmt.Sprintf("%x", m.Sum(nil)), nil -} - -// mustHashBinarySelf masks any error returned by hashBinary -func mustHashBinarySelf() string { - hash, _ := hashBinary(os.Args[0]) - return hash -} diff --git a/main.go b/main.go index ea38f5848..eeb5c171d 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,6 @@ package main import ( "fmt" - "net/http" "os" "os/user" "runtime" @@ -103,18 +102,6 @@ func getWebServerConfigFunc(c *cli.Context) server.StartServerFunc { } */ -// Build date -var BuildDate string - -// getBuildDate - -func getBuildDate() string { - t, _ := time.Parse(time.RFC3339Nano, BuildDate) - if t.IsZero() { - return "" - } - return t.Format(http.TimeFormat) -} - // Tries to get os/arch/platform specific information // Returns a map of current os/arch/platform/memstats func getSystemData() map[string]string { @@ -141,33 +128,6 @@ func getSystemData() map[string]string { } } -// Version is based on MD5SUM of its binary -var Version = mustHashBinarySelf() - -// Help template -var minioHelpTemplate = `NAME: - {{.Name}} - {{.Usage}} - -USAGE: - {{.Name}} {{if .Flags}}[global flags] {{end}}command{{if .Flags}} [command flags]{{end}} [arguments...] - -COMMANDS: - {{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}} - {{end}}{{if .Flags}} -GLOBAL FLAGS: - {{range .Flags}}{{.}} - {{end}}{{end}} -VERSION: - {{.Version}} - {{if .Compiled}} -BUILD: - {{.Compiled}}{{end}} - {{range $key, $value := .ExtraInfo}} -{{$key}}: - {{$value}} -{{end}} -` - func main() { // set up iodine iodine.SetGlobalState("minio.version", Version) @@ -180,7 +140,7 @@ func main() { app := cli.NewApp() app.Name = "minio" app.Version = Version - app.Compiled = getBuildDate() + app.Compiled = getVersion() app.Author = "Minio.io" app.Usage = "Minimalist Object Storage" app.Flags = flags @@ -191,6 +151,25 @@ func main() { } return nil } - app.CustomAppHelpTemplate = minioHelpTemplate + app.CustomAppHelpTemplate = `NAME: + {{.Name}} - {{.Usage}} + +USAGE: + {{.Name}} {{if .Flags}}[global flags] {{end}}command{{if .Flags}} [command flags]{{end}} [arguments...] + +COMMANDS: + {{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}} + {{end}}{{if .Flags}} +GLOBAL FLAGS: + {{range .Flags}}{{.}} + {{end}}{{end}} +VERSION: + {{if .Compiled}} + {{.Compiled}}{{end}} + {{range $key, $value := .ExtraInfo}} +{{$key}}: + {{$value}} +{{end}} +` app.RunAndExitOnError() } diff --git a/make.go b/make.go new file mode 100644 index 000000000..102ed3c92 --- /dev/null +++ b/make.go @@ -0,0 +1,168 @@ +// +build ignore + +/* + * Minio Client (C) 2014, 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "strconv" + "text/template" + "time" + + "github.com/minio/cli" +) + +type Version struct { + Date string + Tag string +} + +func writeVersion(version Version) error { + var versionTemplate = `// -------- DO NOT EDIT -------- +// this is an autogenerated file + +package main + +import ( + "net/http" + "time" +) + +// Version autogenerated +var Version = {{if .Date}}"{{.Date}}"{{else}}""{{end}} + +// Tag is of following format +// +// [[STRING]-[EPOCH] +// +// STRING is release string of your choice. +// EPOCH is unix seconds since Jan 1, 1970 UTC. +var Tag = {{if .Tag}}"{{.Tag}}"{{else}}""{{end}} + +// getVersion - +func getVersion() string { + t, _ := time.Parse(time.RFC3339Nano, Version) + if t.IsZero() { + return "" + } + return t.Format(http.TimeFormat) +} +` + t := template.Must(template.New("version").Parse(versionTemplate)) + versionFile, err := os.OpenFile("version.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + return err + } + defer versionFile.Close() + err = t.Execute(versionFile, version) + if err != nil { + return err + } + return nil +} + +type command struct { + cmd *exec.Cmd + stderr *bytes.Buffer + stdout *bytes.Buffer +} + +func (c command) runCommand() error { + c.cmd.Stdout = c.stdout + c.cmd.Stderr = c.stderr + return c.cmd.Run() +} +func (c command) String() string { + message := c.stderr.String() + message += c.stdout.String() + return message +} + +func runMinioInstall(ctx *cli.Context) { + if ctx.Args().First() == "help" { + cli.ShowCommandHelpAndExit(ctx, "install", 1) // last argument is exit code + } + minioGenerate := command{exec.Command("godep", "go", "generate", "./..."), &bytes.Buffer{}, &bytes.Buffer{}} + minioBuild := command{exec.Command("godep", "go", "build", "-a", "./..."), &bytes.Buffer{}, &bytes.Buffer{}} + minioTest := command{exec.Command("godep", "go", "test", "-race", "./..."), &bytes.Buffer{}, &bytes.Buffer{}} + minioInstall := command{exec.Command("godep", "go", "install", "-a", "github.com/minio/minio"), &bytes.Buffer{}, &bytes.Buffer{}} + minioGenerateErr := minioGenerate.runCommand() + if minioGenerateErr != nil { + fmt.Print(minioGenerate) + os.Exit(1) + } + fmt.Println(minioGenerate) + minioBuildErr := minioBuild.runCommand() + if minioBuildErr != nil { + fmt.Print(minioBuild) + os.Exit(1) + } + fmt.Print(minioBuild) + minioTestErr := minioTest.runCommand() + if minioTestErr != nil { + fmt.Println(minioTest) + os.Exit(1) + } + fmt.Print(minioTest) + minioInstallErr := minioInstall.runCommand() + if minioInstallErr != nil { + fmt.Println(minioInstall) + os.Exit(1) + } + fmt.Print(minioInstall) +} + +func runMinioRelease(ctx *cli.Context) { + if ctx.Args().First() == "help" { + cli.ShowCommandHelpAndExit(ctx, "release", 1) // last argument is exit code + } + t := time.Now().UTC() + date := t.Format(time.RFC3339Nano) + // [[STRING]-[EPOCH] + // + // STRING is release string of your choice. + // EPOCH is unix seconds since Jan 1, 1970 UTC. + tag := "release" + "-" + strconv.FormatInt(t.Unix(), 10) + + version := Version{Date: date, Tag: tag} + err := writeVersion(version) + if err != nil { + fmt.Print(err) + os.Exit(1) + } +} + +func main() { + app := cli.NewApp() + app.Usage = "Minimalist Object Storage" + app.Commands = []cli.Command{ + { + Name: "release", + Action: runMinioRelease, + }, + { + Name: "install", + Action: runMinioInstall, + }, + } + app.Author = "Minio.io" + app.RunAndExitOnError() +} diff --git a/version.go b/version.go new file mode 100644 index 000000000..e73f9d3c1 --- /dev/null +++ b/version.go @@ -0,0 +1,29 @@ +// -------- DO NOT EDIT -------- +// this is an autogenerated file + +package main + +import ( + "net/http" + "time" +) + +// Version autogenerated +var Version = "2015-06-17T03:17:23.789648634Z" + +// Tag is of following format +// +// [[STRING]-[EPOCH] +// +// STRING is release string of your choice. +// EPOCH is unix seconds since Jan 1, 1970 UTC. +var Tag = "release-1434511043" + +// getVersion - +func getVersion() string { + t, _ := time.Parse(time.RFC3339Nano, Version) + if t.IsZero() { + return "" + } + return t.Format(http.TimeFormat) +}