salt

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2023 License: MIT Imports: 13 Imported by: 0

README

Go SaltStack

go 调用 salt-api 接口

安装

go get github.com/daixijun/go-salt

使用

package main

import (
	"context"
	"fmt"

	salt "github.com/daixijun/go-salt/v2"
)

func main() {

	ctx := context.TODO()
	// 初始化客户端
	client := salt.NewClient(
		salt.WithEndpoint("https://saltapi.example.com"),
		salt.WithUsername("saltapi"),
		salt.WithPassword("saltapi"),
		salt.WithAuthBackend("pam"),
		salt.WithInsecure(),
	)
	if err := client.Login(ctx); err != nil {
		panic(err)
	}

	// 列表 minions
	minions, err := client.ListMinions(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Println(minions)

	// 执行命令
	resp, err := client.LocalClient(ctx, "cmd.run", []string{"whoami"}, salt.WithGlobTarget("minion1"))
	if err != nil {
		panic(err)
	}
	fmt.Println(resp)
}

支持的接口

Documentation

Index

Constants

View Source
const (
	CP_APPLICATIONS      = "CherryPy Applications"
	CP_HTTPSERVER_PREFIX = "Cheroot HTTPServer"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Applications

type Applications struct {
	Enabled             bool               `json:"Enabled"`
	BytesReadRequest    float64            `json:"Bytes Read/Request"`
	BytesReadSecond     float64            `json:"Bytes Read/Second"`
	BytesWrittenRequest float64            `json:"Bytes Written/Request"`
	BytesWrittenSecond  float64            `json:"Bytes Written/Second"`
	CurrentTime         float64            `json:"Current Time"`
	CurrentRequests     int                `json:"Current Requests"`
	RequestsSecond      float64            `json:"Requests/Second"`
	ServerVersion       string             `json:"Server Version"`
	StartTime           float64            `json:"Start Time"`
	TotalBytesRead      int                `json:"Total Bytes Read"`
	TotalBytesWritten   int                `json:"Total Bytes Written"`
	TotalRequests       int                `json:"Total Requests"`
	TotalTime           float64            `json:"Total Time"`
	Uptime              float64            `json:"Uptime"`
	Requests            map[string]Request `json:"Requests"`
}

type Client

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

func NewClient

func NewClient(opts ...ClientOption) *Client

func (*Client) AcceptKey

func (c *Client) AcceptKey(ctx context.Context, mid string) ([]string, error)

func (*Client) DeleteKey

func (c *Client) DeleteKey(ctx context.Context, match string) ([]string, error)

func (*Client) GetKeyFinger

func (c *Client) GetKeyFinger(ctx context.Context, match string) (map[string]string, error)

func (*Client) GetKeyString

func (c *Client) GetKeyString(ctx context.Context, match string) (map[string]string, error)

func (*Client) GetMinion

func (c *Client) GetMinion(ctx context.Context, mid string) (*Minion, error)

func (*Client) Hook

func (c *Client) Hook(ctx context.Context, id string, payload interface{}) error

func (*Client) ListKeys

func (c *Client) ListKeys(ctx context.Context) (*ListKeysReturn, error)

func (*Client) ListMinions

func (c *Client) ListMinions(ctx context.Context) ([]Minion, error)

func (*Client) LocalClient

func (c *Client) LocalClient(ctx context.Context, fun string, arg []string, opts ...RunOption) (map[string]LocalClientReturn, error)

func (*Client) LocalClientAsync

func (c *Client) LocalClientAsync(ctx context.Context, tgt, fun string, arg []string, opts ...RunOption) (string, error)

func (*Client) ManageStatus

func (c *Client) ManageStatus(ctx context.Context) (*ManageStatusReturn, error)

func (*Client) RejectedKey

func (c *Client) RejectedKey(ctx context.Context, mid string) ([]string, error)

func (*Client) RunnerClient

func (c *Client) RunnerClient(ctx context.Context, fun string, arg []string, opts ...RunOption) (map[string]LocalClientReturn, error)

func (*Client) RunnerClientAsync

func (c *Client) RunnerClientAsync(ctx context.Context, tgt, fun string, arg []string, opts ...RunOption) (string, error)

func (*Client) Stats

func (c *Client) Stats(ctx context.Context) (*Stats, error)

type ClientOption

type ClientOption func(options *Client)

func WithAuthBackend

func WithAuthBackend(eauth string) ClientOption

func WithEndpoint

func WithEndpoint(s string) ClientOption

func WithInsecure

func WithInsecure() ClientOption

func WithPassword

func WithPassword(password string) ClientOption

func WithTimeout

func WithTimeout(timeout time.Duration) ClientOption

func WithUsername

func WithUsername(username string) ClientOption

type CommandClient

type CommandClient string
const (
	LocalClient       CommandClient = "local"
	LocalAsyncClient  CommandClient = "local_async"
	RunnerClient      CommandClient = "runner"
	RunnerAsyncClient CommandClient = "runner_async"
	WheelClient       CommandClient = "wheel"
	WheelAsyncClient  CommandClient = "wheel_async"
	SSHClient         CommandClient = "ssh"
)

type HTTPServer

type HTTPServer struct {
	Enabled         bool                    `json:"Enabled"`
	BindAddress     string                  `json:"Bind Address"`
	RunTime         int                     `json:"Run time"`
	Accepts         int                     `json:"Accepts"`
	AcceptsSec      float64                 `json:"Accepts/sec"`
	Queue           int                     `json:"Queue"`
	Threads         int                     `json:"Threads"`
	ThreadsIdle     int                     `json:"Threads Idle"`
	SocketErrors    int                     `json:"Socket Errors"`
	Requests        int                     `json:"Requests"`
	BytesRead       int                     `json:"Bytes Read"`
	BytesWritten    int                     `json:"Bytes Written"`
	WorkTime        int                     `json:"Work Time"`
	ReadThroughput  float64                 `json:"Read Throughput"`
	WriteThroughput float64                 `json:"Write Throughput"`
	WorkerThreads   map[string]WorkerThread `json:"Worker Threads"`
}

type Job

type Job struct {
	JID       string        `json:"jid,omitempty"`
	Function  string        `json:"Function,omitempty"`
	Arguments []interface{} `json:"Arguments,omitempty"`
	// KWArguments map[string]interface{} `json:"KWArguments,omitempty"`
	Target     interface{}          `json:"Target,omitempty"`
	TargetType TargetType           `json:"Target-Type,omitempty"`
	StartTime  saltTime             `json:"StartTime"`
	User       string               `json:"User"`
	Minions    []string             `json:"Minions,omitempty"`
	Result     map[string]jobResult `json:"Result,omitempty"`
	Error      string               `json:"Error,omitempty"`
}

type ListKeysReturn

type ListKeysReturn struct {
	Local           []string `json:"local"`
	Minions         []string `json:"minions"`
	MinionsPre      []string `json:"minions_pre"`
	MinionsRejected []string `json:"minions_rejected"`
	MinionsDenied   []string `json:"minions_denied"`
}

type LocalClientReturn

type LocalClientReturn struct {
	Jid     string      `json:"jid"`
	Ret     interface{} `json:"ret"`
	Retcode int         `json:"retcode"`
}

type ManageStatusReturn

type ManageStatusReturn struct {
	UP   []string `json:"up"`
	Down []string `json:"down"`
}

type Minion

type Minion struct {
	ID        string      `json:"id"`
	CWD       string      `json:"cwd"`
	NodeName  string      `json:"nodename"`
	MachineID string      `json:"machine_id"`
	Master    string      `json:"master"`
	ServerID  int64       `json:"server_id"`
	Localhost string      `json:"localhost"`
	Host      string      `json:"host"`
	Domain    string      `json:"domain"`
	IPGW      interface{} `json:"ip_gw"`
	IP4GW     interface{} `json:"ip4_gw"`
	IP6GW     interface{} `json:"ip6_gw"`
	DNS       struct {
		Nameservers   []string `json:"nameservers"`
		IP4Namespaces []string `json:"ip4_namespaces"`
		IP6Namespaces []string `json:"ip6_namespaces"`
		SortList      []string `json:"sortlist"`
		Domain        string   `json:"domain"`
		Search        []string `json:"search"`
		Options       []string `json:"options"`
	} `json:"dns"`
	FQDN            string              `json:"fqdn"`
	FQDNs           []string            `json:"fqdns"`
	HWAddrInterface map[string]string   `json:"hwaddr_interface"`
	IP4Interfaces   map[string][]string `json:"ip4_interfaces"`
	IP6Interfaces   map[string][]string `json:"ip6_interfaces"`
	IPInterfaces    map[string][]string `json:"ip_interfaces"`
	IPv4            []string            `json:"ipv4"`
	IPv6            []string            `json:"ipv6"`
	FQDNIP4         []string            `json:"fqdn_ip4"`
	FQDNIP6         []string            `json:"fqdn_ip6"`
	Kernel          string              `json:"kernel"`
	KernelRelease   string              `json:"kernelrelease"`
	KernelVersion   string              `json:"kernelversion"`
	KernelParams    [][]string          `json:"kernelparams"`
	LocaleInfo      struct {
		DefaultLanguage  string `json:"defaultlanguage"`
		DefaultEncoding  string `json:"defaultencoding"`
		DetectedEncoding string `json:"detectedencoding"`
		TimeZone         string `json:"timezone"`
	} `json:"locale_info"`
	NumGPUS int `json:"num_gpus"`
	GPUS    []struct {
		Vendor string `json:"vendor"`
		Model  string `json:"model"`
	} `json:"gpus"`
	NumCPUS  int      `json:"num_cpus"`
	CPUArch  string   `json:"cpuarch"`
	CPUModel string   `json:"cpu_model"`
	CPUFlags []string `json:"cpu_flags"`
	Selinux  struct {
		Enabled  bool   `json:"enabled"`
		Enforced string `json:"enforced"`
	} `json:"selinux"`
	Systemd struct {
		Version  string `json:"version"`
		Features string `json:"features"`
	} `json:"systemd"`
	Init               string              `json:"init"`
	LSBDistribID       string              `json:"lsb_distrib_id"`
	LSBDistribCodename string              `json:"lsb_distrib_codename"`
	OS                 string              `json:"os"`
	OSFullName         string              `json:"osfullname"`
	OSRelease          string              `json:"osrelease"`
	OSReleaseInfo      []int               `json:"osrelease_info"`
	OSCodename         string              `json:"oscodename"`
	OSMajorRelease     int                 `json:"osmajorrelease"`
	OSFinger           string              `json:"osfinger"`
	OSFamily           string              `json:"os_family"`
	OSArch             string              `json:"osarch"`
	MemTotal           int                 `json:"mem_total"`
	SwapTotal          int                 `json:"swap_total"`
	BiosVersion        string              `json:"biosversion"`
	BiosReleaseDate    string              `json:"biosreleasedate"`
	ProductName        string              `json:"productname"`
	Manufacturer       string              `json:"manufacturer"`
	UUID               string              `json:"uuid"`
	SerialNumber       string              `json:"serialnumber"`
	Virtual            string              `json:"virtual"`
	PS                 string              `json:"ps"`
	Path               string              `json:"path"`
	SystemPath         []string            `json:"systempath"`
	PythonExecutable   string              `json:"pythonexecutable"`
	PythonPath         []string            `json:"pythonpath"`
	PythonVersion      []interface{}       `json:"pythonversion"`
	SaltPath           string              `json:"saltpath"`
	SaltVersion        string              `json:"saltversion"`
	SaltVersionInfo    []int               `json:"saltversioninfo"`
	ZMQVersion         string              `json:"zmqversion"`
	Disks              []string            `json:"disks"`
	SSDs               []string            `json:"ssds"`
	Shell              string              `json:"shell"`
	Lvm                map[string][]string `json:"lvm"`
	MDAdm              []string            `json:"mdadm"`
	Username           string              `json:"username"`
	GroupName          string              `json:"groupname"`
	Pid                int64               `json:"pid"`
	Gid                int                 `json:"gid"`
	Uid                int                 `json:"uid"`
	ZFSSupport         bool                `json:"zfs_support"`
	ZFSFeatureFlags    bool                `json:"zfs_feature_flags"`
	Transactional      bool                `json:"transactional,omitempty"`
	Roles              []string            `json:"roles,omitempty"`
}

type Request

type Request struct {
	BytesRead      int     `json:"Bytes Read"`
	BytesWritten   int     `json:"Bytes Written"`
	ResponseStatus string  `json:"Response Status"`
	StartTime      float64 `json:"Start Time"`
	EndTime        float64 `json:"End Time"`
	Client         string  `json:"Client"`
	ProcessingTime float64 `json:"Processing Time"`
	RequestLine    string  `json:"Request-Line"`
}

type RunOption

type RunOption func(*commandRequest)

func WithGlobTarget

func WithGlobTarget(t string) RunOption

func WithGrainPcreTarget

func WithGrainPcreTarget(t string) RunOption

func WithGrainTarget

func WithGrainTarget(t string) RunOption

func WithIPCIDRTarget

func WithIPCIDRTarget(t net.IPNet) RunOption

func WithKeywordArguments

func WithKeywordArguments(kw map[string]interface{}) RunOption

func WithListTarget

func WithListTarget(t []string) RunOption

func WithNodeGroupTarget

func WithNodeGroupTarget(t string) RunOption

func WithPcreTarget

func WithPcreTarget(t string) RunOption

func WithPillarPcreTarget

func WithPillarPcreTarget(t string) RunOption

func WithPillarTarget

func WithPillarTarget(t string) RunOption

type Stats

type Stats struct {
	Applications Applications `json:"CherryPy Applications"`
	HTTPServer   HTTPServer   `json:"Cheroot HTTPServer"`
}

type TMPStats

type TMPStats map[string]interface{}

type TargetType

type TargetType string
const (
	Glob       TargetType = "glob"
	Pcre       TargetType = "pcre"
	List       TargetType = "list"
	Grain      TargetType = "grain"
	GrainPcre  TargetType = "grain_pcre"
	Pillar     TargetType = "pillar"
	PillarPcre TargetType = "pillar_pcre"
	NodeGroup  TargetType = "nodegroup"
	Range      TargetType = "range"
	Compound   TargetType = "compound"
	IPCIDR     TargetType = "ipcidr"
)

type WorkerThread

type WorkerThread struct {
	Requests        int     `json:"Requests"`
	BytesRead       int     `json:"Bytes Read"`
	BytesWritten    int     `json:"Bytes Written"`
	WorkTime        float64 `json:"Work Time"`
	ReadThroughput  float64 `json:"Read Throughput"`
	WriteThroughput float64 `json:"Write Throughput"`
}

Jump to

Keyboard shortcuts

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