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

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/cloudinit/sshinit/configure.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:
4
4
package sshinit
5
5
 
6
6
import (
7
 
        "encoding/base64"
8
7
        "fmt"
9
8
        "io"
10
9
        "strings"
22
21
        // Host is the host to configure, in the format [user@]hostname.
23
22
        Host string
24
23
 
 
24
        // Client is the SSH client to connect with.
 
25
        // If Client is nil, ssh.DefaultClient will be used.
 
26
        Client ssh.Client
 
27
 
25
28
        // Config is the cloudinit config to carry out.
26
29
        Config *cloudinit.Config
27
30
 
28
 
        // Stdin is required to respond to sudo prompts,
29
 
        // and must be a terminal (except in tests)
30
 
        Stdin io.Reader
31
 
 
32
 
        // Stdout is required to present sudo prompts to the user.
33
 
        Stdout io.Writer
34
 
 
35
31
        // Stderr is required to present bootstrap progress to the user.
36
32
        Stderr io.Writer
37
33
}
40
36
// and executes a script that carries out cloud-config.
41
37
func Configure(params ConfigureParams) error {
42
38
        logger.Infof("Provisioning machine agent on %s", params.Host)
43
 
        script, err := generateScript(params.Config)
 
39
        script, err := ConfigureScript(params.Config)
44
40
        if err != nil {
45
41
                return err
46
42
        }
47
 
        scriptBase64 := base64.StdEncoding.EncodeToString([]byte(script))
48
 
        script = fmt.Sprintf(`F=$(mktemp); echo %s | base64 -d > $F; . $F`, scriptBase64)
49
 
        cmd := ssh.Command(
50
 
                params.Host,
51
 
                []string{"sudo", fmt.Sprintf("bash -c '%s'", script)},
52
 
                ssh.AllocateTTY,
53
 
        )
54
 
        cmd.Stdout = params.Stdout
 
43
        logger.Debugf("running script on %s: %s", params.Host, script)
 
44
        client := params.Client
 
45
        if client == nil {
 
46
                client = ssh.DefaultClient
 
47
        }
 
48
        cmd := ssh.Command(params.Host, []string{"sudo", "/bin/bash"}, nil)
 
49
        cmd.Stdin = strings.NewReader(script)
55
50
        cmd.Stderr = params.Stderr
56
 
        cmd.Stdin = params.Stdin
57
51
        return cmd.Run()
58
52
}
59
53
 
60
 
// generateScript generates the script that applies
 
54
// ConfigureScript generates the bash script that applies
61
55
// the specified cloud-config.
62
 
func generateScript(cloudcfg *cloudinit.Config) (string, error) {
 
56
func ConfigureScript(cloudcfg *cloudinit.Config) (string, error) {
63
57
        // TODO(axw): 2013-08-23 bug 1215777
64
58
        // Carry out configuration for ssh-keys-per-user,
65
59
        // machine-updates-authkeys, using cloud-init config.