github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/cmd/juju/synctools.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/loggo"
     9  	"launchpad.net/gnuflag"
    10  
    11  	"github.com/juju/juju/cmd/envcmd"
    12  	"github.com/juju/juju/environs/filestorage"
    13  	"github.com/juju/juju/environs/sync"
    14  	"github.com/juju/juju/version"
    15  )
    16  
    17  var syncTools = sync.SyncTools
    18  
    19  // SyncToolsCommand copies all the tools from the us-east-1 bucket to the local
    20  // bucket.
    21  type SyncToolsCommand struct {
    22  	envcmd.EnvCommandBase
    23  	allVersions  bool
    24  	versionStr   string
    25  	majorVersion int
    26  	minorVersion int
    27  	dryRun       bool
    28  	dev          bool
    29  	public       bool
    30  	source       string
    31  	localDir     string
    32  	destination  string
    33  }
    34  
    35  var _ cmd.Command = (*SyncToolsCommand)(nil)
    36  
    37  func (c *SyncToolsCommand) Info() *cmd.Info {
    38  	return &cmd.Info{
    39  		Name:    "sync-tools",
    40  		Purpose: "copy tools from the official tool store into a local environment",
    41  		Doc: `
    42  This copies the Juju tools tarball from the official tools store (located
    43  at https://streams.canonical.com/juju) into your environment.
    44  This is generally done when you want Juju to be able to run without having to
    45  access the Internet. Alternatively you can specify a local directory as source.
    46  
    47  Sometimes this is because the environment does not have public access,
    48  and sometimes you just want to avoid having to access data outside of
    49  the local cloud.
    50  `,
    51  	}
    52  }
    53  
    54  func (c *SyncToolsCommand) SetFlags(f *gnuflag.FlagSet) {
    55  	f.BoolVar(&c.allVersions, "all", false, "copy all versions, not just the latest")
    56  	f.StringVar(&c.versionStr, "version", "", "copy a specific major[.minor] version")
    57  	f.BoolVar(&c.dryRun, "dry-run", false, "don't copy, just print what would be copied")
    58  	f.BoolVar(&c.dev, "dev", false, "consider development versions as well as released ones")
    59  	f.BoolVar(&c.public, "public", false, "tools are for a public cloud, so generate mirrors information")
    60  	f.StringVar(&c.source, "source", "", "local source directory")
    61  	f.StringVar(&c.localDir, "local-dir", "", "local destination directory")
    62  	f.StringVar(&c.destination, "destination", "", "local destination directory")
    63  }
    64  
    65  func (c *SyncToolsCommand) Init(args []string) error {
    66  	if c.destination != "" {
    67  		// Override localDir with destination as localDir now replaces destination
    68  		c.localDir = c.destination
    69  		logger.Warningf("Use of the --destination flag is deprecated in 1.18. Please use --local-dir instead.")
    70  	}
    71  	if c.versionStr != "" {
    72  		var err error
    73  		if c.majorVersion, c.minorVersion, err = version.ParseMajorMinor(c.versionStr); err != nil {
    74  			return err
    75  		}
    76  	}
    77  	return cmd.CheckEmpty(args)
    78  }
    79  
    80  func (c *SyncToolsCommand) Run(ctx *cmd.Context) (resultErr error) {
    81  	// Register writer for output on screen.
    82  	loggo.RegisterWriter("synctools", cmd.NewCommandLogWriter("juju.environs.sync", ctx.Stdout, ctx.Stderr), loggo.INFO)
    83  	defer loggo.RemoveWriter("synctools")
    84  	environ, cleanup, err := environFromName(ctx, c.EnvName, &resultErr, "Sync-tools")
    85  	if err != nil {
    86  		return err
    87  	}
    88  	defer cleanup()
    89  	target := environ.Storage()
    90  	if c.localDir != "" {
    91  		target, err = filestorage.NewFileStorageWriter(c.localDir)
    92  		if err != nil {
    93  			return err
    94  		}
    95  	}
    96  
    97  	// Prepare syncing.
    98  	sctx := &sync.SyncContext{
    99  		Target:       target,
   100  		AllVersions:  c.allVersions,
   101  		MajorVersion: c.majorVersion,
   102  		MinorVersion: c.minorVersion,
   103  		DryRun:       c.dryRun,
   104  		Dev:          c.dev,
   105  		Public:       c.public,
   106  		Source:       c.source,
   107  	}
   108  	return syncTools(sctx)
   109  }