~ubuntu-branches/ubuntu/trusty/juju-core/trusty-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/environs/sshstorage/storage.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-29 11:40:20 UTC
  • mfrom: (23.1.1 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20140129114020-ejieitm8smtt5vln
Tags: 1.17.1-0ubuntu2
d/tests/local-provider: Don't fail tests if ~/.juju is present as its
created by the juju version command. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
        "fmt"
12
12
        "io"
13
13
        "io/ioutil"
14
 
        "os/exec"
15
14
        "path"
16
15
        "sort"
17
16
        "strconv"
37
36
        remotepath string
38
37
        tmpdir     string
39
38
 
40
 
        cmd     *exec.Cmd
 
39
        cmd     *ssh.Cmd
41
40
        stdin   io.WriteCloser
42
41
        stdout  io.ReadCloser
43
42
        scanner *bufio.Scanner
44
43
}
45
44
 
46
 
var sshCommand = func(host string, tty bool, command string) *exec.Cmd {
47
 
        var options []ssh.Option
48
 
        if tty {
49
 
                options = append(options, ssh.AllocateTTY)
50
 
        }
51
 
        return ssh.Command(host, []string{command}, options...)
 
45
var sshCommand = func(host string, command ...string) *ssh.Cmd {
 
46
        return ssh.Command(host, command, nil)
52
47
}
53
48
 
54
49
type flockmode string
73
68
        // will attempt to reassign ownership to the login user, and will return
74
69
        // an error if it cannot do so.
75
70
        TmpDir string
76
 
 
77
 
        // Stdin in required to respond to sudo passwords,
78
 
        // and must be a terminal (except in tests).
79
 
        Stdin io.Reader
80
 
 
81
 
        // Stdout is required to present sudo prompts to the user.
82
 
        Stdout io.Writer
83
71
}
84
72
 
85
73
// NewSSHStorage creates a new SSHStorage, connected to the
98
86
                utils.ShQuote(params.TmpDir),
99
87
        )
100
88
 
101
 
        cmd := sshCommand(params.Host, true, fmt.Sprintf("sudo bash -c %s", utils.ShQuote(script)))
102
 
        cmd.Stdin = params.Stdin
103
 
        cmd.Stdout = params.Stdout // for sudo prompts/output
 
89
        cmd := sshCommand(params.Host, "sudo", "-n", "/bin/bash")
104
90
        var stderr bytes.Buffer
105
91
        cmd.Stderr = &stderr
 
92
        cmd.Stdin = strings.NewReader(script)
106
93
        if err := cmd.Run(); err != nil {
107
94
                err = fmt.Errorf("failed to create storage dir: %v (%v)", err, strings.TrimSpace(stderr.String()))
108
95
                return nil, err
111
98
        // We could use sftp, but then we'd be at the mercy of
112
99
        // sftp's output messages for checking errors. Instead,
113
100
        // we execute an interactive bash shell.
114
 
        cmd = sshCommand(params.Host, false, "bash")
 
101
        cmd = sshCommand(params.Host, "bash")
115
102
        stdin, err := cmd.StdinPipe()
116
103
        if err != nil {
117
104
                return nil, err