github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cloudconfig/sshinit/configure.go (about)

     1  // Copyright 2013, 2015 Canonical Ltd.
     2  // Copyright 2015 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package sshinit
     6  
     7  import (
     8  	"io"
     9  	"strings"
    10  
    11  	"github.com/juju/loggo"
    12  
    13  	"github.com/juju/juju/cloudconfig/cloudinit"
    14  	"github.com/juju/juju/utils/ssh"
    15  )
    16  
    17  var logger = loggo.GetLogger("juju.cloudinit.sshinit")
    18  
    19  type ConfigureParams struct {
    20  	// Host is the host to configure, in the format [user@]hostname.
    21  	Host string
    22  
    23  	// Client is the SSH client to connect with.
    24  	// If Client is nil, ssh.DefaultClient will be used.
    25  	Client ssh.Client
    26  
    27  	// Config is the cloudinit config to carry out.
    28  	Config cloudinit.CloudConfig
    29  
    30  	// ProgressWriter is an io.Writer to which progress will be written,
    31  	// for realtime feedback.
    32  	ProgressWriter io.Writer
    33  
    34  	// Series is the series of the machine on which the script will be carried out
    35  	Series string
    36  }
    37  
    38  // Configure connects to the specified host over SSH,
    39  // and executes a script that carries out cloud-config.
    40  // This isn't actually used anywhere because everybody wants to add custom stuff
    41  // in between getting the script and actually running it
    42  // I really suggest deleting it
    43  func Configure(params ConfigureParams) error {
    44  	logger.Infof("Provisioning machine agent on %s", params.Host)
    45  	script, err := params.Config.RenderScript()
    46  	if err != nil {
    47  		return err
    48  	}
    49  	return RunConfigureScript(script, params)
    50  }
    51  
    52  // RunConfigureScript connects to the specified host over
    53  // SSH, and executes the provided script which is expected
    54  // to have been returned by cloudinit ConfigureScript.
    55  func RunConfigureScript(script string, params ConfigureParams) error {
    56  	logger.Tracef("Running script on %s: %s", params.Host, script)
    57  	cmd := ssh.Command(params.Host, []string{"sudo", "/bin/bash"}, nil)
    58  	cmd.Stdin = strings.NewReader(script)
    59  	cmd.Stderr = params.ProgressWriter
    60  	return cmd.Run()
    61  }