Skip to content

Commit

Permalink
CLI: Uninstall command (#4832)
Browse files Browse the repository at this point in the history
* Add uninstall command + embed install script

* Parse version into canonical form

* Fix embed directive

* Self review

* Change bash to sh
  • Loading branch information
begelundmuller authored May 7, 2024
1 parent 2ec9c2d commit adc205a
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ tsc-tmp/
/runtime/sql/deps
/runtime/release
/runtime/pkg/examples/embed/dist
/cli/pkg/web/embed/dist
/rill
/cli/pkg/examples/embed/dist
/cli/pkg/installscript/embed/install.sh
/cli/pkg/web/embed/dist
/dev-project*
/dev-cloud-state*

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cli.prepare:
mkdir -p runtime/pkg/examples/embed/dist
git clone --quiet https://github.com/rilldata/rill-examples.git runtime/pkg/examples/embed/dist
rm -rf runtime/pkg/examples/embed/dist/.git
cp scripts/install.sh cli/pkg/installscript/embed/install.sh

.PHONY: coverage.go
coverage.go:
Expand Down
2 changes: 2 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/rilldata/rill/cli/cmd/service"
"github.com/rilldata/rill/cli/cmd/start"
"github.com/rilldata/rill/cli/cmd/sudo"
"github.com/rilldata/rill/cli/cmd/uninstall"
"github.com/rilldata/rill/cli/cmd/upgrade"
"github.com/rilldata/rill/cli/cmd/user"
versioncmd "github.com/rilldata/rill/cli/cmd/version"
Expand Down Expand Up @@ -153,6 +154,7 @@ func runCmd(ctx context.Context, ver cmdutil.Version) error {
completionCmd,
versioncmd.VersionCmd(),
upgrade.UpgradeCmd(ch),
uninstall.UninstallCmd(ch),
sudo.SudoCmd(ch),
devtool.DevtoolCmd(ch),
admin.AdminCmd(ch),
Expand Down
18 changes: 18 additions & 0 deletions cli/cmd/uninstall/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package uninstall

import (
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/installscript"
"github.com/spf13/cobra"
)

func UninstallCmd(ch *cmdutil.Helper) *cobra.Command {
return &cobra.Command{
Use: "uninstall",
Short: "Uninstall the Rill binary",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return installscript.Uninstall(cmd.Context())
},
}
}
49 changes: 17 additions & 32 deletions cli/cmd/upgrade/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,41 @@
package upgrade

import (
"io"
"net/http"
"os"
"os/exec"

goversion "github.com/hashicorp/go-version"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/installscript"
"github.com/spf13/cobra"
)

const installScriptURL = "https://cdn.rilldata.com/install.sh"

func UpgradeCmd(ch *cmdutil.Helper) *cobra.Command {
var version string
var nightly bool

upgradeCmd := &cobra.Command{
Use: "upgrade",
Short: "Upgrade Rill to the latest version",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
// Download the install script to a temporary file
f, err := os.CreateTemp("", "install*.sh")
if err != nil {
return err
}
defer os.Remove(f.Name())

resp, err := http.Get(installScriptURL)
if err != nil {
return err
RunE: func(cmd *cobra.Command, _ []string) error {
if version != "" {
// Parse the version into the canonical form
v, err := goversion.NewVersion(version)
if err != nil {
return err
}
version = "v" + v.String()

return installscript.Install(cmd.Context(), version)
}
defer resp.Body.Close()

_, err = io.Copy(f, resp.Body)
if err != nil {
return err
}
f.Close()

// Run the install script with bash
args := []string{f.Name()}
if nightly {
args = append(args, "--nightly")
return installscript.Install(cmd.Context(), "nightly")
}
cmd := exec.Command("/bin/bash", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()

return installscript.Install(cmd.Context(), "")
},
}

upgradeCmd.Flags().StringVar(&version, "version", "", "Install a specific version of Rill")
upgradeCmd.Flags().BoolVar(&nightly, "nightly", false, "Install the latest nightly build")

return upgradeCmd
Expand Down
Empty file.
63 changes: 63 additions & 0 deletions cli/pkg/installscript/installscript.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package installscript

import (
"context"
"embed"
"fmt"
"io"
"os"
"os/exec"
)

//go:embed embed/*
var embedFS embed.FS

func Install(ctx context.Context, version string) error {
if version != "" {
return execScript(ctx, "--version", version)
}
return execScript(ctx)
}

func Uninstall(ctx context.Context) error {
return execScript(ctx, "--uninstall")
}

func execScript(ctx context.Context, args ...string) error {
script, err := createScriptFile()
if err != nil {
return err
}
defer os.Remove(script)

// Execute the script with bash
args = append([]string{script}, args...)
cmd := exec.CommandContext(ctx, "/bin/sh", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func createScriptFile() (string, error) {
// Open the embedded install script file
in, err := embedFS.Open("embed/install.sh")
if err != nil {
return "", fmt.Errorf("install script not embedded (is this a dev build?): %w", err)
}
defer in.Close()

// Write the install script to a temporary file
out, err := os.CreateTemp("", "install*.sh")
if err != nil {
return "", err
}
defer out.Close()

_, err = io.Copy(out, in)
if err != nil {
return "", err
}

// Return the temp script path
return out.Name(), nil
}

0 comments on commit adc205a

Please sign in to comment.