bee

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: Apache-2.0 Imports: 33 Imported by: 0

README

简介

bee 是一套远程自动化命令行工具,支持在本地批量运行远程主机上的脚本。

支持的脚本格式:

  • .tengo (windows | linux)
  • .bat .ps (windows)
  • .sh .bash (linux)

支持的远程连接协议

  • ssh
  • winrm
  • grpc

实例

直接执行内置模块命令

package main

import (
	"context"

	"go.uber.org/zap"

	"github.com/olive-io/bee"
	inv "github.com/olive-io/bee/inventory"
	"github.com/olive-io/bee/parser"
	"github.com/olive-io/bee/plugins/callback"
	"github.com/olive-io/bee/vars"
)

var inventoryText = `
host1 bee_host=localhost:22 bee_user=root bee_ssh_passwd=123456
`

func main() {
	sources := []string{"host1"}

	lg, _ := zap.NewDevelopment()
	dataloader := parser.NewDataLoader()
	if err := dataloader.ParseString(inventoryText); err != nil {
		lg.Fatal("parse inventory", zap.Error(err))
	}
	inventory, err := inv.NewInventoryManager(dataloader, sources...)
	if err != nil {
		lg.Fatal("inventory manager", zap.Error(err))
	}
	variables := vars.NewVariablesManager(dataloader, inventory)

	options := []bee.Option{
		bee.SetDir(".bee"), // 该目录为 bee 根目录,存放内置模块 和 tengo 解释器
		bee.SetLogger(lg),
	}
	rt, err := bee.NewRuntime(inventory, variables, dataloader, &callback.BaseCallBack{}, options...)
	if err != nil {
		lg.Fatal("bee runtime", zap.Error(err))
	}

	ctx := context.TODO()
	execOptions := make([]bee.RunOption, 0)
	data, err := rt.Execute(ctx, "host1", "ping", execOptions...)
	if err != nil {
		lg.Fatal("bee runtime", zap.Error(err))
	}

	lg.Info("output", zap.String("data", string(data)))
}

bee 根目录结构

_output/bee
├── db
├── modules
│   └── builtin
│       └── ping
│           ├── bee.yml
│           └── ping.tengo
└── repl
    ├── tengo.linux.amd64
    ├── tengo.linux.arm64
    └── tengo.windows.amd64.exe

自定义模块

模块结构

_output/mymodule
└── hello_world
    ├── bee.yml
    └── hello_world.tengo

hello_world.tengo

os := import("os")
fmt := import("fmt")
text := import("text")

name := "world"
if len(os.args()) != 0 {
    flag := os.args()[2]
	name = text.trim_prefix(flag, "--name=")
}

fmt.printf("{\"message\": \"%s\"}\n", name)

执行自定义模块命令


var inventoryText = `
host1 bee_host=localhost:22 bee_user=root bee_ssh_passwd=123456
`

func main() {
	sources := []string{"host1"}

	lg, _ := zap.NewDevelopment()
	dataloader := parser.NewDataLoader()
	if err := dataloader.ParseString(inventoryText); err != nil {
		lg.Fatal("parse inventory", zap.Error(err))
	}
	inventory, err := inv.NewInventoryManager(dataloader, sources...)
	if err != nil {
		lg.Fatal("inventory manager", zap.Error(err))
	}
	variables := vars.NewVariablesManager(dataloader, inventory)

	options := []bee.Option{
		bee.SetDir(".bee"), // 该目录为 bee 根目录,存放内置模块 和 tengo 解释器
		bee.SetLogger(lg),
	}
	rt, err := bee.NewRuntime(inventory, variables, dataloader, &callback.BaseCallBack{}, options...)
	if err != nil {
		lg.Fatal("bee runtime", zap.Error(err))
	}

	ctx := context.TODO()
	execOptions := make([]bee.RunOption, 0)
	data, err := rt.Execute(ctx, "host1", "ping", execOptions...)
	if err != nil {
		lg.Fatal("bee runtime", zap.Error(err))
	}

	lg.Info("output", zap.String("data", string(data)))
}

Documentation

Index

Examples

Constants

View Source
const (
	DefaultCacheSize = 1024 * 1024 * 10
)

Variables

View Source
var (
	DefaultParallel = runtime.NumCPU() * 2
)
View Source
var (
	Version = "0.1.0"
)

Functions

func BuildLogger

