Skip to content

ioanaf06/overthere-pylib

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Table of Contents

Introduction

Overtherepy is a Jython wrapper library around Overthere to manipulate files and execute processes on remote hosts, i.e. do stuff "over there".

Overtherepy is available under the GPLv2 with XebiaLabs FLOSS License Exception.

Getting Overtherepy

To get Overtherepy, you have two options:

  1. Add a dependency to your project.
  2. Build Overtherepy yourself.

Binary releases of Overtherepy can be downloaded straight from the Maven Central repository or from Github releases

Depending on Overtherepy

  1. If your project is built with Maven, add the following dependency to the pom.xml:

     <dependency>
     	<groupId>com.xebialabs.overthere</groupId>
     	<artifactId>overtherepy</artifactId>
     	<version>0.0.3</version>
     </dependency>
    
  2. If your project is built using another build tool that uses the Maven Central repository, translate these dependencies into the format used by your build tool.

Building Overtherepy

  1. Install Gradle 1.0 or up.
  2. Clone the Overthere repository.
  3. Define the jython interpreter executable in the ~/.gradle/gradle.properties.
    • jythonInterpreter=$JYTHON_HOME/bin/jython
  4. Run the command gradle clean build.

Programming Overtherepy

To program Overtherepy, browse the source code and browse the Overtherepy PyDoc. Below gives you a basic usage example.

Setup connection settings

Depending on the target host, the following options are available :

  • overtherepy.SshConnectionOptions
  • overtherepy.CifsConnectionOptions
  • overtherepy.LocalConnectionOptions

Please refer to the Overthere documentation for available options. Options are passed to the contructor as named parameters

from overtherepy import LocalConnectionOptions, OverthereHost, OverthereHostSession
from com.xebialabs.overthere import OperatingSystemFamily
from com.xebialabs.overthere.cifs import CifsConnectionType

# Sample Local Connection Options
localOpts = LocalConnectionOptions(os=OperatingSystemFamily.UNIX)

# Sample SSH Connection options using custom port
sshOpts = SshConnectionOptions("myhost","myusername", port=2222 )

# Sample CIFS Connection options using TELNET for connection type
cifsOpts = CifsConnectionOptions("172.16.92.237", "Administrator", "secret",
            connectionType=CifsConnectionType.TELNET)

Sample a session and execution of remote os command

...
host = OverthereHost(cifsOpts)
session =  OverthereHostSession(host)

f = session.upload_classpath_resource_to_work_dir("testfiles/echo.sh", executable=True)
response = session.execute([f.path, "ping"], check_success=False)
if response.rc != 0:
	print "Failed to execute command"
	print response.stderr
else:
	print "Response", str(response.stdout)
	
session.close_conn()

Sample session using 'with'

The session supports the 'with' statement. You must first import the statement from the ___future___ package. The session's connection is automatically closed at the end of the 'with' body.

from __future__ import with_statement
...
with session:
	# do stuff

API Reference

Refer to Overtherepy PyDoc for a complete API reference. Below is a description of the main OverthereHostSession class.

 
class OverthereHostSession(__builtin__.object)
    Session with a target host
 
  Methods defined here:
__enter__(self)
__exit__(self, type, value, traceback)
__init__(self, host, enable_logging=True, stream_command_output=False, execution_context=None)
:param host: to connect to. Can either be an OverthereHost or an XL Deploy's HostContainer class
:param enable_logging: Enables info logging to console.
:param execution_context: XLD ExecutionContext. Can be None.
:param stream_command_output: True when remote command execution output is to be send to stdout and stderr
close_conn(self)
Close connection to target host
copy_diff(self, target_path, diff)
Apply the changes represented by the Diff object to the target path.
:param target_path: absolute path to folder on target system
:param diff: Diff containing all changes to be applied to target path
copy_text_to_file(self, content, target, mkdirs=True)
Copies the content to the specified file
:param content: to write to file
:param target: com.xebialabs.overthere.OverthereFile
:param mkdirs: Automatically create target directory
copy_to(self, source, target, mkdirs=True)
Copy the source file to the target file
:param source: com.xebialabs.overthere.OverthereFile
:param target: com.xebialabs.overthere.OverthereFile
:param mkdirs: Automatically create target directory
:return:
delete_from(self, source, target, target_dir_shared=False)
Uses the source directory to determine the files to delete from the target directory.
Only the immediate sub-directories and files in the source directory base are used.
If the target is a file, then it is deleted without analysing the source.
When there are files present in the target directory after deleting source files from it, the target is not deleted.
:param source: directory of files to be deleted.
:param target: directory or file to be deleted.
:param target_dir_shared: When True, the target directory itself will not be deleted.
:return:
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
:return: CommandResponse
get_conn(self)
Get connection to host.  Create new connection if one does not exist.
:return: com.xebialabs.overthere.OverthereConnection.
is_windows(self)
:return: True if target host is a Windows machine
local_file(self, file)
Get reference to local file as an OverthereFile
:param file: java.util.File
:return: com.xebialabs.overthere.OverthereFile
read_file(self, filepath, encoding='UTF-8')
Reads the content of a remote file as a string
:param filepath: absolute path on target system
:param encoding: target file encoding
:return: String
read_file_lines(self, filepath, encoding='UTF-8')
Reads the content of a remote file split by newline
:param filepath: absolute path on target system
:param encoding: target file encoding
:return: Array of String
remote_file(self, filepath)
Get reference to remote file
:param filepath: absolute path on target system
:return: com.xebialabs.overthere.OverthereFile
upload_classpath_resource_to_work_dir(self, resource, executable=False)
Uploads the classpath resource to the session's working directory.
:param resource: to find on the classpath to copy
:param executable: True if the uploaded file should be made executable
:return: com.xebialabs.overthere.OverthereFile
upload_file_to_work_dir(self, source_otfile, executable=False)
Uploads specified file to the session's working directory
:param source_otfile: com.xebialabs.overthere.OverthereFile
:param executable:
:return:  com.xebialabs.overthere.OverthereFile
upload_text_content_to_work_dir(self, content, filename, executable=False)
Creates a file in the session's working directory with the specified content.
:param content:  to write to file
:param filename: relative path to file that will be created in session's working directory
:param executable: True if file should be an executable file
:return: com.xebialabs.overthere.OverthereFile
work_dir(self)
Get the temporary working directory on the target system for the current session.
:return: com.xebialabs.overthere.OverthereFile
work_dir_file(self, filepath)
Create a file in the session's working directory
:param filepath: relative path to working directory
:return: com.xebialabs.overthere.OverthereFile

About

Jython library to wrap Overthere

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 68.3%
  • Java 31.5%
  • Shell 0.2%