helper

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: GPL-3.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArgOptionalPartition

func ArgOptionalPartition(args map[string]any) (*int32, error)

ArgOptionalPartition parses the optional kafka_message partition field.

func ClosersAsOne

func ClosersAsOne(closers []io.Closer) io.Closer

ClosersAsOne 将多个 closer 打包成单个 io.Closer(用于只接受单 closer 的 API,如 ConnCache)。

func CopyBetweenAssets

func CopyBetweenAssets(ctx context.Context, srcAssetID int64, srcPath string, dstAssetID int64, dstPath string) error

CopyBetweenAssets 在两个资产间直接传输文件(SFTP 流式,不经本地磁盘)

func DialAssetSSH

func DialAssetSSH(ctx context.Context, assetID int64) (*ssh.Client, func(), error)

DialAssetSSH 建立到资产的 SSH 连接,返回 client 与一次性清理函数。 底层委托给 credential_resolver.DialAssetSSH,统一支持 SOCKS5/HTTP 代理与跳板机链。 调用方必须在使用结束后调用返回的 cleanup,一并关闭 client 与跳板机链上的中间连接。

func DialSSHClient

func DialSSHClient(ctx context.Context, assetID int64) (*ssh.Client, func(), error)

DialSSHClient 创建 SSH 客户端连接,自动解析凭据、代理、跳板机链。 调用者必须调用返回的 cleanup 关闭 client 与链路资源。

func ExecWithStdio

func ExecWithStdio(ctx context.Context, assetID int64, command string, stdin io.Reader, stdout, stderr io.Writer) error

ExecWithStdio 在远程服务器执行命令,直接连接 stdio(支持管道)

func ExecuteMongoDB

func ExecuteMongoDB(ctx context.Context, client *mongo.Client, database, collection, operation, query string) (string, error)

ExecuteMongoDB 执行 MongoDB 操作并返回 JSON 结果

func ExecuteRedis

func ExecuteRedis(ctx context.Context, client *redis.Client, command string) (string, error)

ExecuteRedis 执行 Redis 命令并返回 JSON 结果

func ExecuteRedisRaw

func ExecuteRedisRaw(ctx context.Context, client *redis.Client, args []string) (string, error)

ExecuteRedisRaw 使用预拆分的参数执行 Redis 命令(支持含空格的值)

func ExecuteSQL

func ExecuteSQL(ctx context.Context, db *sql.DB, sqlText string) (string, error)

ExecuteSQL 执行 SQL 并返回 JSON 结果

func ExecuteSQLPaged

func ExecuteSQLPaged(ctx context.Context, db *sql.DB, sqlText string, page, pageSize int, driver asset_entity.DatabaseDriver) (string, error)

ExecuteSQLPaged 对 SELECT/WITH 语句进行子查询包装分页,其他语句走 ExecuteSQL

func ExecuteSSHCommand

func ExecuteSSHCommand(ctx context.Context, assetID int64, command string) (string, error)

ExecuteSSHCommand 执行一次性 SSH 命令并返回输出(每次新建连接)

func ExecuteWithSFTP

func ExecuteWithSFTP(ctx context.Context, assetID int64, fn func(*sftp.Client) error) error

ExecuteWithSFTP 创建临时 SSH+SFTP 连接并执行操作。 ctx 取消时主动关闭底层连接以打断 fn 内部可能的 io.Copy 阻塞, 从而让 AI 停止会话能立即生效(否则大文件传输会挂住 runner.Stop)。

func FormatEtcdCommand

func FormatEtcdCommand(req *etcd_svc.ExecRequest) string

FormatEtcdCommand 把结构化 ExecRequest 还原为策略匹配 / 审计可读的命令字符串。

规则:

  • 复合 op("member_list" / "endpoint_status" 等)还原为 "member list" / "endpoint status";
  • 依次追加 key、value 与 --prefix 标志;
  • 与 audit 提取器、SaveGrantPattern 共用同一份格式,避免策略匹配与审计文本漂移。

func HandleExecEtcd

func HandleExecEtcd(ctx context.Context, args map[string]any) (string, error)

HandleExecEtcd 是 exec_etcd AI 工具的入口。

策略 / grant / 审批由 permission.CheckForAsset 在 svc 调用前完成; svc.Exec 内部已有三态日志与 dispatch,这里不重复记录。

func HandleExecMongo

func HandleExecMongo(ctx context.Context, args map[string]any) (string, error)

func HandleExecRedis

func HandleExecRedis(ctx context.Context, args map[string]any) (string, error)

func HandleExecSQL

func HandleExecSQL(ctx context.Context, args map[string]any) (string, error)

func HandleKafkaACL

func HandleKafkaACL(ctx context.Context, args map[string]any) (string, error)

func HandleKafkaCluster

func HandleKafkaCluster(ctx context.Context, args map[string]any) (string, error)

