-
Notifications
You must be signed in to change notification settings - Fork 264
[version] part 1: update command for devbox and launcher updates #965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly future hardening is adding There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.