Skip to content

Commit

Permalink
Merge branch 'mefellows-feature/masterzen-pr'
Browse files Browse the repository at this point in the history
Merged PR #14 to ease development of the Packer WinRM communicator system
  • Loading branch information
Brice Figureau committed Nov 25, 2014
2 parents 5859cc0 + 63bb7d8 commit b7546c3
Show file tree
Hide file tree
Showing 25 changed files with 356 additions and 69 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

*.coverprofile
*.cov
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: go
go:
- 1.2
- 1.3
- tip
install:
- make updatedeps
- go build -o winrm-cli
before_install:
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- go get code.google.com/p/go.tools/cmd/cover
- go get github.com/modocache/gover
- go get gopkg.in/check.v1
script:
- make ci
after_script:
- $HOME/gopath/bin/goveralls -service="travis-ci" -coverprofile=profile.cov -repotoken $COVERALLS_TOKEN
matrix:
allow_failures:
- go: tip
env:
global:
secure: "GTrEtbp3sq14Jjz34pgIO0/Zv19YaDOOUJay4qnzGxs527HkW7YdsWENz0/yGHet+0jMOPatfP3uLaQHVFCNFZLfMYVmD5apMl7hPFrCaDZVvI9+ZwngIZ8gHzcf2Q+L6LxUT523ypjmRR+T1qYfbOy4UXlfGCyHFBbRB7AbMDk="
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ deps:
updatedeps:
@echo "$(OK_COLOR)==> Updating all dependencies$(NO_COLOR)"
@go get -d -v -u ./...
@echo $(DEPS) | xargs -n1 go get -d -u
@echo $(DEPS) | xargs -n1 go get -d -u -t

clean:
@rm -rf bin/ pkg/ src/

format:
go fmt ./...

ci: deps
@echo "$(OK_COLOR)==> Testing Packer with Coveralls...$(NO_COLOR)"
"$(CURDIR)/scripts/test.sh"

test: deps
@echo "$(OK_COLOR)==> Testing Packer...$(NO_COLOR)"
go test ./...
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ the use of WinRM/WinRS.

_Note_: this library doesn't support domain users (it doesn't support GSSAPI nor Kerberos). It's primary target is to execute remote commands on EC2 windows machines.

