2015-06-14 01:00:40 -04:00
|
|
|
package winrm
|
|
|
|
|
|
|
|
import (
|
2017-04-26 12:15:30 -04:00
|
|
|
"encoding/base64"
|
2015-06-14 01:00:40 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-04-26 12:15:30 -04:00
|
|
|
"io/ioutil"
|
2015-06-14 01:00:40 -04:00
|
|
|
"log"
|
2015-06-14 01:05:48 -04:00
|
|
|
"os"
|
2017-01-22 00:45:08 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2015-10-14 11:25:39 -04:00
|
|
|
"sync"
|
2015-06-14 01:00:40 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-10-01 16:34:42 -04:00
|
|
|
"github.com/masterzen/winrm"
|
2015-06-14 01:00:40 -04:00
|
|
|
"github.com/packer-community/winrmcp/winrmcp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Communicator represents the WinRM communicator
|
|
|
|
type Communicator struct {
|
|
|
|
config *Config
|
|
|
|
client *winrm.Client
|
|
|
|
endpoint *winrm.Endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new communicator implementation over WinRM.
|
|
|
|
func New(config *Config) (*Communicator, error) {
|
|
|
|
endpoint := &winrm.Endpoint{
|
2016-01-12 21:28:20 -05:00
|
|
|
Host: config.Host,
|
|
|
|
Port: config.Port,
|
|
|
|
HTTPS: config.Https,
|
|
|
|
Insecure: config.Insecure,
|
2015-06-14 01:00:40 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
TODO
|
|
|
|
HTTPS: connInfo.HTTPS,
|
|
|
|
Insecure: connInfo.Insecure,
|
|
|
|
CACert: connInfo.CACert,
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the client
|
2016-10-05 19:43:38 -04:00
|
|
|
params := *winrm.DefaultParameters
|
2016-03-10 13:47:30 -05:00
|
|
|
|
|
|
|
if config.TransportDecorator != nil {
|
|
|
|
params.TransportDecorator = config.TransportDecorator
|
|
|
|
}
|
|
|
|
|
2015-06-14 01:00:40 -04:00
|
|
|
params.Timeout = formatDuration(config.Timeout)
|
|
|
|
client, err := winrm.NewClientWithParameters(
|
2016-10-05 19:43:38 -04:00
|
|
|
endpoint, config.Username, config.Password, ¶ms)
|
2015-06-14 01:00:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the shell to verify the connection
|
|
|
|
log.Printf("[DEBUG] connecting to remote shell using WinRM")
|
|
|
|
shell, err := client.CreateShell()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] connection error: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := shell.Close(); err != nil {
|
|
|
|
log.Printf("[ERROR] error closing connection: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Communicator{
|
|
|
|
config: config,
|
|
|
|
client: client,
|
|
|
|
endpoint: endpoint,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implementation of communicator.Communicator interface
|
|
|
|
func (c *Communicator) Start(rc *packer.RemoteCmd) error {
|
|
|
|
shell, err := c.client.CreateShell()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] starting remote command: %s", rc.Command)
|
|
|
|
cmd, err := shell.Execute(rc.Command)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go runCommand(shell, cmd, rc)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCommand(shell *winrm.Shell, cmd *winrm.Command, rc *packer.RemoteCmd) {
|
|
|
|
defer shell.Close()
|
2015-10-14 11:25:39 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
copyFunc := func(w io.Writer, r io.Reader) {
|
|
|
|
defer wg.Done()
|
|
|
|
io.Copy(w, r)
|
|
|
|
}
|
2015-06-14 01:00:40 -04:00
|
|
|
|
2015-07-15 15:26:19 -04:00
|
|
|
if rc.Stdout != nil && cmd.Stdout != nil {
|
2015-10-14 12:08:39 -04:00
|
|
|
wg.Add(1)
|
2015-10-14 11:25:39 -04:00
|
|
|
go copyFunc(rc.Stdout, cmd.Stdout)
|
2015-07-15 15:29:42 -04:00
|
|
|
} else {
|
|
|
|
log.Printf("[WARN] Failed to read stdout for command '%s'", rc.Command)
|
2015-07-15 15:26:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if rc.Stderr != nil && cmd.Stderr != nil {
|
2015-10-14 12:08:39 -04:00
|
|
|
wg.Add(1)
|
2015-10-14 11:25:39 -04:00
|
|
|
go copyFunc(rc.Stderr, cmd.Stderr)
|
2015-07-15 15:29:42 -04:00
|
|
|
} else {
|
|
|
|
log.Printf("[WARN] Failed to read stderr for command '%s'", rc.Command)
|
2015-07-15 15:26:19 -04:00
|
|
|
}
|
2015-06-14 01:00:40 -04:00
|
|
|
|
|
|
|
cmd.Wait()
|
2015-10-14 11:25:39 -04:00
|
|
|
wg.Wait()
|
2015-06-14 14:19:26 -04:00
|
|
|
|
|
|
|
code := cmd.ExitCode()
|
|
|
|
log.Printf("[INFO] command '%s' exited with code: %d", rc.Command, code)
|
|
|
|
rc.SetExited(code)
|
2015-06-14 01:00:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upload implementation of communicator.Communicator interface
|
2018-07-11 16:30:20 -04:00
|
|
|
func (c *Communicator) Upload(path string, input io.Reader, _ *os.FileInfo) error {
|
2015-06-14 01:00:40 -04:00
|
|
|
wcp, err := c.newCopyClient()
|
2017-10-03 18:35:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Was unable to create winrm client: %s", err)
|
|
|
|
}
|
2015-06-14 01:00:40 -04:00
|
|
|
log.Printf("Uploading file to '%s'", path)
|
|
|
|
return wcp.Write(path, input)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadDir implementation of communicator.Communicator interface
|
2015-06-14 01:05:48 -04:00
|
|
|
func (c *Communicator) UploadDir(dst string, src string, exclude []string) error {
|
2017-01-22 00:45:08 -05:00
|
|
|
if !strings.HasSuffix(src, "/") {
|
|
|
|
dst = fmt.Sprintf("%s\\%s", dst, filepath.Base(src))
|
|
|
|
}
|
2015-06-14 01:00:40 -04:00
|
|
|
log.Printf("Uploading dir '%s' to '%s'", src, dst)
|
|
|
|
wcp, err := c.newCopyClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return wcp.Copy(src, dst)
|
|
|
|
}
|
|
|
|
|
2015-06-14 01:05:48 -04:00
|
|
|
func (c *Communicator) Download(src string, dst io.Writer) error {
|
2018-02-06 19:53:26 -05:00
|
|
|
client, err := c.newWinRMClient()
|
2017-04-26 12:15:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
encodeScript := `$file=[System.IO.File]::ReadAllBytes("%s"); Write-Output $([System.Convert]::ToBase64String($file))`
|
|
|
|
|
|
|
|
base64DecodePipe := &Base64Pipe{w: dst}
|
|
|
|
|
|
|
|
cmd := winrm.Powershell(fmt.Sprintf(encodeScript, src))
|
|
|
|
_, err = client.Run(cmd, base64DecodePipe, ioutil.Discard)
|
|
|
|
|
|
|
|
return err
|
2015-06-14 01:05:48 -04:00
|
|
|
}
|
|
|
|
|
2015-11-02 06:22:52 -05:00
|
|
|
func (c *Communicator) DownloadDir(src string, dst string, exclude []string) error {
|
|
|
|
return fmt.Errorf("WinRM doesn't support download dir.")
|
|
|
|
}
|
|
|
|
|
2018-02-06 19:53:26 -05:00
|
|
|
func (c *Communicator) getClientConfig() *winrmcp.Config {
|
|
|
|
return &winrmcp.Config{
|
2015-06-14 01:00:40 -04:00
|
|
|
Auth: winrmcp.Auth{
|
|
|
|
User: c.config.Username,
|
|
|
|
Password: c.config.Password,
|
|
|
|
},
|
2016-02-08 20:34:06 -05:00
|
|
|
Https: c.config.Https,
|
|
|
|
Insecure: c.config.Insecure,
|
2015-06-14 01:00:40 -04:00
|
|
|
OperationTimeout: c.config.Timeout,
|
|
|
|
MaxOperationsPerShell: 15, // lowest common denominator
|
2016-03-10 13:47:30 -05:00
|
|
|
TransportDecorator: c.config.TransportDecorator,
|
2018-02-06 19:53:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) newCopyClient() (*winrmcp.Winrmcp, error) {
|
|
|
|
addr := fmt.Sprintf("%s:%d", c.endpoint.Host, c.endpoint.Port)
|
|
|
|
clientConfig := c.getClientConfig()
|
|
|
|
return winrmcp.New(addr, clientConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) newWinRMClient() (*winrm.Client, error) {
|
|
|
|
conf := c.getClientConfig()
|
|
|
|
|
|
|
|
// Shamelessly borrowed from the winrmcp client to ensure
|
|
|
|
// that the client is configured using the same defaulting behaviors that
|
|
|
|
// winrmcp uses even we we aren't using winrmcp. This ensures similar
|
|
|
|
// behavior between upload, download, and copy functions. We can't use the
|
|
|
|
// one generated by winrmcp because it isn't exported.
|
|
|
|
var endpoint *winrm.Endpoint
|
|
|
|
endpoint = &winrm.Endpoint{
|
|
|
|
Host: c.endpoint.Host,
|
|
|
|
Port: c.endpoint.Port,
|
|
|
|
HTTPS: conf.Https,
|
|
|
|
Insecure: conf.Insecure,
|
|
|
|
TLSServerName: conf.TLSServerName,
|
|
|
|
CACert: conf.CACertBytes,
|
|
|
|
Timeout: conf.ConnectTimeout,
|
|
|
|
}
|
|
|
|
params := winrm.NewParameters(
|
|
|
|
winrm.DefaultParameters.Timeout,
|
|
|
|
winrm.DefaultParameters.Locale,
|
|
|
|
winrm.DefaultParameters.EnvelopeSize,
|
|
|
|
)
|
|
|
|
|
|
|
|
params.TransportDecorator = conf.TransportDecorator
|
|
|
|
params.Timeout = "PT3M"
|
|
|
|
|
|
|
|
client, err := winrm.NewClientWithParameters(
|
|
|
|
endpoint, conf.Auth.User, conf.Auth.Password, params)
|
|
|
|
return client, err
|
2015-06-14 01:00:40 -04:00
|
|
|
}
|
2017-04-26 12:15:30 -04:00
|
|
|
|
|
|
|
type Base64Pipe struct {
|
|
|
|
w io.Writer // underlying writer (file, buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Base64Pipe) ReadFrom(r io.Reader) (int64, error) {
|
|
|
|
b, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var i int
|
|
|
|
i, err = d.Write(b)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return int64(i), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Base64Pipe) Write(p []byte) (int, error) {
|
|
|
|
dst := make([]byte, base64.StdEncoding.DecodedLen(len(p)))
|
|
|
|
|
2017-04-26 12:15:57 -04:00
|
|
|
decodedBytes, err := base64.StdEncoding.Decode(dst, p)
|
|
|
|
if err != nil {
|
2017-04-26 12:15:30 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2017-04-26 12:15:57 -04:00
|
|
|
return d.w.Write(dst[0:decodedBytes])
|
2017-04-26 12:15:30 -04:00
|
|
|
}
|