diff --git a/.gitignore b/.gitignore index 35b2d00..4b3a147 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ ### Gradle ### .gradle +.idea /build/ # Ignore Gradle GUI config diff --git a/contents/winrm-exec.py b/contents/winrm-exec.py index 2e5858b..2278ee0 100644 --- a/contents/winrm-exec.py +++ b/contents/winrm-exec.py @@ -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() @@ -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 "------------------------------------------" @@ -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) diff --git a/contents/winrm-filecopier.py b/contents/winrm-filecopier.py index bbb51a7..c655bbc 100644 --- a/contents/winrm-filecopier.py +++ b/contents/winrm-filecopier.py @@ -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__( @@ -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): @@ -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') @@ -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") @@ -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) diff --git a/plugin.yaml b/plugin.yaml index 1ca9936..8e4cc2a 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -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: @@ -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 @@ -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: @@ -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