exec

package
v0.0.0-...-6819fcb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2024 License: AGPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultOutputBufferSize = 1000000
	DefaultStreamBufferSize = 4096
	InitialRows             = 27
	InitialCols             = 72
	ResizeTimeout           = 500 * time.Millisecond
)

Variables

This section is empty.

Functions

func RegisterPlugin

func RegisterPlugin(
	p *sdk.Plugin,
	opts PluginOpts,
) error

Types

type ClientFactory

type ClientFactory[ClientT any] interface {
	CreateClient(ctx *types.PluginContext) (*ClientT, error)
	RefreshClient(ctx *types.PluginContext, client *ClientT) error
	StartClient(ctx *types.PluginContext, client *ClientT) error
	StopClient(ctx *types.PluginContext, client *ClientT) error
}

type CommandHandler

type CommandHandler func(ctx *types.PluginContext, opts SessionOptions) (
	stdout io.Reader,
	stderr io.Reader,
	err error,
)

CommandHandler is the expected signature for the non-tty handler. It should immediately return it's standard output and error.

type Handler

type Handler struct {
	Plugin         string                          `json:"plugin"`
	Resource       string                          `json:"resource"`
	TargetBuilder  sdkresource.ActionTargetBuilder `json:"target_builder"`
	TTYHandler     TTYHandler                      `json:"-"`
	CommandHandler CommandHandler                  `json:"-"`
	// if the handler supports resizing, it will be sent through the channel instead of the pty file
	HandlesResize bool `json:"-"`
}

Handler handles performs running commands and creating sessions for a resource.

func HandlerFromProto

func HandlerFromProto(p *proto.ExecHandler) Handler

func (Handler) ID

func (h Handler) ID() string

func (Handler) String

func (h Handler) String() string

func (Handler) ToProto

func (h Handler) ToProto() *proto.ExecHandler

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages the lifecycle of the terminal sessions.

func NewManager

func NewManager(
	log hclog.Logger,
	sp settings.Provider,
	handlers map[string]Handler,
) *Manager

NewManager initializes a new Manager instance.

func (*Manager) AttachSession

func (m *Manager) AttachSession(
	_ *types.PluginContext,
	sessionID string,
) (*Session, []byte, error)

AttachToSession marks a session as attached and returns its current output buffer.

func (*Manager) CloseSession

func (m *Manager) CloseSession(_ *types.PluginContext, sessionID string) error

CloseSession cancels the session's context, effectively terminating its command, and removes it from the manager.

func (*Manager) CreateSession

func (m *Manager) CreateSession(
	pluginctx *types.PluginContext,
	opts SessionOptions,
) (*Session, error)

CreateSession creates a new terminal session with a given command.

func (*Manager) DetachSession

func (m *Manager) DetachSession(_ *types.PluginContext, sessionID string) (*Session, error)

DetachFromSession marks a session as not attached, stopping output broadcast.

func (*Manager) GetSession

func (m *Manager) GetSession(
	_ *types.PluginContext,
	sessionID string,
) (*Session, error)

GetSession returns a session by ID.

func (*Manager) GetSupportedResources

func (m *Manager) GetSupportedResources(_ *types.PluginContext) []Handler

func (*Manager) ListSessions

func (m *Manager) ListSessions(_ *types.PluginContext) ([]*Session, error)

ListSessions returns a list of details for all active sessions.

func (*Manager) ResizeSession

func (m *Manager) ResizeSession(
	_ *types.PluginContext,
	sessionID string,
	rows, cols int32,
) error

ResizeSession resizes a session.

func (*Manager) Stream

func (m *Manager) Stream(ctx context.Context, in chan StreamInput) (chan StreamOutput, error)

Stream creates a new stream to multiplex sessions.

type OutputBuffer

type OutputBuffer struct {
	// contains filtered or unexported fields
}

OutputBuffer stores terminal output lines, providing a fixed-size cyclic buffer.

func NewDefaultOutputBuffer

func NewDefaultOutputBuffer() *OutputBuffer

func NewOutputBuffer

func NewOutputBuffer(capacity int) *OutputBuffer

NewOutputBuffer initializes an OutputBuffer with a specified capacity.

func (*OutputBuffer) Append

func (b *OutputBuffer) Append(data []byte)

Append adds a line to the buffer, managing overflow by removing the oldest line.

func (*OutputBuffer) GetAll

func (b *OutputBuffer) GetAll() []byte

GetAll retrieves a copy of all stored lines in the buffer.

type Plugin

type Plugin struct {
	plugin.Plugin
	Impl Provider
}

