Skip to content

Instantly share code, notes, and snippets.

@dreamingbinary
Forked from adamjkeller/fab roles example
Created June 27, 2018 18:16
Show Gist options
  • Save dreamingbinary/58c66c65346c47fc04c988bdd4f909ec to your computer and use it in GitHub Desktop.
Save dreamingbinary/58c66c65346c47fc04c988bdd4f909ec to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''
This is a high level basic example of roles
'''
from fabric.api import *
@task
def load_hosts(region='region'):
rack = CloudProviderAPI(region=region)
env.roledefs = {
'web': ['web-ip-address', 'web-hostname-2', 'web-ip-address-1'],
'db': ['db-server-1', 'db-server-2'],
'app': ['app-server-1', 'app-server-2', 'app-server-3']
}
@parallel
def ship_code():
put("/tmp/artifact.tar.gz", "/opt/code-location/")
@roles('web', 'app') # Only will run on web and app hosts
@parallel # Task will run in parallel
def restart_service_x():
run("service service_x restart")
@roles('db') #This will ensure that task is only run on db hosts
@serial #This will ensure tasks are run on one host at a time
def do_database_stuff():
run('echo "RUN DB COMMAND HERE ONLY ON DB SERVERS"')
@task
@roles('web', 'app', 'db')
def run():
execute(ship_code)
execute(do_database_stuff)
execute(restart_service_x)
@dreamingbinary
Copy link
Author

then to actually run the deploy, you would do this:

fab load_hosts run

OR you can do this:

fab -R web run << something like that, I need to test it but i'm too busy right now :-P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment