github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/commands/add_sshkeys.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  
    10  	"github.com/juju/cmd"
    11  
    12  	"github.com/juju/juju/cmd/juju/block"
    13  	"github.com/juju/juju/cmd/modelcmd"
    14  )
    15  
    16  var usageAddSSHKeySummary = `
    17  Adds a public SSH key to a model.`[1:]
    18  
    19  var usageAddSSHKeyDetails = `
    20  Juju maintains a per-model cache of public SSH keys which it copies to
    21  each unit (including units already deployed). By default this includes the
    22  key of the user who created the model (assuming it is stored in the
    23  default location ~/.ssh/). Additional keys may be added with this command,
    24  quoting the entire public key as an argument.
    25  
    26  Examples:
    27      juju add-ssh-key "ssh-rsa qYfS5LieM79HIOr535ret6xy
    28      AAAAB3NzaC1yc2EAAAADAQA6fgBAAABAQCygc6Rc9XgHdhQqTJ
    29      Wsoj+I3xGrOtk21xYtKijnhkGqItAHmrE5+VH6PY1rVIUXhpTg
    30      pSkJsHLmhE29OhIpt6yr8vQSOChqYfS5LieM79HIOJEgJEzIqC
    31      52rCYXLvr/BVkd6yr4IoM1vpb/n6u9o8v1a0VUGfc/J6tQAcPR
    32      ExzjZUVsfjj8HdLtcFq4JLYC41miiJtHw4b3qYu7qm3vh4eCiK
    33      1LqLncXnBCJfjj0pADXaL5OQ9dmD3aCbi8KFyOEs3UumPosgmh
    34      VCAfjjHObWHwNQ/ZU2KrX1/lv/+lBChx2tJliqQpyYMiA3nrtS
    35      jfqQgZfjVF5vz8LESQbGc6+vLcXZ9KQpuYDt joe@ubuntu"
    36  
    37  For ease of use it is possible to use shell substitution to pass the key 
    38  to the command:
    39  
    40  juju add-ssh-key "$(cat ~/mykey.pub)"
    41  
    42  See also: 
    43      list-ssh-key
    44      remove-ssh-key
    45      import-ssh-key`[1:]
    46  
    47  // NewAddKeysCommand is used to add a new ssh key to a model.
    48  func NewAddKeysCommand() cmd.Command {
    49  	return modelcmd.Wrap(&addKeysCommand{})
    50  }
    51  
    52  // addKeysCommand is used to add a new authorized ssh key for a user.
    53  type addKeysCommand struct {
    54  	SSHKeysBase
    55  	user    string
    56  	sshKeys []string
    57  }
    58  
    59  // Info implements Command.Info.
    60  func (c *addKeysCommand) Info() *cmd.Info {
    61  	return &cmd.Info{
    62  		Name:    "add-ssh-key",
    63  		Args:    "<ssh key> ...",
    64  		Purpose: usageAddSSHKeySummary,
    65  		Doc:     usageAddSSHKeyDetails,
    66  		Aliases: []string{"add-ssh-keys"},
    67  	}
    68  }
    69  
    70  // Init implements Command.Init.
    71  func (c *addKeysCommand) Init(args []string) error {
    72  	switch len(args) {
    73  	case 0:
    74  		return errors.New("no ssh key specified")
    75  	default:
    76  		c.sshKeys = args
    77  	}
    78  	return nil
    79  }
    80  
    81  // Run implements Command.Run.
    82  func (c *addKeysCommand) Run(context *cmd.Context) error {
    83  	client, err := c.NewKeyManagerClient()
    84  	if err != nil {
    85  		return err
    86  	}
    87  	defer client.Close()
    88  	// TODO(alexisb) - currently keys are global which is not ideal.
    89  	// keymanager needs to be updated to allow keys per user
    90  	c.user = "admin"
    91  	results, err := client.AddKeys(c.user, c.sshKeys...)
    92  	if err != nil {
    93  		return block.ProcessBlockedError(err, block.BlockChange)
    94  	}
    95  	for i, result := range results {
    96  		if result.Error != nil {
    97  			fmt.Fprintf(context.Stderr, "cannot add key %q: %v\n", c.sshKeys[i], result.Error)
    98  		}
    99  	}
   100  	return nil
   101  }