Documentation ¶
Overview ¶
Package client is the Go runtime for Watermelon modules.
A module is an executable that connects to the Watermelon GRPC server and accepts commands from the server. The commands are usually requests to execute a function. To execute these functions, the module must publish them before connecting the server. This is done in the module main:
package main import ( "os" "github.com/bserdar/watermelon/client" ) func main() { f := client.Functions{} f.Add("db.Bootstrap", dbBootstrap) f.Add("db.Config", dbConfig) f.Add("controller.Bootstrap", controllerBootstrap) client.Run(os.Args[1:], f, nil) }
The above main declares three functions, assigns them to Go functions and calls the client runtime with program arguments. The program arguments will contain the Watermelon server address, and optional `--log loglevel` argument. The client runtime connect the server, accepts function call requests and dispatches them. The runtime also deals with requests made from within this module, such as calling other modules, or calling the server to get inventory/host information, or to run a command on a remote host.
In addition to registering functions as above, this runtime allows publishing functions as part of a GRPC server as well. You can implement the module as a GRPC server, and publish the server as follows:
package main import ( "os" grpc "google.golang.org/grpc" "github.com/bserdar/watermelon/client" "github.com/bserdar/watermelon/modules/pkg/yum" ) func main() { yumServer := yum.Server{} client.Run(os.Args[1:], nil, func(server *grpc.Server, rt *client.Runtime) { // Notify the runtime that there is a GRPC server for "yum" rt.RegisterGRPCServer(&yumServer, "yum") // Register the server yum.RegisterYumServer(server, yumServer) }) }
The published functions are called with a client session, and arguments. The arguments is a JSON document containing the arguments to the function. When completed, the function returns a JSON document containing the response, and an optional error. The function should either return a non-nil response, or a non-nil error, or both nils.
func dbBootstrap(session *client.Session, args[]byte) (output []byte,error) { }
The Go client runtime has a function wrapper for convenience, so you can declare functions also as follows:
// Does not get any arguments, does not return an output or error func dbBootstrap(session *client.Session) { } // Accepts arguments in a InStruct, and returns the output in OutStruct. The // wrapper unmarshals input arguments into InStruct, and marshals the output // to OutStruct func dbBootstrap(session *client.Session,in InStruct) OutStruct { } // No input/output, but may return error func dbBootstrap(client *client.Session) error { }
If the service is registered as a GRPC service, the functions are regular GRPC functions that return ModuleResponse:
// Yum update GRPC function func (s Server) Update(ctx context.Context, req *PackageParams) (*module.Response, error) { }
If a module publishes GRPC functions, those functions can be called from other modules via GRPC, or by calling session.Call. If the module published functions without GRPC, then only session.Call can be used to call them.
Index ¶
- Constants
- Variables
- func Export(name string, function interface{}) int
- func Flags() *flag.FlagSet
- func Run(args []string, funcs Functions, ...)
- func Wrap(in interface{}) func(*Session, []byte) ([]byte, error)
- type Args
- type CmdResponse
- type CommandError
- type CommonFileInfo
- type Ensure
- type FileOwner
- type Functions
- type GRPCServer
- type HasAllLabels
- type HasAnyLabel
- type HasNoneLabels
- type Host
- func (h Host) Chmod(path string, mode int) error
- func (h Host) Chown(path string, user, group string) error
- func (h Host) Command(cmd string) CmdResponse
- func (h Host) CommandMayFail(cmd string) (CmdResponse, error)
- func (h Host) Commandf(format string, args ...interface{}) CmdResponse
- func (h Host) CopyFromLocal(fromPath, toPath string) error
- func (h Host) CopyFromLocalIfDifferent(fromPath, toPath string) (bool, error)
- func (h Host) Ensure(path string, req Ensure) bool
- func (h Host) Exists(path string) bool
- func (h Host) GetCfg(path string, out interface{})
- func (h Host) GetCfgJSON(path string) []byte
- func (h Host) GetFileInfo(path string) (os.FileInfo, FileOwner)
- func (h Host) GetInfo() pb.HostInfo
- func (h Host) HasLabel(label string) bool
- func (h Host) Logf(format string, args ...interface{})
- func (h Host) MarshalJSON() ([]byte, error)
- func (h Host) Mkdir(path string) error
- func (h Host) ReadFile(file string) (os.FileInfo, []byte)
- func (h Host) String() string
- func (h Host) WaitHost(timeout time.Duration) error
- func (h Host) WriteFile(file string, perms os.FileMode, data []byte) error
- func (h Host) WriteFileFromTemplate(file string, perms os.FileMode, template string, templateData interface{}) (bool, error)
- func (h Host) WriteFileFromTemplateFile(file string, perms os.FileMode, templateFile string, templateData interface{}) (bool, error)
- func (h Host) WriteFileIfDifferent(file string, perms os.FileMode, data []byte) (bool, error)
- type Inventory
- func (inv Inventory) Add(session string, to string, hosts []string) (string, error)
- func (inv Inventory) GetHostIDs(session string, invID string) ([]string, error)
- func (inv Inventory) GetHostInfo(session string, IDs []string) ([]*pb.HostInfo, error)
- func (inv Inventory) GetHosts(session string, invID string) ([]*pb.HostInfo, error)
- func (inv Inventory) Make(session string, from []string) (string, error)
- func (inv Inventory) Release(session string, id string)
- func (inv Inventory) Select(session string, from string, what ...Selector) (string, error)
- func (inv Inventory) Union(session string, what []string) (string, error)
- type KeyAndValues
- type Remote
- func (r Remote) Chmod(session string, hostID string, path string, mode int) (*pb.CommandError, error)
- func (r Remote) Chown(session string, hostID string, path string, user, group string) (*pb.CommandError, error)
- func (r Remote) Command(session string, hostID string, cmd string) (CmdResponse, error)
- func (r Remote) Commandf(session string, hostID string, format string, args ...interface{}) (CmdResponse, error)
- func (r Remote) CopyFile(session string, from string, fromPath string, to string, toPath string, ...) (bool, error)
- func (r Remote) Ensure(session string, hostID string, path string, req Ensure) (bool, error)
- func (r Remote) GetFileInfo(session string, hostID string, path string) (os.FileInfo, FileOwner, error)
- func (r Remote) Mkdir(session string, hostID string, path string) (*pb.CommandError, error)
- func (r Remote) ReadFile(session string, hostID string, file string) (os.FileInfo, []byte, error)
- func (r Remote) WaitHost(session string, hostID string, timeout time.Duration) error
- func (r Remote) WriteFile(session string, hostID string, file string, perms os.FileMode, data []byte, ...) (bool, *pb.CommandError, error)
- func (r Remote) WriteFileFromTemplate(session string, hostID string, file string, perms os.FileMode, template string, ...) (bool, *pb.CommandError, error)
- type Runtime
- func (rt *Runtime) Call(session, module, function string, data interface{}) (*pb.Response, error)
- func (rt *Runtime) ConnectModule(module string) (*grpc.ClientConn, error)
- func (rt *Runtime) GetArgs(session string) []string
- func (rt *Runtime) GetCfg(session, path string, out interface{}) error
- func (rt *Runtime) GetCfgJSON(session, path string) ([]byte, error)
- func (rt *Runtime) GetHostCfg(session, host, path string, out interface{}) error
- func (rt *Runtime) GetHostCfgJSON(session, host, path string) ([]byte, error)
- func (rt *Runtime) LoadModule(name string) (string, error)
- func (rt *Runtime) Logf(session string, hostID string, format string, args ...interface{})
- func (rt *Runtime) Printf(session string, format string, args ...interface{})
- func (rt *Runtime) RegisterGRPCServer(server grpcServer, funcNamePrefix string)
- func (rt *Runtime) Session(id string) *Session
- func (rt *Runtime) Start() error
- type SelectByAllProperty
- type SelectByAnyProperty
- type SelectByID
- type SelectByName
- type Selector
- type Session
- func (s Session) Add(to string, hosts []string) string
- func (s *Session) Call(module, function string, data interface{}) *pb.Response
- func (s *Session) Chmod(hostID string, path string, mode int) error
- func (s *Session) Chown(hostID string, path string, user, group string) error
- func (s Session) Command(hostID string, cmd string) CmdResponse
- func (s Session) CommandMayFail(hostID string, cmd string) (CmdResponse, error)
- func (s Session) Commandf(hostID string, format string, args ...interface{}) CmdResponse
- func (s *Session) ConnectModule(module string) (*grpc.ClientConn, error)
- func (s *Session) Context() context.Context
- func (s *Session) CopyFile(from string, fromPath string, to string, toPath string) error
- func (s *Session) CopyFromLocal(fromPath string, to string, toPath string) error
- func (s *Session) CopyFromLocalIfDifferent(fromPath string, to string, toPath string) (bool, error)
- func (s *Session) CopyIfDifferent(from string, fromPath string, to string, toPath string) (bool, error)
- func (s *Session) Ensure(hostID string, path string, req Ensure) bool
- func (s *Session) Exists(hostID string, path string) bool
- func (s *Session) ForAll(inv string, f func(Host) error) bool
- func (s *Session) ForAllSelected(sel Selector, f func(Host) error) bool
- func (s *Session) ForAllSerial(inv string, f func(Host) error) bool
- func (s Session) GetArgs() []string
- func (s Session) GetCfg(path string, out interface{})
- func (s Session) GetCfgJSON(path string) []byte
- func (s *Session) GetFileInfo(hostID string, path string) (os.FileInfo, FileOwner)
- func (s Session) GetHostCfg(host, path string, out interface{})
- func (s Session) GetHostCfgJSON(host, path string) []byte
- func (s Session) GetHostIDs(invID string) []string
- func (s Session) GetHostInfo(IDs []string) []*pb.HostInfo
- func (s Session) GetHosts(invID string) []*pb.HostInfo
- func (s *Session) Host(h string) Host
- func (s *Session) LoadModule(module string) (string, error)
- func (s Session) Logf(hostID string, format string, args ...interface{})
- func (s Session) Make(from []string) string
- func (s *Session) Mkdir(hostID string, path string) error
- func (s Session) Printf(format string, args ...interface{})
- func (s Session) ReadFile(hostID string, file string) (os.FileInfo, []byte)
- func (s Session) Release(id string)
- func (s Session) Select(from string, what ...Selector) string
- func (s Session) Union(what []string) string
- func (s *Session) WaitHost(hostID string, timeout time.Duration) error
- func (s *Session) WriteFile(hostID string, file string, perms os.FileMode, data []byte) error
- func (s *Session) WriteFileFromTemplate(hostID string, file string, perms os.FileMode, template string, ...) (bool, error)
- func (s *Session) WriteFileFromTemplateFile(hostID string, file string, perms os.FileMode, templateFile string, ...) (bool, error)
- func (s *Session) WriteFileIfDifferent(hostID string, file string, perms os.FileMode, data []byte) (bool, error)
- type WorkServer
Constants ¶
const LocalhostID = "localhost"
LocalhostID is the localhost
Variables ¶
var AllHosts = "all"
AllHosts of the inventory
Functions ¶
func Export ¶
Export can be used to register a function as an anonymous variable
var _ = client.Export("myFunc",myFunc) func myFunc() { }
func Wrap ¶
Wrap wraps a function and translates json input/output for the wrapped function. The input function must be of the form:
in func(*Session,InStruct) (OutStruct,error) in func(*Session,InStruct) error in func(*Session,InStruct) in func(*Session)
InStruct and OutStruct are public structs. Wrap translates the input to InStruct, calls the function, and then translates the OutStruct to byte array. If either in or outstruct are []byte, they are untranslated. If the function returns only error, or nothing, those are passed as nil
Types ¶
type CmdResponse ¶
CmdResponse is a command response
type CommandError ¶
CommandError is an error message from a host
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
type Ensure ¶
Ensure describes the attributes of a file/directory
func (Ensure) EnsureMode ¶
EnsureMode returns a copy of e with mode set
type GRPCServer ¶
type GRPCServer struct {
RT *Runtime
}
GRPCServer has the dispatch function that helps implement expose functions as GRPC functions. Embed GRPCServer to implement a custom GRPC server for a module.
type MyServer struct { client.GRPCServer } func (s MyServer) Function(req *Request) (*Response,errror) { var out Response err:=s.Dispatch(req.SessionID,function,*req,&out) if err!=nil { return nil,err } return &out, nil }
func (GRPCServer) Dispatch ¶
func (s GRPCServer) Dispatch(sessionID string, f, input, output interface{}) error
Dispatch calls f
func (*GRPCServer) GetSession ¶
func (s *GRPCServer) GetSession(ID string) *Session
GetSession returns a session with the given ID
func (GRPCServer) SessionFromContext ¶
func (s GRPCServer) SessionFromContext(ctx context.Context) *Session
SessionFromContext retrieves the session from the GRPC context on the receiving end of a GRPC call
type HasAllLabels ¶
type HasAllLabels struct {
Labels []string
}
HasAllLabels is a selector that selects hosts containing all the labels
func Has ¶
func Has(label string) HasAllLabels
Has returns a selector that selects hosts containing label
func HasAllOf ¶
func HasAllOf(label ...string) HasAllLabels
HasAllOf returns a selector that selects hosts containing all of the labels
type HasAnyLabel ¶
type HasAnyLabel struct {
Labels []string
}
HasAnyLabel is a selector that selects hosts containing any of the labels
func HasAnyOf ¶
func HasAnyOf(label ...string) HasAnyLabel
HasAnyOf returns a selector that selects hosts containing any of the labels
type HasNoneLabels ¶
type HasNoneLabels struct {
Labels []string
}
HasNoneLabels is a selector that selects hosts containing none of the labels
func HasNoneOf ¶
func HasNoneOf(label ...string) HasNoneLabels
HasNoneOf returns a selector that selects hosts containing none of the labels
type Host ¶
Host encapsulates a single host
func (Host) Command ¶
func (h Host) Command(cmd string) CmdResponse
Command executes a command on the host
func (Host) CommandMayFail ¶
func (h Host) CommandMayFail(cmd string) (CmdResponse, error)
CommandMayFail returns error if command fails, instead of panicking
func (Host) Commandf ¶
func (h Host) Commandf(format string, args ...interface{}) CmdResponse
Commandf executes a command on the host
func (Host) CopyFromLocal ¶
CopyFromLocal copies a file from local to host
func (Host) CopyFromLocalIfDifferent ¶
CopyFromLocalIfDifferent copies a file from local to h if different
func (Host) GetCfg ¶
GetCfg retrieves a configuration ite pointed to by path, a JSON pointer, and unmarshals the JSON to out
func (Host) GetCfgJSON ¶
GetCfgJSON retrieves a configuration item pointed to by path, a JSON pointer, and returns the JSON for it. It looks at host first, and if item is not found there, it looks at global configuration
func (Host) GetFileInfo ¶
GetFileInfo retrieves file information
func (Host) WriteFileFromTemplate ¶
func (h Host) WriteFileFromTemplate(file string, perms os.FileMode, template string, templateData interface{}) (bool, error)
WriteFileFromTemplate writes a file to a remote host based on a template. TemplateData is marshaled in JSON
func (Host) WriteFileFromTemplateFile ¶
func (h Host) WriteFileFromTemplateFile(file string, perms os.FileMode, templateFile string, templateData interface{}) (bool, error)
WriteFileFromTemplateFile writes a file to a remote host based on a template file on localhost. TemplateData is marshaled in JSON
type Inventory ¶
type Inventory struct {
// contains filtered or unexported fields
}
Inventory is the inventory runtime implementation for clients
func (Inventory) GetHostIDs ¶
GetHostIDs returns the host IDs included in the inventory
func (Inventory) GetHostInfo ¶
GetHostInfo returns the host information for the given hosts
func (Inventory) Release ¶
Release notifies the server that the inventory is no longer needed, and can be freed
type KeyAndValues ¶
KeyAndValues contains a property key, and a set of values one of which should match
type Remote ¶
type Remote struct {
// contains filtered or unexported fields
}
Remote is the remote runtime implementation for clients
func (Remote) Chmod ¶
func (r Remote) Chmod(session string, hostID string, path string, mode int) (*pb.CommandError, error)
Chmod runs chmod on host
func (Remote) Chown ¶
func (r Remote) Chown(session string, hostID string, path string, user, group string) (*pb.CommandError, error)
Chown runs chown on host
func (Remote) Commandf ¶
func (r Remote) Commandf(session string, hostID string, format string, args ...interface{}) (CmdResponse, error)
Commandf executes a command on a host
func (Remote) CopyFile ¶
func (r Remote) CopyFile(session string, from string, fromPath string, to string, toPath string, onlyIfDifferent bool) (bool, error)
CopyFile copies a file
func (Remote) GetFileInfo ¶
func (r Remote) GetFileInfo(session string, hostID string, path string) (os.FileInfo, FileOwner, error)
GetFileInfo retrieves file information
func (Remote) WriteFile ¶
func (r Remote) WriteFile(session string, hostID string, file string, perms os.FileMode, data []byte, onlyIfDifferent bool) (bool, *pb.CommandError, error)
WriteFile writes a file to a remote host
func (Remote) WriteFileFromTemplate ¶
func (r Remote) WriteFileFromTemplate(session string, hostID string, file string, perms os.FileMode, template string, templateData interface{}, onlyIfDifferent bool) (bool, *pb.CommandError, error)
WriteFileFromTemplate writes a file to a remote host based on a template. TemplateData is marshaled in JSON
type Runtime ¶
type Runtime struct { Port int Worker WorkServer Inv Inventory Rmt Remote ClientConn *grpc.ClientConn LCClient pb.LifecycleClient }
Runtime is the client runtime to be used by module implementations.
func NewRuntime ¶
func NewRuntime(server string, listener net.Listener, funcs Functions, registerGRPCServers func(*grpc.Server, *Runtime)) (*Runtime, error)
NewRuntime creates a new runtime with the given server connection. It creates a server on the client side using localhost:myport. The server must be of the form host:port
func (*Runtime) ConnectModule ¶
func (rt *Runtime) ConnectModule(module string) (*grpc.ClientConn, error)
ConnectModule loads a module and returns a client connection to it
func (*Runtime) GetCfg ¶
GetCfg retrieves a configuration ite pointed to by path, a JSON pointer, and unmarshals the JSON to out
func (*Runtime) GetCfgJSON ¶
GetCfgJSON retrieves a configuration item pointed to by path, a JSON pointer, and returns the JSON for it.
func (*Runtime) GetHostCfg ¶
GetHostCfg retrieves a configuration ite pointed to by path, a JSON pointer, and unmarshals the JSON to out. It looks at host specific configuration first, and then the global configuration
func (*Runtime) GetHostCfgJSON ¶
GetHostCfgJSON retrieves a host-specific configuration item pointed to by path, a JSON pointer, and returns the JSON for it. If the host does not have the configuration item, this looks at the global configuration
func (*Runtime) LoadModule ¶
LoadModule loads a module and returns its GRPC address
func (*Runtime) RegisterGRPCServer ¶
RegisterGRPCServer registers a GRPC server with the runtime so its methods can be exposed as funcNamePrefix.MethodName
type SelectByAllProperty ¶
type SelectByAllProperty struct {
All []KeyAndValues
}
SelectByAllProperty is a selector that selects hosts having all the given properties
func WithAllProperty ¶
func WithAllProperty(kv ...KeyAndValues) SelectByAllProperty
WithAllProperty returns a selector that selects hosts containing all given properties
type SelectByAnyProperty ¶
type SelectByAnyProperty struct {
Any []KeyAndValues
}
SelectByAnyProperty is a selector that selects hosts having any of the given properties
func WithAnyProperty ¶
func WithAnyProperty(kv ...KeyAndValues) SelectByAnyProperty
WithAnyProperty returns a selector that selects hosts containing any of the given properties
type SelectByID ¶
type SelectByID struct {
IDs []string
}
SelectByID is a selector that selects hosts by ID
func WithIds ¶
func WithIds(id ...string) SelectByID
WithIds returns a selector that selects hosts by ID
type SelectByName ¶
type SelectByName struct {
Names []string
}
SelectByName is a selector that selects hosts by name
func WithNames ¶
func WithNames(name ...string) SelectByName
WithNames returns a selector that selects hosts by name
type Session ¶
Session represents a client session. All session functions will panic if there is an error
func (Session) Command ¶
func (s Session) Command(hostID string, cmd string) CmdResponse
Command executes a command on a host
func (Session) CommandMayFail ¶
func (s Session) CommandMayFail(hostID string, cmd string) (CmdResponse, error)
CommandMayFail returns error if command fails, instead of panicking
func (Session) Commandf ¶
func (s Session) Commandf(hostID string, format string, args ...interface{}) CmdResponse
Commandf executes a command on a host
func (*Session) ConnectModule ¶
func (s *Session) ConnectModule(module string) (*grpc.ClientConn, error)
ConnectModule loads a module and returns a client connection to it
func (*Session) Context ¶
Context returns a context to be used in GRPC calls. The returned contex contains the session ID as metadata
func (*Session) CopyFromLocal ¶
CopyFromLocal copies a file from localhost
func (*Session) CopyFromLocalIfDifferent ¶
CopyFromLocalIfDifferent copies a file from localhost if different
func (*Session) CopyIfDifferent ¶
func (s *Session) CopyIfDifferent(from string, fromPath string, to string, toPath string) (bool, error)
CopyIfDifferent copies a file if it is different in destination
func (*Session) ForAllSelected ¶
ForAllSelected selects hosts mathcing the selector from all hosts, and call f for each
func (*Session) ForAllSerial ¶
ForAllSerial runs f for all hosts in inv one by one. Returns true if everything is fine.
func (Session) GetCfg ¶
GetCfg retrieves a configuration ite pointed to by path, a JSON pointer, and unmarshals the JSON to out
func (Session) GetCfgJSON ¶
GetCfgJSON retrieves a configuration item pointed to by path, a JSON pointer, and returns the JSON for it.
func (*Session) GetFileInfo ¶
GetFileInfo retrieves file information
func (Session) GetHostCfg ¶
GetHostCfg retrieves a configuration ite pointed to by path, a JSON pointer, and unmarshals the JSON to out. It looks at the host configuration first, and if it is not found there, it looks at the global configuration
func (Session) GetHostCfgJSON ¶
GetHostCfgJSON retrieves a configuration item pointed to by path, a JSON pointer, and returns the JSON for it. It looks at the host configuration first, and if not found there, looks at the global configuration
func (Session) GetHostIDs ¶
GetHostIDs returns the host IDs included in the inventory
func (Session) GetHostInfo ¶
GetHostInfo returns the host information for the given hosts
func (*Session) LoadModule ¶
LoadModule loads a module and returns its GRPC location
func (Session) Release ¶
Release notifies the server that the inventory is no longer needed, and can be freed
func (Session) Select ¶
Select a subset of an inventory based on the given criteria, and returns a new inventory id representing the subset
func (Session) Union ¶
Union combines inventories to build a new host set containing all the hosts in the sources
func (*Session) WriteFileFromTemplate ¶
func (s *Session) WriteFileFromTemplate(hostID string, file string, perms os.FileMode, template string, templateData interface{}) (bool, error)
WriteFileFromTemplate writes a file to a remote host based on a template. TemplateData is marshaled in JSON
func (*Session) WriteFileFromTemplateFile ¶
func (s *Session) WriteFileFromTemplateFile(hostID string, file string, perms os.FileMode, templateFile string, templateData interface{}) (bool, error)
WriteFileFromTemplateFile writes a file to a remote host based on a template file loaded from localhost. TemplateData is marshaled in JSON
type WorkServer ¶
type WorkServer struct { Functions Functions Server *grpc.Server Listener net.Listener // contains filtered or unexported fields }
WorkServer contains the functions defined in this module
func (*WorkServer) Process ¶
func (w *WorkServer) Process(ctx context.Context, req *pb.Request) (returnResponse *pb.Response, e error)
Process calls the function
func (*WorkServer) Start ¶
func (w *WorkServer) Start() error
Start starts the work server. It doesn't return until the listener is closed