Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #972 from hashicorp/f-uninstall
Browse files Browse the repository at this point in the history
feature: Uninstall server for all platforms
  • Loading branch information
krantzinator authored Jan 15, 2021
2 parents a88ba6b + a986825 commit 3a9d48b
Show file tree
Hide file tree
Showing 17 changed files with 752 additions and 62 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# A lot of this Makefile right now is temporary since we have a private
# repo so that we can more sanely create
ASSETFS_PATH?=internal/server/gen/bindata_ui.go

GIT_COMMIT=$$(git rev-parse --short HEAD)
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/deployment_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ Usage: waypoint deployment destroy [options] [id...]
instance of an application.
When no arguments are given, this will default to destroying ALL
deployments. This will require interactive confirmation by the user
unless the force flag (-force) is specified.
unreleased deployments. This will require interactive confirmation
by the user unless the force flag (-force) is specified.
` + c.Flags().Help())
}
5 changes: 5 additions & 0 deletions internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ func Commands(
baseCommand: baseCommand,
}, nil
},
"server uninstall": func() (cli.Command, error) {
return &UninstallCommand{
baseCommand: baseCommand,
}, nil
},
"server run": func() (cli.Command, error) {
return &ServerRunCommand{
baseCommand: baseCommand,
Expand Down
213 changes: 213 additions & 0 deletions internal/cli/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
package cli

import (
"fmt"
"os"
"strings"
"time"

"github.com/posener/complete"

"github.com/hashicorp/waypoint-plugin-sdk/terminal"
"github.com/hashicorp/waypoint/internal/clierrors"
"github.com/hashicorp/waypoint/internal/clisnapshot"
"github.com/hashicorp/waypoint/internal/pkg/flag"
"github.com/hashicorp/waypoint/internal/serverinstall"
)

type UninstallCommand struct {
*baseCommand

platform string
snapshotName string
skipSnapshot bool
autoApprove bool
deleteContext bool
}

func (c *UninstallCommand) Run(args []string) int {
ctx := c.Ctx
log := c.Log.Named("uninstall")
defer c.Close()

// Initialize. If we fail, we just exit since Init handles the UI.
if err := c.Init(
WithArgs(args),
WithFlags(c.Flags()),
WithNoConfig(),
); err != nil {
return 1
}

if !c.autoApprove {
c.ui.Output(strings.TrimSpace(autoApproveMsg), terminal.WithErrorStyle())
return 1
}

// output the context we'll be uninstalling
contextDefault, err := c.contextStorage.Default()
if err != nil {
c.ui.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return 1
}
c.ui.Output(
"Uninstalling Waypoint server with context %q",
contextDefault,
terminal.WithSuccessStyle(),
)

sg := c.ui.StepGroup()
defer sg.Wait()

// Pre-uninstall work
// - generate a snapshot of the current install
s := sg.Add("")
defer func() { s.Abort() }()

// Generate a snapshot
if !c.skipSnapshot {
s.Update("Generating server snapshot...")

// set config snapshot name with default or flag value + timestamp
if c.snapshotName == "" {
c.snapshotName = uninstallSnapshotName
}
c.snapshotName = fmt.Sprintf("%s-%d", c.snapshotName, time.Now().Unix())

// take the snapshot
w, err := os.Create(c.snapshotName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating snapshot file: %s", err)
return 1
}
if err = clisnapshot.WriteSnapshot(ctx, c.project.Client(), w); err != nil {
fmt.Fprintf(os.Stderr, "Error generating snapshot: %s", err)
return 1
}
s.Update("Snapshot %q generated", c.snapshotName)
} else {
s.Update("skip-snapshot set; not generating server snapshot")
s.Status(terminal.StatusWarn)
}
s.Done()

// Uninstall
p, ok := serverinstall.Platforms[strings.ToLower(c.platform)]
if !ok {
c.ui.Output(
"Error uninstalling server from %s: invalid platform",
c.platform,
terminal.WithErrorStyle(),
)

return 1
}

err = p.Uninstall(ctx, &serverinstall.InstallOpts{
Log: log,
UI: c.ui,
})
if err != nil {
c.ui.Output(
"Error uninstalling server from %s: %s\nSee Troubleshooting docs "+
"for guidance on manual uninstall: https://www.waypointproject.io/docs/troubleshooting",
c.platform,
clierrors.Humanize(err),
terminal.WithErrorStyle(),
)

return 1
}

// Post-uninstall cleanup of context
if c.deleteContext {
if err := c.contextStorage.Delete(contextDefault); err != nil {
c.ui.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return 1
}
}

c.ui.Output("Waypoint server successfully uninstalled for %s platform", c.platform, terminal.WithSuccessStyle())

return 0
}

func (c *UninstallCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}

func (c *UninstallCommand) AutocompleteFlags() complete.Flags {
return c.Flags().Completions()
}

func (c *UninstallCommand) Synopsis() string {
return "Uninstall the Waypoint server"
}

func (c *UninstallCommand) Help() string {
return formatHelp(`
Usage: waypoint server uninstall [options]
Uninstall the Waypoint server. The platform should be specified as kubernetes,
nomad, or docker. '-auto-approve' is required.
By default, this command deletes the default server's context.
This command does not destroy Waypoint resources, such as deployments and
releases. Clear all workspaces prior to uninstall to prevent hanging resources.
` + c.Flags().Help())
}

func (c *UninstallCommand) Flags() *flag.Sets {
return c.flagSet(0, func(set *flag.Sets) {
f := set.NewSet("Command Options")
f.BoolVar(&flag.BoolVar{
Name: "auto-approve",
Target: &c.autoApprove,
Default: false,
Usage: "Auto-approve server uninstallation.",
})

f.BoolVar(&flag.BoolVar{
Name: "delete-context",
Target: &c.deleteContext,
Default: true,
Usage: "Delete the context for the server once it's uninstalled.",
})

f.StringVar(&flag.StringVar{
Name: "platform",
Target: &c.platform,
Default: "",
Usage: "Platform to uninstall the Waypoint server from.",
})

f.StringVar(&flag.StringVar{
Name: "snapshot-name",
Target: &c.snapshotName,
Default: "",
Usage: "Filename to write the snapshot to.",
})

f.BoolVar(&flag.BoolVar{
Name: "skip-snapshot",
Target: &c.skipSnapshot,
Default: false,
Usage: "Skip creating a snapshot of the Waypoint server.",
})

for name, platform := range serverinstall.Platforms {
platformSet := set.NewSet(name + " Options")
platform.UninstallFlags(platformSet)
}
})
}

var (
uninstallSnapshotName = "waypoint-server-snapshot"
autoApproveMsg = strings.TrimSpace(`
Uninstalling Waypoint server requires approval.
Rerun the command with -auto-approve to continue with the uninstall.
`)
)
Loading

0 comments on commit 3a9d48b

Please sign in to comment.