From 21604dbe568b1d997453575ba9717168ee1316d2 Mon Sep 17 00:00:00 2001 From: mpl Date: Tue, 9 Feb 2016 17:31:25 +0100 Subject: [PATCH] misc/docker: stamp version as well to binaries Version in binaries will now be: "X.Y (git rev xxxxx)" if X.Y is specified with --tarball_version, "xxxx" otherwise. Also uniformized the flags names and use in other programs. Fixes #665 Change-Id: I958094f69b279437504854a3ff10e924d42c4fb6 --- make.go | 2 +- misc/docker/dock.go | 8 ++- misc/docker/release/build-binaries.go | 70 ++++++++++++------- misc/docker/server/build-camlistore-server.go | 53 ++++++++------ 4 files changed, 80 insertions(+), 53 deletions(-) diff --git a/make.go b/make.go index 4ed16dbc8..edbbc3033 100644 --- a/make.go +++ b/make.go @@ -184,7 +184,7 @@ func main() { } var ldFlags string if *stampVersion { - ldFlags = "-X camlistore.org/pkg/buildinfo.GitInfo=" + version + ldFlags = "-X \"camlistore.org/pkg/buildinfo.GitInfo=" + version + "\"" } baseArgs = append(baseArgs, "--ldflags="+ldFlags, "--tags="+strings.Join(tags, " ")) diff --git a/misc/docker/dock.go b/misc/docker/dock.go index 2e24061bc..efacbe08a 100644 --- a/misc/docker/dock.go +++ b/misc/docker/dock.go @@ -114,10 +114,9 @@ func genCamlistore(ctxDir string) { "--volume=" + ctxDir + "/camlistore.org:/OUT", "--volume=" + path.Join(dockDir, "server/build-camlistore-server.go") + ":" + genCamliProgram + ":ro", } - // TODO(mpl, bradfitz): pass the version to genCamliProgram so it can stamp it into camlistored when building it. if isWIP() { args = append(args, "--volume="+localCamliSource()+":/IN:ro", - goDockerImage, goCmd, "run", genCamliProgram, "--rev="+rev(), "--camlisource=/IN") + goDockerImage, goCmd, "run", genCamliProgram, "--rev=WIP:/IN") } else { args = append(args, goDockerImage, goCmd, "run", genCamliProgram, "--rev="+rev()) } @@ -140,10 +139,13 @@ func genBinaries(ctxDir string) { } if isWIP() { args = append(args, "--volume="+localCamliSource()+":/IN:ro", - image, goCmd, "run", genBinariesProgram, "--rev="+rev(), "--camlisource=/IN", "--os="+*buildOS) + image, goCmd, "run", genBinariesProgram, "--rev=WIP:/IN", "--os="+*buildOS) } else { args = append(args, image, goCmd, "run", genBinariesProgram, "--rev="+rev(), "--os="+*buildOS) } + if *flagVersion != "" { + args = append(args, "--version="+*flagVersion) + } cmd := exec.Command("docker", args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr diff --git a/misc/docker/release/build-binaries.go b/misc/docker/release/build-binaries.go index ae5db6c0d..c1153894c 100644 --- a/misc/docker/release/build-binaries.go +++ b/misc/docker/release/build-binaries.go @@ -31,19 +31,20 @@ import ( "os/exec" "path" "runtime" + "strings" ) var ( - rev = flag.String("rev", "", "Camlistore revision to build (tag or commit hash)") - localSrc = flag.String("camlisource", "", "(dev flag) Path to a local Camlistore source tree from which to build. It is ignored unless -rev=WORKINPROGRESS") - outDir = flag.String("outdir", "/OUT/", "Output directory, where the binaries will be written") - buildOS = flag.String("os", runtime.GOOS, "Operating system to build for.") + flagRev = flag.String("rev", "", "Camlistore revision to build (tag or commit hash). For development purposes, you can instead specify the path to a local Camlistore source tree from which to build, with the form \"WIP:/path/to/dir\".") + flagVersion = flag.String("version", "", "The optional version number (e.g. 0.9) that will be stamped into the binaries, in addition to the revision.") + outDir = flag.String("outdir", "/OUT/", "Output directory, where the binaries will be written") + buildOS = flag.String("os", runtime.GOOS, "Operating system to build for.") ) func usage() { fmt.Fprintf(os.Stderr, "Usage:\n") fmt.Fprintf(os.Stderr, "%s --rev=camlistore_revision\n", os.Args[0]) - fmt.Fprintf(os.Stderr, "%s --rev=WORKINPROGRESS --camlisource=/path/to/camli/source/dir\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "%s --rev=WIP:/path/to/camli/source/dir\n", os.Args[0]) flag.PrintDefaults() example(os.Args[0]) os.Exit(1) @@ -52,23 +53,47 @@ func usage() { func example(program string) { fmt.Fprintf(os.Stderr, "Examples:\n") fmt.Fprintf(os.Stderr, "\tdocker run --rm --volume=/tmp/camli-build/camlistore.org:/OUT camlistore/go %s --rev=4e8413c5012c\n", program) - fmt.Fprintf(os.Stderr, "\tdocker run --rm --volume=/tmp/camli-build/camlistore.org:/OUT --volume=~/camlistore.org:/IN camlistore/go %s --rev=WORKINPROGRESS --camlisource=/IN\n", program) + fmt.Fprintf(os.Stderr, "\tdocker run --rm --volume=/tmp/camli-build/camlistore.org:/OUT --volume=~/camlistore.org:/IN camlistore/go %s --rev=WIP:/IN\n", program) +} + +func isWIP() bool { + return strings.HasPrefix(*flagRev, "WIP") +} + +// localCamliSource returns the path to the local Camlistore source tree +// that should be specified in *flagRev if *flagRev starts with "WIP:", +// empty string otherwise. +func localCamliSource() string { + if !isWIP() { + return "" + } + return strings.TrimPrefix(*flagRev, "WIP:") +} + +func rev() string { + if isWIP() { + return "WORKINPROGRESS" + } + return *flagRev +} + +func version() string { + if *flagVersion != "" { + return fmt.Sprintf("%v (git rev %v)", *flagVersion, rev()) + } + return rev() } func getCamliSrc() { - if *localSrc != "" { - mirrorCamliSrc(*localSrc) + if localCamliSource() != "" { + mirrorCamliSrc(localCamliSource()) } else { fetchCamliSrc() } - // if missing, we insert a VERSION FILE, so make.go does no need git in the container to detect the Camlistore version. + // we insert the version in the VERSION file, so make.go does no need git + // in the container to detect the Camlistore version. check(os.Chdir("/gopath/src/camlistore.org")) - if _, err := os.Stat("VERSION"); err != nil { - if !os.IsNotExist(err) { - log.Fatal(err) - } - check(ioutil.WriteFile("VERSION", []byte(*rev), 0777)) - } + check(ioutil.WriteFile("VERSION", []byte(version()), 0777)) } func mirrorCamliSrc(srcDir string) { @@ -85,7 +110,7 @@ func fetchCamliSrc() { check(os.MkdirAll("/gopath/src/camlistore.org", 0777)) check(os.Chdir("/gopath/src/camlistore.org")) - res, err := http.Get("https://camlistore.googlesource.com/camlistore/+archive/" + *rev + ".tar.gz") + res, err := http.Get("https://camlistore.googlesource.com/camlistore/+archive/" + *flagRev + ".tar.gz") check(err) defer res.Body.Close() gz, err := gzip.NewReader(res.Body) @@ -142,17 +167,8 @@ func checkArgs() { if flag.NArg() != 0 { usage() } - if *rev == "" { - usage() - } - if *rev == "WORKINPROGRESS" { - if *localSrc == "" { - usage() - } - return - } - if *localSrc != "" { - fmt.Fprintf(os.Stderr, "Usage error: --camlisource can only be used with --rev WORKINPROGRESS.\n") + if *flagRev == "" { + fmt.Fprintf(os.Stderr, "Usage error: --rev is required.\n") usage() } } diff --git a/misc/docker/server/build-camlistore-server.go b/misc/docker/server/build-camlistore-server.go index 6008d6415..dffa1ffe5 100644 --- a/misc/docker/server/build-camlistore-server.go +++ b/misc/docker/server/build-camlistore-server.go @@ -35,16 +35,14 @@ import ( ) var ( - // TODO(mpl): make the flags the same as in dock.go - rev = flag.String("rev", "", "Camlistore revision to build (tag or commit hash)") - localSrc = flag.String("camlisource", "", "(dev flag) Path to a local Camlistore source tree from which to build. It is ignored unless -rev=WORKINPROGRESS") - outDir = flag.String("outdir", "/OUT/", "Output directory, where camlistored and all the resources will be written") + flagRev = flag.String("rev", "", "Camlistore revision to build (tag or commit hash). For development purposes, you can instead specify the path to a local Camlistore source tree from which to build, with the form \"WIP:/path/to/dir\".") + outDir = flag.String("outdir", "/OUT/", "Output directory, where camlistored and all the resources will be written") ) func usage() { fmt.Fprintf(os.Stderr, "Usage:\n") fmt.Fprintf(os.Stderr, "%s --rev=camlistore_revision\n", os.Args[0]) - fmt.Fprintf(os.Stderr, "%s --rev=WORKINPROGRESS --camlisource=/path/to/camli/source/dir\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "%s --rev=WIP:/path/to/camli/source/dir\n", os.Args[0]) flag.PrintDefaults() example(os.Args[0]) os.Exit(1) @@ -53,12 +51,33 @@ func usage() { func example(program string) { fmt.Fprintf(os.Stderr, "Examples:\n") fmt.Fprintf(os.Stderr, "\tdocker run --rm --volume=/tmp/camli-build/camlistore.org:/OUT camlistore/go %s --rev=4e8413c5012c\n", program) - fmt.Fprintf(os.Stderr, "\tdocker run --rm --volume=/tmp/camli-build/camlistore.org:/OUT --volume=~/camlistore.org:/IN camlistore/go %s --rev=WORKINPROGRESS --camlisource=/IN\n", program) + fmt.Fprintf(os.Stderr, "\tdocker run --rm --volume=/tmp/camli-build/camlistore.org:/OUT --volume=~/camlistore.org:/IN camlistore/go %s --rev=WIP:/IN\n", program) +} + +func isWIP() bool { + return strings.HasPrefix(*flagRev, "WIP") +} + +// localCamliSource returns the path to the local Camlistore source tree +// that should be specified in *flagRev if *flagRev starts with "WIP:", +// empty string otherwise. +func localCamliSource() string { + if !isWIP() { + return "" + } + return strings.TrimPrefix(*flagRev, "WIP:") +} + +func rev() string { + if isWIP() { + return "WORKINPROGRESS" + } + return *flagRev } func getCamliSrc() { - if *localSrc != "" { - mirrorCamliSrc(*localSrc) + if localCamliSource() != "" { + mirrorCamliSrc(localCamliSource()) return } fetchCamliSrc() @@ -78,7 +97,7 @@ func fetchCamliSrc() { check(os.MkdirAll("/gopath/src/camlistore.org", 0777)) check(os.Chdir("/gopath/src/camlistore.org")) - res, err := http.Get("https://camlistore.googlesource.com/camlistore/+archive/" + *rev + ".tar.gz") + res, err := http.Get("https://camlistore.googlesource.com/camlistore/+archive/" + *flagRev + ".tar.gz") check(err) defer res.Body.Close() gz, err := gzip.NewReader(res.Body) @@ -116,10 +135,9 @@ func buildCamlistored() { os.Setenv("PATH", "/usr/local/go/bin:"+oldPath) os.Setenv("CGO_ENABLED", "0") os.Setenv("GO15VENDOREXPERIMENT", "1") - // TODO(mpl, bradfitz): stamp the 0.9 version here with ldflags if a version was passed as a flag to the program. cmd := exec.Command("go", "build", "-o", path.Join(*outDir, "/bin/camlistored"), - `--ldflags`, "-w -d -linkmode internal -X camlistore.org/pkg/buildinfo.GitInfo="+*rev, + `--ldflags`, "-w -d -linkmode internal -X camlistore.org/pkg/buildinfo.GitInfo="+rev(), "--tags=netgo", "camlistore.org/server/camlistored") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr @@ -157,17 +175,8 @@ func checkArgs() { if flag.NArg() != 0 { usage() } - if *rev == "" { - usage() - } - if *rev == "WORKINPROGRESS" { - if *localSrc == "" { - usage() - } - return - } - if *localSrc != "" { - fmt.Fprintf(os.Stderr, "Usage error: --camlisource can only be used with --rev WORKINPROGRESS.\n") + if *flagRev == "" { + fmt.Fprintf(os.Stderr, "Usage error: --rev is required.\n") usage() } }