Menu

Switch to the dark mode that's kinder on your eyes at night time.

Switch to the light mode that's kinder on your eyes at day time.

Switch to the dark mode that's kinder on your eyes at night time.

Switch to the light mode that's kinder on your eyes at day time.

in ,

A Python library for writing distributed self-replicating programs, Hacker News


                  

        

          

               

https://d33wubrfki0l68.cloudfront.net/be86b6f40a357000aaa1338b3542cfaedb0e0a12/d106b/_images/mitogen.svg

) Mitogen is a Python library for writing distributed self-replicating programs.

There is no requirement for installing packages, copying files around, writing shell snippets, upfront configuration, or providing any secondary link to a remote machine aside from an SSH connection. Due to its origins for use in managing potentially damaged infrastructure, theremote machine need not even have free disk space or a writeable filesystem.

It is not intended as a generic RPC framework; the goal is to provide a robust and efficient low-level API on which tools likeSalt,Ansible, orFabriccan be built, and while the API is quite friendly and comparable toFabric, ultimately it is not intended for direct use by consumer software.

The focus is to centralize and perfect the intricate dance required to run Python code safely and efficiently on a remote machine, whileavoiding temporary files or large chunks of error-prone shell scripts, and supporting common privilege escalation techniques likesudo, potentially in combination with exotic connection methods such as WMI,telnet, or console-over-IPMI.

Automatic Bootstrap

Mitogen’s main feature is enabling your Python program to bootstrap and communicate with new copies of itself under its control running on remote machines,using only an existing installed Python interpreter and SSH client, something that by default can be found on almost all contemporary machines in the wild. To accomplish bootstrap, Mitogen uses a single 400 byte SSH command line and 8KB of its own source code sent to stdin of the remote SSH connection.

