def __init__(self, cliJar, nexusiqUrl, nexusiqUserName, nexusiqPassword, nexusiqProxyUrl, nexusiqProxyUserName, nexusiqProxyPassword, app, stage, targetUrl): self.cmdLine = CmdLine() self.cmdLine.addArgument('java') self.cmdLine.addArgument('-jar') self.cmdLine.addArgument(cliJar) self.cmdLine.addArgument('--application-id') self.cmdLine.addArgument(app) self.cmdLine.addArgument('--server-url') self.cmdLine.addArgument(nexusiqUrl) self.cmdLine.addArgument('--authentication') self.cmdLine.addArgument("%s:%s" % (nexusiqUserName, nexusiqPassword)) if nexusiqProxyUrl: self.cmdLine.addArgument('--proxy') self.cmdLine.addArgument(nexusiqProxyUrl) self.cmdLine.addArgument( "%s:%s" % (nexusiqProxyUserName, nexusiqProxyPassword)) self.cmdLine.addArgument('--ignore-system-errors') self.cmdLine.addArgument('--stage') self.cmdLine.addArgument(stage) self.cmdLine.addArgument(targetUrl) self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler( ) self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler( )
def execute_knife_command(self, command, script_name, options=None, zip_workspace=False): connection = None try: options = Workstation.process_additional_options(options) connection = LocalConnection.getLocalConnection() workspace_path = self.create_chef_tmp_workspace(connection) knife_command = self.get_os_specific_knife_command( workspace_path, command, options) script_file = connection.getFile( OverthereUtils.constructPath( connection.getFile(workspace_path), script_name)) OverthereUtils.write(String(knife_command).getBytes(), script_file) script_file.setExecutable(True) if zip_workspace: self.zip_workspace(workspace_path, connection) command = CmdLine() command.addArgument(script_file.getPath()) output_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) error_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) exit_code = connection.execute(output_handler, error_handler, command) return [exit_code, output_handler, error_handler] except Exception: traceback.print_exc(file=sys.stdout) sys.exit(1) finally: if connection is not None: connection.close()
def zip_workspace(self, workspace_path, connection): zip_script = self.get_os_specific_zip_command(workspace_path) zip_script_file = connection.getFile(OverthereUtils.constructPath(connection.getFile(workspace_path), 'zip.cmd')) OverthereUtils.write(String(zip_script).getBytes(), zip_script_file) zip_script_file.setExecutable(True) command = CmdLine() command.addArgument(zip_script_file.getPath()) return connection.execute(command)
def __init__(self, workstation_config): self.cmd_line = CmdLine() self.chef_sdk_path = workstation_config['chef_sdk_path'] self.client_key = Workstation.format_key( workstation_config['client_key']) self.unix = (workstation_config['os'] == 'Unix') self.chef_server_url = workstation_config['chef_server_url'] self.node_name = workstation_config['node_name'] self.log_level = workstation_config['log_level'] self.log_location = workstation_config['log_location'] self.cookbook_path = workstation_config['cookbook_path'] self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler( ) self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler( )
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection(CifsConnectionBuilder.CIFS_PROTOCOL, self.options) connection.setWorkingDirectory(connection.getFile(self.remotePath)) # upload the script and pass it to powershell targetFile = connection.getTempFile("uploaded-powershell-script", ".ps1") OverthereUtils.write(String(self.script).getBytes(), targetFile) targetFile.setExecutable(True) scriptCommand = CmdLine.build( "powershell", "-NoLogo", "-NonInteractive", "-InputFormat", "None", "-ExecutionPolicy", "Unrestricted", "-Command", targetFile.getPath(), ) return connection.execute(self.stdout, self.stderr, scriptCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def __init__(self, cliHome, xldHost, xldPort, xldSecure, xldContext, xldProxyHost, xldProxyPort, xldSocketTimeout, xldUserName, xldPassword, script, cliExecutable, options): self.cmdLine = CmdLine() self.osname = System.getProperty('os.name').lower() if self.osname.startswith('win'): cliExecutable = "%s\\bin\\%s.cmd" % (cliHome, cliExecutable) else: cliExecutable = "%s/bin/%s.sh" % (cliHome, cliExecutable) # End if self.cmdLine.addArgument(cliExecutable) self.cmdLine.addArgument('-quiet') if xldHost != "DEFAULT": self.cmdLine.addArgument('-host') self.cmdLine.addArgument(xldHost) if xldPort != "DEFAULT": self.cmdLine.addArgument('-port') self.cmdLine.addArgument(xldPort) if xldSecure: self.cmdLine.addArgument('-secure') if xldContext != "DEFAULT": self.cmdLine.addArgument('-context') self.cmdLine.addArgument(xldContext) if xldProxyHost != "DEFAULT": self.cmdLine.addArgument('-proxyHost') self.cmdLine.addArgument(xldProxyHost) if xldProxyPort != "DEFAULT": self.cmdLine.addArgument('-proxyPort') self.cmdLine.addArgument(xldProxyPort) if xldSocketTimeout != "DEFAULT": self.cmdLine.addArgument('-socketTimeout') self.cmdLine.addArgument(xldSocketTimeout) if xldUserName != "DEFAULT": self.cmdLine.addArgument('-username') self.cmdLine.addArgument(xldUserName) if xldPassword != "DEFAULT": self.cmdLine.addArgument('-password') self.cmdLine.addPassword(xldPassword) if options is not None: self.options = str(options) # End if self.script = script self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler( ) self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler( )
def execute_command(connection, workspace_path, command): try: print "executing command: %s" % command script_file = connection.getFile(OverthereUtils.constructPath(connection.getFile(workspace_path), 'command.cmd')) OverthereUtils.write(String(command).getBytes(), script_file) script_file.setExecutable(True) command = CmdLine() command.addArgument(script_file.getPath()) output_handler = CapturingOverthereExecutionOutputHandler.capturingHandler() error_handler = CapturingOverthereExecutionOutputHandler.capturingHandler() exit_code = connection.execute(output_handler, error_handler, command) print "exit_code : %s" % exit_code print "output: %s" % output_handler.getOutput() print "errors: %s" % error_handler.getOutput() return [exit_code, output_handler, error_handler] except Exception: traceback.print_exc(file=sys.stdout) sys.exit(1)
def __init__( self, cliHome, xldHost, xldPort, xldContext, xldProxyHost, xldProxyPort, xldSocketTimeout, xldUserName, xldPassword, script, cliExecutable, options, ): self.cmdLine = CmdLine() self.osname = System.getProperty("os.name").lower() if self.osname.startswith("win"): cliExecutable = "%s\\bin\\%s.cmd" % (cliHome, cliExecutable) else: cliExecutable = "%s/bin/%s.sh" % (cliHome, cliExecutable) # End if self.cmdLine.addArgument(cliExecutable) self.cmdLine.addArgument("-quiet") if xldHost != "DEFAULT": self.cmdLine.addArgument("-host") self.cmdLine.addArgument(xldHost) if xldPort != "DEFAULT": self.cmdLine.addArgument("-port") self.cmdLine.addArgument(xldPort) if xldContext != "DEFAULT": self.cmdLine.addArgument("-context") self.cmdLine.addArgument(xldContext) if xldProxyHost != "DEFAULT": self.cmdLine.addArgument("-proxyHost") self.cmdLine.addArgument(xldProxyHost) if xldProxyPort != "DEFAULT": self.cmdLine.addArgument("-proxyPort") self.cmdLine.addArgument(xldProxyPort) if xldSocketTimeout != "DEFAULT": self.cmdLine.addArgument("-socketTimeout") self.cmdLine.addArgument(xldSocketTimeout) if xldUserName != "DEFAULT": self.cmdLine.addArgument("-username") self.cmdLine.addArgument(xldUserName) if xldPassword != "DEFAULT": self.cmdLine.addArgument("-password") self.cmdLine.addPassword(xldPassword) if options is not None: self.options = str(options) # End if self.script = script self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler() self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler()
def execute(self, cmd, check_success=True, suppress_streaming_output=False): """ Executes the command on the remote system and returns the result :param cmd: Command line as an Array of Strings or String. A String is split by space. :param check_success: checks the return code is 0. On failure the output is printed to stdout and a system exit is performed :param suppress_streaming_output: suppresses the output of the execution when the session is in streaming mode. :return: CommandResponse """ capture_so_handler = CapturingOverthereExecutionOutputHandler.capturingHandler() capture_se_handler = CapturingOverthereExecutionOutputHandler.capturingHandler() if self._stream_command_output and not suppress_streaming_output: console_so_handler = ConsoleOverthereExecutionOutputHandler.sysoutHandler() console_se_handler = ConsoleOverthereExecutionOutputHandler.syserrHandler() so_handler = MultipleOverthereExecutionOutputHandler.multiHandler([capture_so_handler, console_so_handler]) se_handler = MultipleOverthereExecutionOutputHandler.multiHandler([capture_se_handler, console_se_handler]) else: so_handler = capture_so_handler se_handler = capture_se_handler if isinstance(cmd, basestring): cmd = cmd.split() cmdline = CmdLine() for s in cmd: cmdline.addRaw(s) rc = self.get_conn().execute(so_handler, se_handler, cmdline) #wait for output to drain time.sleep(1) response = CommandResponse(rc=rc, stdout=capture_so_handler.outputLines, stderr=capture_se_handler.outputLines) if response.rc != 0 and check_success: if not suppress_streaming_output: mdl.print_error(StringUtils.strip_ansi(StringUtils.concat(response.stdout))) mdl.print_error(StringUtils.strip_ansi(StringUtils.concat(response.stderr))) raise Exception(response.rc) return response
def execute(self): connection = None try: connection = self.getConnection() # upload the script and pass it to python exeFile = connection.getTempFile('script', '.py') OverthereUtils.write(String(self.script).getBytes(), exeFile) exeFile.setExecutable(True) scriptCommand = CmdLine.build('/usr/bin/python', exeFile.getPath()) return connection.execute(self.stdout, self.stderr, scriptCommand) except AttributeError, e: self.logger.error(str(e))
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection(CifsConnectionBuilder.CIFS_PROTOCOL, self.options) connection.setWorkingDirectory(connection.getFile(self.TestPath)) # upload the ResultName and pass it to csResultName.exe ResultNameCommand = CmdLine.build(WlrunExe, "-Run", "-TestPath", TestPath, "-ResultName", ResultName) return connection.execute(self.stdout, self.stderr, ResultNameCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection(SshConnectionBuilder.CONNECTION_TYPE, self.options) # upload the script and pass it to python exeFile = connection.getTempFile('f5_disable', '.py') OverthereUtils.write(String(self.script).getBytes(), targetFile) exeFile.setExecutable(True) # run cscript in batch mode scriptCommand = CmdLine.build( '/usr/bin/python', exeFile.getPath() ) return connection.execute(self.stdout, self.stderr, scriptCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection(CifsConnectionBuilder.CIFS_PROTOCOL, self.options) connection.setWorkingDirectory(connection.getFile(self.remotePath)) # upload the script and pass it to cscript.exe targetFile = connection.getTempFile('uploaded-script', '.vbs') OverthereUtils.write(String(self.script).getBytes(), targetFile) targetFile.setExecutable(True) # run cscript in batch mode scriptCommand = CmdLine.build(cscriptExecutable, '//B', '//nologo', targetFile.getPath()) return connection.execute(self.stdout, self.stderr, scriptCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection( CifsConnectionBuilder.CIFS_PROTOCOL, self.options) connection.setWorkingDirectory(connection.getFile(self.remotePath)) # upload the script and pass it to cscript.exe targetFile = connection.getTempFile('uploaded-script', '.vbs') OverthereUtils.write(String(self.script).getBytes(), targetFile) targetFile.setExecutable(True) # run cscript in batch mode scriptCommand = CmdLine.build(cscriptExecutable, '//B', '//nologo', targetFile.getPath()) return connection.execute(self.stdout, self.stderr, scriptCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def __init__(self, cliHome, xldHost, xldPort, xldContext, xldProxyHost, xldProxyPort, xldSocketTimeout, xldUserName, xldPassword, script, cliExecutable, options): self.cmdLine = CmdLine() self.osname = System.getProperty('os.name').lower() if ( self.osname.startswith('win') ): cliExecutable = "%s\\bin\\%s.cmd" % ( cliHome, cliExecutable ) else: cliExecutable = "%s/bin/%s.sh" % ( cliHome, cliExecutable ) # End if self.cmdLine.addArgument( cliExecutable ) self.cmdLine.addArgument( '-quiet' ) if ( xldHost != "DEFAULT" ): self.cmdLine.addArgument( '-Host' ) self.cmdLine.addArgument( xldHost ) if ( xldPort != "DEFAULT" ): self.cmdLine.addArgument( '-Port' ) self.cmdLine.addArgument( xldPort ) if ( xldContext != "DEFAULT" ): self.cmdLine.addArgument( '-Context' ) self.cmdLine.addArgument( xldContext ) if ( xldProxyHost != "DEFAULT" ): self.cmdLine.addArgument( '-ProxyHost' ) self.cmdLine.addArgument( xldProxyHost ) if ( xldProxyPort != "DEFAULT" ): self.cmdLine.addArgument( '-ProxyPort' ) self.cmdLine.addArgument( xldProxyPort ) if ( xldSocketTimeout != "DEFAULT" ): self.cmdLine.addArgument( '-SocketTimeout' ) self.cmdLine.addArgument( xldSocketTimeout ) if ( xldUserName != "DEFAULT" ): self.cmdLine.addArgument( '-username' ) self.cmdLine.addArgument( xldUserName ) if ( xldPassword != "DEFAULT" ): self.cmdLine.addArgument( '-password' ) self.cmdLine.addPassword( xldPassword ) if ( options is not None ): self.options = str(options) # End if self.script = script self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler() self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler()
def local_execute(session, cmd): """ Executes the command on the remote system and returns the result :param session: checks the return code is 0. On failure the output is printed to stdout and a system exit is performed :param cmd: Command line as an Array of Strings or String. A String is split by space. :return: CommandResponse """ if isinstance(cmd, basestring): cmd = cmd.split() cmdline = CmdLine.build(cmd) capture_so_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) capture_se_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) console_so_handler = PyLoggerExecutionOutputHandler.sysoutHandler( session.logger) console_se_handler = PyLoggerExecutionOutputHandler.syserrHandler( session.logger) so_handler = MultipleOverthereExecutionOutputHandler.multiHandler( [capture_so_handler, console_so_handler]) se_handler = MultipleOverthereExecutionOutputHandler.multiHandler( [capture_se_handler, console_se_handler]) rc = session.get_conn().execute(so_handler, se_handler, cmdline) # wait for output to drain time.sleep(1) response = CommandResponse(rc=rc, stdout=capture_so_handler.outputLines, stderr=capture_se_handler.outputLines) if response.rc != 0: session.logger.error(StringUtils.concat(response.stdout)) session.logger.error(StringUtils.concat(response.stderr)) session.logger.error("Exit code {0}".format(response.rc)) sys.exit(response.rc) return response
def execute(self, cmd, check_success=True, suppress_streaming_output=False, wait_time=1): """ Executes the command on the remote system and returns the result :param cmd: Command line as an Array of Strings or String. A String is split by space. :param check_success: checks the return code is 0. On failure the output is printed to stdout and a system exit is performed :param suppress_streaming_output: suppresses the output of the execution when the session is in streaming mode. :param wait_time: wait time in second after launching the command and capture the result, default 1 second :return: CommandResponse """ if isinstance(cmd, basestring): import shlex cmd = shlex.split(cmd) cmdline = CmdLine.build(cmd) capture_so_handler = CapturingOverthereExecutionOutputHandler.capturingHandler() capture_se_handler = CapturingOverthereExecutionOutputHandler.capturingHandler() if self._stream_command_output and not suppress_streaming_output: console_so_handler = PyLoggerExecutionOutputHandler.sysoutHandler(self.logger) console_se_handler = PyLoggerExecutionOutputHandler.syserrHandler(self.logger) so_handler = MultipleOverthereExecutionOutputHandler.multiHandler([capture_so_handler, console_so_handler]) se_handler = MultipleOverthereExecutionOutputHandler.multiHandler([capture_se_handler, console_se_handler]) else: so_handler = capture_so_handler se_handler = capture_se_handler rc = self.get_conn().execute(so_handler, se_handler, cmdline) #wait for output to drain time.sleep(wait_time) response = CommandResponse(rc=rc, stdout=capture_so_handler.outputLines, stderr=capture_se_handler.outputLines) if response.rc != 0 and check_success: if not self._stream_command_output and suppress_streaming_output: self.logger.error(StringUtils.concat(response.stdout)) self.logger.error(StringUtils.concat(response.stderr)) sys.exit(response.rc) return response
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection(CifsConnectionBuilder.CIFS_PROTOCOL, self.options) connection.setWorkingDirectory(connection.getFile(self.remotePath)) # upload the script and pass it to cscript.exe targetFile = connection.getTempFile('parameters', '.txt') OverthereUtils.write(String(self.script).getBytes(), targetFile) targetFile.setExecutable(True) exeFile = connection.getTempFile('HpToolsLauncher', '.exe') sysloader = ClassLoader.getSystemClassLoader() OverthereUtils.write(sysloader.getResourceAsStream("HpTools/HpToolsLauncher.exe"), exeFile) exeFile.setExecutable(True) # run cscript in batch mode scriptCommand = CmdLine.build(exeFile.getPath(), '-paramfile', targetFile.getPath()) return connection.execute(self.stdout, self.stderr, scriptCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
class Localcliscript(): def __init__(self, cliHome, xldHost, xldPort, xldSecure, xldContext, xldProxyHost, xldProxyPort, xldSocketTimeout, xldUserName, xldPassword, script, cliExecutable, options): self.cmdLine = CmdLine() self.osname = System.getProperty('os.name').lower() if self.osname.startswith('win'): cliExecutable = "%s\\bin\\%s.cmd" % (cliHome, cliExecutable) else: cliExecutable = "%s/bin/%s.sh" % (cliHome, cliExecutable) # End if self.cmdLine.addArgument(cliExecutable) self.cmdLine.addArgument('-quiet') if xldHost != "DEFAULT": self.cmdLine.addArgument('-host') self.cmdLine.addArgument(xldHost) if xldPort != "DEFAULT": self.cmdLine.addArgument('-port') self.cmdLine.addArgument(xldPort) if xldSecure: self.cmdLine.addArgument('-secure') if xldContext != "DEFAULT": self.cmdLine.addArgument('-context') self.cmdLine.addArgument(xldContext) if xldProxyHost != "DEFAULT": self.cmdLine.addArgument('-proxyHost') self.cmdLine.addArgument(xldProxyHost) if xldProxyPort != "DEFAULT": self.cmdLine.addArgument('-proxyPort') self.cmdLine.addArgument(xldProxyPort) if xldSocketTimeout != "DEFAULT": self.cmdLine.addArgument('-socketTimeout') self.cmdLine.addArgument(xldSocketTimeout) if xldUserName != "DEFAULT": self.cmdLine.addArgument('-username') self.cmdLine.addArgument(xldUserName) if xldPassword != "DEFAULT": self.cmdLine.addArgument('-password') self.cmdLine.addPassword(xldPassword) if options is not None: self.options = str(options) # End if self.script = script self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler( ) self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler( ) # End __init__ def execute(self): connection = None try: connection = LocalConnection.getLocalConnection() scriptFile = connection.getTempFile('xlrScript', '.py') OverthereUtils.write(String(self.script).getBytes(), scriptFile) scriptFile.setExecutable(True) self.cmdLine.addArgument('-source') self.cmdLine.addArgument(scriptFile.getPath()) if len(self.options) > 1: self.cmdLine.addArgument('--') optionsList = self.options.split(' ') for opt in optionsList: self.cmdLine.addArgument(opt) # End for # End if exitCode = connection.execute(self.stdout, self.stderr, self.cmdLine) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1 finally:
def stash_downloadcode(self, variables): downloadURL = "%s/rest/archive/latest/projects/%s/repos/%s/archive?at=refs/heads/%s&format=zip" % ( variables['server']['url'], variables['project'], variables['repository'], variables['branch']) connection = LocalConnection.getLocalConnection() capturedOutput = "" self.logger.warn("Cleaning up download folder : %s" % variables['downloadPath']) command = CmdLine() command.addArgument("mkdir") command.addArgument("-p") command.addArgument(variables['downloadPath']) output_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) error_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) exit_code = connection.execute(output_handler, error_handler, command) capturedOutput = self.parse_output( output_handler.getOutputLines()) + self.parse_output( error_handler.getOutputLines()) self.logger.warn(" Now downloading code in download folder : %s" % variables['downloadPath']) command = CmdLine() script = ''' cd %s ls | grep -v extract.sh | xargs rm -rf wget --user %s --password %s -O code.zip '%s' unzip -o code.zip rm code.zip ''' % (variables['downloadPath'], self.http_request.username, self.http_request.password, downloadURL) script_file = connection.getFile( OverthereUtils.constructPath( connection.getFile(variables['downloadPath']), 'extract.sh')) OverthereUtils.write(String(script).getBytes(), script_file) script_file.setExecutable(True) command.addArgument(script_file.getPath()) output_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) error_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) exit_code = connection.execute(output_handler, error_handler, command) capturedOutput += self.parse_output( output_handler.getOutputLines()) + self.parse_output( error_handler.getOutputLines()) command = CmdLine() command.addArgument("rm") command.addArgument("-f") command.addArgument(variables['downloadPath'] + "/extract.sh") output_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) error_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) exit_code = connection.execute(output_handler, error_handler, command) capturedOutput += self.parse_output( output_handler.getOutputLines()) + self.parse_output( error_handler.getOutputLines()) return {'output': capturedOutput}
class localCliScript(): def __init__(self, cliHome, xldHost, xldPort, xldContext, xldProxyHost, xldProxyPort, xldSocketTimeout, xldUserName, xldPassword, script, cliExecutable, options): self.cmdLine = CmdLine() self.osname = System.getProperty('os.name').lower() if ( self.osname.startswith('win') ): cliExecutable = "%s\\bin\\%s.cmd" % ( cliHome, cliExecutable ) else: cliExecutable = "%s/bin/%s.sh" % ( cliHome, cliExecutable ) # End if self.cmdLine.addArgument( cliExecutable ) self.cmdLine.addArgument( '-quiet' ) if ( xldHost != "DEFAULT" ): self.cmdLine.addArgument( '-Host' ) self.cmdLine.addArgument( xldHost ) if ( xldPort != "DEFAULT" ): self.cmdLine.addArgument( '-Port' ) self.cmdLine.addArgument( xldPort ) if ( xldContext != "DEFAULT" ): self.cmdLine.addArgument( '-Context' ) self.cmdLine.addArgument( xldContext ) if ( xldProxyHost != "DEFAULT" ): self.cmdLine.addArgument( '-ProxyHost' ) self.cmdLine.addArgument( xldProxyHost ) if ( xldProxyPort != "DEFAULT" ): self.cmdLine.addArgument( '-ProxyPort' ) self.cmdLine.addArgument( xldProxyPort ) if ( xldSocketTimeout != "DEFAULT" ): self.cmdLine.addArgument( '-SocketTimeout' ) self.cmdLine.addArgument( xldSocketTimeout ) if ( xldUserName != "DEFAULT" ): self.cmdLine.addArgument( '-username' ) self.cmdLine.addArgument( xldUserName ) if ( xldPassword != "DEFAULT" ): self.cmdLine.addArgument( '-password' ) self.cmdLine.addPassword( xldPassword ) if ( options is not None ): self.options = str(options) # End if self.script = script self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler() self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler() # End __init__ def execute( self ): connection = None try: connection = LocalConnection.getLocalConnection() scriptFile = connection.getTempFile('xlrScript', '.py') OverthereUtils.write( String( self.script ).getBytes(), scriptFile ) scriptFile.setExecutable(True) self.cmdLine.addArgument( '-source' ) self.cmdLine.addArgument( scriptFile.getPath() ) if ( len( self.options ) > 1 ): self.cmdLine.addArgument( '--' ) optionsList = self.options.split(' ') for opt in optionsList: self.cmdLine.addArgument( opt ) # End for # End if exitCode = connection.execute( self.stdout, self.stderr, self.cmdLine ) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1 finally:
class Workstation(object): def __init__(self, workstation_config): self.cmd_line = CmdLine() self.chef_sdk_path = workstation_config['chef_sdk_path'] self.client_key = Workstation.format_key( workstation_config['client_key']) self.unix = (workstation_config['os'] == 'Unix') self.chef_server_url = workstation_config['chef_server_url'] self.node_name = workstation_config['node_name'] self.log_level = workstation_config['log_level'] self.log_location = workstation_config['log_location'] self.cookbook_path = workstation_config['cookbook_path'] self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler( ) self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler( ) @staticmethod def get_workstation(workstation_config): return Workstation(workstation_config) @staticmethod def format_key(key): chef_key = re.sub("---- .*", "----\n", key) footer = re.sub(".* ----", "----", key) body = re.sub(".*--- ", "", key) body = re.sub(" ---.*", "", body) for l in body.split(): chef_key = "%s%s\n" % (chef_key, l) chef_key = "%s%s\n" % (chef_key, footer) return chef_key @staticmethod def process_flag_with_arguments(arguments, flag): if arguments is not None and arguments: return "%s %s" % (flag, arguments) return "" @staticmethod def process_boolean_flag(variables, name, value): if variables[name]: return value return "" @staticmethod def process_unix_authentication(variables): # Handle authentication, identity_file takes precedence. if variables['identity_file'] is not None and variables[ 'identity_file']: authentication_option = "--identity-file %s" % variables[ 'identity_file'] else: authentication_option = "--ssh-password '%s'" % variables[ 'ssh_password'] return authentication_option @staticmethod def process_additional_options(options): if options is not None and options: return options return "" def chef_bootstrapunix(self, variables): sudo_param = Workstation.process_boolean_flag(variables, "sudo", "--sudo") sudo_password_param = Workstation.process_boolean_flag( variables, "sudo_password", "--sudo-password") run_list_param = Workstation.process_flag_with_arguments( variables['run_list'], "--run-list") authentication_option = Workstation.process_unix_authentication( variables) return self.execute_knife_command("bootstrap %s --ssh-user %s %s %s %s --node-name %s %s --yes" \ % (variables['address'], variables['ssh_user'], authentication_option, sudo_param, sudo_password_param, variables['node_name'], run_list_param), 'chef_bootstrapunix.cmd', variables['options'], True) def chef_bootstrapwindows(self, variables): run_list_param = Workstation.process_flag_with_arguments( variables['run_list'], "--run-list") return self.execute_knife_command( "bootstrap windows winrm %s --winrm-user %s --winrm-password '%s' --node-name %s %s --yes" % (variables['address'], variables['username'], variables['password'], variables['node_name'], run_list_param), 'chef_bootstrapwindows.cmd', variables['options'], True) def chef_cookbooklist(self, variables): return self.execute_knife_command("cookbook list", 'chef_cookbooklist.cmd', variables['options'], True) def chef_nodelist(self, variables): return self.execute_knife_command("node list", 'chef_nodelist.cmd', variables['options'], True) def chef_clientlist(self, variables): return self.execute_knife_command("client list", 'chef_clientlist.cmd', variables['options'], True) def chef_deleteoperation(self, target, variables): return self.execute_knife_command( "%s delete %s --yes" % (target, variables['node_name']), 'chef_delete.cmd', variables['options'], True) def chef_deletenode(self, variables): return self.chef_deleteoperation("node", variables) def chef_deleteclient(self, variables): return self.chef_deleteoperation("client", variables) def chef_applycookbook(self, variables, is_unix=True): if is_unix: os_specific_command = "ssh" os_chef_client_command = "'sudo chef-client'" else: os_specific_command = "winrm" os_chef_client_command = "'chef-client'" command ="%s --yes 'name:%s' %s --yes --%s-user %s --%s-password '%s'" \ % (os_specific_command, variables['node_name'], os_chef_client_command, os_specific_command, variables['username'], os_specific_command, variables['password']) return self.execute_knife_command(command, 'chef_applycookbook.cmd', variables['options'], True) def chef_applycookbookunix(self, variables): return self.chef_applycookbook(variables, True) def chef_applycookbookwindows(self, variables): return self.chef_applycookbook(variables, False) def chef_setrunlist(self, variables): return self.execute_knife_command( "node run_list set %s 'recipe[%s]'" % (variables['node_name'], variables['run_list']), 'chef_setrunlist.cmd', variables['options'], True) def chef_shownode(self, variables): return self.execute_knife_command( "node show %s" % variables['node_name'], 'chef_shownode.cmd', variables['options'], True) def chef_uploadcookbook(self, variables): if variables['override_cookbook_path'] is not None and variables[ 'override_cookbook_path']: self.cookbook_path = variables['override_cookbook_path'] return self.execute_knife_command( "cookbook upload %s" % variables['cookbook_name'], 'chef_uploadcookbook.cmd', variables['options'], True) def execute_knife_command(self, command, script_name, options=None, zip_workspace=False): connection = None try: options = Workstation.process_additional_options(options) connection = LocalConnection.getLocalConnection() workspace_path = self.create_chef_tmp_workspace(connection) knife_command = self.get_os_specific_knife_command( workspace_path, command, options) script_file = connection.getFile( OverthereUtils.constructPath( connection.getFile(workspace_path), script_name)) OverthereUtils.write(String(knife_command).getBytes(), script_file) script_file.setExecutable(True) if zip_workspace: self.zip_workspace(workspace_path, connection) command = CmdLine() command.addArgument(script_file.getPath()) output_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) error_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) exit_code = connection.execute(output_handler, error_handler, command) return [exit_code, output_handler, error_handler] except Exception: traceback.print_exc(file=sys.stdout) sys.exit(1) finally: if connection is not None: connection.close() def create_chef_tmp_workspace(self, connection): try: tmp_workspace_file = connection.getTempFile('tmp_workspace') workspace_path = re.sub('tmp_workspace', '', tmp_workspace_file.getPath()) workspace_directory = connection.getFile(workspace_path) connection.setWorkingDirectory(workspace_directory) new_path = "%s.chef" % workspace_path if not os.path.exists(new_path): os.makedirs(new_path) self.generate_knife_rb(new_path, connection) chef_key_file = connection.getFile( OverthereUtils.constructPath(connection.getFile(new_path), 'chefkey.pem')) OverthereUtils.write( String(self.client_key).getBytes(), chef_key_file) ssl_script = self.get_os_specific_ssl_script(workspace_path) script_file = connection.getFile( OverthereUtils.constructPath( connection.getFile(workspace_path), 'ssl.cmd')) OverthereUtils.write(String(ssl_script).getBytes(), script_file) script_file.setExecutable(True) self.cmd_line.addArgument(script_file.getPath()) connection.execute(self.stdout, self.stderr, self.cmd_line) return workspace_path except Exception: traceback.print_exc(file=sys.stdout) sys.exit(1) def get_os_specific_ssl_script(self, workspace_path): if self.unix: return """#!/bin/bash\ncd %s\n%s/bin/knife ssl fetch\n%s/bin/knife ssl check""" % ( workspace_path, self.chef_sdk_path, self.chef_sdk_path) else: return """@echo off\r\ncd %s\r\n%s\\bin\\knife.bat ssl fetch\r\n%s\\bin\\knife.bat ssl check\r\n""" % ( workspace_path, self.chef_sdk_path, self.chef_sdk_path) def get_os_specific_knife_command(self, workspace_path, command, options): if self.unix: return "#!/bin/bash\ncd %s\n%s/bin/knife %s %s" % ( workspace_path, self.chef_sdk_path, command, options) else: return "@echo off\r\ncd %s\r\n%s\\bin\\knife.bat %s %s\r\n" % ( workspace_path, self.chef_sdk_path, command, options) def generate_knife_rb(self, path, connection): knife_contents = '''current_dir = File.dirname(__FILE__) log_level %s log_location %s node_name "%s" client_key "#{current_dir}/chefkey.pem" chef_server_url "%s" cookbook_path ["%s"]''' % (self.log_level, self.log_location, self.node_name, self.chef_server_url, self.cookbook_path) knife_rb_file = connection.getFile( OverthereUtils.constructPath(connection.getFile(path), 'knife.rb')) OverthereUtils.write(knife_contents, knife_rb_file) def zip_workspace(self, workspace_path, connection): zip_script = self.get_os_specific_zip_command(workspace_path) zip_script_file = connection.getFile( OverthereUtils.constructPath(connection.getFile(workspace_path), 'zip.cmd')) OverthereUtils.write(String(zip_script).getBytes(), zip_script_file) zip_script_file.setExecutable(True) command = CmdLine() command.addArgument(zip_script_file.getPath()) return connection.execute(command) def get_os_specific_zip_command(self, workspace_path): if self.unix: return "#!/bin/bash\ncd %s\ntar -czf /tmp/chef.tgz ." % workspace_path else: return "@echo off\r\ncd %s\r\ntar -czf C:\\Windows\\Temp\\chef.tgz .\r\n" % workspace_path
class Localcliscript(): def __init__(self, cliJar, nexusiqUrl, nexusiqUserName, nexusiqPassword, nexusiqProxyUrl, nexusiqProxyUserName, nexusiqProxyPassword, app, stage, targetUrl): self.cmdLine = CmdLine() self.cmdLine.addArgument('java') self.cmdLine.addArgument('-jar') self.cmdLine.addArgument(cliJar) self.cmdLine.addArgument('--application-id') self.cmdLine.addArgument(app) self.cmdLine.addArgument('--server-url') self.cmdLine.addArgument(nexusiqUrl) self.cmdLine.addArgument('--authentication') self.cmdLine.addArgument("%s:%s" % (nexusiqUserName, nexusiqPassword)) if nexusiqProxyUrl: self.cmdLine.addArgument('--proxy') self.cmdLine.addArgument(nexusiqProxyUrl) self.cmdLine.addArgument( "%s:%s" % (nexusiqProxyUserName, nexusiqProxyPassword)) self.cmdLine.addArgument('--ignore-system-errors') self.cmdLine.addArgument('--stage') self.cmdLine.addArgument(stage) self.cmdLine.addArgument(targetUrl) self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler( ) self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler( ) # End __init__ def execute(self): connection = None try: connection = LocalConnection.getLocalConnection() exitCode = connection.execute(self.stdout, self.stderr, self.cmdLine) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1 finally:
def bitbucket_downloadcode(self, variables): downloadURL = "%s/%s/get/%s.zip" % (variables['server']['url'].replace( "api.", "www."), variables['repo_full_name'], variables['branch']) connection = LocalConnection.getLocalConnection() capturedOutput = "" self.logger.warn("Cleaning up download folder : %s" % variables['downloadPath']) command = CmdLine() command.addArgument("rm") command.addArgument("-rf") command.addArgument(variables['downloadPath'] + "/*") output_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) error_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) exit_code = connection.execute(output_handler, error_handler, command) capturedOutput = self.parse_output( output_handler.getOutputLines()) + self.parse_output( error_handler.getOutputLines()) self.logger.warn(" Now downloading code in download folder : %s" % variables['downloadPath']) command = CmdLine() script = ''' cd %s wget --user %s --password %s -O code.zip %s unzip code.zip rm -rf *.zip foldername=`ls -d */` mv -f $foldername* `pwd` rm -rf $foldername ''' % (variables['downloadPath'], self.http_request.username, self.http_request.password, downloadURL) script_file = connection.getFile( OverthereUtils.constructPath( connection.getFile(variables['downloadPath']), 'extract.sh')) OverthereUtils.write(String(script).getBytes(), script_file) script_file.setExecutable(True) command.addArgument(script_file.getPath()) output_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) error_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) exit_code = connection.execute(output_handler, error_handler, command) capturedOutput += self.parse_output( output_handler.getOutputLines()) + self.parse_output( error_handler.getOutputLines()) command = CmdLine() command.addArgument("rm") command.addArgument("-f") command.addArgument(variables['downloadPath'] + "/extract.sh") output_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) error_handler = CapturingOverthereExecutionOutputHandler.capturingHandler( ) exit_code = connection.execute(output_handler, error_handler, command) capturedOutput += self.parse_output( output_handler.getOutputLines()) + self.parse_output( error_handler.getOutputLines()) return {'output': capturedOutput}
%s %s """ % (cmdLogon, cmdProject) #print script print "-------------------------" stdout = CapturingOverthereExecutionOutputHandler.capturingHandler() stderr = CapturingOverthereExecutionOutputHandler.capturingHandler() try: connection = LocalConnection.getLocalConnection() targetScript = connection.getTempFile('oc-script', '.bat') OverthereUtils.write( String(script).getBytes(), targetScript) targetScript.setExecutable(True) cmd = CmdLine.build( targetScript.getPath() ) connection.execute( stdout, stderr, cmd ) except Exception, e: stacktrace = StringWriter() writer = PrintWriter( stacktrace, True ) e.printStackTrace(writer) stderr.hadleLine(stacktrace.toString()) # set variables output = stdout.getOutput() error = stderr.getOutput() if len(output) > 0: print "```" print output