This is second tutorial about the fabric.
Using the old example, I will show you how can use it with some basic features.
The example cover these issues:
- show fabric version;
- use task decorator;
- import and use multiple hosts;
The documentation page come with this info about task:
Fabric 1.1 introduced the Task class to facilitate new features and enable some programming best practices, specifically:
Object-oriented tasks. Inheritance and all that comes with it can make for much more sensible code reuse than passing around simple function objects. The classic style of task declaration didn’t entirely rule this out, but it also didn’t make it terribly easy.
Namespaces. Having an explicit method of declaring tasks makes it easier to set up recursive namespaces without e.g. polluting your task list with the contents of Python’s os module (which would show up as valid “tasks” under the classic methodology.)
With the introduction of Task, there are two ways to set up new tasks:
Decorate a regular module level function with @task, which transparently wraps the function in a Task subclass. The function name will be used as the task name when invoking.
Subclass Task (Task itself is intended to be abstract), define a run method, and instantiate your subclass at module level. Instances’ name attributes are used as the task name; if omitted the instance’s variable name will be used instead.
Let's see the example:
import fabric
from fabric import Connection
print("fabric.__version__")
print(fabric.__version__)
print("fabric.__version_info__")
print(fabric.__version_info__)
print("dir(fabric)")
print(dir(fabric))
import getpass
host = "catafest@tty.sdf.org"
password = getpass.getpass('Password for SDF account:')
with Connection("catafest@tty.sdf.org", connect_kwargs={"password":password}) as con:
print("I will run command: ls")
con.run("ls")
# using the task decorator
from fabric import task
@task
def processes(con):
return con.run("ps -aux", hide = True).stdout.strip()
# print processes
print(processes(con))
con.run("exit")
# use the import for multiple host with all modules
from fabric import Connection, Config, SerialGroup, ThreadingGroup, exceptions, runners
from fabric.exceptions import GroupException
hosts = "catafest@tty.sdf.org,catafest@tty.sdf.org"
def testHosts(hosts):
# Get list of hosts from somewhere, and convert them to connections
hosts = hosts.split(",")
servers = [Connection(host=host,connect_kwargs={"password":password}) for host in hosts]
thread_group = ThreadingGroup.from_connections(servers)
results = thread_group.run("who -a", hide=True)
for r in results:
connection = results[r]
return connection.stdout.strip()
print(testHosts(hosts))
The result of this python script: fabric.__version__
2.5.0
fabric.__version_info__
(2, 5, 0)
dir(fabric)
['Config', 'Connection', 'Executor', 'Group', 'GroupResult', 'Remote', 'Result', 'SerialGroup', 'Task',
'ThreadingGroup', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__',
'__package__', '__path__', '__spec__', '__version__', '__version_info__', '_version', 'config', 'connection',
'exceptions', 'executor', 'group', 'runners', 'task', 'tasks', 'transfer', 'tunnels', 'util']
Password for SDF account:
I will run command: ls
USER PID %CPU %MEM VSZ RSS TTY STAT STARTED TIME COMMAND
...
catafest 10431 0.0 0.0 30496 3120 ? S 5:50PM 0:00.00 sshd:
...
catafest 19243 0.0 0.0 12008 1096 ? O 5:50PM 0:00.00 ps -a
...
user pts/0 Jun 7 17:40 00:10 0 term=0 exit=0 sess=0 type=user process ...