Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created February 19, 2012 00:00
Show Gist options
  • Save jordanorelli/1861363 to your computer and use it in GitHub Desktop.
Save jordanorelli/1861363 to your computer and use it in GitHub Desktop.
runtime-generated fabric connection functions, yay
from fabric.api import env
import os
import string
_conf_root = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'conf')
_conf_dirs = [x for x in os.listdir(_conf_root)
if os.path.isdir(os.path.join(_conf_root, x))
and x != 'base']
def _mk_sethost_fn(host_dicts, context):
def inner():
""" (Autogenerated host connection function.) """
env.update(context)
hoststrings = []
if env.key_filename == None: env.key_filename = []
for host in host_dicts:
hostname = host['hostname']
user = host.get('user', '')
port = host.get('port', '')
hoststring = '%s%s%s' % (user and user + '@',
hostname,
port and ':' + str(port),
)
hoststrings.append(hoststring)
key_filename = host.get('key_filename')
if key_filename:
env.key_filename.append(key_filename)
env.hosts = hoststrings
return inner
for x in _conf_dirs:
try:
module_name = string.join(('conf', x, 'hosts'), '.')
module = __import__(module_name, fromlist=['hosts', 'prefix'])
hosts = module.hosts
context = {
'prefix': module.prefix,
}
module_name = string.join(('conf', x, 'gunicorn'), '.')
module = __import__(module_name, fromlist=['pidfile'])
context['pidfile'] = module.pidfile
locals()[x] = _mk_sethost_fn(hosts, context)
except ImportError:
pass
# this would go in conf/sample/hosts.py
import os
current_dir = os.path.dirname(os.path.abspath(__file__))
hosts = (
{
'hostname': '0.0.0.0', # IP address here
'port': 666, # port here
'key_filename': os.path.join(current_dir, 'ssh_key'), # symlink a ssh key in if you need one
},
)
virtualenv_path = '/path/to/your/virtual-env' # location of the virtualenv on that host
project_path = '/path/to/your/project' # location to the project on that host.
activate_cmd = 'source %s/bin/activate' % virtualenv_path
prefix = '%s && cd %s' % (activate_cmd, project_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment