github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/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/loggo"
     8  	"launchpad.net/gnuflag"
     9  
    10  	"launchpad.net/juju-core/cmd"
    11  	"launchpad.net/juju-core/environs/filestorage"
    12  	"launchpad.net/juju-core/environs/sync"
    13  	"launchpad.net/juju-core/version"
    14  )
    15  
    16  var syncTools = sync.SyncTools
    17  
    18  // SyncToolsCommand copies all the tools from the us-east-1 bucket to the local
    19  // bucket.
    20  type SyncToolsCommand struct {
    21  	cmd.EnvCommandBase
    22  	allVersions  bool
    23  	versionStr   string
    24  	majorVersion int
    25  	minorVersion int
    26  	dryRun       bool
    27  	dev          bool
    28  	public       bool
    29  	source       string
    30  	localDir     string
    31  	destination  string
    32  }
    33  
    34  var _ cmd.Command = (*SyncToolsCommand)(nil)
    35  
    36  func (c *SyncToolsCommand) Info() *cmd.Info {
    37  	return &cmd.Info{
    38  		Name:    "sync-tools",
    39  		Purpose: "copy tools from the official tool store into a local environment",
    40  		Doc: `
    41  This copies the Juju tools tarball from the official tools store (located
    42  at https://streams.canonical.com/juju) into your environment.
    43  This is generally done when you want Juju to be able to run without having to
    44  access the Internet. Alternatively you can specify a local directory as source.
    45  
    46  Sometimes this is because the environment does not have public access,
    47  and sometimes you just want to avoid having to access data outside of
    48  the local cloud.
    49  `,
    50  	}
    51  }
    52  
    53  func (c *SyncToolsCommand) SetFlags(f *gnuflag.FlagSet) {
    54  	c.EnvCommandBase.SetFlags(f)
    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  }