Skip to content

Commit

Permalink
Kill relayer & all containers on ctrl+c / kill endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Jul 29, 2023
1 parent 16f0696 commit 51530bb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
29 changes: 17 additions & 12 deletions local-interchain/interchain/handlers/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type actions struct {
ic *interchaintest.Interchain
vals map[string]*cosmos.ChainNode

relayer *ibc.Relayer
relayer ibc.Relayer
eRep ibc.RelayerExecReporter
}

Expand All @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -88,27 +88,27 @@ 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") {
// does this ever change for any other relayer?
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
Expand Down Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion local-interchain/interchain/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions local-interchain/interchain/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
Expand All @@ -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)
}
}()

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 51530bb

Please sign in to comment.