Skip to content

Commit

Permalink
feat: new version detection (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laitr0n authored Jan 8, 2025
1 parent 3f6a44c commit 6653fef
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 15 deletions.
12 changes: 6 additions & 6 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ builds:
ldflags:
- -s
- -w
- -X "kyanos/cmd.Version={{.Version}}"
- -X "kyanos/cmd.CommitID={{.Commit}}"
- -X "kyanos/cmd.BuildTime={{.Date}}"
- -X "kyanos/version.Version={{.Version}}"
- -X "kyanos/version.CommitID={{.Commit}}"
- -X "kyanos/version.BuildTime={{.Date}}"
- -linkmode 'external'
- -extldflags "-static"
goos:
Expand All @@ -55,9 +55,9 @@ builds:
ldflags:
- -s
- -w
- -X "kyanos/cmd.Version={{.Version}}"
- -X "kyanos/cmd.CommitID={{.Commit}}"
- -X "kyanos/cmd.BuildTime={{.Date}}"
- -X "kyanos/version.Version={{.Version}}"
- -X "kyanos/version.CommitID={{.Commit}}"
- -X "kyanos/version.BuildTime={{.Date}}"
- -linkmode 'external'
- -extldflags "-static"
goos:
Expand Down
8 changes: 8 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"context"
"errors"
"fmt"
"kyanos/agent/analysis"
anc "kyanos/agent/analysis/common"
Expand All @@ -15,6 +16,7 @@ import (
"kyanos/bpf"
"kyanos/bpf/loader"
"kyanos/common"
"kyanos/version"
"os"
"os/exec"
"os/signal"
Expand All @@ -29,6 +31,12 @@ import (
)

func SetupAgent(options ac.AgentOptions) {
err := version.UpgradeDetect()
if err != nil {
if errors.Is(err, version.ErrBehindLatest) {
common.AgentLog.Warn(err)
}
}
if enabled, err := common.IsEnableBPF(); err == nil && !enabled {
common.AgentLog.Error("BPF is not enabled in your kernel. This might be because your kernel version is too old. " +
"Please check the requirements for Kyanos at https://kyanos.io/quickstart.html#installation-requirements.")
Expand Down
13 changes: 4 additions & 9 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@ package cmd

import (
"fmt"
"kyanos/version"

"github.com/spf13/cobra"
)

var (
// Version -X "kyanos/cmd.Version={{.Version}}"
Version string
// BuildTime -X "kyanos/cmd.CommitID={{.Commit}}"
BuildTime string
// CommitID -X "kyanos/cmd.BuildTime={{.Date}}"
CommitID string
versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version of kyanos",
Run: func(cmd *cobra.Command, args []string) {
// The following vars are set by the linker during build. See the .goreleaser.yaml
// reference: https://goreleaser.com/customization/builds/
cmd.Println(fmt.Sprintf("Version: %v", Version))
cmd.Println(fmt.Sprintf("BuildTime: %v", BuildTime))
cmd.Println(fmt.Sprintf("CommitID: %v", CommitID))
cmd.Println(fmt.Sprintf("Version: %v", version.GetVersion()))
cmd.Println(fmt.Sprintf("BuildTime: %v", version.GetBuildTime()))
cmd.Println(fmt.Sprintf("CommitID: %v", version.GetCommitID()))
},
}
)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/docker/docker v27.3.1+incompatible
github.com/emirpasic/gods v1.18.1
github.com/go-logr/logr v1.4.2
github.com/google/go-github/v68 v68.0.0
github.com/google/gops v0.3.28
github.com/hashicorp/go-version v1.7.0
github.com/jefurry/logrus v2.0.6+incompatible
Expand Down Expand Up @@ -70,6 +71,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,14 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v68 v68.0.0 h1:ZW57zeNZiXTdQ16qrDiZ0k6XucrxZ2CGmoTvcCyQG6s=
github.com/google/go-github/v68 v68.0.0/go.mod h1:K9HAUBovM2sLwM408A18h+wd9vqdLOEqTUCbnRIcx68=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gops v0.3.28 h1:2Xr57tqKAmQYRAfG12E+yLcoa2Y42UJo2lOrUFL9ark=
github.com/google/gops v0.3.28/go.mod h1:6f6+Nl8LcHrzJwi8+p0ii+vmBFSlB4f8cOOkTJ7sk4c=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down
82 changes: 82 additions & 0 deletions version/detect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package version

import (
"context"
"fmt"
"strconv"
"strings"
)

var (
ErrAheadLatest = fmt.Errorf("local version is ahead of the latest release")
ErrBehindLatest = fmt.Errorf("local version is out of date.")
)

func UpgradeDetect() error {
localVersion, err := ParseVersion(Version)
if err != nil {
return fmt.Errorf("Failed to parse local version: %s err: %v", Version, err)
}

ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
releaseTagName, downloadUrl, err := ReleaseVersion(ctx)
if err != nil {
return fmt.Errorf("Failed to get latest release: %v", err)
}
remoteVersion, err := ParseVersion(strings.TrimPrefix(releaseTagName, "v"))
if err != nil {
return err
}

compare := CompareVersions(localVersion, remoteVersion)
switch {
case compare < 0:
return fmt.Errorf("%w Current verion: %q.\n 🎉 New verion: %q from %q", ErrBehindLatest, Version, releaseTagName, downloadUrl)
case compare > 0:
return ErrAheadLatest
default:
return nil
}
}

// SemanticVersion represents a semantic version in the format "major.minor.patch".
type SemanticVersion struct {
Major int
Minor int
Patch int
}

// ParseVersion parses a version string in the format "major.minor.patch" into a SemVer struct.
func ParseVersion(versionStr string) (SemanticVersion, error) {
parts := strings.Split(versionStr, ".")
if len(parts) != 3 {
return SemanticVersion{}, fmt.Errorf("invalid version format")
}

major, err := strconv.Atoi(parts[0])
if err != nil {
return SemanticVersion{}, fmt.Errorf("invalid verision.major format")
}
minor, err := strconv.Atoi(parts[1])
if err != nil {
return SemanticVersion{}, fmt.Errorf("invalid version.minor format")
}
patch, err := strconv.Atoi(parts[2])
if err != nil {
return SemanticVersion{}, fmt.Errorf("invalid version.patch format")
}

return SemanticVersion{Major: major, Minor: minor, Patch: patch}, nil
}

// CompareVersions compares two semantic versions.
func CompareVersions(v1, v2 SemanticVersion) int {
if v1.Major != v2.Major {
return v1.Major - v2.Major
}
if v1.Minor != v2.Minor {
return v1.Minor - v2.Minor
}
return v1.Patch - v2.Patch
}
65 changes: 65 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package version

import (
"context"
"fmt"
"kyanos/common"
"strings"
"time"

"github.com/google/go-github/v68/github"
)

var (
// String -X "kyanos/version.Version={{.Version}}"
Version string
// BuildTime -X "kyanos/version.CommitID={{.Commit}}"
BuildTime string
// CommitID -X "kyanos/version.BuildTime={{.Date}}"
CommitID string
)

const unknown = "<unknown>"

func GetVersion() string {
if Version != "" {
return Version
}
return unknown
}

func GetBuildTime() string {
if BuildTime != "" {
return BuildTime
}
return unknown
}

func GetCommitID() string {
if CommitID != "" {
return CommitID
}
return unknown
}

func ReleaseVersion(ctx context.Context) (string, string, error) {
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

client := github.NewClient(nil)
releases, _, err := client.Repositories.GetLatestRelease(ctx, "hengyoush", "kyanos")
if err != nil {
return "", "", err
}
arch, err := common.UnameMachine()
if err != nil {
return "", "", err
}
kyanosAsset := fmt.Sprintf("kyanos_%s_linux_%s.tar.gz", strings.TrimPrefix(*releases.TagName, "v"), arch)
for _, asset := range releases.Assets {
if *asset.Name == kyanosAsset {
return *releases.TagName, *asset.BrowserDownloadURL, nil
}
}
return *releases.TagName, "", fmt.Errorf("no asset found for %s in github release", kyanosAsset)
}

0 comments on commit 6653fef

Please sign in to comment.