diff --git a/CHANGELOG.md b/CHANGELOG.md index b91caca4..1791bb74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - New macOS .pkgs built automatically by CircleCI - Add Docker support - Add `-json` command line flag for generating and normalising configuration in plain JSON +- Build name and version numbers are now imprinted onto the build, accessible through `yggdrasil -version` and `yggdrasilctl getSelf` ### Changed - Switched to Chord DHT (instead of Kademlia, although protocol-compatible) diff --git a/VERSION b/VERSION deleted file mode 100644 index 3b04cfb6..00000000 --- a/VERSION +++ /dev/null @@ -1 +0,0 @@ -0.2 diff --git a/build b/build index c07e5eac..2e7c20cc 100755 --- a/build +++ b/build @@ -14,10 +14,11 @@ go get -d -v yggdrasil for file in *.go ; do echo "Building: $file" #go build $@ $file + IMPRINT="-X yggdrasil.buildName=$(sh contrib/semver/name.sh) -X yggdrasil.buildVersion=$(sh contrib/semver/version.sh)" if [ $DEBUG ]; then - go build -tags debug -v $file + go build -ldflags="$IMPRINT" -tags debug -v $file else - go build -ldflags="-s -w" -v $file + go build -ldflags="$IMPRINT -s -w" -v $file fi if [ $UPX ]; then upx --brute ${file%.go} diff --git a/contrib/semver/name.sh b/contrib/semver/name.sh index d749d3ff..9cab7e96 100644 --- a/contrib/semver/name.sh +++ b/contrib/semver/name.sh @@ -1,7 +1,16 @@ #!/bin/sh -# Get the branch name, removing any "/" characters from pull requests -BRANCH=$(git symbolic-ref --short HEAD | tr -d "/" 2>/dev/null) +# Get the current branch name +BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null) + +# Complain if the git history is not available +if [ $? != 0 ]; then + printf "unknown" + exit -1 +fi + +# Remove "/" characters from the branch name if present +BRANCH=$(echo $BRANCH | tr -d "/") # Check if the branch name is not master if [ "$BRANCH" = "master" ]; then diff --git a/contrib/semver/version.sh b/contrib/semver/version.sh index 6eeffc5f..f7769a34 100644 --- a/contrib/semver/version.sh +++ b/contrib/semver/version.sh @@ -16,6 +16,12 @@ PATCH=$(git rev-list $TAG..master --count --merges --grep="from $DEVELOPBRANCH" if [ $? != 0 ]; then PATCH=$(git rev-list HEAD --count 2>/dev/null) + # Complain if the git history is not available + if [ $? != 0 ]; then + printf 'unknown' + exit -1 + fi + printf 'v0.0.%d' "$PATCH" exit -1 fi diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index 2b5bc645..af59734e 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -556,6 +556,8 @@ func (a *admin) getData_getSelf() *admin_nodeInfo { table := a.core.switchTable.table.Load().(lookupTable) coords := table.self.getCoords() self := admin_nodeInfo{ + {"build_name", GetBuildName()}, + {"build_version", GetBuildVersion()}, {"box_pub_key", hex.EncodeToString(a.core.boxPub[:])}, {"ip", a.core.GetAddress().String()}, {"subnet", a.core.GetSubnet().String()}, diff --git a/src/yggdrasil/core.go b/src/yggdrasil/core.go index 706b8aa3..b57bd863 100644 --- a/src/yggdrasil/core.go +++ b/src/yggdrasil/core.go @@ -12,6 +12,9 @@ import ( "yggdrasil/defaults" ) +var buildName string +var buildVersion string + // The Core object represents the Yggdrasil node. You should create a Core // object for each Yggdrasil node you plan to run. type Core struct { @@ -59,12 +62,38 @@ func (c *Core) init(bpub *boxPubKey, c.tun.init(c) } +// Get the current build name. This is usually injected if built from git, +// or returns "unknown" otherwise. +func GetBuildName() string { + if buildName == "" { + return "unknown" + } + return buildName +} + +// Get the current build version. This is usually injected if built from git, +// or returns "unknown" otherwise. +func GetBuildVersion() string { + if buildVersion == "" { + return "unknown" + } + return buildVersion +} + // Starts up Yggdrasil using the provided NodeConfig, and outputs debug logging // through the provided log.Logger. The started stack will include TCP and UDP // sockets, a multicast discovery socket, an admin socket, router, switch and // DHT node. func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error { c.log = log + + if buildName != "" { + c.log.Println("Build name:", buildName) + } + if buildVersion != "" { + c.log.Println("Build version:", buildVersion) + } + c.log.Println("Starting up...") var boxPub boxPubKey diff --git a/yggdrasil.go b/yggdrasil.go index 5244a8ec..d326d18e 100644 --- a/yggdrasil.go +++ b/yggdrasil.go @@ -100,10 +100,15 @@ func main() { normaliseconf := flag.Bool("normaliseconf", false, "use in combination with either -useconf or -useconffile, outputs your configuration normalised") confjson := flag.Bool("json", false, "print configuration from -genconf or -normaliseconf as JSON instead of HJSON") autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)") + version := flag.Bool("version", false, "prints the version of this build") flag.Parse() var cfg *nodeConfig switch { + case *version: + fmt.Println("Build name:", yggdrasil.GetBuildName()) + fmt.Println("Build version:", yggdrasil.GetBuildVersion()) + os.Exit(0) case *autoconf: // Use an autoconf-generated config, this will give us random keys and // port numbers, and will use an automatically selected TUN/TAP interface. diff --git a/yggdrasilctl.go b/yggdrasilctl.go index 79b5f86d..6919ec33 100644 --- a/yggdrasilctl.go +++ b/yggdrasilctl.go @@ -181,6 +181,12 @@ func main() { } case "getself": for k, v := range res["self"].(map[string]interface{}) { + if buildname, ok := v.(map[string]interface{})["build_name"].(string); ok { + fmt.Println("Build name:", buildname) + } + if buildversion, ok := v.(map[string]interface{})["build_version"].(string); ok { + fmt.Println("Build version:", buildversion) + } fmt.Println("IPv6 address:", k) if subnet, ok := v.(map[string]interface{})["subnet"].(string); ok { fmt.Println("IPv6 subnet:", subnet)