Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/boxcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func RootCmd() *cobra.Command {
Use: "devbox",
Short: "Instant, easy, predictable development environments",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
vercheck.CheckLauncherVersion(cmd.ErrOrStderr())
vercheck.CheckVersion(cmd.ErrOrStderr())
if flags.quiet {
cmd.SetErr(io.Discard)
}
Expand Down
17 changes: 11 additions & 6 deletions internal/boxcli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package boxcli

import (
"fmt"
"os"
"runtime"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -56,19 +57,21 @@ func versionCmdFunc(cmd *cobra.Command, _ []string, flags versionFlags) error {
fmt.Fprintf(w, "Commit: %v\n", v.Commit)
fmt.Fprintf(w, "Commit Time: %v\n", v.CommitDate)
fmt.Fprintf(w, "Go Version: %v\n", v.GoVersion)
fmt.Fprintf(w, "Launcher: %v\n", v.LauncherVersion)
} else {
fmt.Fprintf(w, "%v\n", v.Version)
}
return nil
}

type versionInfo struct {
Version string
IsPrerelease bool
Platform string
Commit string
CommitDate string
GoVersion string
Version string
IsPrerelease bool
Platform string
Commit string
CommitDate string
GoVersion string
LauncherVersion string
}

func getVersionInfo() *versionInfo {
Expand All @@ -78,6 +81,8 @@ func getVersionInfo() *versionInfo {
Commit: build.Commit,
CommitDate: build.CommitDate,
GoVersion: runtime.Version(),
// Change to env.LauncherVersion. Not doing so to minimize merge conflicts.
LauncherVersion: os.Getenv("LAUNCHER_VERSION"),
}

return v
Expand Down
246 changes: 218 additions & 28 deletions internal/vercheck/vercheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,75 @@
package vercheck

import (
"bytes"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/fatih/color"
"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/build"
"golang.org/x/mod/semver"

"go.jetpack.io/devbox/internal/boxcli/usererr"
"go.jetpack.io/devbox/internal/envir"
"go.jetpack.io/devbox/internal/ux"
"go.jetpack.io/devbox/internal/xdg"
)

// Keep this in-sync with latest version in launch.sh. If this version is newer
// Than the version in launch.sh, we'll print a warning.
const expectedLauncherVersion = "v0.1.0"
// than the version in launch.sh, we'll print a notice.
const expectedLauncherVersion = "v0.2.0"

func CheckLauncherVersion(w io.Writer) {
launcherVersion := os.Getenv(envir.LauncherVersion)
if launcherVersion == "" || envir.IsDevboxCloud() {
// currentDevboxVersion is the version of the devbox CLI binary that is currently running.
// We use this variable so we can mock it in tests.
var currentDevboxVersion = build.Version

// envDevboxLatestVersion is the latest version available of the devbox CLI binary.
// Change to env.DevboxLatestVersion. Not doing so to minimize merge conflicts.
var envDevboxLatestVersion = "DEVBOX_LATEST_VERSION"

// CheckVersion checks the launcher and binary versions and prints a notice if
// they are out of date.
func CheckVersion(w io.Writer) {

// Replace with envir.IsDevboxCloud(). Not doing so to minimize merge conflicts.
if os.Getenv("DEVBOX_REGION") != "" {
return
}

// If launcherVersion is invalid, this will return 0 and we won't print a warning
if semver.Compare("v"+launcherVersion, expectedLauncherVersion) < 0 {
ux.Fwarning(
w,
"newer launcher version %s is available (current = v%s), please update "+
"using `devbox version update`\n",
expectedLauncherVersion,
launcherVersion,
)
launcherNotice := launcherVersionNotice()
if launcherNotice != "" {
// TODO: use ux.FNotice
color.New(color.FgYellow).Fprintf(w, launcherNotice)

// fallthrough to alert the user about a new Devbox CLI binary being possibly available
}

devboxNotice := devboxVersionNotice()
if devboxNotice != "" {
// TODO: use ux.FNotice
color.New(color.FgYellow).Fprintf(w, devboxNotice)
}
}

// SelfUpdate updates the devbox launcher and binary. It ignores and deletes the
// version cache
// SelfUpdate updates the devbox launcher and devbox CLI binary.
// It ignores and deletes the version cache.
//
// The launcher is a wrapper bash script introduced to manage the auto-update process
// for devbox. The production devbox application is actually this launcher script
// that acts as "devbox" and delegates commands to the devbox CLI binary.
func SelfUpdate(stdOut, stdErr io.Writer) error {
if isNewLauncherAvailable() {
return selfUpdateLauncher(stdOut, stdErr)
}

return selfUpdateDevbox(stdErr)
}

func selfUpdateLauncher(stdOut, stdErr io.Writer) error {
installScript := ""
if _, err := exec.LookPath("curl"); err == nil {
installScript = "curl -fsSL https://get.jetpack.io/devbox | bash"
Expand All @@ -53,26 +82,187 @@ func SelfUpdate(stdOut, stdErr io.Writer) error {
return usererr.New("curl or wget is required to update devbox. Please install either and try again.")
}

// Delete version cache. Keep this in-sync with whatever logic is in launch.sh
cacheDir := xdg.CacheSubpath("devbox")
versionCacheFile := filepath.Join(cacheDir, "latest-version")
_ = os.Remove(versionCacheFile)
// Delete current version file. This will trigger an update when invoking any devbox command;
// in this case, inside triggerUpdate function.
if err := removeCurrentVersionFile(); err != nil {
return err
}

// Fetch the new launcher.
cmd := exec.Command("sh", "-c", installScript)
cmd.Stdout = stdOut
cmd.Stderr = stdErr
if err := cmd.Run(); err != nil {
return errors.WithStack(err)
}

fmt.Fprint(stdErr, "Latest version: ")
exe, err := os.Executable()
// Invoke a devbox command to trigger an update of the devbox CLI binary.
updated, err := triggerUpdate(stdErr)
if err != nil {
return errors.WithStack(err)
}

printSuccessMessage(stdErr, "Launcher", currentLauncherVersion(), updated.launcherVersion)
printSuccessMessage(stdErr, "Devbox", currentDevboxVersion, updated.devboxVersion)

return nil
}

// selfUpdateDevbox will update the devbox CLI binary to the latest version.
func selfUpdateDevbox(stdErr io.Writer) error {
// Delete current version file. This will trigger an update when the next devbox command is run;
// in this case, inside triggerUpdate function.
if err := removeCurrentVersionFile(); err != nil {
return err
}

updated, err := triggerUpdate(stdErr)
if err != nil {
return errors.WithStack(err)
}
cmd = exec.Command(exe, "version")
// The output of version is incidental, so just send it all to stdErr
cmd.Stdout = stdErr

printSuccessMessage(stdErr, "Devbox", currentDevboxVersion, updated.devboxVersion)

return nil
}

type updatedVersions struct {
devboxVersion string
launcherVersion string
}

// triggerUpdate runs `devbox version -v` and triggers an update since a new
// version is available. It parses the output to get the new launcher and
// devbox versions.
func triggerUpdate(stdErr io.Writer) (*updatedVersions, error) {

exe, err := os.Executable()
if err != nil {
return nil, errors.WithStack(err)
}
// TODO savil. Add a --json flag to devbox version and parse the output as JSON
cmd := exec.Command(exe, "version", "-v")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly future hardening is adding --json

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would be a good idea


buf := new(bytes.Buffer)
cmd.Stdout = io.MultiWriter(stdErr, buf)
cmd.Stderr = stdErr
return errors.WithStack(cmd.Run())
if err := cmd.Run(); err != nil {
return nil, errors.WithStack(err)
}

// Parse the output to ascertain the new devbox and launcher versions
updated := &updatedVersions{}
for _, line := range strings.Split(buf.String(), "\n") {
if strings.HasPrefix(line, "Version:") {
updated.devboxVersion = strings.TrimSpace(strings.TrimPrefix(line, "Version:"))
}

if strings.HasPrefix(line, "Launcher:") {
updated.launcherVersion = strings.TrimSpace(strings.TrimPrefix(line, "Launcher:"))
}
}
return updated, nil
}

func printSuccessMessage(w io.Writer, toolName, oldVersion, newVersion string) {
var msg string
if semverCompare(oldVersion, newVersion) == 0 {
msg = fmt.Sprintf("already at %s version %s", toolName, newVersion)
} else {
msg = fmt.Sprintf("updated to %s version %s", toolName, newVersion)
}

// Prints a <green>Success:</green> message to the writer.
// Move to ux.Success. Not doing so to minimize merge-conflicts.
fmt.Fprintf(w, "%s%s\n", color.New(color.FgGreen).Sprint("Success: "), msg)
}

func launcherVersionNotice() string {
if !isNewLauncherAvailable() {
return ""
}

return fmt.Sprintf(
"New launcher available: %s -> %s. Please run `devbox version update`.\n",
currentLauncherVersion(),
expectedLauncherVersion,
)
}

func devboxVersionNotice() string {
if !isNewDevboxAvailable() {
return ""
}

return fmt.Sprintf(
"New devbox available: %s -> %s. Please run `devbox version update`.\n",
currentDevboxVersion,
latestVersion(),
)
}

// isNewLauncherAvailable returns true if a new launcher version is available.
func isNewLauncherAvailable() bool {
launcherVersion := currentLauncherVersion()
if launcherVersion == "" {
return false
}
return semverCompare(launcherVersion, expectedLauncherVersion) < 0
}

// isNewDevboxAvailable returns true if a new devbox CLI binary version is available.
func isNewDevboxAvailable() bool {
latest := latestVersion()
if latest == "" {
return false
}
return semverCompare(currentDevboxVersion, latest) < 0
}

// currentLauncherAvailable returns launcher's version if it is
// available, or empty string if it is not.
func currentLauncherVersion() string {
// Change to envir.LauncherVersion. Not doing so to minimize merge-conflicts.
launcherVersion := os.Getenv("LAUNCHER_VERSION")
if launcherVersion == "" {
return ""
}
return "v" + launcherVersion
}

func removeCurrentVersionFile() error {
// currentVersionFilePath is the path to the file that contains the cached
// version. The launcher checks this file to see if a new version is available.
// If the version is newer, then the launcher updates.
//
// Note: keep this in sync with launch.sh code
currentVersionFilePath := filepath.Join(xdg.CacheSubpath("devbox"), "current-version")

if err := os.Remove(currentVersionFilePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
return usererr.WithLoggedUserMessage(
err,
"Failed to delete version-cache at %s. Please manually delete it and try again.",
currentVersionFilePath,
)
}
return nil
}

func semverCompare(ver1, ver2 string) int {
if !strings.HasPrefix(ver1, "v") {
ver1 = "v" + ver1
}
if !strings.HasPrefix(ver2, "v") {
ver2 = "v" + ver2
}
return semver.Compare(ver1, ver2)
}

// latestVersion returns the latest version available for the binary.
func latestVersion() string {
version := os.Getenv(envDevboxLatestVersion)
if version == "" {
return ""
}
return "v" + version
}
Loading