github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/worker/uniter/operation/runcommands.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package operation
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/worker/uniter/runner"
    12  )
    13  
    14  type runCommands struct {
    15  	args         CommandArgs
    16  	sendResponse CommandResponseFunc
    17  
    18  	callbacks     Callbacks
    19  	runnerFactory runner.Factory
    20  
    21  	runner runner.Runner
    22  
    23  	RequiresMachineLock
    24  }
    25  
    26  // String is part of the Operation interface.
    27  func (rc *runCommands) String() string {
    28  	suffix := ""
    29  	if rc.args.RelationId != -1 {
    30  		infix := ""
    31  		if rc.args.RemoteUnitName != "" {
    32  			infix = "; " + rc.args.RemoteUnitName
    33  		}
    34  		suffix = fmt.Sprintf(" (%d%s)", rc.args.RelationId, infix)
    35  	}
    36  	return "run commands" + suffix
    37  }
    38  
    39  // Prepare ensures the commands can be run. It never returns a state change.
    40  // Prepare is part of the Operation interface.
    41  func (rc *runCommands) Prepare(state State) (*State, error) {
    42  	rnr, err := rc.runnerFactory.NewCommandRunner(runner.CommandInfo{
    43  		RelationId:      rc.args.RelationId,
    44  		RemoteUnitName:  rc.args.RemoteUnitName,
    45  		ForceRemoteUnit: rc.args.ForceRemoteUnit,
    46  	})
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	rc.runner = rnr
    51  	return nil, nil
    52  }
    53  
    54  // Execute runs the commands and dispatches their results. It never returns a
    55  // state change.
    56  // Execute is part of the Operation interface.
    57  func (rc *runCommands) Execute(state State) (*State, error) {
    58  	if err := rc.callbacks.SetExecutingStatus("running commands"); err != nil {
    59  		return nil, errors.Trace(err)
    60  	}
    61  
    62  	response, err := rc.runner.RunCommands(rc.args.Commands)
    63  	switch err {
    64  	case runner.ErrRequeueAndReboot:
    65  		logger.Warningf("cannot requeue external commands")
    66  		fallthrough
    67  	case runner.ErrReboot:
    68  		err = ErrNeedsReboot
    69  	}
    70  	rc.sendResponse(response, err)
    71  	return nil, nil
    72  }
    73  
    74  // Commit does nothing.
    75  // Commit is part of the Operation interface.
    76  func (rc *runCommands) Commit(state State) (*State, error) {
    77  	return nil, nil
    78  }