github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 ssh-keys 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 } 67 } 68 69 // Init implements Command.Init. 70 func (c *addKeysCommand) Init(args []string) error { 71 switch len(args) { 72 case 0: 73 return errors.New("no ssh key specified") 74 default: 75 c.sshKeys = args 76 } 77 return nil 78 } 79 80 // Run implements Command.Run. 81 func (c *addKeysCommand) Run(context *cmd.Context) error { 82 client, err := c.NewKeyManagerClient() 83 if err != nil { 84 return err 85 } 86 defer client.Close() 87 // TODO(alexisb) - currently keys are global which is not ideal. 88 // keymanager needs to be updated to allow keys per user 89 c.user = "admin" 90 results, err := client.AddKeys(c.user, c.sshKeys...) 91 if err != nil { 92 return block.ProcessBlockedError(err, block.BlockChange) 93 } 94 for i, result := range results { 95 if result.Error != nil { 96 fmt.Fprintf(context.Stderr, "cannot add key %q: %v\n", c.sshKeys[i], result.Error) 97 } 98 } 99 return nil 100 }