github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/commands/authorizedkeys_import.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 "launchpad.net/gnuflag" 12 13 "github.com/juju/juju/cmd/juju/block" 14 ) 15 16 var importKeysDoc = ` 17 Import new authorised ssh keys to allow the holder of those keys to log on to Juju nodes or machines. 18 The keys are imported using ssh-import-id. 19 ` 20 21 // ImportKeysCommand is used to add new authorized ssh keys for a user. 22 type ImportKeysCommand struct { 23 AuthorizedKeysBase 24 user string 25 sshKeyIds []string 26 } 27 28 func (c *ImportKeysCommand) Info() *cmd.Info { 29 return &cmd.Info{ 30 Name: "import", 31 Args: "<ssh key id> [...]", 32 Doc: importKeysDoc, 33 Purpose: "using ssh-import-id, import new authorized ssh keys for a Juju user", 34 } 35 } 36 37 func (c *ImportKeysCommand) Init(args []string) error { 38 switch len(args) { 39 case 0: 40 return errors.New("no ssh key id specified") 41 default: 42 c.sshKeyIds = args 43 } 44 return nil 45 } 46 47 func (c *ImportKeysCommand) SetFlags(f *gnuflag.FlagSet) { 48 f.StringVar(&c.user, "user", "admin", "the user for which to import the keys") 49 } 50 51 func (c *ImportKeysCommand) Run(context *cmd.Context) error { 52 client, err := c.NewKeyManagerClient() 53 if err != nil { 54 return err 55 } 56 defer client.Close() 57 58 results, err := client.ImportKeys(c.user, c.sshKeyIds...) 59 if err != nil { 60 return block.ProcessBlockedError(err, block.BlockChange) 61 } 62 for i, result := range results { 63 if result.Error != nil { 64 fmt.Fprintf(context.Stderr, "cannot import key id %q: %v\n", c.sshKeyIds[i], result.Error) 65 } 66 } 67 return nil 68 }