func HandleKafkaConnect

func HandleKafkaConnect(ctx context.Context, args map[string]any) (string, error)

func HandleKafkaConsumerGroup

func HandleKafkaConsumerGroup(ctx context.Context, args map[string]any) (string, error)

func HandleKafkaMessage

func HandleKafkaMessage(ctx context.Context, args map[string]any) (string, error)

func HandleKafkaSchema

func HandleKafkaSchema(ctx context.Context, args map[string]any) (string, error)

func HandleKafkaTopic

func HandleKafkaTopic(ctx context.Context, args map[string]any) (string, error)

func HandleRunSerialCommand

func HandleRunSerialCommand(ctx context.Context, args map[string]any) (string, error)

func IsExpectedCloseErr

func IsExpectedCloseErr(err error) bool

IsExpectedCloseErr 判断 SSH/网络连接关闭时的预期错误。 取消路径会主动 Close session/client 打断阻塞,随后的 defer 关闭就会返回这些错误; 归类为预期错误后,上层可以跳过 warn 日志,避免噪音。

func KafkaACLCommand

func KafkaACLCommand(operation string) (string, error)

func KafkaAlterTopicConfigRequestFromArgs

func KafkaAlterTopicConfigRequestFromArgs(assetID int64, args map[string]any) (kafka_svc.AlterTopicConfigRequest, error)

func KafkaCheckSchemaCompatibilityRequestFromArgs

func KafkaCheckSchemaCompatibilityRequestFromArgs(assetID int64, args map[string]any) (kafka_svc.CheckSchemaCompatibilityRequest, error)

func KafkaClusterCommand

func KafkaClusterCommand(operation string) (string, error)

KafkaClusterCommand 将 kafka_cluster 工具的 operation 映射为策略命令字符串。

func KafkaConnectCommand

func KafkaConnectCommand(operation, connector string) (string, error)

func KafkaConnectorConfigRequestFromArgs

func KafkaConnectorConfigRequestFromArgs(assetID int64, args map[string]any) (kafka_svc.ConnectorConfigRequest, error)

func KafkaConsumerGroupCommand

func KafkaConsumerGroupCommand(operation, group string) (string, error)

func KafkaCreateACLRequestFromArgs

func KafkaCreateACLRequestFromArgs(assetID int64, args map[string]any) kafka_svc.CreateACLRequest

func KafkaCreateTopicRequestFromArgs

func KafkaCreateTopicRequestFromArgs(assetID int64, args map[string]any) (kafka_svc.CreateTopicRequest, error)

func KafkaDeleteACLRequestFromArgs

func KafkaDeleteACLRequestFromArgs(assetID int64, args map[string]any) kafka_svc.DeleteACLRequest

func KafkaDeleteRecordsRequestFromArgs

func KafkaDeleteRecordsRequestFromArgs(assetID int64, args map[string]any) (kafka_svc.DeleteRecordsRequest, error)

func KafkaDeleteSchemaRequestFromArgs

func KafkaDeleteSchemaRequestFromArgs(assetID int64, args map[string]any) kafka_svc.DeleteSchemaRequest

func KafkaInt32SliceFromJSON

func KafkaInt32SliceFromJSON(raw string) ([]int32, error)

func KafkaListACLsRequestFromArgs

func KafkaListACLsRequestFromArgs(assetID int64, args map[string]any) kafka_svc.ListACLsRequest

func KafkaMessageCommand

func KafkaMessageCommand(operation, topic string) (string, error)

func KafkaProduceHeadersFromArgs

func KafkaProduceHeadersFromArgs(args map[string]any) ([]kafka_svc.ProduceMessageHeader, error)

func KafkaRegisterSchemaRequestFromArgs

func KafkaRegisterSchemaRequestFromArgs(assetID int64, args map[string]any) (kafka_svc.RegisterSchemaRequest, error)

func KafkaResetConsumerGroupOffsetRequestFromArgs

func KafkaResetConsumerGroupOffsetRequestFromArgs(assetID int64, args map[string]any) (kafka_svc.ResetConsumerGroupOffsetRequest, error)

func KafkaRestartConnectorRequestFromArgs

func KafkaRestartConnectorRequestFromArgs(assetID int64, args map[string]any) kafka_svc.RestartConnectorRequest

func KafkaSchemaCommand

func KafkaSchemaCommand(operation, subject string) (string, error)

func KafkaSchemaReferencesFromJSON

func KafkaSchemaReferencesFromJSON(raw string) ([]kafka_svc.SchemaReference, error)

func KafkaStringMapFromJSON

func KafkaStringMapFromJSON(raw string) (map[string]string, error)

func KafkaTopicCommand

func KafkaTopicCommand(operation, topic string) (string, error)

func ListMongoCollections

func ListMongoCollections(ctx context.Context, client *mongo.Client, database string) ([]string, error)

