Skip to content

Commit

Permalink
Add func to write to container stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
kaelemc committed Dec 14, 2024
1 parent 8eff445 commit f1b6857
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
23 changes: 23 additions & 0 deletions runtime/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,3 +1044,26 @@ func (d *DockerRuntime) IsHealthy(ctx context.Context, cID string) (bool, error)
}
return inspect.State.Health.Status == "healthy", nil
}

func (d *DockerRuntime) WriteToStdinNoWait(ctx context.Context, cID string, data []byte) error {

stdin, err := d.Client.ContainerAttach(ctx, cID, container.AttachOptions{
Stdin: true,
Stream: true,
Stdout: true,
Stderr: true,
})

if err != nil {
return err
}

log.Debugf("Writing to %s: %v", cID, data)

_, err = stdin.Conn.Write(data)
if err != nil {
return err
}

return stdin.Conn.Close()
}
5 changes: 5 additions & 0 deletions runtime/ignite/ignite.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,8 @@ func (*IgniteRuntime) IsHealthy(_ context.Context, _ string) (bool, error) {
log.Errorf("function GetContainerHealth(...) not implemented in the Containerlab IgniteRuntime")
return true, nil
}

func (*IgniteRuntime) WriteToStdinNoWait(ctx context.Context, cID string, data []byte) error {
log.Infof("WriteToStdinNoWait is not yet implemented for Ignite runtime")
return nil
}
2 changes: 2 additions & 0 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type ContainerRuntime interface {
GetContainerStatus(ctx context.Context, cID string) ContainerStatus
// IsHealthy returns true is the container is reported as being healthy, false otherwise
IsHealthy(ctx context.Context, cID string) (bool, error)
// Immediately write to the stdin of a container, returns error
WriteToStdinNoWait(ctx context.Context, cID string, data []byte) error
}

type ContainerStatus string
Expand Down

0 comments on commit f1b6857

Please sign in to comment.