Giter Site home page Giter Site logo

bee's Introduction

简介

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)))
}

bee's People

Contributors

lack30 avatar

Watchers

 avatar KingKong avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.