Skip to content
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

fix: daytona update command with elevated perms #1713

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions docs/daytona_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ daytona update [flags]
### Options

```
-c, --show-changelog Show changelog (default true)
-v, --version string Version to update to
```

Expand Down
4 changes: 4 additions & 0 deletions hack/docs/daytona_update.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ synopsis: Update Daytona CLI
description: Update Daytona CLI to the latest version
usage: daytona update [flags]
options:
- name: show-changelog
shorthand: c
default_value: "true"
usage: Show changelog
- name: version
shorthand: v
usage: Version to update to
Expand Down
56 changes: 44 additions & 12 deletions pkg/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
package cmd

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"github.com/daytonaio/daytona/internal"
"github.com/inconshreveable/go-update"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
"net/http"
"net/url"
"os"
"os/exec"
"regexp"
"runtime"
"strings"

"github.com/daytonaio/daytona/internal"
"github.com/inconshreveable/go-update"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
)

const (
Expand All @@ -29,6 +33,7 @@ type GitHubRelease struct {
}

var versionFlag string
var showChangelog bool

var updateCmd = &cobra.Command{
Use: "update",
Expand Down Expand Up @@ -87,16 +92,19 @@ var updateCmd = &cobra.Command{
changelog += "\n\nThere might be more important changes since you updated. Please visit https://github.com/daytonaio/daytona/releases for the complete changelog\n"
}

fmt.Println("Updating to version", version, "from", currentVersion)
fmt.Print("\nChangelog:\n\n")
fmt.Println(changelog)
if showChangelog {
fmt.Println("Updating to version", version, "from", currentVersion)
fmt.Print("\nChangelog:\n\n")
fmt.Println(changelog)
}

return updateToVersion(version)
},
}

func init() {
updateCmd.Flags().StringVarP(&versionFlag, "version", "v", "", "Version to update to")
updateCmd.Flags().BoolVarP(&showChangelog, "show-changelog", "c", true, "Show changelog")
}

func fetchLatestRelase() (*GitHubRelease, error) {
Expand Down Expand Up @@ -165,18 +173,42 @@ func updateToVersion(version string) error {
}

err = update.Apply(resp.Body, update.Options{})

if err != nil {
fmt.Println("Failed to update binary")
if rollbackErr := update.RollbackError(err); rollbackErr != nil {
return fmt.Errorf("failed to rollback: %w", rollbackErr)
}

return fmt.Errorf("failed to update binary: %w", err)
}
if strings.Contains(err.Error(), "permission") {
if runtime.GOOS == "windows" {
return fmt.Errorf("please run 'daytona update' as administrator")
}

fmt.Print("The update requires sudo privileges. Would you like to continue with sudo? [y/N]: ")
reader := bufio.NewReader(os.Stdin)
response, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("failed to read user input: %v", err)
}

fmt.Println("\nSuccessfully updated to version", version)
fmt.Println("If your Server is running, you need to restart it for the changes to take effect.")
response = strings.TrimSpace(strings.ToLower(response))
if response != "y" && response != "yes" {
return fmt.Errorf("update cancelled by user")
}

cmd := exec.Command("sudo", "daytona", "update", "--show-changelog=false")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin

if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to update binary with sudo: %w", err)
}
}
} else {
fmt.Println("\nSuccessfully updated to version", version)
fmt.Println("If your Server is running, you need to restart it for the changes to take effect.")
}
return nil
}

Expand Down
Loading