github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/resource/context/cmd/get.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cmd
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    12  )
    13  
    14  // GetCmdName is the name of the resource-get command.
    15  const GetCmdName = "resource-get"
    16  
    17  // NewGetCmd creates a new GetCmd for the given hook context.
    18  func NewGetCmd(c jujuc.ContextComponent) (*GetCmd, error) {
    19  	return &GetCmd{
    20  		compContext: c,
    21  	}, nil
    22  }
    23  
    24  // GetCmd provides the functionality of the resource-get command.
    25  type GetCmd struct {
    26  	cmd.CommandBase
    27  
    28  	compContext  jujuc.ContextComponent
    29  	resourceName string
    30  }
    31  
    32  // TODO(ericsnow) Also provide an indicator of whether or not
    33  // the resource has changed (in addition to the file path)?
    34  
    35  // Info implements cmd.Command.
    36  func (c GetCmd) Info() *cmd.Info {
    37  	return &cmd.Info{
    38  		Name:    GetCmdName,
    39  		Args:    "<resource name>",
    40  		Purpose: "get the path to the locally cached resource file",
    41  		Doc: `
    42  "resource-get" is used while a hook is running to get the local path
    43  to the file for the identified resource. This file is an fs-local copy,
    44  unique to the unit for which the hook is running. It is downloaded from
    45  the controller, if necessary.
    46  
    47  If "resource-get" for a resource has not been run before (for the unit)
    48  then the resource is downloaded from the controller at the revision
    49  associated with the unit's application. That file is stored in the unit's
    50  local cache. If "resource-get" *has* been run before then each
    51  subsequent run syncs the resource with the controller. This ensures
    52  that the revision of the unit-local copy of the resource matches the
    53  revision of the resource associated with the unit's application.
    54  
    55  Either way, the path provided by "resource-get" references the
    56  up-to-date file for the resource. Note that the resource may get
    57  updated on the controller for the application at any time, meaning the
    58  cached copy *may* be out of date at any time after you call
    59  "resource-get". Consequently, the command should be run at every
    60  point where it is critical that the resource be up to date.
    61  
    62  The "upgrade-charm" hook is useful for keeping your charm's resources
    63  on a unit up to date.  Run "resource-get" there for each of your
    64  charm's resources to do so. The hook fires whenever the the file for
    65  one of the application's resources changes on the controller (in addition
    66  to when the charm itself changes). That means it happens in response
    67  to "juju upgrade-charm" as well as to "juju push-resource".
    68  
    69  Note that the "upgrade-charm" hook does not run when the unit is
    70  started up. So be sure to run "resource-get" for your resources in the
    71  "install" hook (or "config-changed", etc.).
    72  
    73  Note that "resource-get" only provides an FS path to the resource file.
    74  It does not provide any information about the resource (e.g. revision).
    75  `,
    76  	}
    77  }
    78  
    79  // Init implements cmd.Command.
    80  func (c *GetCmd) Init(args []string) error {
    81  	if len(args) < 1 {
    82  		return errors.Errorf("missing required resource name")
    83  	} else if err := cmd.CheckEmpty(args[1:]); err != nil {
    84  		return errors.Trace(err)
    85  	}
    86  	c.resourceName = args[0]
    87  	return nil
    88  }
    89  
    90  // Run implements cmd.Command.
    91  func (c GetCmd) Run(ctx *cmd.Context) error {
    92  	hookContext, ok := c.compContext.(downloader)
    93  	if !ok {
    94  		return errors.Errorf("invalid component context")
    95  	}
    96  	filePath, err := hookContext.Download(c.resourceName)
    97  	if err != nil {
    98  		return errors.Annotate(err, "could not download resource")
    99  	}
   100  
   101  	if _, err := fmt.Fprintf(ctx.Stdout, filePath); err != nil {
   102  		return errors.Annotate(err, "could not write resource path to stdout")
   103  	}
   104  	return nil
   105  }
   106  
   107  type downloader interface {
   108  	Download(name string) (string, error)
   109  }