github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/runner/jujuc/context.go (about)

     1  // Copyright 2012, 2013, 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc
     5  
     6  import (
     7  	"fmt"
     8  	"strconv"
     9  	"strings"
    10  	"time"
    11  
    12  	"gopkg.in/juju/charm.v4"
    13  
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/network"
    16  	"github.com/juju/juju/storage"
    17  )
    18  
    19  type RebootPriority int
    20  
    21  const (
    22  	// noop
    23  	RebootSkip RebootPriority = iota
    24  	// wait for current hook to finish before rebooting
    25  	RebootAfterHook
    26  	// reboot immediately, killing and requeueing the calling hook
    27  	RebootNow
    28  )
    29  
    30  // Context is the interface that all hook helper commands
    31  // depend on to interact with the rest of the system.
    32  type Context interface {
    33  
    34  	// Unit returns the executing unit's name.
    35  	UnitName() string
    36  
    37  	// PublicAddress returns the executing unit's public address.
    38  	PublicAddress() (string, bool)
    39  
    40  	// PrivateAddress returns the executing unit's private address.
    41  	PrivateAddress() (string, bool)
    42  
    43  	// AvailabilityZone returns the executing unit's availablilty zone.
    44  	AvailabilityZone() (string, bool)
    45  
    46  	// OpenPorst marks the supplied port range for opening when the
    47  	// executing unit's service is exposed.
    48  	OpenPorts(protocol string, fromPort, toPort int) error
    49  
    50  	// ClosePorts ensures the supplied port range is closed even when
    51  	// the executing unit's service is exposed (unless it is opened
    52  	// separately by a co- located unit).
    53  	ClosePorts(protocol string, fromPort, toPort int) error
    54  
    55  	// OpenedPorts returns all port ranges currently opened by this
    56  	// unit on its assigned machine. The result is sorted first by
    57  	// protocol, then by number.
    58  	OpenedPorts() []network.PortRange
    59  
    60  	// Config returns the current service configuration of the executing unit.
    61  	ConfigSettings() (charm.Settings, error)
    62  
    63  	// ActionParams returns the map of params passed with an Action.
    64  	ActionParams() (map[string]interface{}, error)
    65  
    66  	// UpdateActionResults inserts new values for use with action-set.
    67  	// The results struct will be delivered to the state server upon
    68  	// completion of the Action.
    69  	UpdateActionResults(keys []string, value string) error
    70  
    71  	// SetActionMessage sets a message for the Action.
    72  	SetActionMessage(string) error
    73  
    74  	// SetActionFailed sets a failure state for the Action.
    75  	SetActionFailed() error
    76  
    77  	// HookRelation returns the ContextRelation associated with the executing
    78  	// hook if it was found, and whether it was found.
    79  	HookRelation() (ContextRelation, bool)
    80  
    81  	// RemoteUnitName returns the name of the remote unit the hook execution
    82  	// is associated with if it was found, and whether it was found.
    83  	RemoteUnitName() (string, bool)
    84  
    85  	// Relation returns the relation with the supplied id if it was found, and
    86  	// whether it was found.
    87  	Relation(id int) (ContextRelation, bool)
    88  
    89  	// RelationIds returns the ids of all relations the executing unit is
    90  	// currently participating in.
    91  	RelationIds() []int
    92  
    93  	// OwnerTag returns the user tag of the service the executing
    94  	// units belongs to.
    95  	OwnerTag() string
    96  
    97  	// AddMetric records a metric to return after hook execution.
    98  	AddMetric(string, string, time.Time) error
    99  
   100  	// RequestReboot will set the reboot flag to true on the machine agent
   101  	RequestReboot(prio RebootPriority) error
   102  
   103  	// StorageInstance returns the storage instance with the given id.
   104  	StorageInstance(storageId string) (*storage.StorageInstance, bool)
   105  
   106  	// HookStorageInstance returns the storage instance associated
   107  	// the executing hook.
   108  	HookStorageInstance() (*storage.StorageInstance, bool)
   109  }
   110  
   111  // ContextRelation expresses the capabilities of a hook with respect to a relation.
   112  type ContextRelation interface {
   113  
   114  	// Id returns an integer which uniquely identifies the relation.
   115  	Id() int
   116  
   117  	// Name returns the name the locally executing charm assigned to this relation.
   118  	Name() string
   119  
   120  	// FakeId returns a string of the form "relation-name:123", which uniquely
   121  	// identifies the relation to the hook. In reality, the identification
   122  	// of the relation is the integer following the colon, but the composed
   123  	// name is useful to humans observing it.
   124  	FakeId() string
   125  
   126  	// Settings allows read/write access to the local unit's settings in
   127  	// this relation.
   128  	Settings() (Settings, error)
   129  
   130  	// UnitNames returns a list of the remote units in the relation.
   131  	UnitNames() []string
   132  
   133  	// ReadSettings returns the settings of any remote unit in the relation.
   134  	ReadSettings(unit string) (params.Settings, error)
   135  }
   136  
   137  // Settings is implemented by types that manipulate unit settings.
   138  type Settings interface {
   139  	Map() params.Settings
   140  	Set(string, string)
   141  	Delete(string)
   142  }
   143  
   144  // newRelationIdValue returns a gnuflag.Value for convenient parsing of relation
   145  // ids in ctx.
   146  func newRelationIdValue(ctx Context, result *int) *relationIdValue {
   147  	v := &relationIdValue{result: result, ctx: ctx}
   148  	id := -1
   149  	if r, found := ctx.HookRelation(); found {
   150  		id = r.Id()
   151  		v.value = r.FakeId()
   152  	}
   153  	*result = id
   154  	return v
   155  }
   156  
   157  // relationIdValue implements gnuflag.Value for use in relation commands.
   158  type relationIdValue struct {
   159  	result *int
   160  	ctx    Context
   161  	value  string
   162  }
   163  
   164  // String returns the current value.
   165  func (v *relationIdValue) String() string {
   166  	return v.value
   167  }
   168  
   169  // Set interprets value as a relation id, if possible, and returns an error
   170  // if it is not known to the system. The parsed relation id will be written
   171  // to v.result.
   172  func (v *relationIdValue) Set(value string) error {
   173  	trim := value
   174  	if idx := strings.LastIndex(trim, ":"); idx != -1 {
   175  		trim = trim[idx+1:]
   176  	}
   177  	id, err := strconv.Atoi(trim)
   178  	if err != nil {
   179  		return fmt.Errorf("invalid relation id")
   180  	}
   181  	if _, found := v.ctx.Relation(id); !found {
   182  		return fmt.Errorf("unknown relation id")
   183  	}
   184  	*v.result = id
   185  	v.value = value
   186  	return nil
   187  }