$ python ../preamble_size.py SSH command size: 653 Bootstrap (mitogen.core) size: 16987 ( 16.  KiB)                                Original Minimized Compressed mitogen.parent 97188 94 9KiB 50287  1KiB 51 .7% 12649  (4KiB)  .0% mitogen.fork 8436 8.2KiB 4130 4.0KiB 49 .0%  (1.6KiB)  .5% mitogen.ssh 10423 10. 2KiB 6762 6.6KiB 64 .9%  (2.0KiB)  .5% mitogen.sudo 11163 10 9KiB  (5.5KiB)  .7% 2138 2.1KiB 19 2% mitogen.select 12325 12 .0KiB 2929 2.9KiB 23 .8%  0.9KiB 7.8% mitogen.service 39420 38 5KiB 21708 21 .2KiB (*******************************************************************************************************************************************. 1% 5708 5.6KiB 14 .5% mitogen.fakessh 15599 15. 2KiB 8011 7.8KiB 51 .4% 2624 2.6KiB 16 .8% mitogen.master 47163 46. 1KiB 24055 23 5KiB 51 .0%  (6.5KiB)  0%

Once bootstrapped, the remote process is configured with a customizableargv [0], readily visible to system administrators of the remote machine using the UNIXpscommand:

20051? SS 0: 00  _ sshd: dmw [priv] 20053? S 0: 00  _ sshd: dmw @ notty 20054? SSL 0: 00  _ / /r / usr/bin/python(mitogen:[email protected]: ) 20103? S 0: 00  _ tar zxvf myapp.tar.gz

The example context was started by UIDdmwon hostEldil.home, process ID22476.

IO Multiplexer

The bootstrap includes a compact IO multiplexer (like Twisted or asyncio) that allows it to perform work in the background while executing your program’s code. For example, the remote context can be used toconnect to a new user on the remote machine using sudo, or as an intermediary for extending the program’s domain of control outward to other machines, enabling your program tomanipulate machines behind a firewall, or enable itsdata plane to cohere to your network topology.

https://d33wubrfki0l68.cloudfront.net/d80fa47fcc33965115802733bc785dc051ae43f0/e8141/_images/billing.svg

bastion_host=router.ssh(    hostname='jump-box.mycorp.com')docker_host=router.ssh(    via=bastion_host,    hostname='docker-a.prod.mycorp.com')sudo_account=router.sudo(    via=docker_host,    username='user_with_magic_ssh_key',    password='sudo password',)internal_box=router.Docker(    via=sudo_account,    container='billing0',)internal_box.call(OS.system,'./ run-nightly-billing.py')

The multiplexer also ensures the remote process is terminated if your Python program crashes, communication is lost, or the application code running in the context has hung.

Module Forwarder

Slaves are configured with a customPEP – 302 importerthat forwards requests for unknown Python modules back to the host program. When your program asks a context to execute code from an unknown module, all requisite modules are transferred automatically and imported entirely in RAM without need for further configuration.

importmyapp.mypkg.mymodule# myapp / __ init__.py, myapp / mypkg / __ init__.py, and myapp / mypkg / mymodule.py# are transferred automatically.print(context.call(myapp.mymodule.my_function))

As the forwarder reuses the import mechanism, it should integrate cleanly with any tool such aspy2exethat correctly implement the protocols in PEP – 302, allowing truly single file applications to run across multiple machines without further effort.

Common sources of import latency and bandwidth consumption are mitigated:

  • Modules need only be uploaded once per directly connected context. Subsequent requests for modules from children of that context will be served by the

  • Imports by threads within a context triggering a load are deduplicated and joined with any identical requests triggered by other threads in the same context and children in the context’s subtree.

  • roundtrip is required for negative responses due to Python 2’s import statement semantics: children have a list of submodules belonging to a package, and ignore requests for submodules that did not exist on the master.
  • Imports are extracted from each module, compared to those found in memory, and recursively preloaded into children requesting that module, minimizing round-trips to one per package nesting level. For example,django.db.modelsonly requires 3 round-trips to transfer 456 KiB, representing 1.7MiB of uncompressed source split across 148 modules.

Message Routing

https://d33wubrfki0l68.cloudfront.net/7e0cff641f508339f04c239fcd253454464be76e/bedbe/_images/route.svg

Slaves may communicate autonomously without direct interaction with the master, allowing a wide variety of complex data and control flows to be expressed using the links between the processes.

Logging Forwarder

The bootstrap configures the remote process’s Python logging package to forward all logs back to the local process, enabling management of program logs in one location.

18:15:29Dmitogen.ctx.K3:mitogen:Importer.find_module()  'mitogen.zlib')18:15:29(D)   (mitogen) *************************.ctx.(k3):mitogen:_ dispatch_calls((1002L,False,'POSIX',(None),'system',('ls -l / proc / self / fd',),{}))

Stdio Forwarder

To ease porting of crusty old infrastructure scripts to Python, the bootstrap redirects stdio for itself and any child processes back into the logging framework. This allows use of functions as basic asos.system (‘hostname; uptime ’)without further need to capture or manage output.

