Skip to content

Commit

Permalink
Python winrm plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ltamaster committed Dec 20, 2017
1 parent 83fc42b commit 9a92456
Show file tree
Hide file tree
Showing 10 changed files with 668 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Created by https://www.gitignore.io/api/java,gradle


### Gradle ###
.gradle
/build/

# Ignore Gradle GUI config
gradle-app.setting

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

# Cache of project
.gradletasknamecache

# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
# gradle/wrapper/gradle-wrapper.properties

# End of https://www.gitignore.io/api/java,gradle
38 changes: 38 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
buildscript {
repositories {
mavenCentral()
}
}
plugins {
id 'pl.allegro.tech.build.axion-release' version '1.7.0'
}

ext.pluginName = 'Python Winrm Node Executor/File Copier Plugin'
ext.pluginDescription = "Sincronize Azure Storage with a folder on remote nodes"
ext.sopsCopyright = "© 2017, Rundeck, Inc."
ext.sopsUrl = "http://rundeck.com"
ext.buildDateString=new Date().format("yyyy-MM-dd'T'HH:mm:ssX")
ext.archivesBaseName = "py-winrm-plugin"
ext.pluginBaseFolder = "."

scmVersion {
ignoreUncommittedChanges = true
tag {
prefix = ''
versionSeparator = ''
def origDeserialize=deserialize
//apend .0 to satisfy semver if the tag version is only X.Y
deserialize = { config, position, tagName ->
def orig = origDeserialize(config, position, tagName)
if (orig.split('\\.').length < 3) {
orig += ".0"
}
orig
}
}
}

project.version = scmVersion.version
ext.archiveFilename = ext.archivesBaseName + '-' + version

apply from: 'https://raw.githubusercontent.com/rundeck-plugins/build-zip/master/build.gradle'
76 changes: 76 additions & 0 deletions contents/winrm-exec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import winrm
import argparse
import os
import sys

from winrm.protocol import Protocol


parser = argparse.ArgumentParser(description='Run Bolt command.')
parser.add_argument('username', help='the username')
parser.add_argument('hostname', help='the hostname')
args = parser.parse_args()

password=None
authentication = "basic"
transport = "http"
port = "5985"
nossl=False
debug=False
shell = "cmd"

if "RD_CONFIG_PASSWORD_STORAGE_PATH" in os.environ:
password = os.getenv("RD_CONFIG_PASSWORD_STORAGE_PATH")

if "RD_CONFIG_AUTHTYPE" in os.environ:
authentication = os.getenv("RD_CONFIG_AUTHTYPE")

if "RD_CONFIG_WINRMTRANSPORT" in os.environ:
transport = os.getenv("RD_CONFIG_WINRMTRANSPORT")

if "RD_CONFIG_WINRMPORT" in os.environ:
port = os.getenv("RD_CONFIG_WINRMPORT")

if "RD_CONFIG_NOSSL" in os.environ:
nossl = os.getenv("RD_CONFIG_NOSSL")

if "RD_CONFIG_SHELL" in os.environ:
shell = os.getenv("RD_CONFIG_SHELL")

if os.getenv("RD_JOB_LOGLEVEL") == "DEBUG":
debug = True


exec_command = os.getenv("RD_EXEC_COMMAND")

endpoint=transport+'://'+args.hostname+':'+port

if(debug):
print "------------------------------------------"
print "endpoint:" +endpoint
print "authentication:" +authentication
print "username:" +args.username
print "------------------------------------------"


if(nossl):
session = winrm.Session(endpoint, auth=(args.username, password),
transport=authentication,
server_cert_validation='ignore')
else:
session = winrm.Session(endpoint, auth=(args.username, password),
transport=authentication)

#print exec_command

if shell == "cmd":
result = session.run_cmd(exec_command)

if shell == "powershell":
result = session.run_ps(exec_command)


print result.std_out
print result.std_err

sys.exit(result.status_code)
124 changes: 124 additions & 0 deletions contents/winrm-filecopier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import winrm
import argparse
import os
import sys
import base64
import time
from base64 import b64encode

from winrm.protocol import Protocol

class RemoteCommandError(Exception):
def __init__(self, command, return_code, std_out='', std_err=''):
super(RemoteCommandError, self).__init__(
'Remote execution of "{}" failed with exit code {}. '
'STDOUT: {}. STDERR: {}.'.format(
command, return_code, std_out, std_err
)
)


class WinRmError(RemoteCommandError):
pass

class CopyFiles(object):

def __init__(self, session):
self.session=session

def winrm_upload(
self,
remote_path,
local_path,
step=1024,
winrm_kwargs=dict(),
quiet=False,
**kwargs
):

self.session.run_ps('if (Test-Path {0}) {{ Remove-Item {0} }}'.format(remote_path))
size = os.stat(local_path).st_size
start = time.time()
with open(local_path, 'rb') as f:
for i in range(0, size, step):
script = (
'add-content -value '
'$([System.Convert]::FromBase64String("{}")) '
'-encoding byte -path {}'.format(
base64.b64encode(f.read(step)),
remote_path
)
)
while True:
result = self.session.run_ps(script)
code=result.status_code
stdout=result.std_out
stderr=result.std_err

if code == 0:
break
elif code == 1 and 'used by another process' in stderr:
# Small delay so previous write can settle down
time.sleep(0.1)
else:
raise WinRmError(script, code, stdout, stderr)
if not quiet:
transferred = i + step
if transferred > size:
transferred = size
progress_blocks = transferred * 30 // size
percentage_string = str(
(100 * transferred) // size
) + ' %'
percentage_string = (
' ' * (5 - len(percentage_string)) +
percentage_string
)
sys.stdout.flush()


parser = argparse.ArgumentParser(description='Run Bolt command.')
parser.add_argument('username', help='the username')
parser.add_argument('hostname', help='the hostname')
parser.add_argument('source', help='Source File')
parser.add_argument('destination', help='Destination File')
args = parser.parse_args()

#it is necesarry to avoid the debug error
print args.destination

password=None
authentication = "basic"
transport = "http"
port = "5985"
nossl=False
debug=False

if "RD_CONFIG_PASSWORD_STORAGE_PATH" in os.environ:
password = os.getenv("RD_CONFIG_PASSWORD_STORAGE_PATH")

if "RD_CONFIG_AUTHTYPE" in os.environ:
authentication = os.getenv("RD_CONFIG_AUTHTYPE")

if "RD_CONFIG_WINRMTRANSPORT" in os.environ:
transport = os.getenv("RD_CONFIG_WINRMTRANSPORT")

if "RD_CONFIG_WINRMPORT" in os.environ:
port = os.getenv("RD_CONFIG_WINRMPORT")

if "RD_CONFIG_NOSSL" in os.environ:
nossl = os.getenv("RD_CONFIG_NOSSL")

endpoint=transport+'://'+args.hostname+':'+port


if(nossl):
session = winrm.Session(endpoint, auth=(args.username, password),
transport=authentication,
server_cert_validation='ignore')
else:
session = winrm.Session(endpoint, auth=(args.username, password),
transport=authentication)

copy = CopyFiles(session)
copy.winrm_upload(args.destination,args.source)
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Tue Dec 05 14:39:55 CLST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip
Loading

0 comments on commit 9a92456

Please sign in to comment.