Skip to main content

A tool for reaching out to remote machine - excecute code and collect artificats

Project description

Reacher

From your local machine, Reacher will,

  • build a docker image on the remote (docker must already be installed and ssh enabled) according to specifications
  • set up container on the remote with port port-forwarding and enviroment variables
  • execute your local code on the remote, in the container, with printouts shown as your were running it locally
  • upload/download files from the container to your local machine

Getting started...

To get started,

pip install reacher
pip install python-dotenv

First we must setup a connection to the remote, RemoteClient will create a ssh connection between the local and remote machine.

from reacher.reacher import Reacher, RemoteClient
from dotenv import dotenv_values
config = dotenv_values()  # take environment variables from .env.
client = RemoteClient(
    host=config["HOST"],
    user=config["USER"],
    password=config["PASSWORD"],
    ssh_key_filepath=config["SSH_KEY_PATH"]
)

the connection is sent to Reacher together with the name of the image that we want to build and the name of the container.

reacher = Reacher(
    client=client,
    build_name="base",
    image_name="base",
    build_context="dockercontext"
)

or send in the aruments for RemoteClient direcly to Reacher

reacher = Reacher(
    client=client,
    build_name="base",
    image_name="base",
    build_context="dockercontext",
    host=config["HOST"],
    user=config["USER"],
    password=config["PASSWORD"],
    ssh_key_filepath=config["SSH_KEY_PATH"]
)

build_context should contain everything for building the docker image on the remote. It might look like,

$ ls dockercontext/
Dockerfile  requirements.txt

Once Reacher has been setup we can build the image on the remote. Reacher will send the build_context to the remote and trigger docker to build an image according to the specifications in the Dockerfile

reacher.build()

[+] Building 0.0s (0/1)                                                         
[+] Building 0.2s (2/3)                                                         
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 528B                                       0.0
 ...
 ...

and thereafter we can setup the docker container. Reacher will make sure this container is running until we have explicitly deleted it.

reacher.setup(ports=[8888, 6666], envs=dotenv_values(".env"))

Running code on the remote

Running a code-snippet

Now we have built the docker image on the remote and have a container ready to execute whatever code that we want to run.

A "Hello World" test can be triggered from a notebook.

First we create the python module that we want to execute,

%%writefile simple_test.py
import time
while 1:
    print("Hello from remote")
    time.sleep(1)

and then we execute it on the remote inside our controlled docker enviroment.

reacher.execute(
    file="simple_test.py",
    command="python simple_test.py",
    named_session="simple_test",
    # Before sending the code to the remote, clean the container from previous runs.
    clear_container=True, 
)

Preparing to copy...

Copying to container - 2.56kB

Successfully copied 2.56kB to base://workspace

Hello from remote
Hello from remote
...

simple_test will continue to run in the background (even if you kill the cell/script that you instantiated the reacher.execute from) until we explicitly have killed it.

With list_named_sessions you can get all currently running sessions.

reacher.list_named_sessions()
There is a screen on:
	22.simple_test	(03/19/23 18:20:07)	(Attached)
1 Socket in /run/screen/S-root.

we can always attach to a named session to continue to get printouts.

reacher.attach_named_session("simple_test")

or kill it

reacher.kill_named_session("simple_test")

Running with dependencies

To execute some code that depends on other modules inside a src directory, simply add src as a context_folder when calling reacher.execute.

%%writefile dependency_test.py
from dependency import Dependency
d = Dependency()
reacher.execute(
    context_folder="src",
    file="dependency_test.py",
    command="python dependency_test.py",
    named_session="dependency_test",
)

Preparing to copy...

Copying to container - 3.584kB

Successfully copied 3.584kB to base://workspace

Hello from class Dependency
[screen is terminating]

Generate artifact

In many cases we want to run some code on the remote and afterwards collect some artifacts.

Everything that is saved in the artifact directory will be available for us to download to the local machine.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reacher-0.3.3-py2.py3-none-any.whl (6.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file reacher-0.3.3-py2.py3-none-any.whl.

File metadata

  • Download URL: reacher-0.3.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for reacher-0.3.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e8517806a9d0b3220fb4beac56bb65a5eec6e234e2e8dca30c23ac61817dda68
MD5 2efee630f5025a232c9dc038df8db38a
BLAKE2b-256 00ec7bd1c7e7824076465da7519aa7da0f04d24ac602ba7d55d676558ad44108

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page