From 51530bb2de7c8f5f6bef1e3b8eab1b2f32025690 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Sat, 29 Jul 2023 14:53:55 -0500 Subject: [PATCH] Kill relayer & all containers on ctrl+c / kill endpoint --- .../interchain/handlers/actions.go | 29 +++++++++++-------- local-interchain/interchain/router/router.go | 2 +- local-interchain/interchain/start.go | 15 +++++----- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/local-interchain/interchain/handlers/actions.go b/local-interchain/interchain/handlers/actions.go index 43d2da7c9..8333a1cf7 100644 --- a/local-interchain/interchain/handlers/actions.go +++ b/local-interchain/interchain/handlers/actions.go @@ -19,7 +19,7 @@ type actions struct { ic *interchaintest.Interchain vals map[string]*cosmos.ChainNode - relayer *ibc.Relayer + relayer ibc.Relayer eRep ibc.RelayerExecReporter } @@ -29,7 +29,7 @@ type ActionHandler struct { Cmd string `json:"cmd"` } -func NewActions(ctx context.Context, ic *interchaintest.Interchain, vals map[string]*cosmos.ChainNode, relayer *ibc.Relayer, eRep ibc.RelayerExecReporter) *actions { +func NewActions(ctx context.Context, ic *interchaintest.Interchain, vals map[string]*cosmos.ChainNode, relayer ibc.Relayer, eRep ibc.RelayerExecReporter) *actions { return &actions{ ctx: ctx, ic: ic, @@ -49,7 +49,7 @@ func (a *actions) PostActions(w http.ResponseWriter, r *http.Request) { action := ah.Action if action == "kill-all" { - a.killAll() + KillAll(a.ctx, a.ic, a.vals, a.relayer, a.eRep) return } @@ -88,13 +88,13 @@ func (a *actions) PostActions(w http.ResponseWriter, r *http.Request) { switch action { case "stop-relayer", "stop_relayer", "stopRelayer": - err = (*a.relayer).StopRelayer(a.ctx, a.eRep) + err = a.relayer.StopRelayer(a.ctx, a.eRep) case "start-relayer", "start_relayer", "startRelayer": paths := strings.FieldsFunc(ah.Cmd, func(c rune) bool { return c == ',' || c == ' ' }) - err = (*a.relayer).StartRelayer(a.ctx, a.eRep, paths...) + err = a.relayer.StartRelayer(a.ctx, a.eRep, paths...) case "relayer", "relayer-exec", "relayer_exec", "relayerExec": if !strings.Contains(ah.Cmd, "--home") { @@ -102,13 +102,13 @@ func (a *actions) PostActions(w http.ResponseWriter, r *http.Request) { cmd = append(cmd, "--home", "/home/relayer") } - res := (*a.relayer).Exec(a.ctx, a.eRep, cmd, []string{}) + res := a.relayer.Exec(a.ctx, a.eRep, cmd, []string{}) stdout = []byte(res.Stdout) stderr = []byte(res.Stderr) err = res.Err case "get_channels", "get-channels", "getChannels": - res, err := (*a.relayer).GetChannels(a.ctx, a.eRep, chainId) + res, err := a.relayer.GetChannels(a.ctx, a.eRep, chainId) if err != nil { util.WriteError(w, err) return @@ -148,10 +148,15 @@ func (a *actions) relayerCheck(w http.ResponseWriter, r *http.Request) error { return err } -func (a *actions) killAll() { - for _, v := range a.vals { - v.StopContainer(a.ctx) +func KillAll(ctx context.Context, ic *interchaintest.Interchain, vals map[string]*cosmos.ChainNode, relayer ibc.Relayer, eRep ibc.RelayerExecReporter) { + if relayer != nil { + relayer.StopRelayer(ctx, eRep) } - a.ic.Close() - a.ctx.Done() + + for _, v := range vals { + go v.StopContainer(ctx) + } + + ic.Close() + <-ctx.Done() } diff --git a/local-interchain/interchain/router/router.go b/local-interchain/interchain/router/router.go index 08e1c570e..98a768564 100644 --- a/local-interchain/interchain/router/router.go +++ b/local-interchain/interchain/router/router.go @@ -20,7 +20,7 @@ type Route struct { Methods []string `json:"methods"` } -func NewRouter(ctx context.Context, ic *interchaintest.Interchain, config *ictypes.Config, vals map[string]*cosmos.ChainNode, relayer *ibc.Relayer, eRep ibc.RelayerExecReporter, installDir string) *mux.Router { +func NewRouter(ctx context.Context, ic *interchaintest.Interchain, config *ictypes.Config, vals map[string]*cosmos.ChainNode, relayer ibc.Relayer, eRep ibc.RelayerExecReporter, installDir string) *mux.Router { r := mux.NewRouter() infoH := handlers.NewInfo(config, installDir) diff --git a/local-interchain/interchain/start.go b/local-interchain/interchain/start.go index 0142f78db..017d9fc50 100644 --- a/local-interchain/interchain/start.go +++ b/local-interchain/interchain/start.go @@ -16,6 +16,7 @@ import ( interchaintestrelayer "github.com/strangelove-ventures/interchaintest/v7/relayer" "github.com/strangelove-ventures/interchaintest/v7/testreporter" "github.com/strangelove-ventures/interchaintest/v7/testutil" + "github.com/strangelove-ventures/localinterchain/interchain/handlers" "github.com/strangelove-ventures/localinterchain/interchain/router" "go.uber.org/zap" ) @@ -24,6 +25,9 @@ func StartChain(installDir, chainCfgFile string) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() + var relayer ibc.Relayer + var eRep *testreporter.RelayerExecReporter + vals := make(map[string]*cosmos.ChainNode) ic := interchaintest.NewInterchain() defer ic.Close() @@ -34,11 +38,7 @@ func StartChain(installDir, chainCfgFile string) { go func() { for sig := range c { log.Printf("Closing from signal: %s\n", sig) - for _, v := range vals { - v.StopContainer(ctx) - } - ic.Close() - cancel() + handlers.KillAll(ctx, ic, vals, relayer, eRep) } }() @@ -99,12 +99,11 @@ func StartChain(installDir, chainCfgFile string) { // Base setup rep := testreporter.NewNopReporter() - eRep := rep.RelayerExecReporter(&fakeT) + eRep = rep.RelayerExecReporter(&fakeT) client, network := interchaintest.DockerSetup(fakeT) // setup a relayer if we have IBC paths to use. - var relayer ibc.Relayer if len(ibcpaths) > 0 { rlyCfg := config.Relayer @@ -161,7 +160,7 @@ func StartChain(installDir, chainCfgFile string) { // Starts a non blocking REST server to take action on the chain. go func() { - r := router.NewRouter(ctx, ic, config, vals, &relayer, eRep, installDir) + r := router.NewRouter(ctx, ic, config, vals, relayer, eRep, installDir) server := fmt.Sprintf("%s:%s", config.Server.Host, config.Server.Port) if err := http.ListenAndServe(server, r); err != nil {