Skip to content

Commit

Permalink
Don't read tool version from a file.
Browse files Browse the repository at this point in the history
Instead, rely on the version stored inside the compiled binary.
This version is automatically filled by `go install`.

This change will allow us to simply tag the release and not have
to also update the version manually.
  • Loading branch information
spetrovic77 committed Sep 18, 2023
1 parent 5fe586c commit 3e2dcde
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 48 deletions.
6 changes: 2 additions & 4 deletions cmd/weaver-kube/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ const (
)

var (
flags = flag.NewFlagSet("deploy", flag.ContinueOnError)
runInDevMode = flags.Bool("runInDevMode", false, "Whether deploy in development mode.")

flags = flag.NewFlagSet("deploy", flag.ContinueOnError)
deployCmd = tool.Command{
Name: "deploy",
Description: "Deploy a Service Weaver app",
Expand Down Expand Up @@ -141,7 +139,7 @@ func deploy(ctx context.Context, args []string) error {
}

// Build the docker image for the deployment.
image, err := impl.BuildAndUploadDockerImage(ctx, dep, config.LocalTag, config.Repo, *runInDevMode)
image, err := impl.BuildAndUploadDockerImage(ctx, dep, config.LocalTag, config.Repo)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/weaver-kube/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"fmt"
"runtime"

"github.com/ServiceWeaver/weaver-kube/internal/version"
"github.com/ServiceWeaver/weaver-kube/internal/impl"
"github.com/ServiceWeaver/weaver/runtime/tool"
)

Expand All @@ -30,7 +30,11 @@ var versionCmd = tool.Command{
Description: "Show weaver kube version",
Help: "Usage:\n weaver kube version",
Fn: func(context.Context, []string) error {
fmt.Printf("weaver kube v%d.%d.%d %s/%s\n", version.Major, version.Minor, version.Patch, runtime.GOOS, runtime.GOARCH)
version, _, err := impl.ToolVersion()
if err != nil {
return err
}
fmt.Printf("weaver kube %s %s/%s\n", version, runtime.GOOS, runtime.GOARCH)
return nil
},
}
56 changes: 36 additions & 20 deletions internal/impl/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package impl

import (
"bufio"
"context"
"fmt"
"os"
Expand All @@ -25,7 +26,6 @@ import (
"text/template"
"time"

"github.com/ServiceWeaver/weaver-kube/internal/version"
"github.com/ServiceWeaver/weaver/runtime/protos"
"github.com/google/uuid"
)
Expand Down Expand Up @@ -59,9 +59,9 @@ type buildSpec struct {
// BuildAndUploadDockerImage builds a docker image and uploads it to a remote
// repo, if one is specified. It returns the docker image tag that should
// be used in the application containers.
func BuildAndUploadDockerImage(ctx context.Context, dep *protos.Deployment, buildTag, dockerRepo string, runInDevMode bool) (string, error) {
func BuildAndUploadDockerImage(ctx context.Context, dep *protos.Deployment, buildTag, dockerRepo string) (string, error) {
// Create the build specifications.
spec, err := dockerBuildSpec(dep, buildTag, runInDevMode)
spec, err := dockerBuildSpec(dep, buildTag)
if err != nil {
return "", fmt.Errorf("unable to build image spec: %w", err)
}
Expand All @@ -82,32 +82,48 @@ func BuildAndUploadDockerImage(ctx context.Context, dep *protos.Deployment, buil
}

// dockerBuildSpec creates a build specification for an app deployment.
func dockerBuildSpec(dep *protos.Deployment, buildTag string, runInDevMode bool) (*buildSpec, error) {
files := []string{dep.App.Binary}
toolVersion := fmt.Sprintf("v%d.%d.%d", version.Major, version.Minor, version.Patch)
goInstall := []string{
"github.com/ServiceWeaver/weaver-kube/cmd/weaver-kube@" + toolVersion,
}
if buildTag == "" {
buildTag = fmt.Sprintf("%s:%s", dep.App.Name, dep.Id[:8])
func dockerBuildSpec(dep *protos.Deployment, buildTag string) (*buildSpec, error) {
// Figure out which tool binary will run inside the container.
toolVersion, toolIsDev, err := ToolVersion()
if err != nil {
return nil, err
}

// If we run the kube deployer in the development mode, we should copy the
// local kube binary to the image instead.
if runInDevMode && runtime.GOOS == "linux" && runtime.GOARCH == "amd64" {
// Use the running weaver-kube tool binary.
toCopy := []string{dep.App.Binary}
var toInstall []string
if runtime.GOOS == "linux" && runtime.GOARCH == "amd64" {
// The running tool binary can run inside the container: copy it.
toolBinPath, err := os.Executable()
if err != nil {
return nil, err
}
files = append(files, toolBinPath)
goInstall = []string{}
toCopy = append(toCopy, toolBinPath)
} else if toolIsDev {
// Devel tool binary that's not linux/amd64: prompt the user.
scanner := bufio.NewScanner(os.Stdin)
fmt.Print(
`The running weaver-kube binary hasn't been cross-compiled for linux/amd64 and
cannot run inside the container. Instead, the latest weaver-kube binary will be
downloaded and installed in the container. Do you want to proceed? [Y/n] `)
scanner.Scan()
text := scanner.Text()
if text != "" && text != "y" && text != "Y" {
return nil, fmt.Errorf("user bailed out")
}
toInstall = append(toInstall, "github.com/ServiceWeaver/weaver-kube/cmd/weaver-kube@latest")
} else {
// Released tool binary that's not compiled to linux/amd64. Re-install
// it inside the container.
toInstall = append(toInstall, "github.com/ServiceWeaver/weaver-kube/cmd/weaver-kube@"+toolVersion)
}

if buildTag == "" {
buildTag = fmt.Sprintf("%s:%s", dep.App.Name, dep.Id[:8])
}

return &buildSpec{
tag: buildTag,
files: files,
goInstall: goInstall,
files: toCopy,
goInstall: toInstall,
}, nil
}

Expand Down
18 changes: 18 additions & 0 deletions internal/impl/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package impl

import (
"fmt"
"runtime/debug"
)

// ToolVersion returns the version of the running tool binary, along with
// an indication whether the tool was built manually, i.e., not via go install.
func ToolVersion() (string, bool, error) {
info, ok := debug.ReadBuildInfo()
if !ok {
// Should never happen.
return "", false, fmt.Errorf("tool binary must be built from a module")
}
dev := info.Main.Version == "(devel)"
return info.Main.Version, dev, nil
}
22 changes: 0 additions & 22 deletions internal/version/version.go

This file was deleted.

0 comments on commit 3e2dcde

Please sign in to comment.