func (*Plugin) GRPCClient

func (p *Plugin) GRPCClient(
	_ context.Context,
	_ *plugin.GRPCBroker,
	c *grpc.ClientConn,
) (interface{}, error)

func (*Plugin) GRPCServer

func (p *Plugin) GRPCServer(_ *plugin.GRPCBroker, s *grpc.Server) error

type PluginClient

type PluginClient struct {
	// contains filtered or unexported fields
}

func (*PluginClient) AttachSession

func (c *PluginClient) AttachSession(
	ctx *types.PluginContext,
	sessionID string,
) (*Session, []byte, error)

func (*PluginClient) CloseSession

func (c *PluginClient) CloseSession(ctx *types.PluginContext, sessionID string) error

func (*PluginClient) CreateSession

func (c *PluginClient) CreateSession(
	ctx *types.PluginContext,
	opts SessionOptions,
) (*Session, error)

func (*PluginClient) DetachSession

func (c *PluginClient) DetachSession(
	ctx *types.PluginContext,
	sessionID string,
) (*Session, error)

func (*PluginClient) GetSession

func (c *PluginClient) GetSession(
	ctx *types.PluginContext,
	sessionID string,
) (*Session, error)

func (*PluginClient) GetSupportedResources

func (c *PluginClient) GetSupportedResources(ctx *types.PluginContext) []Handler

func (*PluginClient) ListSessions

func (c *PluginClient) ListSessions(ctx *types.PluginContext) ([]*Session, error)

func (*PluginClient) ResizeSession

func (c *PluginClient) ResizeSession(
	ctx *types.PluginContext,
	sessionID string,
	cols, rows int32,
) error

func (*PluginClient) Stream

func (c *PluginClient) Stream(
	ctx context.Context,
	in chan StreamInput,
) (chan StreamOutput, error)

type PluginOpts

type PluginOpts struct {
	Handlers []Handler `json:"handlers"`
}

PluginOpts contains the options for the exec plugin.

type PluginServer

type PluginServer struct {
	Impl Provider
	// contains filtered or unexported fields
}

func (*PluginServer) AttachSession

func (*PluginServer) CloseSession

func (*PluginServer) CreateSession

func (*PluginServer) DetachSession

func (*PluginServer) GetSession

func (*PluginServer) GetSupportedResources

func (s *PluginServer) GetSupportedResources(
	ctx context.Context,
	_ *emptypb.Empty,
) (*proto.GetSupportedResourcesResponse, error)

func (*PluginServer) ListSessions

func (s *PluginServer) ListSessions(
	ctx context.Context,
	in *emptypb.Empty,
) (*proto.ListSessionsResponse, error)

func (*PluginServer) ResizeSession

func (*PluginServer) Stream

func (s *PluginServer) Stream(stream proto.ExecPlugin_StreamServer) error

type Provider

type Provider interface {
	// GetSupportedResources returns the supported resource types
	GetSupportedResources(ctx *types.PluginContext) []Handler
	// GetSession returns a session by ID
	GetSession(ctx *types.PluginContext, sessionID string) (*Session, error)
	// ListSessions returns all of the sessions
	ListSessions(ctx *types.PluginContext) ([]*Session, error)
	// CreateSession creates a new session
	CreateSession(ctx *types.PluginContext, opts SessionOptions) (*Session, error)
	// AttachSession attaches a session
	AttachSession(ctx *types.PluginContext, sessionID string) (*Session, []byte, error)
	// DetachSession detaches a session
	DetachSession(ctx *types.PluginContext, sessionID string) (*Session, error)
	// CloseSession closes a session
	CloseSession(ctx *types.PluginContext, sessionID string) error
	// ResizeSession resizes a session
	ResizeSession(ctx *types.PluginContext, sessionID string, cols, rows int32) error
	// Stream starts a new stream to multiplex sessions
	Stream(context.Context, chan StreamInput) (chan StreamOutput, error)
}

Provider is the interface satisfied by the plugin server and client to provide the exec functionality.

type Session

type Session struct {
	CreatedAt time.Time `json:"created_at"`

	Labels   map[string]string `json:"labels"`
	Params   map[string]string `json:"params"`
	ID       string            `json:"id"`
	Command  []string          `json:"command"`
	Attached bool              `json:"attached"`
	// contains filtered or unexported fields
}

Session represents an individual terminal session (shell session with a tty).

func NewSessionFromOpts

func NewSessionFromOpts(
	ctx context.Context,
	canceller context.CancelFunc,
	opts SessionOptions,
) *Session

func NewSessionFromProto

func NewSessionFromProto(s *proto.Session) *Session

