Skip to content

Commit

Permalink
VPC: Improve Python3 Script
Browse files Browse the repository at this point in the history
  • Loading branch information
steelhead31 committed Aug 15, 2024
1 parent 04bbf55 commit 0627328
Showing 1 changed file with 54 additions and 39 deletions.
93 changes: 54 additions & 39 deletions ansible/pbTestScripts/startScriptWin_v2.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,73 @@
#!/usr/bin/python
#!/usr/bin/env python3

import sys
import getopt
import winrm
import logging

def usage():
print("Usage: %s -i <VM_IPAddress> -a <buildJDKWin_arguments>" % sys.argv[0])
def setup_logging():
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def print_usage():
print(f"Usage: {sys.argv[0]} -i <VM_IPAddress> -a <buildJDKWin_arguments>")
print(" Use '-b' to run a build or '-t' to run a test")
sys.exit(1)

def run_winrm(vmIP, buildArgs, mode):
cmd_str = "Start-Process powershell.exe -Verb runAs; cd C:/tmp; sh C:/vagrant/pbTestScripts/"
print(mode)
if mode == 1:
cmd_str += "buildJDKWin.sh "
else:
cmd_str += "testJDKWin.sh "
cmd_str += buildArgs
print("Running : %s" %cmd_str)
def run_command_over_winrm(vm_ip, command_args, mode):
command_base = "Start-Process powershell.exe -Verb runAs; cd C:/tmp; bash C:/vagrant/pbTestScripts/"
command = command_base + ("buildJDKWin.sh " if mode == 1 else "testJDKWin.sh ") + command_args

logging.info(f"Executing command: {command} on VM: {vm_ip}")

try:
session = winrm.Session(vm_ip, auth=('vagrant', 'vagrant'))
result = session.run_ps(command)

stdout = result.std_out.decode().strip()
stderr = result.std_err.decode().strip()

session = winrm.Session(str(vmIP), auth=('vagrant', 'vagrant'))
result = session.run_ps(cmd_str) # Only the command string is needed
if stdout:
logging.info(f"Command Output: {stdout}")
if stderr:
logging.error(f"Command Error: {stderr}")

# Print the output and error messages
print("Stdout: ", result.std_out.decode())
print("Stderr: ", result.std_err.decode())
return result.status_code
except Exception as e:
logging.error(f"Failed to execute command: {str(e)}")
sys.exit(1)

def main(argv):
# mode refers to whether it's running a build or a test
mode = 1
print("Running python script")
inputArgs = ""
ipAddress = ""
setup_logging()

mode = 1 # Default mode is build
command_args = ""
vm_ip_address = ""

try:
opts, args = getopt.getopt(argv, "ha:i:bt")
opts, _ = getopt.getopt(argv, "ha:i:bt")
except getopt.GetoptError as error:
print(str(error))
usage()

for current_option, current_value in opts:
if current_option == '-a':
inputArgs = current_value
elif current_option == '-i':
ipAddress = current_value
elif current_option == '-h':
usage()
elif current_option == '-b':
logging.error(f"Argument parsing error: {str(error)}")
print_usage()

for option, value in opts:
if option == '-a':
command_args = value
elif option == '-i':
vm_ip_address = value
elif option == '-h':
print_usage()
elif option == '-b':
mode = 1
elif current_option == '-t':
elif option == '-t':
mode = 2

print(" This is what is in the 'inputArgs' var: %s " %str(inputArgs))
print(" This is what is in the 'ipAddress' var: %s " %str(ipAddress))
run_winrm(str(ipAddress), str(inputArgs), mode)
if not vm_ip_address or not command_args:
logging.error("VM IP address and command arguments are required.")
print_usage()

logging.info(f"Command Arguments: {command_args}")
logging.info(f"VM IP Address: {vm_ip_address}")
run_command_over_winrm(vm_ip_address, command_args, mode)

if __name__ == "__main__": # Execute only if run as a script
if __name__ == "__main__":
main(sys.argv[1:])

0 comments on commit 0627328

Please sign in to comment.