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