func (*Session) Close

func (s *Session) Close()

func (*Session) GetBufferData

func (s *Session) GetBufferData() []byte

func (*Session) GetCanceller

func (s *Session) GetCanceller() context.CancelFunc

func (*Session) GetPty

func (s *Session) GetPty() *os.File

func (*Session) GetTty

func (s *Session) GetTty() *os.File

func (*Session) RecordToBuffer

func (s *Session) RecordToBuffer(data []byte)

func (*Session) SetPty

func (s *Session) SetPty(pty *os.File)

func (*Session) SetTty

func (s *Session) SetTty(tty *os.File)

func (*Session) ToProto

func (s *Session) ToProto() *proto.Session

func (*Session) Write

func (s *Session) Write(data []byte) (int, error)

type SessionHandler

type SessionHandler func(ctx *types.PluginContext, opts SessionOptions) (
	stdin io.Writer,
	stdout io.Reader,
	stderr io.Reader,
	err error,
)

SessionHandler is the expected signature for a function that creates a new session.

The session handler should start a new session against the given resource and return the standard input, output, and error streams which will be multiplexed to the client.

type SessionOptions

type SessionOptions struct {
	Params         map[string]string      `json:"params"`
	Labels         map[string]string      `json:"labels"`
	ID             string                 `json:"id"`
	ResourcePlugin string                 `json:"resource_plugin"`
	ResourceKey    string                 `json:"resource_key"`
	ResourceData   map[string]interface{} `json:"resource_data"`
	Command        []string               `json:"command"`
	TTY            bool                   `json:"tty"`
}

SessionOptions contains options for creating a new terminal session.

func NewSessionOptionsFromProto

func NewSessionOptionsFromProto(opts *proto.SessionOptions) *SessionOptions

func (*SessionOptions) ToProto

func (o *SessionOptions) ToProto() *proto.SessionOptions

type SessionResizeInput

type SessionResizeInput struct {
	Rows int32 `json:"rows"`
	Cols int32 `json:"cols"`
}

type StreamInput

type StreamInput struct {
	// SessionID
	SessionID string `json:"session_id"`
	// Data
	Data []byte `json:"data"`
}

func NewStreamInputFromProto

func NewStreamInputFromProto(p *proto.StreamInput) StreamInput

func (*StreamInput) ToProto

func (i *StreamInput) ToProto() *proto.StreamInput

type StreamOutput

type StreamOutput struct {
	SessionID string       `json:"session_id"`
	Data      []byte       `json:"data"`
	Target    StreamTarget `json:"target"`
	Signal    StreamSignal `json:"signal"`
}

func NewStreamOutputFromProto

func NewStreamOutputFromProto(p *proto.StreamOutput) StreamOutput

func (*StreamOutput) ToProto

func (o *StreamOutput) ToProto() *proto.StreamOutput

type StreamResize

type StreamResize struct {
	SessionID string `json:"session_id"`
	Cols      uint16 `json:"cols"`
	Rows      uint16 `json:"rows"`
}

func NewStreamResizeFromProto

func NewStreamResizeFromProto(p *proto.ResizeSessionRequest) StreamResize

func (*StreamResize) ToProto

func (r *StreamResize) ToProto() *proto.ResizeSessionRequest

type StreamSignal

type StreamSignal int
const (
	// while close isn't a signal, it's used to close the stream.
	StreamSignalNone StreamSignal = iota
	StreamSignalClose
	StreamSignalSigint
	StreamSignalSigquit
	StreamSignalSigterm
	StreamSignalSigkill
	StreamSignalSighup
	StreamSignalSigusr1
	StreamSignalSigusr2
	StreamSignalSigwinch
)

func NewStreamSignalFromProto

func NewStreamSignalFromProto(p proto.StreamSignal) StreamSignal

func (StreamSignal) String

func (s StreamSignal) String() string

func (StreamSignal) ToProto

func (s StreamSignal) ToProto() proto.StreamSignal

pointer receiver to allow nil signal (when none is actually sent).

type StreamTarget

type StreamTarget int
const (
	StreamTargetStdOut StreamTarget = iota
	StreamTargetStdErr
)

func (StreamTarget) String

func (t StreamTarget) String() string

type TTYHandler

type TTYHandler func(ctx *types.PluginContext, opts SessionOptions, tty *os.File, stopCh chan struct{}, resize <-chan SessionResizeInput) error

TTYHandler is the expected signature for a function that creates a new session with a TTY. It is passed the TTY file descriptor for the session, and a resize channel that will receive resize events for the TTY, of which.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL