server

package
v0.0.0-...-84310d3 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const Primary = "primary"

Primary interface name.

View Source
const Primary4 = "primary4"

Primary4 IP4 interface name

View Source
const Primary6 = "primary6"

Primary6 IP6 interface name

Variables

View Source
var AllHosts = "all"

AllHosts is the inventory that contains all known hosts

View Source
var LocalModules = map[string]Module{}

LocalModules keeps a map of all local modules

View Source
var Localhost = &Host{HostInfo: pb.HostInfo{ID: LocalhostID,
	Labels:     make([]string, 0),
	Properties: make(map[string]string)}}

Localhost is the localhost

View Source
var LocalhostID = "localhost"

LocalhostID is the "localhost"

View Source
var SessionFactory func() Session

SessionFactory should be set in main to create new instances of session configured using the flags or config file(s)

View Source
var Sessions = sessionRegistry{Sessions: map[string]Session{}}

Sessions keeps all active sessions

Functions

func ArrayContains

func ArrayContains(arr []string, str string) bool

ArrayContains returns if the array contains the string

func ErrInvalidSession

func ErrInvalidSession(sessionID string) error

ErrInvalidSession returns invalid session error

func GetHostAndSession

func GetHostAndSession(sessionID, hostID string) (Session, *Host, error)

GetHostAndSession returns a host from the session

func HasLabel

func HasLabel(h *pb.HostInfo, l string) bool

HasLabel returns true if host has the label l

func LockForInteraction

func LockForInteraction()

LockForInteraction prevents multiple goroutines from entering user interaction

func MapYaml

func MapYaml(in interface{}) interface{}

MapYaml converts map[interface{}]interface{} to map[string]interface{}

func NewInventoryID

func NewInventoryID() string

NewInventoryID returns a new inventory ID. It skips AllHosts.

func ParseFileMode

func ParseFileMode(str string) os.FileMode

ParseFileMode parses -rwxrwxrwx string

func RegisterBackend

func RegisterBackend(name string, b func(*Host) HostBackend)

RegisterBackend registers a new backend

func RegisterModule

func RegisterModule(name string, m Module)

RegisterModule registers a local module

func UnlockForInteraction

func UnlockForInteraction()

UnlockForInteraction unlocks the interaction lock

func Words

func Words(in string) []string

Words breaks the line into words separated by whitespace

Types

type CmdErr

type CmdErr interface {
	Error() string
	Host() string
	Msg() string
	ToPb() *pb.CommandError
}

CmdErr is used for command errors. Regular go errors are used for fatal infrastructure errors

func CmdErrFromErr

func CmdErrFromErr(host *Host, err error) CmdErr

CmdErrFromErr returns a CmdErr from error. if err is nil, returns nil

func NewCmdErr

func NewCmdErr(host *Host, msg string, args ...interface{}) CmdErr

NewCmdErr constructs a new CmdErr

type CommonFileInfo

type CommonFileInfo struct {
	FileName    string
	FileSize    int64
	FileMode    os.FileMode
	FileModTime time.Time
	FileIsDir   bool
}

CommonFileInfo is an os.FileInfo

func (CommonFileInfo) IsDir

func (c CommonFileInfo) IsDir() bool

IsDir returns true if this is a directory

func (CommonFileInfo) ModTime

func (c CommonFileInfo) ModTime() time.Time

ModTime returns the file modification time

func (CommonFileInfo) Mode

func (c CommonFileInfo) Mode() os.FileMode

Mode returns the file mode

func (CommonFileInfo) Name

func (c CommonFileInfo) Name() string

Name returns the file name

func (CommonFileInfo) Size

func (c CommonFileInfo) Size() int64

Size returns the file size

func (CommonFileInfo) Sys

func (c CommonFileInfo) Sys() interface{}

Sys returns nil

type Ctx

type Ctx interface {
	New(Session) (HostSession, error)
	Close()
}

Ctx is host session context

type Dispatcher

type Dispatcher struct {
	Name string
	// contains filtered or unexported fields
}

Dispatcher is the default module function dispatcher

func NewDispatcher

func NewDispatcher(name string) *Dispatcher

NewDispatcher returns a new dispatcher with the given module name

func (*Dispatcher) Add

func (d *Dispatcher) Add(name string, f Funcdef) *Dispatcher

Add adds a new function to dispatcher

func (Dispatcher) Func

func (d Dispatcher) Func(session Session, name string, in []byte) (Response, error)

Func dispatches the function call. This allows embedding Dispatcher into module

type FileDesc

type FileDesc struct {
	Mode  *int
	UID   *string
	GID   *string
	User  *string
	Group *string
	Dir   *bool
}

FileDesc describes the attributes of a file/directory

type FileOwner

type FileOwner struct {
	OwnerName string
	OwnerID   string
	GroupName string
	GroupID   string
}

FileOwner contains owner information

type Funcdef

type Funcdef struct {
	// ParamsFactory returns an object instance that will be unmarshaled
	// from json. It should me a pointer.
	ParamsFactory func() interface{}
	// This is the actual implementation function. The input interface
	// is what's returned from ParamsFactory
	Impl func(Session, interface{}) (Response, error)
}

Funcdef describes an implementation of the function

type Host

type Host struct {
	sync.Mutex
	pb.HostInfo

	Configuration interface{}

	// The bastion host to use to connect this host
	Bastion *Host
	// SSH hostname
	Hostname string
	Network  string
	Port     int

	// If nonempty, this user is used to login
	LoginUser string
	// Password of the user
	LoginPassword string
	// If non-nil, the public key signer derived from the private key will be used
	KeyAuth *sshdial.RawPrivateKey
	// HostPublicKey is initialized if empty, validated if nonempty
	HostPublicKey ssh.PublicKey

	// Become user methos
	Become string

	Backend HostBackend
}

Host contains information about a host in the inventory

func GetHostFromSession

func GetHostFromSession(sessionID, hostID string) (*Host, error)

GetHostFromSession returns a host from the session

func (*Host) Chmod

func (h *Host) Chmod(ctx Ctx, s Session, path string, mode int) (CmdErr, error)

Chmod changes mode

func (*Host) Chown

func (h *Host) Chown(ctx Ctx, s Session, path string, user, group string) (CmdErr, error)

Chown changes user/group, whichever is nonempty

func (*Host) Defaults

func (h *Host) Defaults()

Defaults initializes uninitialized host fields with defaults

func (*Host) DiscoverIPs

func (h *Host) DiscoverIPs() error

DiscoverIPs initializes the host IPs

func (*Host) Ensure

func (h *Host) Ensure(ctx Ctx, s Session, path string, desc FileDesc) (bool, CmdErr, error)

Ensure a file has the desired attributes

func (*Host) GetAuth

func (h *Host) GetAuth() ([]ssh.AuthMethod, error)

GetAuth returns the supported SSH auth methods

func (*Host) GetFileInfo

func (h *Host) GetFileInfo(ctx Ctx, s Session, file string) (FileOwner, os.FileInfo, CmdErr, error)

GetFileInfo retrieves file info from a host. If the file is missing, will return FileOwner{},nil,nil

func (*Host) GetHostAndPort

func (h *Host) GetHostAndPort() string

GetHostAndPort returns the host and port to connect

func (*Host) GetNetwork

func (h *Host) GetNetwork() string

GetNetwork returns the network to connect to this host

func (*Host) GetUserName

func (h *Host) GetUserName() string

GetUserName returns the login user

func (*Host) MkDir

func (h *Host) MkDir(ctx Ctx, s Session, path string) (CmdErr, error)

MkDir creates dir

func (*Host) NewCtx

func (h *Host) NewCtx() Ctx

NewCtx returns a new host context

func (*Host) ReadFile

func (h *Host) ReadFile(ctx Ctx, s Session, name string) (os.FileInfo, []byte, CmdErr, error)

ReadFile reads a file from the host

func (*Host) RunCmd

func (h *Host) RunCmd(ctx Ctx, s Session, cmd string, env map[string]string) (HostCommandResponse, error)

RunCmd runs cmd

func (*Host) Via

func (h *Host) Via() sshdial.Host

Via returns a bastion host

func (*Host) WriteFile

func (h *Host) WriteFile(ctx Ctx, s Session, name string, perms os.FileMode, content []byte) (CmdErr, error)

WriteFile writes a file on the host

type HostBackend

type HostBackend interface {
	NewSession(Session, *Host) (HostSession, error)
}

HostBackend opens a new session to run commands on the host

