hook

package
v0.0.0-...-ef933ab Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2014 License: LGPL-3.0 Imports: 13 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main(r *Registry) (err error)

Main creates a new context from the environment and invokes the appropriate command or hook functions from the given registry or sub-registries of it.

Types

type Context

type Context struct {
	*ContextInfo
	// contains filtered or unexported fields
}

Context provides the context for a running Juju hook.

func (*Context) AllRelationUnits

func (ctxt *Context) AllRelationUnits(relationName string) (map[string][]string, error)

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) ClosePort

func (ctxt *Context) ClosePort(proto string, port int) error

func (*Context) CommandName

func (ctxt *Context) CommandName(name string) string

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 (ctxt *Context) GetAllConfig() (map[string]interface{}, error)

func (*Context) GetAllRelation

func (ctxt *Context) GetAllRelation() (map[string]string, error)

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

func (ctxt *Context) GetAllRelationUnit(relationId, unit string) (map[string]string, error)

GetAllRelationUnit returns all the settings from the given unit associated with the relation with the given id.

func (*Context) GetConfig

func (ctxt *Context) GetConfig(key string) (interface{}, error)

GetConfig returns the charm configuration value for the given key. Both int- and float-typed values will be returned as float64.

func (*Context) GetRelation

func (ctxt *Context) GetRelation(key string) (string, error)

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

func (ctxt *Context) GetRelationUnit(relationId, unit, key string) (string, error)

GetRelationUnit returns the value with the given key from the given unit associated with the relation with the given id.

func (*Context) IsRelationHook

func (ctxt *Context) IsRelationHook() bool

func (*Context) LocalState

func (ctxt *Context) LocalState(name string, ptr interface{}) error

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) Logf

func (ctxt *Context) Logf(f string, a ...interface{}) error

Log logs a message through the juju logging facility.

func (*Context) OpenPort

func (ctxt *Context) OpenPort(proto string, port int) error

func (*Context) PrivateAddress

func (ctxt *Context) PrivateAddress() (string, error)

PrivateAddress returns the private address of the local unit.

func (*Context) PublicAddress

func (ctxt *Context) PublicAddress() (string, error)

PublicAddress returns the public address of the local unit.

func (*Context) RelationIds

func (ctxt *Context) RelationIds(relationName string) ([]string, error)

RelationIds returns all the relation ids associated with the relation with the given name,

func (*Context) RelationUnits

func (ctxt *Context) RelationUnits(relationId string) ([]string, error)

func (*Context) SetRelation

func (ctxt *Context) SetRelation(keyvals ...string) error

SetRelation sets the given key-value pairs on the current relation instance.

func (*Context) SetRelationWithId

func (ctxt *Context) SetRelationWithId(relationId string, keyvals ...string) error

SetRelationWithId sets the given key-value pairs on the relation with the given id.

func (*Context) StateDir

func (ctxt *Context) StateDir() string

StateDir returns the path to the directory where local state for the given context will be stored. The directory is not guaranteed to exist.

func (*Context) UnitTag

func (ctxt *Context) UnitTag() string

UnitTag returns the tag of the current unit, useful for using as a file name.

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 NewRegistry

func NewRegistry() *Registry

NewRegistry returns a new hook registry.

func (*Registry) LocalContext

func (r *Registry) LocalContext(ctxt *Context) *Context

LocalContext transforms an existing context into a context with state local to r.

func (*Registry) NewRegistry

func (r *Registry) NewRegistry(localStateName string) *Registry

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

func (r *Registry) Register(name string, f func(ctxt *Context) error)

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

func (r *Registry) RegisterCommand(name string, f func())

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

func (r *Registry) RegisteredHooks() []string

RegisteredHooks returns the names of all currently registered hooks.

Jump to

Keyboard shortcuts

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