Skip to content

Commit

Permalink
Merge pull request #4 from JustRiedy/communityImprovements
Browse files Browse the repository at this point in the history
Community improvements
  • Loading branch information
Justin Riedyk authored Oct 13, 2017
2 parents b9ca01d + ca03857 commit 48a85f8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Compatible with Rundeck 2.9.x+

## Features
Can run scripts or commands.
Supports PowerShell, CMD and WQL shells
Can avoid quoting problems (should be removed after core Rundeck fixes)
Supports PowerShell, CMD and WQL shells
Can copy files to Windows

### Installation
Expand All @@ -18,13 +17,14 @@ CentOS/RHEL: `yum install make ruby ruby-devel`
Install following gems:
`gem install winrm -v 2.2.3`
`gem install winrm-fs -v 1.0.2`
`gem install rubyntlm -v 0.6.2`

Download from the [releases page](https://github.com/NetDocuments/rd-winrm-plugin/releases).

Copy the `rd-winrm-plugin.zip` to the `libext/` directory for Rundeck. It must be named like `rd-winrm-plugin-x.x.x.zip`. There is no need to restart rundeck.

```bash
RD_WINRM='1.6.2'
RD_WINRM='1.7.0'
curl https://github.com/NetDocuments/rd-winrm-plugin/archive/$RD_WINRM.zip -o /var/lib/rundeck/libext/rd-winrm-plugin-$RD_WINRM.zip
```

Expand Down Expand Up @@ -60,8 +60,7 @@ Settings:

### Limitations
- Scripts in c:/tmp with .sh extension will be renamed into .ps1, .bat or .wql
- Quotes behaviour can be strange (we trying to fix rundeck core strange behaviour, so our own also not perfect)
- WQL execution never been tested :)
- Quotes behaviour can be strange, [Rundeck bug](https://github.com/rundeck/rundeck/issues/602)

### Troubleshooting
You may have some errors like ```WinRM::WinRMAuthorizationError```.
Expand Down
21 changes: 19 additions & 2 deletions contents/winrmcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
if File.exist?("#{ENV['RD_PLUGIN_BASE']}/winrmexe.rb") && !File.executable?("#{ENV['RD_PLUGIN_BASE']}/winrmexe.rb")
File.chmod(0764, "#{ENV['RD_PLUGIN_BASE']}/winrmexe.rb") # https://github.com/rundeck/rundeck/issues/1421
end
#---

# Wrapper for avoid unix style file copying then scripts run
# - not accept chmod call
Expand All @@ -52,7 +53,9 @@
dest = dest.gsub(/\.sh/, '.wql')
end
end
#---

# Build connection options
connections_opts = {
endpoint: endpoint
}
Expand Down Expand Up @@ -81,10 +84,24 @@
else
fail "Invalid authtype '#{auth}' specified, expected: negotiate, kerberos, plaintext, ssl."
end
#---

# Create session
winrm = WinRM::Connection.new(connections_opts)

file_manager = WinRM::FS::FileManager.new(winrm)
#---

## Upload file to host
file_manager.upload(file, dest)
begin
file_manager.upload(file, dest)
rescue => e
if ENV['RD_JOB_LOGLEVEL'] == 'DEBUG'
STDERR.print e.message + "\n"
STDERR.print e.backtrace.inspect
exit 1
else
STDERR.print e.message
exit 1
end
end
#---
50 changes: 37 additions & 13 deletions contents/winrmexe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@
if File.exist?("#{ENV['RD_PLUGIN_BASE']}/winrmcp.rb") && !File.executable?("#{ENV['RD_PLUGIN_BASE']}/winrmcp.rb")
File.chmod(0764, "#{ENV['RD_PLUGIN_BASE']}/winrmcp.rb")
end

# Wrapper to avoid strange and undocumented behavior of rundeck
# Should be deleted after rundeck fix
# https://github.com/rundeck/rundeck/issues/602
command = command.gsub(/'"'"'' /, '\'')
command = command.gsub(/ ''"'"'/, '\'')
command = command.gsub(/ '"/, '"')
command = command.gsub(/"' /, '"')
#---

# Wrapper for avoid unix style file copying then scripts run
# - not accept chmod call
Expand All @@ -66,7 +59,9 @@
command = command.gsub(/\.sh/, '.wql')
end
end
#---

# Output DEBUG messages
if ENV['RD_JOB_LOGLEVEL'] == 'DEBUG'
puts 'variables:'
puts "realm => #{realm}"
Expand Down Expand Up @@ -99,6 +94,7 @@ def stderr_text(stderr)
end
end

# Build connection options
connections_opts = {
endpoint: endpoint
}
Expand Down Expand Up @@ -127,25 +123,53 @@ def stderr_text(stderr)
else
fail "Invalid authtype '#{auth}' specified, expected: negotiate, kerberos, plaintext, ssl."
end
#---

# Create and connect session
winrm = WinRM::Connection.new(connections_opts)

shell_session = nil
case shell
when 'powershell'
shell_session = winrm.shell(:powershell)
result = shell_session.run(command)
begin
shell_session = winrm.shell(:powershell)
result = shell_session.run(command)
rescue => e
shell_session = nil
if ENV['RD_JOB_LOGLEVEL'] == 'DEBUG'
STDERR.print "Connection Failure: " + e.message + "\n"
STDERR.print e.backtrace.inspect
exit 1
else
STDERR.print "Connection Failure: " + e.message + "\n"
exit 1
end
end
when 'cmd'
shell_session = winrm.shell(:cmd)
result = shell_session.run(command)
begin
shell_session = winrm.shell(:cmd)
result = shell_session.run(command)
rescue => e
shell_session = nil
if ENV['RD_JOB_LOGLEVEL'] == 'DEBUG'
STDERR.print "Connection Failure: " + e.message + "\n"
STDERR.print e.backtrace.inspect
exit 2
else
STDERR.print "Connection Failure: " + e.message + "\n"
exit 2
end
end
when 'wql'
result = winrm.run_wql(command)
end
#---

# Organise output for return to Runeck
if shell_session != nil
STDERR.print stderr_text(result.stderr) if result.stderr != ''
STDOUT.print result.stdout
exit result.exitcode if result.exitcode !=0
else # WQL
STDOUT.print result
end
#---
6 changes: 2 additions & 4 deletions plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: rd-winrm-plugin
version: 1.6.2
rundeckPluginVersion: 1.0
version: 1.7.0
rundeckPluginVersion: 1.2
author: Volodymyr Babchynskyy
date: 26.10.2015
providers:
Expand All @@ -11,8 +11,6 @@ providers:
plugin-type: script
script-interpreter: ruby
script-file: winrmexe.rb
# script-args: ${config.realm} ${config.username} ${config.password} ${node.hostname} ${exec.command}
interpreter-args-quoted: true
config:
- name: krb5_realm
title: Kerberos Realm
Expand Down

0 comments on commit 48a85f8

Please sign in to comment.