github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/worker/uniter/op_callbacks.go (about)

     1  // Copyright 2012-2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/names"
    11  	corecharm "gopkg.in/juju/charm.v5"
    12  	"gopkg.in/juju/charm.v5/hooks"
    13  
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/worker/uniter/charm"
    16  	"github.com/juju/juju/worker/uniter/hook"
    17  	"github.com/juju/juju/worker/uniter/runner"
    18  )
    19  
    20  // operationCallbacks implements operation.Callbacks, and exists entirely to
    21  // keep those methods off the Uniter itself.
    22  type operationCallbacks struct {
    23  	u *Uniter
    24  }
    25  
    26  // PrepareHook is part of the operation.Callbacks interface.
    27  func (opc *operationCallbacks) PrepareHook(hi hook.Info) (string, error) {
    28  	name := string(hi.Kind)
    29  	switch {
    30  	case hi.Kind.IsRelation():
    31  		var err error
    32  		name, err = opc.u.relations.PrepareHook(hi)
    33  		if err != nil {
    34  			return "", err
    35  		}
    36  	case hi.Kind.IsStorage():
    37  		if err := opc.u.storage.ValidateHook(hi); err != nil {
    38  			return "", err
    39  		}
    40  		storageName, err := names.StorageName(hi.StorageId)
    41  		if err != nil {
    42  			return "", err
    43  		}
    44  		name = fmt.Sprintf("%s-%s", storageName, hi.Kind)
    45  		// TODO(axw) if the agent is not installed yet,
    46  		// set the status to "preparing storage".
    47  	case hi.Kind == hooks.ConfigChanged:
    48  		opc.u.f.DiscardConfigEvent()
    49  	case hi.Kind == hook.LeaderSettingsChanged:
    50  		opc.u.f.DiscardLeaderSettingsEvent()
    51  	}
    52  	return name, nil
    53  }
    54  
    55  // CommitHook is part of the operation.Callbacks interface.
    56  func (opc *operationCallbacks) CommitHook(hi hook.Info) error {
    57  	switch {
    58  	case hi.Kind.IsRelation():
    59  		return opc.u.relations.CommitHook(hi)
    60  	case hi.Kind.IsStorage():
    61  		return opc.u.storage.CommitHook(hi)
    62  	case hi.Kind == hooks.ConfigChanged:
    63  		opc.u.ranConfigChanged = true
    64  	case hi.Kind == hook.LeaderSettingsChanged:
    65  		opc.u.ranLeaderSettingsChanged = true
    66  	}
    67  	return nil
    68  }
    69  
    70  // UpdateRelations is part of the operation.Callbacks interface.
    71  func (opc *operationCallbacks) UpdateRelations(ids []int) error {
    72  	return opc.u.relations.Update(ids)
    73  }
    74  
    75  func notifyHook(hook string, ctx runner.Context, method func(string)) {
    76  	if r, ok := ctx.HookRelation(); ok {
    77  		remote, _ := ctx.RemoteUnitName()
    78  		if remote != "" {
    79  			remote = " " + remote
    80  		}
    81  		hook = hook + remote + " " + r.FakeId()
    82  	}
    83  	method(hook)
    84  }
    85  
    86  // NotifyHookCompleted is part of the operation.Callbacks interface.
    87  func (opc *operationCallbacks) NotifyHookCompleted(hook string, ctx runner.Context) {
    88  	if opc.u.observer != nil {
    89  		notifyHook(hook, ctx, opc.u.observer.HookCompleted)
    90  	}
    91  }
    92  
    93  // NotifyHookFailed is part of the operation.Callbacks interface.
    94  func (opc *operationCallbacks) NotifyHookFailed(hook string, ctx runner.Context) {
    95  	if opc.u.observer != nil {
    96  		notifyHook(hook, ctx, opc.u.observer.HookFailed)
    97  	}
    98  }
    99  
   100  // FailAction is part of the operation.Callbacks interface.
   101  func (opc *operationCallbacks) FailAction(actionId, message string) error {
   102  	if !names.IsValidAction(actionId) {
   103  		return errors.Errorf("invalid action id %q", actionId)
   104  	}
   105  	tag := names.NewActionTag(actionId)
   106  	err := opc.u.st.ActionFinish(tag, params.ActionFailed, nil, message)
   107  	if params.IsCodeNotFoundOrCodeUnauthorized(err) {
   108  		err = nil
   109  	}
   110  	return err
   111  }
   112  
   113  // GetArchiveInfo is part of the operation.Callbacks interface.
   114  func (opc *operationCallbacks) GetArchiveInfo(charmURL *corecharm.URL) (charm.BundleInfo, error) {
   115  	ch, err := opc.u.st.Charm(charmURL)
   116  	if err != nil {
   117  		return nil, errors.Trace(err)
   118  	}
   119  	return ch, nil
   120  }
   121  
   122  // SetCurrentCharm is part of the operation.Callbacks interface.
   123  func (opc *operationCallbacks) SetCurrentCharm(charmURL *corecharm.URL) error {
   124  	return opc.u.f.SetCharm(charmURL)
   125  }
   126  
   127  // ClearResolvedFlag is part of the operation.Callbacks interface.
   128  func (opc *operationCallbacks) ClearResolvedFlag() error {
   129  	return opc.u.f.ClearResolved()
   130  }
   131  
   132  // InitializeMetricsTimers is part of the operation.Callbacks interface.
   133  func (opc *operationCallbacks) InitializeMetricsTimers() error {
   134  	return opc.u.initializeMetricsTimers()
   135  }
   136  
   137  // SetExecutingStatus is part of the operation.Callbacks interface.
   138  func (opc *operationCallbacks) SetExecutingStatus(message string) error {
   139  	return setAgentStatus(opc.u, params.StatusExecuting, message, nil)
   140  }