func GetBackend

func GetBackend(name string, h *Host) HostBackend

GetBackend returns the backend

type HostCommandResponse

type HostCommandResponse struct {
	Out      []byte
	Err      []byte
	ExitCode int
}

HostCommandResponse contains the response of the command

type HostSession

type HostSession interface {
	WriteFile(name string, perms os.FileMode, content []byte) (CmdErr, error)
	ReadFile(name string) (os.FileInfo, []byte, CmdErr, error)
	Run(cmd string, env map[string]string) (HostCommandResponse, error)
	GetFileInfo(file string) (FileOwner, os.FileInfo, CmdErr, error)
	MkDir(string) (CmdErr, error)
	Chmod(string, int) (CmdErr, error)
	Chown(string, string, string) (CmdErr, error)
	Close()
}

HostSession declares the operations that can run on the host

type InternalInventory

type InternalInventory interface {
	Inventory

	GetHost([]string) ([]*Host, error)
}

InternalInentory adds additional functions to the basic inventory interface for server use only.

type Inventory

type Inventory interface {
	// Select returns a new inventory containing only the hosts
	// selected by the given selectors from the given inventory
	Select(string, ...*pb.Selector) (string, error)

	// Union combines inventories to build a new host set containing
	// all the hosts in the sources
	Union([]string) (string, error)

	// Make creates a new inventory from the given host IDs
	Make([]string) (string, error)

	// Add adds new hosts to the given inventory, and returns the new
	// inventory ID
	Add(string, []string) (string, error)

	// GetHostIDs returns the host IDs included in the inventory
	GetHostIDs(string) ([]string, error)

	// GetHostInfo returns the host information for the given hosts
	GetHostInfo([]string) ([]*pb.HostInfo, error)

	// Release notifies the server that the inventory is no longer
	// needed, and can be freed
	Release(string)
}

Inventory server interface. Inventory keeps all known servers

type InventoryConfiguration

type InventoryConfiguration struct {
	PrivateKey *sshdial.RawPrivateKey
}

InventoryConfiguration contains the configuration loaded from the inventory

type Logger

type Logger interface {
	Print(...interface{})
	Printf(string, ...interface{})
}

Logger provides the printing functions. A logger is associated with a single host

type Logging

type Logging interface {
	// New returns the logger for the host. If logToStdout is true, then
	// prints logs also to stdout
	New(h *Host, logToStdout bool) Logger
}

Logging provides access to loggers

type Module

type Module interface {
	// Func calls a module function
	Func(Session, string, []byte) (Response, error)
	// Short help about the module
	Describe() string
	// Long help about the module
	Help() string
}

Module interface is to be implemented by all modules

type ModuleMgr

type ModuleMgr interface {
	// SendRequest to a module function
	SendRequest(session, module, funcName string, data []byte) (Response, error)
	Close()
}

ModuleMgr deals with calling modules

type Response

type Response struct {
	Success bool
	// This should be set to true if the work caused any changes
	Modified bool
	FuncName string
	ErrorMsg string
	Data     []byte
}

Response is the response returned by a module call

func CallLocalModule

func CallLocalModule(session, module, funcname string, data []byte) (Response, bool, error)

CallLocalModule calls a local module if there is one registered

func (*Response) Append

func (w *Response) Append(rsp Response)

Append adds the given work response to this one

type Session

type Session interface {
	GetID() string
	// Close a session
	Close()
	// GetLogger returns a logger for the host
	GetLogger(host *Host) Logger
	// GetHost returns a host from the session
	GetHost(hostID string) (*Host, error)
	// GetCfg returns a path from cfg, either seen by host, or global config
	GetCfg(hostId, path string) interface{}

	// GetInv returns inventory
	GetInv() InternalInventory
	SetInv(InternalInventory)
	GetModules() ModuleMgr
	SetModules(ModuleMgr)
	SetLogStdout(bool)
	SetLog(Logging)
	GetConfig() interface{}
	SetConfig(interface{})
	SetArgs([]string)
	GetArgs() []string
}

Session interface

func GetSession

func GetSession(ID string) Session

GetSession returns a session by ID. Returns nil if not found

func NewSession

func NewSession() Session

NewSession creates a new empty session

Directories

Path Synopsis
backends
yml

Jump to

Keyboard shortcuts

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