Documentation
¶
Overview ¶
The hook package provides a Go interface to the Juju charm hook commands. It is designed to be used alongside the gocharm command (github.com/juju/gocharm/cmd/gocharm)
Index ¶
- func Main(r *Registry) (err error)
- type Context
- func (ctxt *Context) AllRelationUnits(relationName string) (map[string][]string, error)
- func (ctxt *Context) ClosePort(proto string, port int) error
- func (ctxt *Context) CommandName(name string) string
- func (ctxt *Context) GetAllConfig() (map[string]interface{}, error)
- func (ctxt *Context) GetAllRelation() (map[string]string, error)
- func (ctxt *Context) GetAllRelationUnit(relationId, unit string) (map[string]string, error)
- func (ctxt *Context) GetConfig(key string) (interface{}, error)
- func (ctxt *Context) GetRelation(key string) (string, error)
- func (ctxt *Context) GetRelationUnit(relationId, unit, key string) (string, error)
- func (ctxt *Context) IsRelationHook() bool
- func (ctxt *Context) LocalState(name string, ptr interface{}) error
- func (ctxt *Context) Logf(f string, a ...interface{}) error
- func (ctxt *Context) OpenPort(proto string, port int) error
- func (ctxt *Context) PrivateAddress() (string, error)
- func (ctxt *Context) PublicAddress() (string, error)
- func (ctxt *Context) RelationIds(relationName string) ([]string, error)
- func (ctxt *Context) RelationUnits(relationId string) ([]string, error)
- func (ctxt *Context) SetRelation(keyvals ...string) error
- func (ctxt *Context) SetRelationWithId(relationId string, keyvals ...string) error
- func (ctxt *Context) StateDir() string
- func (ctxt *Context) UnitTag() string
- type ContextInfo
- type Registry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Context ¶
type Context struct {
*ContextInfo
// contains filtered or unexported fields
}
Context provides the context for a running Juju hook.
func (*Context) AllRelationUnits ¶
AllRelationUnits returns a map from all the relation ids for the relation with the given name to all the units with that name
func (*Context) CommandName ¶
CommandName returns a value that can be used to make runhook run the given command when passed as its first argument. The command name is relative to the registry from which ctxt was created. TODO better explanation and an example.
func (*Context) GetAllConfig ¶
func (*Context) GetAllRelation ¶
GetAllRelation returns all the settings for the relation and unit that triggered the hook execution. It is equivalent to GetAllRelationUnit(RelationId, RemoteUnit).
func (*Context) GetAllRelationUnit ¶
GetAllRelationUnit returns all the settings from the given unit associated with the relation with the given id.
func (*Context) GetConfig ¶
GetConfig returns the charm configuration value for the given key. Both int- and float-typed values will be returned as float64.
func (*Context) GetRelation ¶
GetRelation returns the value with the given key from the relation and unit that triggered the hook execution. It is equivalent to GetRelationUnit(RelationId, RemoteUnit, key).
func (*Context) GetRelationUnit ¶
GetRelationUnit returns the value with the given key from the given unit associated with the relation with the given id.
func (*Context) IsRelationHook ¶
func (*Context) LocalState ¶
LocalState reads charm local state for the given name (which should be valid to use as a file name) and uses it to fill out the value pointed to by ptr, which should be a pointer to a pointer to a type that's marshallable and unmarshallable as JSON.
When the hook has completed, the value will be saved back to the same place, making it persistent.
The first time LocalState is called in a hook, the element pointed to by ptr is allocated with new, and then filled by JSON unmarshalling, or left zero if there's no existing state. When LocalState is called again, the element pointed to by ptr will be set to the previously allocated value.
For example:
func someHook(ctxt *hook.Context) error {
type myState struct {
Called int
}
var state *myState
if err := ctxt.LocalState(&state, "some-hook"); err != nil {
return err
}
if !state.Called {
ctxt.Logf("someHook has never been called before")
}
state.Called = true
}
func (*Context) PrivateAddress ¶
PrivateAddress returns the private address of the local unit.
func (*Context) PublicAddress ¶
PublicAddress returns the public address of the local unit.
func (*Context) RelationIds ¶
RelationIds returns all the relation ids associated with the relation with the given name,
func (*Context) RelationUnits ¶
func (*Context) SetRelation ¶
SetRelation sets the given key-value pairs on the current relation instance.
func (*Context) SetRelationWithId ¶
SetRelationWithId sets the given key-value pairs on the relation with the given id.
type ContextInfo ¶
type ContextInfo struct {
// Valid for all hooks
UUID string
Unit string
CharmDir string
HookName string
// Valid for relation-related hooks.
RelationName string
RelationId string
RemoteUnit string
}
ContextInfo provides information about the context. It should be treated as read-only.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry allows the registration of hook functions.
func (*Registry) LocalContext ¶
LocalContext transforms an existing context into a context with state local to r.
func (*Registry) NewRegistry ¶
NewRegistry returns a sub-registry of r. Local state stored by hooks registered with that will be stored relative to the given name within r; likewise new registries created by NewRegistry on it will store local state relatively to r.
This enables hierarchical local storage for charm hooks.
func (*Registry) Register ¶
Register registers the given function to be called when the charm hook with the given name is invoked. The function must not use its provided Context after it returns.
If more than one function is registered for a given hook, each function will be called in turn until one returns an error; the context's local state will be saved with SaveState after each call.
func (*Registry) RegisterCommand ¶
RegisterCommand registers the given function to be called when the hook is invoked with a first argument of "cmd". The name is relative to the registry's state namespace. It will panic if the same name is registered more than once in the same Registry.
When the function is called, os.Args will be set up as if the function is main - the "cmd-" command selector will be removed.
func (*Registry) RegisteredHooks ¶
RegisteredHooks returns the names of all currently registered hooks.