gosible

package module
v0.0.0-...-292dba2 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: Apache-2.0 Imports: 21 Imported by: 0

README

go.dev reference

Why

I have used ansible as the main tool of DEVOPS for a long time, but it cannot refer to the task in the role according to the specified tag in the role of the playbook, which is very uncomfortable. Later, when developing the workflow of automation tools, there is another obvious "pit" in Ansible's playbook, that is, when an error occurs in a task executed by a server, the entire playbook will not be interrupted. This is when some business lines are released. fatal flaw. I had to make some changes, so I have this gosible repository.

Api

go get github.com/0xe8998e/gosible
package main

import (
	"fmt"
	"os"

	"github.com/0xe8998e/gosible"
	"github.com/urfave/cli/v2"
)

func main() {
	app := &cli.App{
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:     "playbooks",
				Aliases:  []string{"p"},
				Value:    "./examples/playbook/test.yaml",
				Usage:    "Enter the characters",
				Required: true,
			},
			&cli.StringFlag{
				Name:     "inventory",
				Aliases:  []string{"i"},
				Value:    "./examples/playbook/hosts.yaml",
				Usage:    "language for the greeting",
				Required: true,
			},
		},
		Action: func(c *cli.Context) error {
			playbooks := c.String("playbooks")
			fmt.Printf("playbooks = %v\n", playbooks)
			inventory := c.String("inventory")
			fmt.Printf("inventory = %v\n", inventory)

			gosible.InitLogger()
			Gosible := gosible.Gosible{

				Inventory: inventory,
				PlayBooks: playbooks,
			}

			Gosible.Parse()

			return nil
		},
	}

	_ = app.Run(os.Args)
}

Build

git clone github.com/0xe8998e/gosible
make buildmac|buildlinux|buildwin

BINARY Run

./bin/gosible --playbooks ./examples/playbook/test.yaml  --inventory ./examples/playbook/hosts.yaml

2022-02-07T22:19:49.157+0800    INFO    gosible/gosible.go:41   {
    "playbooks": [
        {
            "name": "playbook1",
            "task_results": [
                {
                    "name": "copy plog /tmp/1.txt",
                    "host_results": [
                        {
                            "host": "10.7.180.234",
                            "cmd": "copy",
                            "result": "Copy File[/tmp/1.txt] Sucessful",
                            "start_time": "2022-02-07 22:19:44",
                            "end_time": "2022-02-07 22:19:46",
                            "tag": "test1",
                            "status": "sucessful"
                        },
                        {
                            "host": "10.7.180.234",
                            "cmd": "copy",
                            "result": "Copy File[/tmp/1.txt] Sucessful",
                            "start_time": "2022-02-07 22:19:44",
                            "end_time": "2022-02-07 22:19:46",
                            "tag": "test1",
                            "status": "sucessful"
                        },
                        {
                            "host": "10.7.180.234",
                            "cmd": "copy",
                            "result": "Copy File[/tmp/1.txt] Sucessful",
                            "start_time": "2022-02-07 22:19:44",
                            "end_time": "2022-02-07 22:19:46",
                            "tag": "test1",
                            "status": "sucessful"
                        }
                    ],
                    "start_time": "2022-02-07 22:19:44",
                    "end_time": "2022-02-07 22:19:46"
                },
                {
                    "name": "test shell 1",
                    "host_results": [
                        {
                            "host": "10.7.180.234",
                            "cmd": "shell",
                            "result": "AndroidStudioProjects\nDesktop\nDocuments\nDownloads\nLibrary\nMovies\nMusic\nPictures\nPublic\nantia\nhot.py\ntools\nwork\n",
                            "start_time": "2022-02-07 22:19:46",
                            "end_time": "2022-02-07 22:19:47",
                            "tag": "test1",
                            "status": "sucessful"
                        },
                        {
                            "host": "10.7.180.234",
                            "cmd": "shell",
                            "result": "AndroidStudioProjects\nDesktop\nDocuments\nDownloads\nLibrary\nMovies\nMusic\nPictures\nPublic\nantia\nhot.py\ntools\nwork\n",
                            "start_time": "2022-02-07 22:19:46",
                            "end_time": "2022-02-07 22:19:47",
                            "tag": "test1",
                            "status": "sucessful"
                        },
                        {
                            "host": "10.7.180.234",
                            "cmd": "shell",
                            "result": "AndroidStudioProjects\nDesktop\nDocuments\nDownloads\nLibrary\nMovies\nMusic\nPictures\nPublic\nantia\nhot.py\ntools\nwork\n",
                            "start_time": "2022-02-07 22:19:46",
                            "end_time": "2022-02-07 22:19:47",
                            "tag": "test1",
                            "status": "sucessful"
                        }
                    ],
                    "start_time": "2022-02-07 22:19:46",
                    "end_time": "2022-02-07 22:19:47"
                },
                {
                    "name": "template test",
                    "host_results": [
                        {
                            "host": "10.7.180.234",
                            "cmd": "template",
                            "result": "[Template] Transfer Local File: main.j2  Sucessful",
                            "start_time": "2022-02-07 22:19:47",
                            "end_time": "2022-02-07 22:19:49",
                            "tag": "test1",
                            "status": "sucessful"
                        },
                        {
                            "host": "10.7.180.234",
                            "cmd": "template",
                            "result": "[Template] Transfer Local File: main.j2  Sucessful",
                            "start_time": "2022-02-07 22:19:47",
                            "end_time": "2022-02-07 22:19:49",
                            "tag": "test1",
                            "status": "sucessful"
                        },
                        {
                            "host": "10.7.180.234",
                            "cmd": "template",
                            "result": "[Template] Transfer Local File: main.j2  Sucessful",
                            "start_time": "2022-02-07 22:19:47",
                            "end_time": "2022-02-07 22:19:49",
                            "tag": "test1",
                            "status": "sucessful"
                        }
                    ],
                    "start_time": "2022-02-07 22:19:47",
                    "end_time": "2022-02-07 22:19:49"
                }
            ],
            "start_time": "2022-02-07 22:19:44",
            "end_time": "2022-02-07 22:19:49"
        }
    ],
    "start_time": "2022-02-07 22:19:44",
    "end_time": "2022-02-07 22:19:49"
}

