Skip to content

Commit

Permalink
build: set microovn's binary version at build time
Browse files Browse the repository at this point in the history
This fixes incorrect version (`0.1`) reported by `microovn --version`.
New behavior is to output current version of `MicroOVN` along with
`ovn` and `openvswitch` versions of packages from which it was built.

Closes LP#2049941

Signed-off-by: Martin Kalcok <martin.kalcok@canonical.com>
  • Loading branch information
mkalcok committed Mar 5, 2024
1 parent ff82e5d commit 95105e3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
11 changes: 11 additions & 0 deletions microovn/cmd/microovn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main

import (
"bufio"
"fmt"
"os"

"github.com/spf13/cobra"
Expand All @@ -25,6 +26,15 @@ type CmdControl struct {
asker cli.Asker
}

// MicroOvnVersion contains version of MicroOVN (set at build time)
var MicroOvnVersion string

// OvnVersion contains version of 'ovn' package used to build MicroOVN (set at build time)
var OvnVersion string

// OvsVersion contains version of 'openvswitch' package used to build MicroOVN (set at build time)
var OvsVersion string

func main() {
// common flags.
commonCmd := CmdControl{asker: cli.NewAsker(bufio.NewReader(os.Stdin))}
Expand All @@ -44,6 +54,7 @@ func main() {
app.PersistentFlags().BoolVarP(&commonCmd.FlagLogVerbose, "verbose", "v", false, "Show all information messages")

app.SetVersionTemplate("{{.Version}}\n")
app.Version = fmt.Sprintf("microovn: %s\novn: %s\nopenvswitch: %s", MicroOvnVersion, OvnVersion, OvsVersion)

// Top-level.
var cmdInit = cmdInit{common: &commonCmd}
Expand Down
45 changes: 31 additions & 14 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ base: core24
build-base: devel
assumes:
- snapd2.59
adopt-info: ovn
adopt-info: microovn
grade: devel
source-code: https://github.com/canonical/microovn.git
summary: Simple clustered OVN deployment
Expand Down Expand Up @@ -223,18 +223,6 @@ parts:
craftctl default
mkdir -p "${CRAFT_PART_INSTALL}/etc/ovn/"
override-stage: |
craftctl default
pkg_version=$(
dpkg-deb -f \
$CRAFT_PART_SRC/../stage_packages/ovn-host*.deb \
Version | sed -rne 's/([0-9.]+)[-+~].*$$/\1/p')
git_version=$(
git -C $CRAFT_PROJECT_DIR describe \
--always \
--dirty \
--abbrev=10)
craftctl set version=${pkg_version}+snap${git_version}
ovs:
plugin: nil
Expand Down Expand Up @@ -275,6 +263,8 @@ parts:
source: microovn/
after:
- dqlite
- ovn
- ovs
build-snaps:
- go
plugin: nil
Expand All @@ -292,8 +282,35 @@ parts:
export CGO_LDFLAGS="-L${CRAFT_STAGE}/lib/ -L${CRAFT_STAGE}/usr/local/lib/"
export CGO_LDFLAGS_ALLOW="(-Wl,-wrap,pthread_create)|(-Wl,-z,now)"
# Determine versions of packages used to build MicroOVN
# Note (mkalcok): This code reaches into other snapcraft parts via hardcoded paths
# to determine version of packages from which they were built. It
# will require updates if these part names ever change.
ovn_pkg_version=$(
dpkg-deb -f \
$CRAFT_PART_SRC/../../ovn/stage_packages/ovn-host*.deb Version)
ovs_pkg_version=$(
dpkg-deb -f \
$CRAFT_PART_SRC/../../ovs/stage_packages/openvswitch-switch*.deb Version)
git_version=$(
git -C $CRAFT_PROJECT_DIR describe \
--always \
--dirty \
--abbrev=10)
# Set Snap's version string
# Note (mkalcok): Short version string does not include potential package snapshot hash.
# This is required because Snap's version string is limited to 32 chars
# and inclusion of snapshot hash violates this boundary.
ovn_pkg_short_version=$(echo $ovn_pkg_version | sed -rne 's/([0-9.]+)[-+~].*$$/\1/p')
craftctl set version=${ovn_pkg_short_version}+snap${git_version}
# Build the binaries
go build -o "${CRAFT_PART_INSTALL}/bin/microovn" ./cmd/microovn
go build -o "${CRAFT_PART_INSTALL}/bin/microovn" \
-ldflags "-X 'main.MicroOvnVersion=${git_version}' \
-X 'main.OvnVersion=${ovn_pkg_version}' \
-X 'main.OvsVersion=${ovs_pkg_version}'" \
./cmd/microovn
go build -o "${CRAFT_PART_INSTALL}/bin/microovnd" -tags=libsqlite3 ./cmd/microovnd
prime:
- bin/microovn
Expand Down

0 comments on commit 95105e3

Please sign in to comment.