Skip to content

Commit

Permalink
node name to id mapping for ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Oct 12, 2024
1 parent 9613d4b commit 560ffe2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
63 changes: 39 additions & 24 deletions local-interchain/interchain/handlers/container_log_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,38 @@ import (
"unicode"

dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
dockerclient "github.com/docker/docker/client"
"github.com/strangelove-ventures/interchaintest/v8/dockerutil"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"go.uber.org/zap"
)

var removeColorRegex = regexp.MustCompile("\x1b\\[[0-9;]*m")

type ContainerStream struct {
ctx context.Context
logger *zap.Logger
cli *dockerclient.Client
authKey string
testName string

nameToID map[string]string
}

func NewContainerSteam(ctx context.Context, logger *zap.Logger, cli *dockerclient.Client, authKey, testName string) *ContainerStream {
func NewContainerSteam(ctx context.Context, logger *zap.Logger, cli *dockerclient.Client, authKey, testName string, vals map[string][]*cosmos.ChainNode) *ContainerStream {
nameToID := make(map[string]string)
for _, nodes := range vals {
for _, node := range nodes {
nameToID[node.Name()] = node.ContainerID()
}
}

return &ContainerStream{
ctx: ctx,
authKey: authKey,
cli: cli,
logger: logger,
testName: testName,
nameToID: nameToID,
}
}

Expand All @@ -41,28 +52,37 @@ func (cs *ContainerStream) StreamContainer(w http.ResponseWriter, r *http.Reques
return
}

containerID := r.URL.Query().Get("id") // TODO: get from chain ID as well? (map chain ID to container ID somehow)
containerID := r.URL.Query().Get("id")
if containerID == "" {
// TODO: use this for other sidecar containers that are made? (need to test)
// returns containers only for this testnet. other containers are not shown on this endpoint
c, err := cs.cli.ContainerList(cs.ctx, dockertypes.ContainerListOptions{
Filters: filters.NewArgs(filters.Arg("label", dockerutil.CleanupLabel+"="+cs.testName)),
})
if err != nil {
http.Error(w, "Unable to get container list", http.StatusInternalServerError)
return
}

availableContainers := []string{}
for _, container := range c {
availableContainers = append(availableContainers, container.ID)
}
// c, err := cs.cli.ContainerList(cs.ctx, dockertypes.ContainerListOptions{
// Filters: filters.NewArgs(filters.Arg("label", dockerutil.CleanupLabel+"="+cs.testName)),
// })
// if err != nil {
// http.Error(w, "Unable to get container list", http.StatusInternalServerError)
// return
// }
// availableContainers := []string{}
// for _, container := range c {
// availableContainers = append(availableContainers, container.ID)
// }

output := "No container ID provided. Available containers:\n"
for _, container := range availableContainers {
output += fmt.Sprintf("- %s\n", container)
for name, id := range cs.nameToID {
output += fmt.Sprintf("- %s: %s\n", name, id)
}

fmt.Fprint(w, output)
fmt.Fprint(w, "Provide a container ID with ?id=<containerID>")
return
}

// if container id is in the cs.nameToID map, use the mapped container ID
if id, ok := cs.nameToID[containerID]; ok {
containerID = id
} else {
fmt.Fprintf(w, "Container ID %s not found\n", containerID)
return
}

Expand Down Expand Up @@ -130,12 +150,7 @@ func tailLinesParam(tailInput string) uint64 {
}

func removeAnsiColorCodesFromText(text string) (string, error) {
r, err := regexp.Compile("\x1b\\[[0-9;]*m")
if err != nil {
return "", err
}

return r.ReplaceAllString(text, ""), nil
return removeColorRegex.ReplaceAllString(text, ""), nil
}

func cleanSpecialChars(text string) string {
Expand Down
4 changes: 2 additions & 2 deletions local-interchain/interchain/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func NewRouter(
r.HandleFunc("/logs", logStream.StreamLogs).Methods(http.MethodGet)
r.HandleFunc("/logs_tail", logStream.TailLogs).Methods(http.MethodGet) // ?lines=

containerStream := handlers.NewContainerSteam(ctx, rc.Logger, rc.DockerClient, rc.AuthKey, rc.TestName)
r.HandleFunc("/container_logs", containerStream.StreamContainer).Methods(http.MethodGet) // ?container=<id>
containerStream := handlers.NewContainerSteam(ctx, rc.Logger, rc.DockerClient, rc.AuthKey, rc.TestName, rc.Vals)
r.HandleFunc("/container_logs", containerStream.StreamContainer).Methods(http.MethodGet) // ?container=<id>&colored=true&lines=10000

wd, err := os.Getwd()
if err != nil {
Expand Down

0 comments on commit 560ffe2

Please sign in to comment.