• Stars
    star
    159
  • Rank 234,533 (Top 5 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Chopsticks is an orchestration library: it lets you execute Python code on remote hosts over SSH.

Chopsticks

Join the chat at https://gitter.im/chopsticks-chat/Lobby

Chopsticks is an orchestration library: it lets you manage and configure remote hosts over SSH.

Naturally this is agentless and nothing needs to be installed on the remote host except Python and an SSH agent.

It also has support for executing code in Docker containers.

It's perhaps best compared to Ansible or Fabric, but has some clever transport magic which means it's very easy to develop with: you just write Python functions that can be called from the orchestration host. No invoking bash commands (eg. Fabric) or writing self-contained scripts with constrained input and output formats (eg. Ansible).

One might also draw a comparison with Python's built-in multiprocessing library, but instead of calling code in subprocesses on the same host, the code may be run on remote hosts.

Example

With chopsticks you can simply import functions and hand them to the remote host to be executed.

First stand up an SSH Tunnel:

from chopsticks.tunnel import Tunnel
tun = Tunnel('troy.example.com')

Then you can pass a function, to be called on the remote host:

import time
print('Time on %s:' % tun.host, tun.call(time.time))

You can use any pure-Python function in the current codebase, meaning you can create your own libraries of orchestration functions to call on remote hosts (as well as functions that call out to remote hosts using Chopsticks).

Tunnel provides support for executing on a single host; there is also a Group that can execute a callable on a number of hosts in parallel:

from chopsticks.group import Group

group = Group([
    'web1.example.com',
    'web2.example.com',
    'web3.example.com',
])
for host, addr in group.call(ip).successful():
    print('%s ip:' % host, addr)

Subclasses of tunnels allow connecting using streams other than SSH, such as using sudo, or to fresh Docker containers for sandboxing:

from chopsticks.tunnel import Docker
from chopsticks.group import Group
from chopsticks.facts import python_version

group = Group([
    Docker('worker-1', image='python:3.4'),
    Docker('worker-2', image='python:3.5'),
    Docker('worker-3', image='python:3.6'),
])

for host, python_version in group.call(python_version).items():
    print('%s Python version:' % host, python_version)

Tunnels and Groups connect lazily (or you can connect them proactively by calling connect()). They are also usable as context managers:

# Explictly connect and disconnect
group.connect()
group.call(time.time)
group.close()

# Reconnect and disconnect as context manager
with group:
    group.call(time.time)

# Implicit reconnect
group.call(time.time)

# Disconnect when destroyed
del group

Naturally, any remote state (imports, globals, etc) is lost when the Tunnel/Group is closed.

Installation

Chopsticks can be used directly from a clone of the repo; or installed with pip:

$ pip install chopsticks

API

See the full documentation on Read The Docs.

Python 2/3

Chopsticks supports both Python 2 and Python 3.

Because Chopsticks takes the view that agents run out of the same codebase as the controller, agents will attempt to use a similar Python interpreter to the one for the controller process:

  • /usr/bin/python2 if the controller process is (any) Python 2.
  • /usr/bin/python3 if the controller process is (any) Python 3.

How it works

The SSH tunnel invokes the python binary on the remote host, and feeds it a bootstrap script via stdin.

Once bootstrapped, the remote "agent" sets up bi-directional communication over the stdin/stdout of the tunnel. This communication is used (currently) for two purposes:

  • An RPC system to invoke arbitrary callables within the remote agent and pass the returned values back to the controller.
  • A PEP-302 import hook system, allowing the remote agent to import pure-Python code from the controller (NB. the controller can only serve Python modules that live within the filesystem - import hooks such as zipimport/compressed eggs are not currently supported).

stderr is echoed to the controlling console, prefixed with a hostname to identify which Tunnel it issued from. This can therefore be used to feed debugging information back to the orchestration host.

License

Apache License 2.0

More Repositories

1

pgzero

A zero-boilerplate games programming framework for Python 3, based on Pygame.
Python
490
star
2

wasabi2d

Cutting-edge 2D game framework for Python
Python
144
star
3

adventurelib

A minimal library for writing text adventure games in Python 3
Python
135
star
4

flake8-html

Generate HTML reports of flake8 violations
Python
52
star
5

pyfxr

Sound effects generation for Python, in fast Cython code, compatible with Pygame and Pyglet.
Python
16
star
6

puppy

A prototype IDE for education; the precursor to mu https://github.com/mu-editor/mu
Python
11
star
7

goblit

Award-winning adventure game from PyWeek 19
Python
9
star
8

gamemaths

Slides for my remote.python.pizza 2020 talk
Python
7
star
9

lepton

Lepton: A high-performance, pluggable particle engine and API for Python
C
7
star
10

wasabi-geom

A 2D geometry library for Python, with ergonomic vector classes in optimised Cython code.
Cython
6
star
11

axium

Space shooter being built for PyWeek 32
Python
6
star
12

dark-world

A Pyweek 25 entry, for the theme "Two Worlds"
JavaScript
4
star
13

weddinglights

Fadecandy based lighting
Python
4
star
14

lightvolume

2D light volume rendering in OpenGL
Python
3
star
15

edubundle

Portable python bundle for Windows
Python
3
star
16

what-the-frog

What The Frog? - a PyWeek 27 entry
Python
3
star
17

lzf

CFFI-based Python binding for LZF stream compression
Python
3
star
18

python-now

Interactive Python tutorial, in the browser
JavaScript
3
star
19

wasabi-scenegraph

Python
2
star
20

fake-news

A fake news generator
Python
2
star
21

infinisweeper

Infinite game of minesweeper
Python
2
star
22

empty

Empty files in various languages
2
star
23

wvec

A Python vector implementation in Rust
Rust
2
star
24

doodlr

Python
1
star
25

korovic

Python
1
star
26

murder

A murder mystery game for Pyweek 23
Python
1
star
27

heightfield

Python
1
star
28

difflib-talk

Python
1
star
29

snowgame

Snow fall game
Python
1
star
30

wasabi-peace

Python
1
star
31

battleships

Python
1
star
32

pep666

The anti-linter for Python code
Python
1
star
33

wor

Python
1
star
34

dojos10e01

Instagram-like filters with numpy and Pillow
Jupyter Notebook
1
star
35

bamboo-warrior

Python
1
star
36

jumpatron

A one-button jumping game made for the Rock Band drum kit
Python
1
star
37

moonbaseapollo

Python
1
star
38

tetrish

Something like tetris
1
star
39

art-attack

Python
1
star
40

python-loglab

Python
1
star
41

nucleon

Python
1
star
42

ldnpydojos7e10

Wikipedia bot
Python
1
star
43

gardenkingdom

Exploring Ren'Py
Ren'Py
1
star