Skip to content

Instantly share code, notes, and snippets.

@minskmaz
Last active September 29, 2015 05:56
Show Gist options
  • Save minskmaz/7207f994a8a28451aeaa to your computer and use it in GitHub Desktop.
Save minskmaz/7207f994a8a28451aeaa to your computer and use it in GitHub Desktop.
basic role based delegation of env object in python's Fabric - (untested but should work)
import os
import fabric
from fabric.api import *
from fabric.api import run, roles, execute
from fabric.operations import sudo
if env.ssh_config_path and os.path.isfile(os.path.expanduser(env.ssh_config_path)):
env.use_ssh_config = True
"""
every key in 'localbox' dict (appearing in roledefs) is set as a key/val pair
on the 'env' object when @roles('localbox') decorates a function - context manager
"""
roledefs = {
'localbox':{
'hosts':['nona@127.0.0.1'],
'keyfiles':{'nona@127.0.0.1':'~/.ssh/id_rsa.pub'},
'os':'osx'
},
'ottowa':{
'hosts':['otto@127.0.0.1'],
'passwords':{
'otto@127.0.0.1':uid
},
'os':'osx'
},
'shanghai':{
'hosts':['ubuntu@jws'],
'key_filename':'~/.ssh/_gis.pem',
'os':'ubuntu'
}
}
"""
where we register multiple environments onto env which are selected by @roles decorator
"""
env.roledefs = {
'localbox':roledefs.get('localbox'),
'ottowa':roledefs.get('ottowa'),
'shanghai':roledefs.get('shanghai')
}
def update_ubuntu():
run('sudo apt-get update')
def update_osx():
run('sudo softwareupdate -i -a')
def update_by_os():
if env.os == 'osx':
update_osx()
if env.os == 'ubuntu':
update_ubuntu()
@roles('localbox', 'ottowa', 'shanghai')
def update_servers():
execute(update_by_os)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment