Deploy with fabric on multiple servers

Ever needed to deploy with fabric on multiple machines?
Here's a simple solution to aproach this issue.

In your fabfile.py , add these two methods:
def live():
global PATH, ENV_PATH
env.hosts = ["22.2.222.2"]
env.user = 'test'
PATH = '/path/to/project'
# optional, is using virtualenv
ENV_PATH = '/path/to/virtualenv'
# overwrite whatever variabled you need to change on the current machine

def staging():
global PATH, ENV_PATH
env.hosts = ["11.1.11.1"]
env.user = 'test2'
PATH = '/path/to/project/on/second/machine'
# optional, is using virtualenv
ENV_PATH = '/path/to/virtualenv'
# overwrite whatever variabled you need to change on the current machine

Now, when deploying, for example:
def deploy():
with cd(PATH), virtualenv(ENV_PATH):
run('uber command')
run('another uber command')

you must use the following commands, in the correct order:
#deploy on staging
fab staging deploy

# deploy on live
fab live deploy

Fabric runs the "staging" or "deploy" commands first, setting the envrinoment variables, and then it runs the "deploy" command doing whatever you asked it to do.

Comments

Popular posts from this blog

Improve font rendering for IntelliJ IDEA / Pycharm in Ubuntu

Fix - Updated from OSX El Capitan Gold Master to Final Release, andbeta update keeps showing in AppStore