~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/commands/ssh.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012, 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package commands
 
5
 
 
6
import (
 
7
        "fmt"
 
8
 
 
9
        "github.com/juju/cmd"
 
10
        "github.com/juju/errors"
 
11
        "github.com/juju/utils/ssh"
 
12
 
 
13
        "github.com/juju/juju/cmd/modelcmd"
 
14
)
 
15
 
 
16
var usageSSHSummary = `
 
17
Initiates an SSH session or executes a command on a Juju machine.`[1:]
 
18
 
 
19
var usageSSHDetails = `
 
20
The machine is identified by the <target> argument which is either a 'unit
 
21
name' or a 'machine id'. Both are obtained in the output to "juju status". If
 
22
'user' is specified then the connection is made to that user account;
 
23
otherwise, the default 'ubuntu' account, created by Juju, is used.
 
24
 
 
25
The optional command is executed on the remote machine. Any output is sent back
 
26
to the user. Screen-based programs require the default of '--pty=true'.
 
27
 
 
28
The SSH host keys of the target are verified. The --no-host-key-checks option
 
29
can be used to disable these checks. Use of this option is not recommended as
 
30
it opens up the possibility of a man-in-the-middle attack.
 
31
 
 
32
Examples:
 
33
Connect to machine 0:
 
34
 
 
35
    juju ssh 0
 
36
 
 
37
Connect to machine 1 and run command 'uname -a':
 
38
 
 
39
    juju ssh 1 uname -a
 
40
 
 
41
Connect to a mysql unit:
 
42
 
 
43
    juju ssh mysql/0
 
44
 
 
45
Connect to a jenkins unit as user jenkins:
 
46
 
 
47
    juju ssh jenkins@jenkins/0
 
48
 
 
49
See also: 
 
50
    scp`
 
51
 
 
52
func newSSHCommand() cmd.Command {
 
53
        return modelcmd.Wrap(&sshCommand{})
 
54
}
 
55
 
 
56
// sshCommand is responsible for launching a ssh shell on a given unit or machine.
 
57
type sshCommand struct {
 
58
        SSHCommon
 
59
}
 
60
 
 
61
func (c *sshCommand) Info() *cmd.Info {
 
62
        return &cmd.Info{
 
63
                Name:    "ssh",
 
64
                Args:    "<[user@]target> [command]",
 
65
                Purpose: usageSSHSummary,
 
66
                Doc:     usageSSHDetails,
 
67
        }
 
68
}
 
69
 
 
70
func (c *sshCommand) Init(args []string) error {
 
71
        if len(args) == 0 {
 
72
                return fmt.Errorf("no target name specified")
 
73
        }
 
74
        c.Target, c.Args = args[0], args[1:]
 
75
        return nil
 
76
}
 
77
 
 
78
// Run resolves c.Target to a machine, to the address of a i
 
79
// machine or unit forks ssh passing any arguments provided.
 
80
func (c *sshCommand) Run(ctx *cmd.Context) error {
 
81
        err := c.initRun()
 
82
        if err != nil {
 
83
                return errors.Trace(err)
 
84
        }
 
85
        defer c.cleanupRun()
 
86
 
 
87
        target, err := c.resolveTarget(c.Target)
 
88
        if err != nil {
 
89
                return err
 
90
        }
 
91
 
 
92
        options, err := c.getSSHOptions(c.pty, target)
 
93
        if err != nil {
 
94
                return err
 
95
        }
 
96
 
 
97
        cmd := ssh.Command(target.userHost(), c.Args, options)
 
98
        cmd.Stdin = ctx.Stdin
 
99
        cmd.Stdout = ctx.Stdout
 
100
        cmd.Stderr = ctx.Stderr
 
101
        return cmd.Run()
 
102
}