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 __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 __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 __init__(self, username, password, address, connectionType, script): self.options = ConnectionOptions() self.options.set(ConnectionOptions.USERNAME, username) self.options.set(ConnectionOptions.PASSWORD, password) self.options.set(ConnectionOptions.ADDRESS, address) self.options.set(ConnectionOptions.OPERATING_SYSTEM, OperatingSystemFamily.WINDOWS) self.script = script self.connectionType = connectionType self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler() self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler()
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, 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 """ if isinstance(cmd, basestring): cmd = cmd.split() 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(1) 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 __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 __init__(self, username, password, address, connectionType, timeout, allowDelegate, remotePath, script): self.options = ConnectionOptions() self.options.set(ConnectionOptions.USERNAME, username) self.options.set(ConnectionOptions.PASSWORD, password) self.options.set(ConnectionOptions.ADDRESS, address) self.options.set(ConnectionOptions.OPERATING_SYSTEM, OperatingSystemFamily.WINDOWS) self.remotePath = remotePath self.script = script self.connectionType = connectionType # WINRM_NATIVE only self.allowDelegate = allowDelegate # WINRM_INTERNAL only self.timeout = timeout 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, username, password, address, connectionType, timeout, allowDelegate, cscriptExecutable, remotePath, script): self.options = ConnectionOptions() self.options.set(ConnectionOptions.USERNAME, username) self.options.set(ConnectionOptions.PASSWORD, password) self.options.set(ConnectionOptions.ADDRESS, address) self.options.set(ConnectionOptions.OPERATING_SYSTEM, OperatingSystemFamily.WINDOWS) self.remotePath = remotePath self.script = script self.connectionType = connectionType # WINRM_NATIVE only self.allowDelegate = allowDelegate # WINRM_INTERNAL only self.timeout = timeout self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler( ) self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler( )
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 __init__(self, script, connectionType=None, host=None, password=None): self.logger = logger # self.logger.debug("In Selenium Runner init") self.script = script self.connectionType = connectionType if host: self.options = ConnectionOptions() self.options.set(ConnectionOptions.USERNAME, host.get("username")) self.options.set(ConnectionOptions.PASSWORD, password) self.options.set(ConnectionOptions.ADDRESS, host.get("address")) self.options.set(ConnectionOptions.OPERATING_SYSTEM, OperatingSystemFamily.UNIX) self.options.set(SshConnectionBuilder.CONNECTION_TYPE, SshConnectionType.SCP) else: self.options = None self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler( ) self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler( )
def __init__(self, script, connectionType=None, host=None, password=None): self.logger = getattr(__builtin__, 'logger', None) self.script = script self.connectionType = connectionType if host: self.options = ConnectionOptions() self.options.set(ConnectionOptions.USERNAME, host.getProperty('username')) self.options.set(ConnectionOptions.PASSWORD, password) self.options.set(ConnectionOptions.ADDRESS, host.getProperty('address')) self.options.set(ConnectionOptions.OPERATING_SYSTEM, OperatingSystemFamily.UNIX) self.options.set(SshConnectionBuilder.CONNECTION_TYPE, SshConnectionType.SCP) else: self.options = None self.stdout = CapturingOverthereExecutionOutputHandler.capturingHandler( ) self.stderr = CapturingOverthereExecutionOutputHandler.capturingHandler( )
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 = "" print "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()) print " 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}
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 = "" print "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()) print " 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}
print "URL = %s" % ocUrl print "USERNAME = %s" % ocUsername print "OC = %s" % ocCmd print " " cmdLogon = "%s login --server=%s -u %s -p %s --insecure-skip-tls-verify" % (ocCmd, ocUrl, ocUsername, ocPassword) cmdProject ="%s delete project %s" % (ocCmd, ocProject) script = """ %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())