salt

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2022 License: MIT Imports: 10 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"
)

func main() {

    ctx := context.TODO()
    // 初始化客户端
    client := salt.NewClient("https://saltapi.example.com", true)
    if err := client.Login(ctx, "username", "password", "eauth"); err != nil {
        panic(err)
    }

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

    // 执行命令
    payload := salt.RunRequest{
        Target: "*",
        Function: "cmd.run",
        Arg:      []string{"uptime"},
    }
    resp, err := client.Run(ctx, &payload)
    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            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:"testClient"`
		ProcessingTime float64 `json:"Processing Time"`
		RequestLine    string  `json:"Request-Line"`
	} `json:"Requests"`
}

type AsyncRunRequest

type AsyncRunRequest struct {
	Client   string `json:"client"`
	Target   string `json:"tgt"`
	Function string `json:"fun"`
}

type AsyncRunResponse

type AsyncRunResponse struct {
	Return []struct {
		Jid     string   `json:"jid"`
		Minions []string `json:"minions"`
	} `json:"return"`
	Links struct {
		Jobs []struct {
			Href string `json:"href"`
		} `json:"jobs"`
	} `json:"_links"`
}

type ExternalAuth

type ExternalAuth struct {
	Username string `json:"username"`
	Password string `json:"password"`
	EAuth    string `json:"eauth"`
}

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]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"`
	} `json:"Worker Threads"`
}

type HookResponse

type HookResponse struct {
	Success bool   `json:"success"`
	Status  int    `json:"status,omitempty"`
	Message string `json:"message,omitempty"`
}

type IClient

type IClient interface {
	Login(ctx context.Context, username, password, eauth string) error
	Logout(ctx context.Context) error
	ListMinions(ctx context.Context) (*MinionResponse, error)
	GetMinion(ctx context.Context, mid string) (*MinionResponse, error)
	AsyncRun(ctx context.Context, payload *AsyncRunRequest) (*AsyncRunResponse, error)
	ListKeys(ctx context.Context) (*KeysResponse, error)
	GetKey(ctx context.Context, mid string) (*KeyDetailResponse, error)
	ListJobs(ctx context.Context) (*JobsResponse, error)
	GetJob(ctx context.Context, jid string) (*JobResponse, error)
	Run(ctx context.Context, payload *RunRequest) (*RunResponse, error)
	Hook(ctx context.Context, id string, payload interface{}) (*HookResponse, error)
	Stats(ctx context.Context) (*StatsResponse, error)
}

func NewClient

func NewClient(baseAPI string, skipVerify bool) IClient

type Job

type Job struct {
	Function   string      `json:"Function"`
	Arguments  []string    `json:"Arguments"`
	Target     interface{} `json:"Target"`
	TargetType string      `json:"Target-Type"`
	User       string      `json:"User"`
	StartTime  string      `json:"StartTime"`
}

type JobResponse

type JobResponse struct {
	Info []struct {
		*Job
		JID     string   `json:"jid"`
		Minions []string `json:"ListMinions"`
		Result  map[string]struct {
			Return  bool `json:"return"`
			Retcode int  `json:"retcode"`
			Success bool `json:"success"`
		} `json:"Result"`
	} `json:"info"`
	Return []map[string]bool `json:"return"`
}

type JobsResponse

type JobsResponse struct {
	Return []map[string]Job `json:"return"`
}

type KeyDetailResponse

type KeyDetailResponse struct {
	Return struct {
		Minions map[string]string `json:"minions"`
	} `json:"return"`
}

type KeysResponse

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

type LoginResponse

type LoginResponse struct {
	Return []struct {
		Token  string   `json:"Token"`
		Start  float64  `json:"start"`
		Expire float64  `json:"expire"`
		User   string   `json:"user"`
		EAuth  string   `json:"eauth"`
		Perms  []string `json:"perms"`
	} `json:"return"`
}

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"`
}

type MinionResponse

type MinionResponse struct {
	Return []map[string]Minion `json:"return"`
}

type RunRequest

type RunRequest struct {
	*ExternalAuth
	Client   string                 `json:"client"`
	Target   interface{}            `json:"tgt"`
	Function string                 `json:"fun"`
	JID      string                 `json:"jid,omitempty"` // runner
	Arg      []string               `json:"arg,omitempty"`
	KwArg    map[string]interface{} `json:"kwarg,omitempty"`
}

type RunResponse

type RunResponse struct {
	Return []map[string]interface{} `json:"return"`
}

type StatsResponse

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

type TMPStats

type TMPStats map[string]interface{}

Jump to

Keyboard shortcuts

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