Documentation

Overview

* @Author: 0xe8998e@gmail.com * @Date: 2022-01-01 00:22:03 * @LastEditTime: 2022-02-07 20:34:40 * @LastEditors: 0xe8998e@gmail.com * @FilePath: /gosible/pkg/gosible/gosible.go * @Description: gosible Contol Servers of DevOps's Tool

* @Author: 0xe8998e@gmail.com * @Date: 2022-01-01 00:22:03 * @LastEditTime: 2022-01-28 16:44:06 * @LastEditors: 0xe8998e@gmail.com * @FilePath: /gosible/pkg/gosible/gosible.go * @Description: gosible Contol Servers of DevOps's Tool

* @Author: 0xe8998e@gmail.com * @Date: 2021-09-29 17:08:32 * @LastEditTime: 2022-01-28 16:49:25 * @LastEditors: 0xe8998e@gmail.com * @FilePath: /gosible/pkg/gosible/param.go * @Description: gosible Contol Servers of DevOps's Tool

* @Author: 0xe8998e@gmail.com * @Date: 2022-01-01 00:22:03 * @LastEditTime: 2022-01-28 16:51:12 * @LastEditors: 0xe8998e@gmail.com * @FilePath: /gosible/pkg/gosible/playbook.go * @Description: gosible Contol Servers of DevOps's Tool

* @Author: 0xe8998e@gmail.com * @Date: 2022-01-01 00:22:03 * @LastEditTime: 2022-01-28 20:43:30 * @LastEditors: 0xe8998e@gmail.com * @FilePath: /gosible/pkg/gosible/result.go * @Description: gosible Contol Servers of DevOps's Tool

* @Author: 0xe8998e@gmail.com * @Date: 2022-01-01 00:22:03 * @LastEditTime: 2022-01-28 16:44:06 * @LastEditors: 0xe8998e@gmail.com * @FilePath: /gosible/pkg/gosible/gosible.go * @Description: gosible Contol Servers of DevOps's Tool

* @Author: 0xe8998e@gmail.com * @Date: 2022-01-01 00:22:03 * @LastEditTime: 2022-01-28 22:17:00 * @LastEditors: 0xe8998e@gmail.com * @FilePath: /gosible/pkg/gosible/task.go * @Description: gosible Contol Servers of DevOps's Tool

* @Author: 0xe8998e@gmail.com * @Date: 2022-01-01 00:22:03 * @LastEditTime: 2022-01-28 23:01:27 * @LastEditors: 0xe8998e@gmail.com * @FilePath: /gosible/pkg/gosible/tasks.go * @Description: gosible Contol Servers of DevOps's Tool

* @Author: 0xe8998e@gmail.com * @Date: 2022-01-01 00:22:03 * @LastEditTime: 2022-02-07 20:54:17 * @LastEditors: 0xe8998e@gmail.com * @FilePath: /gosible/gosible/utils.go * @Description: gosible Contol Servers of DevOps's Tool

* @Author: 0xe8998e@gmail.com * @Date: 2021-09-29 17:08:32 * @LastEditTime: 2022-01-28 16:46:38 * @LastEditors: 0xe8998e@gmail.com * @FilePath: /gosible/pkg/gosible/vars.go * @Description: gosible Contol Servers of DevOps's Tool

