How do I copy a directory to a remote machine using Fabric?

You can use put for that as well (at least in 1.0.0): local_path may be a relative or absolute local file or directory path, and may contain shell-style wildcards, as understood by the Python glob module. Tilde expansion (as implemented by os.path.expanduser) is also performed. See: http://docs.fabfile.org/en/1.0.0/api/core/operations.html#fabric.operations.put Update: This example works fine (for me) on … Read more

How to set target hosts in Fabric file

I do this by declaring an actual function for each environment. For example: def test(): env.user=”testuser” env.hosts = [‘test.server.com’] def prod(): env.user=”produser” env.hosts = [‘prod.server.com’] def deploy(): … Using the above functions, I would type the following to deploy to my test environment: fab test deploy …and the following to deploy to production: fab prod … Read more

How to answer to prompts automatically with python fabric?

Starting from version 1.9, Fabric includes a way of managing this properly. The section about Prompts in the Fabric documentation says: The prompts dictionary allows users to control interactive prompts. If a key in the dictionary is found in a command’s standard output stream, Fabric will automatically answer with the corresponding dictionary value. You should … Read more

fabric password

I know you’ve asked about password but wouldn’t it better to configure the system so that you can doing fabric (i.e. SSH) without password? For this, on local machine do: ssh-keygen and agree with all defaults (if you have no reasons do otherwise) cat ~/.ssh/id_rsa.pub and copy that key On remote machine: mkdir ~/.ssh && … Read more

Activate a virtualenv via fabric as deploy user

As an update to bitprophet’s forecast: With Fabric 1.0 you can make use of prefix() and your own context managers. from __future__ import with_statement from fabric.api import * from contextlib import contextmanager as _contextmanager env.hosts = [‘servername’] env.user=”deploy” env.keyfile = [‘$HOME/.ssh/deploy_rsa’] env.directory = ‘/path/to/virtualenvs/project’ env.activate=”source /path/to/virtualenvs/project/bin/activate” @_contextmanager def virtualenv(): with cd(env.directory): with prefix(env.activate): yield def … Read more

Using an SSH keyfile with Fabric

Finding a simple fabfile with a working example of SSH keyfile usage isn’t easy for some reason. I wrote a blog post about it (with a matching gist). Basically, the usage goes something like this: from fabric.api import * env.hosts = [‘host.name.com’] env.user=”user” env.key_filename=”/path/to/keyfile.pem” def local_uname(): local(‘uname -a’) def remote_uname(): run(‘uname -a’) The important part … Read more