diff --git a/local-interchain/interchain/handlers/container_log_stream.go b/local-interchain/interchain/handlers/container_log_stream.go index 1c67ef935..1204bbe98 100644 --- a/local-interchain/interchain/handlers/container_log_stream.go +++ b/local-interchain/interchain/handlers/container_log_stream.go @@ -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, } } @@ -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=") + 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 } @@ -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 { diff --git a/local-interchain/interchain/router/router.go b/local-interchain/interchain/router/router.go index 87ad8053a..7501a22f0 100644 --- a/local-interchain/interchain/router/router.go +++ b/local-interchain/interchain/router/router.go @@ -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= + containerStream := handlers.NewContainerSteam(ctx, rc.Logger, rc.DockerClient, rc.AuthKey, rc.TestName, rc.Vals) + r.HandleFunc("/container_logs", containerStream.StreamContainer).Methods(http.MethodGet) // ?container=&colored=true&lines=10000 wd, err := os.Getwd() if err != nil {