Skip to content

Commit

Permalink
feat(block-explorer): add a command to tear down the block explorer s…
Browse files Browse the repository at this point in the history
…etup (#979)
  • Loading branch information
artemijspavlovs authored Sep 18, 2024
1 parent fb69d9d commit 4890bc7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/block-explorer/block-explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/block-explorer/run"
"github.com/dymensionxyz/roller/cmd/block-explorer/teardown"
)

func Cmd() *cobra.Command {
Expand All @@ -13,6 +14,7 @@ func Cmd() *cobra.Command {
}

cmd.AddCommand(run.Cmd())
cmd.AddCommand(teardown.Cmd())

return cmd
}
1 change: 0 additions & 1 deletion cmd/block-explorer/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func Cmd() *cobra.Command {
Use: "run",
Short: "Run a RollApp node.",
Long: ``,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
isFlagChanged := cmd.Flags().Changed("block-explorer-rpc-endpoint")
Expand Down
70 changes: 70 additions & 0 deletions cmd/block-explorer/teardown/teardown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package teardown

import (
"context"
"fmt"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "teardown",
Short: "Remove block explorer resources",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
err := removeBlockExplorerResources()
if err != nil {
pterm.Error.Println("failed to remove Block Explorer resources: ", err)
return
}
},
}

return cmd
}

func removeBlockExplorerResources() error {
spinner, _ := pterm.DefaultSpinner.Start("Removing Block Explorer resources")

ctx := context.Background()
cc, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return fmt.Errorf("failed to create Docker client: %v", err)
}
defer cc.Close()

Check failure on line 38 in cmd/block-explorer/teardown/teardown.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `cc.Close` is not checked (errcheck)

// Container names
containerNames := []string{"be-postgresql", "be-frontend", "be-indexer"}

// Remove containers
for _, name := range containerNames {
pterm.Info.Printf("Removing container: %s\n", name)
err := cc.ContainerRemove(ctx, name, container.RemoveOptions{Force: true})
if err != nil && !client.IsErrNotFound(err) {
pterm.Warning.Printf("Failed to remove container %s: %v\n", name, err)
}
}

// Remove network
networkName := "block_explorer_network"
pterm.Info.Printf("Removing network: %s\n", networkName)
err = cc.NetworkRemove(ctx, networkName)
if err != nil && !client.IsErrNotFound(err) {
pterm.Warning.Printf("Failed to remove network %s: %v\n", networkName, err)
}

// Remove volume
volumeName := "postgres_data"
pterm.Info.Printf("Removing volume: %s\n", volumeName)
err = cc.VolumeRemove(ctx, volumeName, true)
if err != nil && !client.IsErrNotFound(err) {
pterm.Warning.Printf("Failed to remove volume %s: %v\n", volumeName, err)
}

spinner.Success("Block Explorer resources removed successfully")
return nil
}

0 comments on commit 4890bc7

Please sign in to comment.