Skip to content

Instantly share code, notes, and snippets.

@tachang
Created April 21, 2013 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tachang/5430196 to your computer and use it in GitHub Desktop.
Save tachang/5430196 to your computer and use it in GitHub Desktop.
Fabfile to setup a new application server
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.files import *
import fabtools.require.git
import fabtools
def install_packages():
packages = ['build-essential','git-core','python-dev']
with settings(user='root', key_filename='./root-deploy.key'):
for package in packages:
run("apt-get -qq -y install %s" % package)
run("groupadd -f example")
run("id -u example &> /dev/null || useradd -g example -m -d /home/example example")
keyfile = './example-deployment.pub'
example_public_key = open(keyfile).read()
sudo("mkdir -p /home/example/.ssh", user="example", shell=False)
if exists('/home/example/.ssh/authorized_keys'):
if not contains('/home/example/.ssh/authorized_keys', example_public_key):
append('/home/example/.ssh/authorized_keys', example_public_key)
def setup_app_server():
with settings(user='example', key_filename='./example-deployment.key'):
fabtools.require.python.virtualenv("/home/example/python", user='example', system_site_packages=False, clear=True)
with fabtools.python.virtualenv('/home/example/python'):
# fabtools installs distribute as root. I want to install it as example user
with cd("/tmp"):
run("curl --silent -O http://python-distribute.org/distribute_setup.py")
run("python distribute_setup.py")
# If I don't install distribute manually this makes me sudo to root
fabtools.require.python.distribute()
fabtools.require.python.pip()
put('ssh/config', '/home/example/.ssh/config')
put('example-deployment.key', '/home/example/.ssh/',mode=0700)
run("ssh-keyscan -t rsa,dsa github.com 2>&1 | sort -u - ~/.ssh/known_hosts > ~/.ssh/tmp_hosts")
run("cat ~/.ssh/tmp_hosts >> ~/.ssh/known_hosts")
fabtools.require.git.working_copy("https://github.com/example/example_project.git", path="example_project", branch="master", update=True)
fabtools.require.python.requirements("~/example_project/requirements.txt")
@ronnix
Copy link

ronnix commented Apr 21, 2013

Tip: instead of using apt-get directly, you can write:

fabtools.require.deb.packages([
    'build-essential',
    'git-core',
    'python-dev',
])

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