Skip to content

Commit

Permalink
New cross compilation script. Packages everything for a github release.
Browse files Browse the repository at this point in the history
Add -version print that is set during build script compilation.
  • Loading branch information
jonnenauha committed Sep 8, 2016
1 parent 9848964 commit d75c07f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

bin

prometheus_varnish_exporter
prometheus_varnish_exporter.exe
build/
.idea/
48 changes: 48 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

set -e

rm -rf bin
mkdir -p bin/release

VERSION="1.0"
VERSION_HASH="$(git rev-parse --short HEAD)"
VERSION_DATE="$(date -u '+%d.%m.%Y %H:%M:%S')"

for goos in linux darwin windows freebsd openbsd netbsd ; do
for goarch in amd64 386; do
# path
outdir="bin/$goos/$goarch"
path="$outdir/prometheus_varnish_exporter"
if [ $goos = windows ] ; then
path=$path.exe
fi

# build
echo -e "\nBuilding $goos/$goarch"
GOOS=$goos GOARCH=$goarch go build -o $path -ldflags "-X 'main.Version=$VERSION' -X 'main.VersionHash=$VERSION_HASH' -X 'main.VersionDate=$VERSION_DATE'"
echo " > `du -hc $path | awk 'NR==1{print $1;}'` `file $path`"

# compress (for unique filenames to github release files)
if [ $goos = windows ] ; then
zip -rjX ./bin/release/$goos-$goarch.zip ./$outdir/ > /dev/null 2>&1
else
tar -C ./$outdir/ -cvzf ./bin/release/$goos-$goarch.tar.gz . > /dev/null 2>&1
fi
done
done

go env > .goenv
source .goenv
rm .goenv

echo -e "\nRelease done: $(./bin/$GOOS/$GOARCH/prometheus_varnish_exporter --version)"
for goos in linux darwin windows freebsd openbsd netbsd ; do
for goarch in amd64 386; do
path=bin/release/$goos-$goarch.tar.gz
if [ $goos = windows ] ; then
path=bin/release/$goos-$goarch.zip
fi
echo " > `du -hc $path | awk 'NR==1{print $1;}'` $path"
done
done
26 changes: 25 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
Expand All @@ -12,6 +13,11 @@ import (
)

var (
ApplicationName = "prometheus_varnish_exporter"
Version string
VersionHash string
VersionDate string

PrometheusExporter = NewPrometheusExporter()
VarnishVersion = NewVarnishVersion()

Expand Down Expand Up @@ -63,12 +69,19 @@ func init() {
flag.StringVar(&StartParams.Params.VSM, "N", StartParams.Params.VSM, "varnishstat -N value.")

// modes
version := false
flag.BoolVar(&version, "version", version, "Print version and exit")
flag.BoolVar(&StartParams.Verbose, "verbose", StartParams.Verbose, "Verbose logging.")
flag.BoolVar(&StartParams.Test, "test", StartParams.Test, "Test varnishstat availability, prints available metrics and exits.")
flag.BoolVar(&StartParams.Raw, "raw", StartParams.Test, "Raw stdout logging without timestamps.")

flag.Parse()

if version {
fmt.Printf("%s %s\n", ApplicationName, getVersion(true))
os.Exit(0)
}

logger = log.New(os.Stdout, "", log.Ldate|log.Ltime)

if len(StartParams.Path) == 0 || StartParams.Path[0] != '/' {
Expand All @@ -78,7 +91,7 @@ func init() {

func main() {
if b, err := json.MarshalIndent(StartParams, "", " "); err == nil {
logInfo("Starting up %s", b)
logInfo("%s %s %s", ApplicationName, getVersion(false), b)
} else {
logFatal(err.Error())
}
Expand Down Expand Up @@ -132,3 +145,14 @@ func main() {
http.Handle(StartParams.Path, prometheus.Handler())
logFatalError(http.ListenAndServe(StartParams.ListenAddress, nil))
}

func getVersion(date bool) (version string) {
if Version == "" {
return "dev"
}
version = fmt.Sprintf("v%s (%s)", Version, VersionHash)
if date {
version += " " + VersionDate
}
return version
}

0 comments on commit d75c07f

Please sign in to comment.