ListMongoCollections 列出指定数据库的所有集合名称

func ListMongoDatabases

func ListMongoDatabases(ctx context.Context, client *mongo.Client) ([]string, error)

ListMongoDatabases 列出所有数据库名称

func NormalizeKafkaOperation

func NormalizeKafkaOperation(operation, fallback string) string

func RunSSHCommand

func RunSSHCommand(ctx context.Context, client *ssh.Client, command string) (string, error)

RunSSHCommand 在已有的 SSH 客户端上执行命令

func WithDatabaseCache

func WithDatabaseCache(ctx context.Context, cache *DatabaseClientCache) context.Context

WithDatabaseCache 将数据库缓存注入 context

func WithKafkaService

func WithKafkaService(ctx context.Context, svc *kafka_svc.Service) context.Context

WithKafkaService 将 Kafka 服务注入 context,让 AI handler 在同一次 AI Send 内复用 franz-go client(避免每次工具调用都重新 dial+ping)。

func WithMongoDBCache

func WithMongoDBCache(ctx context.Context, cache *MongoDBClientCache) context.Context

WithMongoDBCache 将 MongoDB 缓存注入 context

func WithRedisCache

func WithRedisCache(ctx context.Context, cache *RedisClientCache) context.Context

WithRedisCache 将 Redis 缓存注入 context

func WithSSHPool

func WithSSHPool(ctx context.Context, pool *sshpool.Pool) context.Context

WithSSHPool 将 SSH 连接池注入 context(供 connpool 隧道使用)

func WithSerialManager

func WithSerialManager(ctx context.Context, mgr serial_svc.CommandManager) context.Context

WithSerialManager 将串口管理器注入 context

Types

type AIPoolDialer

type AIPoolDialer struct{}

AIPoolDialer 实现 sshpool.PoolDialer,委托给 credential_resolver 统一 dial

func (*AIPoolDialer) DialAsset

func (d *AIPoolDialer) DialAsset(ctx context.Context, assetID int64) (*ssh.Client, []io.Closer, error)

type ConnCache

type ConnCache[C io.Closer] struct {
	// contains filtered or unexported fields
}

ConnCache 泛型连接缓存,在同一次 AI Send 中复用连接。 并发模型:mu 保护 maps;sf 保证同一 assetID 并发首拨只发生一次(second waiter 拿到 first dialer 的结果),batch_command 多条命令并发命中同资产时只握手一次。

func NewConnCache

func NewConnCache[C io.Closer](name string) *ConnCache[C]

NewConnCache 创建连接缓存

func (*ConnCache[C]) Close

func (c *ConnCache[C]) Close() error

Close 关闭所有缓存的连接

func (*ConnCache[C]) Forget

func (c *ConnCache[C]) Forget(assetID int64)

Forget 仅把 assetID 对应的连接从缓存中摘除,但不主动调用 Close。 用于 client 已被上游提前关闭(例如 RunSSHCommand 在 ctx 取消时已 Close)的场景, 避免 Remove 触发二次 Close 及伴随的预期关闭错误日志。

func (*ConnCache[C]) GetOrDial

func (c *ConnCache[C]) GetOrDial(assetID int64, dial func() (C, io.Closer, error)) (C, io.Closer, error)

GetOrDial 从缓存获取连接,不存在则通过 dial 创建并缓存。 返回的 closer 始终为 nil —— 缓存命中或新拨号都由 cache 持有 closer, 调用方不需要也不应该 Close 这个连接(应通过 Remove/Forget/Close 释放)。

并发安全:同一 assetID 的并发首拨经 singleflight 合流为一次 dial,second waiter 直接拿到 first dialer 的 client;不同 assetID 完全并行。

func (*ConnCache[C]) Remove

func (c *ConnCache[C]) Remove(assetID int64)

Remove 关闭并移除指定 assetID 的缓存连接

type DatabaseClientCache

type DatabaseClientCache = ConnCache[*sql.DB]

DatabaseClientCache 在同一次 AI Send 中复用数据库连接

func NewDatabaseClientCache

func NewDatabaseClientCache() *DatabaseClientCache

NewDatabaseClientCache 创建数据库连接缓存

type MongoDBClientCache

type MongoDBClientCache = ConnCache[*connpool.MongoClientCloser]

MongoDBClientCache 在同一次 AI Send 中复用 MongoDB 连接

func NewMongoDBClientCache

func NewMongoDBClientCache() *MongoDBClientCache

NewMongoDBClientCache 创建 MongoDB 连接缓存

type RedisClientCache

type RedisClientCache = ConnCache[*redis.Client]

RedisClientCache 在同一次 AI Send 中复用 Redis 连接

func NewRedisClientCache

func NewRedisClientCache() *RedisClientCache

NewRedisClientCache 创建 Redis 连接缓存

Jump to

Keyboard shortcuts

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