github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	corecharm "gopkg.in/juju/charm.v6-unstable"
    11  	"gopkg.in/juju/charm.v6-unstable/hooks"
    12  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/status"
    16  	"github.com/juju/juju/worker/uniter/charm"
    17  	"github.com/juju/juju/worker/uniter/hook"
    18  	"github.com/juju/juju/worker/uniter/runner"
    19  )
    20  
    21  // operationCallbacks implements operation.Callbacks, and exists entirely to
    22  // keep those methods off the Uniter itself.
    23  type operationCallbacks struct {
    24  	u *Uniter
    25  }
    26  
    27  // PrepareHook is part of the operation.Callbacks interface.
    28  func (opc *operationCallbacks) PrepareHook(hi hook.Info) (string, error) {
    29  	name := string(hi.Kind)
    30  	switch {
    31  	case hi.Kind.IsRelation():
    32  		var err error
    33  		name, err = opc.u.relations.PrepareHook(hi)
    34  		if err != nil {
    35  			return "", err
    36  		}
    37  	case hi.Kind.IsStorage():
    38  		if err := opc.u.storage.ValidateHook(hi); err != nil {
    39  			return "", err
    40  		}
    41  		storageName, err := names.StorageName(hi.StorageId)
    42  		if err != nil {
    43  			return "", err
    44  		}
    45  		name = fmt.Sprintf("%s-%s", storageName, hi.Kind)
    46  		// TODO(axw) if the agent is not installed yet,
    47  		// set the status to "preparing storage".
    48  	case hi.Kind == hooks.ConfigChanged:
    49  		// TODO(axw)
    50  		//opc.u.f.DiscardConfigEvent()
    51  	case hi.Kind == hook.LeaderSettingsChanged:
    52  		// TODO(axw)
    53  		//opc.u.f.DiscardLeaderSettingsEvent()
    54  	}
    55  	return name, nil
    56  }
    57  
    58  // CommitHook is part of the operation.Callbacks interface.
    59  func (opc *operationCallbacks) CommitHook(hi hook.Info) error {
    60  	switch {
    61  	case hi.Kind.IsRelation():
    62  		return opc.u.relations.CommitHook(hi)
    63  	case hi.Kind.IsStorage():
    64  		return opc.u.storage.CommitHook(hi)
    65  	}
    66  	return nil
    67  }
    68  
    69  func notifyHook(hook string, ctx runner.Context, method func(string)) {
    70  	if r, err := ctx.HookRelation(); err == nil {
    71  		remote, _ := ctx.RemoteUnitName()
    72  		if remote != "" {
    73  			remote = " " + remote
    74  		}
    75  		hook = hook + remote + " " + r.FakeId()
    76  	}
    77  	method(hook)
    78  }
    79  
    80  // NotifyHookCompleted is part of the operation.Callbacks interface.
    81  func (opc *operationCallbacks) NotifyHookCompleted(hook string, ctx runner.Context) {
    82  	if opc.u.observer != nil {
    83  		notifyHook(hook, ctx, opc.u.observer.HookCompleted)
    84  	}
    85  }
    86  
    87  // NotifyHookFailed is part of the operation.Callbacks interface.
    88  func (opc *operationCallbacks) NotifyHookFailed(hook string, ctx runner.Context) {
    89  	if opc.u.observer != nil {
    90  		notifyHook(hook, ctx, opc.u.observer.HookFailed)
    91  	}
    92  }
    93  
    94  // FailAction is part of the operation.Callbacks interface.
    95  func (opc *operationCallbacks) FailAction(actionId, message string) error {
    96  	if !names.IsValidAction(actionId) {
    97  		return errors.Errorf("invalid action id %q", actionId)
    98  	}
    99  	tag := names.NewActionTag(actionId)
   100  	err := opc.u.st.ActionFinish(tag, params.ActionFailed, nil, message)
   101  	if params.IsCodeNotFoundOrCodeUnauthorized(err) {
   102  		err = nil
   103  	}
   104  	return err
   105  }
   106  
   107  // GetArchiveInfo is part of the operation.Callbacks interface.
   108  func (opc *operationCallbacks) GetArchiveInfo(charmURL *corecharm.URL) (charm.BundleInfo, error) {
   109  	ch, err := opc.u.st.Charm(charmURL)
   110  	if err != nil {
   111  		return nil, errors.Trace(err)
   112  	}
   113  	return ch, nil
   114  }
   115  
   116  // SetCurrentCharm is part of the operation.Callbacks interface.
   117  func (opc *operationCallbacks) SetCurrentCharm(charmURL *corecharm.URL) error {
   118  	return opc.u.unit.SetCharmURL(charmURL)
   119  }
   120  
   121  // SetExecutingStatus is part of the operation.Callbacks interface.
   122  func (opc *operationCallbacks) SetExecutingStatus(message string) error {
   123  	return setAgentStatus(opc.u, status.Executing, message, nil)
   124  }