func BuildLogger() (*zap.Logger, error)

Types

type Callable added in v0.2.2

type Callable func(ctx context.Context, host, action string, in []byte, opts ...RunOption) ([]byte, error)

type Option

type Option func(*Options)

func SetCaller added in v0.2.2

func SetCaller(caller Callable) Option

func SetCheck

func SetCheck(check bool) Option

func SetDir

func SetDir(dir string) Option

func SetLogger

func SetLogger(lg *zap.Logger) Option

func SetModulePath

func SetModulePath(modulePath []string) Option

func SetParallel

func SetParallel(parallel int) Option

type Options

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

type RunOption

type RunOption func(*RunOptions)

func WithMetadata added in v0.2.2

func WithMetadata(md map[string]any) RunOption

func WithRunCallback added in v0.2.0

func WithRunCallback(cb callback.ICallBack) RunOption

func WithRunFilter added in v0.2.0

func WithRunFilter(f filter.IFilter) RunOption

func WithRunSync added in v0.2.0

func WithRunSync(b bool) RunOption

func WithRunTracer added in v0.2.0

func WithRunTracer(tracer chan tracing.ITrace) RunOption

type RunOptions

type RunOptions struct {
	Callback callback.ICallBack
	Filter   filter.IFilter
	Tracer   chan tracing.ITrace
	Metadata map[string]any
	// contains filtered or unexported fields
}

type Runtime

type Runtime struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"context"

	"go.uber.org/zap"

	"github.com/olive-io/bee"
	inv "github.com/olive-io/bee/inventory"
	"github.com/olive-io/bee/parser"
	"github.com/olive-io/bee/vars"
)

var inventoryText = `
host1 bee_host=localhost:22 bee_user=root bee_ssh_passwd=123456
`

func main() {
	sources := []string{"host1"}

	lg, _ := zap.NewDevelopment()
	dataloader := parser.NewDataLoader()
	if err := dataloader.ParseString(inventoryText); err != nil {
		lg.Fatal("parse inventory", zap.Error(err))
	}
	inventory, err := inv.NewInventoryManager(dataloader, sources...)
	if err != nil {
		lg.Fatal("inventory manager", zap.Error(err))
	}
	variables := vars.NewVariablesManager(dataloader, inventory)

	options := []bee.Option{
		bee.SetDir("_output/bee"),
		bee.SetLogger(lg),
		bee.SetModulePath([]string{"_output/mymodule"}),
	}
	rt, err := bee.NewRuntime(inventory, variables, dataloader, options...)
	if err != nil {
		lg.Fatal("bee runtime", zap.Error(err))
	}

	ctx := context.TODO()
	execOptions := make([]bee.RunOption, 0)
	data, err := rt.Execute(ctx, "host1", "hello_world name=lack", execOptions...)
	if err != nil {
		lg.Fatal("bee runtime", zap.Error(err))
	}

	lg.Info("output", zap.String("data", string(data)))
}
Output:

func NewRuntime

func NewRuntime(
	inventory *inv.Manager, variables *vars.VariableManager,
	loader *parser.DataLoader, opts ...Option,
) (*Runtime, error)

func (*Runtime) Execute

func (rt *Runtime) Execute(ctx context.Context, host, shell string, opts ...RunOption) ([]byte, error)

func (*Runtime) Inventory added in v0.2.0

func (rt *Runtime) Inventory() *inv.Manager

func (*Runtime) Logger

func (rt *Runtime) Logger() *zap.Logger

func (*Runtime) Play added in v0.2.0

func (rt *Runtime) Play(ctx context.Context, pr *process.Process, opts ...RunOption) error

func (*Runtime) RunBpmnProcess added in v0.2.0

func (rt *Runtime) RunBpmnProcess(ctx context.Context, definitions *schema.Definitions, dataObjects, properties map[string]string, opts ...RunOption) error

func (*Runtime) Stop

func (rt *Runtime) Stop() error

Directories

Path Synopsis
api
rpc
cmd
bee
rsa
server
slog
Package slog provides structured logging, in which log records include a message, a severity level, and various other attributes expressed as key-value pairs.
Package slog provides structured logging, in which log records include a message, a severity level, and various other attributes expressed as key-value pairs.
slog/internal/buffer
Package buffer provides a pool-allocated byte buffer.
Package buffer provides a pool-allocated byte buffer.
test
db

Jump to

Keyboard shortcuts

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