18:17:28Dmitogen.ctx.K3:mitogen:_ dispatch_calls((1002L,False,'posix '',None,'system',()  'hostname; Uptime ',),{}))18:17:56Imitogen.ctx.K3:stdout:K318:17:Imitogen.ctx.K3:stdout:17:37:10up562days,2:25,(5)users,loadaverage:(1.),1. 13,1. 14

Detached Subtrees

https://d33wubrfki0l68.cloudfront.net/38b7e4bee5933d91b0c3cc67659a9e91b18daede/86fbf/_images/detached-subtree.svg

Contexts may detach from and outlive the running program, while maintaining communication with descendents in their subtree. This enables persistent background tasks that reuse Mitogen features.

@ mitogen.(core).takes_econtextdefbecome_monitoring_master(children,econtext):    kill_old_process('/ var / run / mydaemon.pid')    write_pid_file('/ var / run / mydaemon.pid')    econtext.Detach()    whileTrue:        forchildinchildren:            ifchild.call(get_cpu_load>0.9:                alert_operator('Child is too busy!'(str)(child))        time.Sleep(1)DC1.call_async(become_monitoring_master,children)

Blocking Code Friendly

Within each process, a private thread runs the I / O multiplexer, leaving the main thread and any additional application threads free to perform useful work.

While Mitogen is internally asynchronous, it hides this asynchrony from consumer code. This is since writing asynchronous code is mostly a foreign concept to the target application of managing infrastructure. It should be possible to rewrite a shell script in Python without significant restructuring, or mind-bending feats of comprehension to understand control flow.

Before:

#! / bin / bash# Install our application.tar zxvf app.tar.gz

After:

definstall_app():    "" "Install our application."" "    OS.system('tar zxvf app.tar.gz '')context.call(install_app)

Or even:

context.call(OS.system,'tar zxvf app.tar.gz ')

Exceptions raised by function calls are propagated back to the parent program, and timeouts can be configured to ensure failed calls do not block progress of the parent.

Scatter / Gather Calls

Functions may be invoked asynchronously, with results returned as they become available.

defUsage(path):    returnsum(OS.pathgetsize(OS.(path) .join(dirpath,name))                fordirpath,dirnames,filenamesinOS.walk()  path)                fornameindirnamesfilenames),0)total=0formsg ​​inSelect(C.call_async(Usage,'/ tmp')forCincontexts):    Usage=msg ​​.unpickle()    print('Context% s/ TMP Usage:(% d)'%()  recv.context,Usage))    total=Usageprint('Total / tmp usage across all contexts:% d''%(total,))

Single File Programs

Programs that are self-contained within a single Python script are supported. External contexts are configured such that any attempt to execute a function from the main Python script will correctly cause that script to be imported as usual into the slave process.

#! / usr / bin / env python"" "Install our application on a remote machine.Usage:install_app.pyWhere:Hostname to install to."" "importOSimportsysimportmitogendefinstall_app():    OS.system('tar zxvf my_app.tar.gz '')@ mitogen.main()defmain(router):    iflen(sys.argv!=(2):        print(__ doc __)        sys.exit(1)    context=router.ssh(hostname=sys.argv[1])    context.call(install_app)

Event-driven IO***

Code running in a remote context can be connected to aChannel. Channels are used to send data asynchronously back to the parent, without further need for the parent to poll for changes. This is useful for monitoring systems managing a large fleet of machines, or to alert the parent of unexpected state changes.

deftail_log_file(channel,path='/ var / log / messages'):    "" "Forward new lines in a log file to the parent."" "    size=OS.path.getsize(path)    whilechannel.open():        new_size=OS.path.getsize(path)        ifnew_size==size:            time.Sleep(1)            continue        elifnew_sizesize:            size=0        FP=open(path,'r')        FP.seek(size)        channel.send(FP.read(new_size-size))        FP.close()        size=new_size

Compatibility

Mitogen is compatible withPython 2.4released November 2004, making it suitable for managing a fleet of potentially ancient corporate hardware, such as Red Hat Enterprise Linux 5, released in 2007.

Every combination of Python 3.x / 2.x parent and child should be possible, however at present only Python 2.4, 2.6, 2.7 and 3.6 are tested automatically.

Zero Dependencies

Mitogen is implemented entirely using the standard library functionality and interfaces that were available in Python 2.4.

          

        

      

       

Brave Browser
Read More
Payeer

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Fortnite Downtime Frustrates Gamers Further With Zero Patch Notes, Crypto Coins News

Fortnite Downtime Frustrates Gamers Further With Zero Patch Notes, Crypto Coins News

Address 1st-party tracker blocking · Issue # 780 · uBlockOrigin / uBlock-issues, Hacker News

Address 1st-party tracker blocking · Issue # 780 · uBlockOrigin / uBlock-issues, Hacker News

Back to Top
close

Log In

Forgot password?

Forgot password?

Enter your account data and we will send you a link to reset your password.

Your password reset link appears to be invalid or expired.

Log in

Privacy Policy

To use social login you have to agree with the storage and handling of your data by this website. %privacy_policy%

Add to Collection

No Collections

Here you'll find all collections you've created before.