Index

Constants

View Source
const (
	FAILED    = "failed"
	SUCESSFUL = "sucessful"
)

Variables

This section is empty.

Functions

func CSTLayoutString

func CSTLayoutString() string

func DPanic

func DPanic(args ...interface{})

func DPanicf

func DPanicf(template string, args ...interface{})

func Debug

func Debug(args ...interface{})

func Debugf

func Debugf(template string, args ...interface{})

func Error

func Error(args ...interface{})

func Errorf

func Errorf(template string, args ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

func Fatalf(template string, args ...interface{})

func GetProjectAbsPath

func GetProjectAbsPath(filename string) (projectAbsPath string)

func InStringSlice

func InStringSlice(haystack []string, needle string) bool

func Info

func Info(args ...interface{})

func Infof

func Infof(template string, args ...interface{})

func InitLogger

func InitLogger()

func Panic

func Panic(args ...interface{})

func Panicf

func Panicf(template string, args ...interface{})

func TemplateFile

func TemplateFile(template string, varsMap map[string]interface{}) (string, error)

func TemplateString

func TemplateString(template string, varsMap map[string]interface{}) (string, error)

func Warn

func Warn(args ...interface{})

func Warnf

func Warnf(template string, args ...interface{})

Types

type CopyParam

type CopyParam struct {
	Src string
	Dst string
}

type Executer

type Executer struct {
	SshConfig *ssh.Config
}

func (*Executer) Copy

func (executer *Executer) Copy(task Task) (HostResult, error)

func (*Executer) Run

func (executer *Executer) Run(task Task) (HostResult, error)

func (*Executer) Shell

func (executer *Executer) Shell(task Task) (HostResult, error)

*

  • @description:
  • @param {Task} task
  • @return HostResult{},error

func (*Executer) Template

func (executer *Executer) Template(task Task) (HostResult, error)

func (*Executer) YumRepository

func (executer *Executer) YumRepository(task Task) (HostResult, error)

type ExecuterParam

type ExecuterParam struct {
	Host Host
	Task Task
}

type ExecuterResult

type ExecuterResult struct {
	HostResult HostResult
	Error      error
}

type Gosible

type Gosible struct {
	Inventory string `yaml:"inventory" default:"hosts"`
	PlayBooks string `yaml:"playbooks"   default:"playbooks.yml"`
}

Define Gosible Struct As Entry

func (*Gosible) Parse

func (gosible *Gosible) Parse() PlayBooks

type Group

type Group struct {
	Name  string `yaml:"name"`
	Hosts []Host `yaml:"hosts"`
}

func GetHostsByGroupName

func GetHostsByGroupName(inventory Inventory, groupName string) Group

*

  • @description:
  • @param {Inventory} inventory
  • @param {string} groupName
  • @return {*}

type Groups

type Groups []Group

type Host

type Host struct {
	Ip         string `yaml:"ip"`
	Port       int    `yaml:"port" default:"22"`
	UserName   string `yaml:"username"   default:"root"`
	PassWord   string `yaml:"password"`
	PrivateKey string `yaml:"private_key"`
}

type HostResult

type HostResult struct {
	Host      string `json:"host"`
	Cmd       string `json:"cmd"`
	Result    string `json:"result"`
	StartTime string `json:"start_time"`
	EndTime   string `json:"end_time"`
	Tag       string `json:"tag"`
	Status    string `json:"status"`
}

type Inventory

type Inventory struct {
	Groups Groups `yaml:"groups"`
}

func ParseInventory

func ParseInventory(configName string) Inventory

*

  • @description: Parse Invenory File Return Inventory
  • @param {string} configName
  • @return {*}

type Param

type Param map[string]interface{}

type PlayBook

type PlayBook struct {
	Name          string            `yaml:"name"`
	Priority      int               `yaml:"priority"`
	GroupName     string            `yaml:"groupName"`
	Group         Group             `yaml:"group"`
	Vars          map[string]string `yaml:"vars"`
	Roles         Roles             `yaml:"roles"`
	StartDateTime string
	EndDateTime   string
	BeforeAction  []PlayBookBeforeActionFunc
	AfterAction   []PlayBookAfterActionFunc
}

func (*PlayBook) SetAfterAction

func (playBook *PlayBook) SetAfterAction(afterAction PlayBookAfterActionFunc) bool

func (*PlayBook) SetBeforeAction

func (playBook *PlayBook) SetBeforeAction(beforeAction PlayBookBeforeActionFunc) bool

type PlayBookAfterActionFunc

type PlayBookAfterActionFunc func(playBook *PlayBook) bool

type PlayBookBeforeActionFunc

type PlayBookBeforeActionFunc func(playBook *PlayBook) bool

type PlayBookResult

type PlayBookResult struct {
	Name        string       `json:"name"`
	TaskResults []TaskResult `json:"task_results"`
	StartTime   string       `json:"start_time"`
	EndTime     string       `json:"end_time"`
}

type PlayBooks

type PlayBooks struct {
	Inventory    Inventory
	PlayBooks    []PlayBook `yaml:"playbooks"`
	BeforeAction []PlayBooksBeforeActionFunc
	AfterAction  []PlayBooksAfterActionFunc
}

func (*PlayBooks) Append

func (playBooks *PlayBooks) Append(data PlayBook) *PlayBooks

func (*PlayBooks) ParseInventory

func (playBooks *PlayBooks) ParseInventory(configName string) error

func (*PlayBooks) ParsePlayBooks

func (playBooks *PlayBooks) ParsePlayBooks(configName string) (*PlayBooks, error)

func (*PlayBooks) Run

func (playBooks *PlayBooks) Run() (PlayBooksResult, error)

func (*PlayBooks) SetAfterAction

func (playBooks *PlayBooks) SetAfterAction(afterAction PlayBooksAfterActionFunc) bool

func (*PlayBooks) SetBeforeAction

func (playBooks *PlayBooks) SetBeforeAction(beforeAction PlayBooksBeforeActionFunc) bool

func (*PlayBooks) Sort

func (playBooks *PlayBooks) Sort() *PlayBooks

type PlayBooksAfterActionFunc

type PlayBooksAfterActionFunc func(playBooks *PlayBooks) bool

type PlayBooksBeforeActionFunc

type PlayBooksBeforeActionFunc func(playBooks *PlayBooks) bool

type PlayBooksResult

type PlayBooksResult struct {
	PlayBookResults []PlayBookResult `json:"playbooks"`
	StartTime       string           `json:"start_time"`
	EndTime         string           `json:"end_time"`
}

type Role

type Role struct {
	Name  string                 `yaml:"name"`
	Vars  map[string]interface{} `yaml:"vars"`
	Tags  []string               `yaml:"tags"`
	Tasks []Task                 `yaml:tasks"`
}

type Roles

type Roles []Role

type ShellParam

type ShellParam struct {
	Data string
}

type SortPlayBooks

type SortPlayBooks struct {
	PlayBooks []PlayBook
	By        func(p, q *PlayBook) bool
}

func (SortPlayBooks) Len

func (pw SortPlayBooks) Len() int

func (SortPlayBooks) Less

func (pw SortPlayBooks) Less(i, j int) bool

func (SortPlayBooks) Swap

func (pw SortPlayBooks) Swap(i, j int)

type SortTasks

type SortTasks struct {
	Task []Task
	By   func(p, q *Task) bool
}

=========

func (SortTasks) Len

func (pw SortTasks) Len() int

func (SortTasks) Less

func (pw SortTasks) Less(i, j int) bool

func (SortTasks) Swap

func (pw SortTasks) Swap(i, j int)

type Task

type Task struct {
	Id            string
	Name          string
	Cmd           string
	FileName      string
	Tag           string
	Vars          Vars
	Param         Param
	Priority      int
	StartDateTime string
	EndDateTime   string
	BeforeAction  []TaskBeforeActionFunc
	AfterAction   []TaskAfterActionFunc
}

func (*Task) SetAfterAction

func (task *Task) SetAfterAction(afterAction TaskAfterActionFunc) bool

func (*Task) SetBeforeAction

func (task *Task) SetBeforeAction(beforeAction TaskBeforeActionFunc) bool

type TaskAfterActionFunc

type TaskAfterActionFunc func(task *Task) bool

type TaskBeforeActionFunc

type TaskBeforeActionFunc func(task *Task) bool

type TaskResult

type TaskResult struct {
	Name        string       `json:"name"`
	HostResults []HostResult `json:"host_results"`
	StartTime   string       `json:"start_time"`
	EndTime     string       `json:"end_time"`
}

type Tasks

type Tasks struct {
	Tasks []Task `yaml:"tasks"`
}

func ParseTasks

func ParseTasks(configName string, role Role) (Tasks, error)

*

  • @description:
  • @param {string} configName
  • @return {*}

func (*Tasks) Append

func (tasks *Tasks) Append(data Task) *Tasks

func (*Tasks) Sort

func (tasks *Tasks) Sort() *Tasks

*

  • @description:
  • @param {*}
  • @return {*}

type TemplateParam

type TemplateParam struct {
	Src string
	Dst string
}

type Vars

type Vars map[string]interface{}

type YumRepositoryParam

type YumRepositoryParam struct {
	Config string
	Name   string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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