Skip to content

Commit

Permalink
Merge pull request #3 from ltamaster/add-user-from-projec
Browse files Browse the repository at this point in the history
Username can be set at project level.
  • Loading branch information
ltamaster authored May 7, 2018
2 parents 6fda802 + 36535b9 commit 9d29053
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

# Ignore Gradle GUI config
Expand Down
27 changes: 16 additions & 11 deletions contents/winrm-exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


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()

Expand Down Expand Up @@ -45,11 +44,17 @@

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

if "RD_OPTION_USERNAME" in os.environ:
if "RD_OPTION_USERNAME" in os.environ and os.getenv("RD_OPTION_USERNAME"):
#take user from job
username = os.getenv("RD_OPTION_USERNAME").strip('\'')
else:
username = args.username.strip('\'')

# take user from node
if "RD_NODE_USERNAME" in os.environ and os.getenv("RD_NODE_USERNAME"):
username = os.getenv("RD_NODE_USERNAME").strip('\'')
else:
# take user from project
if "RD_CONFIG_USERNAME" in os.environ and os.getenv("RD_CONFIG_USERNAME"):
username = os.getenv("RD_CONFIG_USERNAME").strip('\'')

if(debug):
print "------------------------------------------"
Expand All @@ -60,14 +65,14 @@


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

#print exec_command
session = winrm.Session(endpoint,
auth=(username, password),
transport=authentication)

if shell == "cmd":
result = session.run_cmd(exec_command)
Expand Down
34 changes: 21 additions & 13 deletions contents/winrm-filecopier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
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__(
Expand All @@ -21,6 +21,7 @@ def __init__(self, command, return_code, std_out='', std_err=''):
class WinRmError(RemoteCommandError):
pass


class CopyFiles(object):

def __init__(self, session):
Expand Down Expand Up @@ -78,7 +79,6 @@ def winrm_upload(


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')
Expand All @@ -91,8 +91,8 @@ def winrm_upload(
authentication = "basic"
transport = "http"
port = "5985"
nossl=False
debug=False
nossl = False
debug = False

if "RD_CONFIG_PASSWORD_STORAGE_PATH" in os.environ:
password = os.getenv("RD_CONFIG_PASSWORD_STORAGE_PATH")
Expand All @@ -109,21 +109,29 @@ def winrm_upload(
if "RD_CONFIG_NOSSL" in os.environ:
nossl = os.getenv("RD_CONFIG_NOSSL")

if "RD_OPTION_USERNAME" in os.environ:
if "RD_OPTION_USERNAME" in os.environ and os.getenv("RD_OPTION_USERNAME"):
#take user from job
username = os.getenv("RD_OPTION_USERNAME").strip('\'')
else:
username = args.username.strip('\'')

endpoint=transport+'://'+args.hostname+':'+port
# take user from node
if "RD_NODE_USERNAME" in os.environ and os.getenv("RD_NODE_USERNAME"):
username = os.getenv("RD_NODE_USERNAME").strip('\'')
else:
# take user from project
if "RD_CONFIG_USERNAME" in os.environ and os.getenv("RD_CONFIG_USERNAME"):
username = os.getenv("RD_CONFIG_USERNAME").strip('\'')

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

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

copy = CopyFiles(session)
copy.winrm_upload(args.destination,args.source)
24 changes: 20 additions & 4 deletions plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ providers:
plugin-type: script
script-interpreter: python -u
script-file: winrm-exec.py
script-args: ${node.username} ${node.hostname}
script-args: ${node.hostname}
config:
- name: authtype
title: Authentication Type
description: "Authentication Type"
type: Select
values: "basic,credssp,ntlm"
default: "plaintext"
default: "basic"
required: true
scope: Instance
renderingOptions:
Expand Down Expand Up @@ -67,6 +67,14 @@ providers:
renderingOptions:
groupName: Connection
instance-scope-node-attribute: "winrm-shell"
- name: username
title: Username
type: String
required: false
description: "Optional username. The username can be set at node level (using the attribute `username`) or at job level (using an input option called `username`)."
scope: Instance
renderingOptions:
groupName: Authentication
- name: password_storage_path
title: Password Storage Path
type: String
Expand All @@ -86,14 +94,14 @@ providers:
plugin-type: script
script-interpreter: python -u
script-file: winrm-filecopier.py
script-args: ${node.username} ${node.hostname} ${file-copy.file} ${file-copy.destination}
script-args: ${node.hostname} ${file-copy.file} ${file-copy.destination}
config:
- name: authtype
title: Authentication Type
description: "Authentication Type"
type: Select
values: "basic,credssp,ntlm"
default: "plaintext"
default: "basic"
required: true
scope: Instance
renderingOptions:
Expand Down Expand Up @@ -131,6 +139,14 @@ providers:
renderingOptions:
groupName: Connection
instance-scope-node-attribute: "winrm-port"
- name: username
title: Username
type: String
required: false
description: "Optional username. The username can be set at node level (using the attribute `username`) or at job level (using an input option called `username`)."
scope: Instance
renderingOptions:
groupName: Authentication
- name: password_storage_path
title: Password Storage Path
type: String
Expand Down

0 comments on commit 9d29053

Please sign in to comment.