Skip to content

Automate your own tasks on pure python and execute it on remote or local hosts. Create The Magic

License

Notifications You must be signed in to change notification settings

Friz-zy/factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

factory

MIT Python 2.7 gevent

Build Status Coverage Status

Description

Automate your own tasks on pure python and execute it on remote or local hosts

fact "uptime | cowsay"
frizzy@localhost in: uptime | cowsay
frizzy@localhost out:  ______________________________________
frizzy@localhost out: /  00:38:46 up 17 days, 8:39, 7 users, \
frizzy@localhost out: \ load average: 0,95, 0,95, 0,94       /
frizzy@localhost out:  --------------------------------------
frizzy@localhost out:         \   ^__^
frizzy@localhost out:          \  (oo)\_______
frizzy@localhost out:             (__)\       )\/\
frizzy@localhost out:                 ||----w |
frizzy@localhost out:                 ||     ||

original idea of example

Long Description

Factory is proof-of-concept realization of fabric with a number of differences:

  • run() function works in the same way with subprocess.popen under localhost as under ssh connect to remote host
  • Factory uses openssh or any another ssh client (you should modified config for this), so you can use all power of ssh sockets
  • Factory uses gevent library for asynchronous executing
  • Factory contains less code and tries to keep everything clear (not perfect, but trying!)

Note that Factory uses pipes for communication with subprocess. So, there is no way to use popen and automatically write passwords for ssh and sudo on localhost, because "smart" programs like ssh and sudo uses tty directly. Also active tty required (needed check it) and for sudo uses "sudo -S".

Alternatives:

  1. Use paramico like fabric = no ssh sockets.

  2. Use pty.fork, waitpid, execv as pexcpect and sh = only unix, no separated stderr, hard to communicate.

  3. Use ssh-copy-id like sh module recommended = ask passwords only one first time.

  4. Use sshpass like ansible = external dependencies.

  5. Use local ssh server and run all commands through it instead of popen (we need to go deeper).

Of course, you can write your own run() function with previous decisions and use it!

Examples:

  • Using build in functions
fact run 'echo "hello, world!"'
frizzy@localhost in: echo "hello, world!"
frizzy@localhost out: hello, world!
  • Using factfile
cat factfile
#!/usr/bin/env python
# coding=utf-8
from factory.api import run

def hello_fact():
    run('echo "this is factfile"')
fact hello_fact
frizzy@localhost in: echo "this is factfile"
frizzy@localhost out: this is factfile
  • Using fabfile
cat fabfile 
#!/usr/bin/env python
# coding=utf-8
from fabric.api import run

def hello_fab():
    run('echo "this is fabfile"')
fact hello_fab
frizzy@localhost in: echo "this is fabfile"
frizzy@localhost out: this is fabfile
  • And even this
cat my_little_script.py 
#!/usr/bin/env python
# coding=utf-8
from factory.api import *

def hello():
    run('echo "hello world!"')

if __name__ == '__main__':
    # running hello on two hosts
    env.hosts = ['localhost', 'test@127.0.0.1']
    for host in env.hosts:
        with set_connect_env(host):
            hello()
            run(raw_input('type the command: '))
python my_little_script.py 
frizzy@localhost in: echo "hello world!"
frizzy@localhost out: hello world!
type the command: echo "hello, username!"
frizzy@localhost in: echo "hello, username!"
frizzy@localhost out: hello, username!
test@127.0.0.1 in: echo "hello world!"
test@127.0.0.1 out: hello world!
type the command: echo "hello, test"             
test@127.0.0.1 in: echo "hello, test"
test@127.0.0.1 out: hello, test

WiKi will be soon

Contributors

About

Automate your own tasks on pure python and execute it on remote or local hosts. Create The Magic

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages