Listen for containers starting
This commit is contained in:
92
main.go
92
main.go
@@ -14,6 +14,9 @@ import (
|
|||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/docker/docker/pkg/stdcopy"
|
"github.com/docker/docker/pkg/stdcopy"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types/events"
|
||||||
|
"github.com/docker/docker/api/types/filters"
|
||||||
)
|
)
|
||||||
|
|
||||||
var containers = make(map[string]int)
|
var containers = make(map[string]int)
|
||||||
@@ -26,22 +29,54 @@ type LogType struct {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
pp.Println("Running")
|
pp.Println("Running1")
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
logschan := make(chan LogType)
|
logschan := make(chan LogType)
|
||||||
listContainersAndLogType(ctx, logschan)
|
go listContainersAndLogType(ctx, logschan)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
slog.Info("Slept")
|
slog.Info("Slept")
|
||||||
done := ctx.Done()
|
|
||||||
test := <- done
|
|
||||||
pp.Println(test)
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for i := range logschan {
|
for i := range logschan {
|
||||||
pp.Println(i.ContainerName + ": " + i.Log)
|
slog.Info(i.ContainerName + ": " + i.Log)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func eventListen(cli *client.Client, logschan chan<- LogType) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
options := events.ListOptions{
|
||||||
|
Filters: filters.NewArgs(
|
||||||
|
filters.Arg("type", "container"),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
eventChan, errChan := cli.Events(ctx, options)
|
||||||
|
|
||||||
|
slog.Info("Waiting for events")
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case event := <-eventChan:
|
||||||
|
switch event.Action {
|
||||||
|
case "create":
|
||||||
|
fmt.Println("Container created:", event.Actor.Attributes["image"])
|
||||||
|
case "start":
|
||||||
|
fmt.Println("Container started:", event.Actor.Attributes["image"])
|
||||||
|
streamContainerLogs(ctx, cli, event.Actor.ID, logschan)
|
||||||
|
case "die":
|
||||||
|
fmt.Println("Container stopped:", event.Actor.Attributes["image"])
|
||||||
|
case "destroy":
|
||||||
|
fmt.Println("Container destroyed:", event.Actor.Attributes["image"])
|
||||||
|
}
|
||||||
|
case err := <-errChan:
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error listening to Docker events:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,46 +91,59 @@ func listContainersAndLogType(ctx context.Context, logschan chan<- LogType) {
|
|||||||
log.Fatalf("Error listing containers: %v", err)
|
log.Fatalf("Error listing containers: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pp.Println(len(containers))
|
||||||
for _, curcont := range containers {
|
for _, curcont := range containers {
|
||||||
go func() {
|
go func() {
|
||||||
defer streamContainerLogs(ctx, cli, curcont, logschan)
|
defer streamContainerLogs(ctx, cli, curcont.ID, logschan)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
fmt.Println("Ending")
|
||||||
|
eventListen(cli, logschan)
|
||||||
}
|
}
|
||||||
|
|
||||||
func streamContainerLogs(ctx context.Context, cli *client.Client, curcont container.Summary, logschan chan<- LogType) {
|
func streamContainerLogs(ctx context.Context, cli *client.Client, containerID string, logschan chan<- LogType) {
|
||||||
inspect, err := cli.ContainerInspect(ctx, curcont.ID)
|
inspect, err := cli.ContainerInspect(ctx, containerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error inspecting container %s: %v", curcont.ID, err)
|
log.Printf("Error inspecting container %s: %v", containerID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Cmd: ", inspect.Config.Cmd)
|
|
||||||
fmt.Println("Entrypoint: ", inspect.Config.Entrypoint)
|
|
||||||
if len(inspect.Config.Cmd) == 0 {
|
if len(inspect.Config.Cmd) == 0 {
|
||||||
slog.Info("Skipping container as cmd empty", "container-id", curcont.ID, "container-image", curcont.Image)
|
slog.Debug("Skipping container as cmd empty", "container-id", containerID, "container-image", inspect.Image)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if inspect.Config.Cmd[0] == "/lilog" {
|
if inspect.Config.Cmd[0] == "/lilog" || inspect.Config.Cmd[0] == "sh" {
|
||||||
slog.Info("Skipping own container logs")
|
slog.Debug("Skipping own container logs")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logType := inspect.HostConfig.LogConfig.Type
|
logType := inspect.HostConfig.LogConfig.Type
|
||||||
fmt.Printf("Container: %-20s ID: %.12s LogDriver: %s\n",
|
|
||||||
curcont.Image, curcont.ID, logType)
|
|
||||||
|
|
||||||
reader, err := cli.ContainerLogs(ctx, curcont.ID, container.LogsOptions{
|
if inspect.State.Status != "running" {
|
||||||
|
slog.Debug("Skipping container which isn't running", "container-id", containerID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Container: %s (%-20s) ID: %.12s LogDriver: %s\n",
|
||||||
|
inspect.Config.Cmd[0], inspect.Image, inspect.ID, logType)
|
||||||
|
reader, err := cli.ContainerLogs(ctx, containerID, container.LogsOptions{
|
||||||
ShowStdout: true,
|
ShowStdout: true,
|
||||||
ShowStderr: true,
|
ShowStderr: true,
|
||||||
Follow: true,
|
Follow: true,
|
||||||
Tail: "3",
|
Tail: "0",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
|
|
||||||
|
if inspect.Config.Tty {
|
||||||
|
scanner := bufio.NewScanner(reader)
|
||||||
|
for scanner.Scan() {
|
||||||
|
logschan <- LogType{ContainerName: inspect.Name, Source: "tty", Log: scanner.Text()}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
stdoutPR, stdoutPW := io.Pipe()
|
stdoutPR, stdoutPW := io.Pipe()
|
||||||
stderrPR, stderrPW := io.Pipe()
|
stderrPR, stderrPW := io.Pipe()
|
||||||
|
|
||||||
@@ -124,5 +172,9 @@ func streamContainerLogs(ctx context.Context, cli *client.Client, curcont contai
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
stdcopy.StdCopy(stdoutPW, stderrPW, reader)
|
_, err := stdcopy.StdCopy(stdoutPW, stderrPW, reader)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user