[![Build Status](https://travis-ci.org/masterzen/winrm.svg?branch=master)](https://travis-ci.org/masterzen/winrm)
[![Coverage Status](https://coveralls.io/repos/masterzen/winrm/badge.png)](https://coveralls.io/r/masterzen/winrm)

## Contact

- Bugs: https://github.com/masterzen/winrm/issues
Expand Down
25 changes: 25 additions & 0 deletions development/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.

# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "kensykora/windows_2012_r2_standard"

config.vm.guest = :windows
config.vm.communicator = "winrm"

config.vm.network :forwarded_port, guest: 5985, host: 5985, id: "winrm", auto_correct: true

config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--vram", "128"]
v.gui = true
end

end
144 changes: 144 additions & 0 deletions development/sample_requests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<!-- Create Shell -->
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
<env:Header>
<a:To>http://localhost:5985/wsman</a:To>
<a:ReplyTo>
<a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
</a:ReplyTo>
<w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
<w:OperationTimeout>PT60S</w:OperationTimeout>
<a:MessageID>uuid:c0fb2e4a-b066-4649-618b-5897f91acafd</a:MessageID>
<w:Locale mustUnderstand="false" xml:lang="en-US" />
<p:DataLocale mustUnderstand="false" xml:lang="en-US" />
<a:Action mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/09/transfer/Create</a:Action>
<w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
<w:OptionSet>
<w:Option Name="WINRS_NOPROFILE">FALSE</w:Option>
<w:Option Name="WINRS_CODEPAGE">437</w:Option>
</w:OptionSet>
</env:Header>
<env:Body>
<rsp:Shell>
<rsp:InputStreams>stdin</rsp:InputStreams>
<rsp:OutputStreams>stdout stderr</rsp:OutputStreams>
</rsp:Shell>
</env:Body>
</env:Envelope>

<!-- shellid: 0179DFE0-5409-4F07-946E-54635608A269 -->



<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
<env:Header>
<a:To>http://localhost:5985/wsman</a:To>
<a:ReplyTo>
<a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
</a:ReplyTo>
<w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
<w:OperationTimeout>PT60S</w:OperationTimeout>
<a:MessageID>uuid:05ea1914-c6dd-423d-5dac-c0e3b1ba056f</a:MessageID>
<w:Locale mustUnderstand="false" xml:lang="en-US" />
<p:DataLocale mustUnderstand="false" xml:lang="en-US" />
<a:Action mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command</a:Action>
<w:SelectorSet>
<w:Selector Name="ShellId">0179DFE0-5409-4F07-946E-54635608A269</w:Selector>
</w:SelectorSet>
<w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
<w:OptionSet>
<w:Option Name="WINRS_CONSOLEMODE_STDIN">TRUE</w:Option>
<w:Option Name="WINRS_SKIP_CMD_SHELL">FALSE</w:Option>
</w:OptionSet>
</env:Header>
<env:Body>
<rsp:CommandLine>
<rsp:Command>"powershell.exe -EncodedCommand ZGly"</rsp:Command>
</rsp:CommandLine>
</env:Body>
</env:Envelope>

<!-- command id: 98EA8A09-FCCF-43A9-8D76-AA678A2FDE80 -->



<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
<env:Header>
<a:To>http://localhost:5985/wsman</a:To>
<a:ReplyTo>
<a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
</a:ReplyTo>
<w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
<w:OperationTimeout>PT60S</w:OperationTimeout>
<a:MessageID>uuid:eccb3559-1a6e-44fc-5cfb-ab2e2a95300b</a:MessageID>
<w:Locale mustUnderstand="false" xml:lang="en-US" />
<p:DataLocale mustUnderstand="false" xml:lang="en-US" />
<a:Action mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive</a:Action>
<w:SelectorSet>
<w:Selector Name="ShellId">C64C758B-1A01-447D-A7BB-F85FD1E88148</w:Selector>
</w:SelectorSet>
<w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
</env:Header>
<env:Body>
<rsp:Receive>
<rsp:DesiredStream CommandId="92394CDB-903A-446C-BE25-1237E290BD1D">stdout stderr</rsp:DesiredStream>
</rsp:Receive>
</env:Body>
</env:Envelope>




<!-- Close shell -->
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
<env:Header>
<a:To>http://localhost:5985/wsman</a:To>
<a:ReplyTo>
<a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
</a:ReplyTo>
<w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
<w:OperationTimeout>PT60S</w:OperationTimeout>
<a:MessageID>uuid:ff1fcd4b-3f7a-4732-62e1-2afa9fbfd344</a:MessageID>
<w:Locale mustUnderstand="false" xml:lang="en-US" />
<p:DataLocale mustUnderstand="false" xml:lang="en-US" />
<a:Action mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete</a:Action>
<w:SelectorSet>
<w:Selector Name="ShellId">D24CE524-7FDD-481F-8824-87A2CC6A80AE</w:Selector>
</w:SelectorSet>
<w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
</env:Header>
<env:Body />
</env:Envelope>



<!-- Working Signal Request -->
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:cfg="http://schemas.microsoft.com/wbem/wsman/1/config" xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
<a:To>http://localhost:5985/wsman</a:To>
<a:ReplyTo>
<a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
</a:ReplyTo>
<w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
<a:MessageID>uuid:BE57E710-1E58-4955-AD2B-FF841ED3F52B</a:MessageID>
<w:Locale xml:lang="en-US" mustUnderstand="false" />
<p:DataLocale xml:lang="en-US" mustUnderstand="false" />
<w:OperationTimeout>PT5M0S</w:OperationTimeout>
<w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
<a:Action mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal</a:Action>
<w:SelectorSet>
<w:Selector Name="ShellId">0179DFE0-5409-4F07-946E-54635608A269</w:Selector>
</w:SelectorSet>
</env:Header>
<env:Body>
<rsp:Signal CommandId="98EA8A09-FCCF-43A9-8D76-AA678A2FDE80">
<rsp:Code>http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate</rsp:Code>
</rsp:Signal>
</env:Body>
</env:Envelope>


9 changes: 9 additions & 0 deletions development/winrm-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
echo "==> Running commands using WinRM against a Vagrant machine:"
echo


echo "==> What's my execution policy?"
winrm "powershell -command \"get-executionpolicy\""
echo "==> Whoami?"
winrm "whoami"
18 changes: 18 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
go test -v ./...

# Run test coverage on each subdirectories and merge the coverage profile.
echo "mode: count" > profile.cov

# Standard go tooling behavior is to ignore dirs with leading underscors
for dir in $(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -type d); do
if ls $dir/*.go &> /dev/null; then
go test -covermode=count -coverprofile=$dir/profile.tmp $dir
if [ -f $dir/profile.tmp ]; then
cat $dir/profile.tmp | tail -n +2 >> profile.cov
rm $dir/profile.tmp
fi
fi
done

go tool cover -func profile.cov
2 changes: 1 addition & 1 deletion soap/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package soap

import (
"github.com/masterzen/simplexml/dom"
. "launchpad.net/gocheck"
. "gopkg.in/check.v1"
"testing"
)

Expand Down
5 changes: 3 additions & 2 deletions winrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ func main() {

cmd = flag.Arg(0)
client := winrm.NewClient(&winrm.Endpoint{hostname, port}, user, pass)
exitCode, err := client.RunWithInput(cmd, os.Stdout, os.Stderr, os.Stdin)
err := client.RunWithInput(cmd, os.Stdout, os.Stderr, os.Stdin)

if err != nil {
fmt.Println(err)
os.Exit(1)
}

os.Exit(exitCode)
os.Exit(0)
}
8 changes: 4 additions & 4 deletions winrm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,20 @@ func (client *Client) RunWithString(command string, stdin string) (stdout string
// Warning stdin (not stdout/stderr) are bufferized, which means reading only one byte in stdin will
// send a winrm http packet to the remote host. If stdin is a pipe, it might be better for
// performance reasons to buffer it.
func (client *Client) RunWithInput(command string, stdout io.Writer, stderr io.Writer, stdin io.Reader) (exitCode int, err error) {
func (client *Client) RunWithInput(command string, stdout io.Writer, stderr io.Writer, stdin io.Reader) (err error) {
shell, err := client.CreateShell()
if err != nil {
return 1, err
return err
}
defer shell.Close()
var cmd *Command
cmd, err = shell.Execute(command)
if err != nil {
return 1, err
return err
}
go io.Copy(cmd.Stdin, stdin)
go io.Copy(stdout, cmd.Stdout)
go io.Copy(stderr, cmd.Stderr)
cmd.Wait()
return cmd.exitCode, nil
return nil
}
2 changes: 1 addition & 1 deletion winrm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package winrm

import (
"github.com/masterzen/winrm/soap"
. "launchpad.net/gocheck"
. "gopkg.in/check.v1"
)

func (s *WinRMSuite) TestNewClient(c *C) {
Expand Down
Loading

0 comments on commit b7546c3

Please sign in to comment.