def setup_webserver():
    """
    Set up (bootstrap) a new server.
    
    This essentially does all the tasks in the script done by hand in one
    fell swoop. In the real world this might not be the best way of doing
    this -- consider, for example, what the various creation of directories,
    git repos, etc. will do if those things already exist. However, it's
    a useful example of a more complex Fabric operation.
    """
    if hasattr(env, 'use_nginx'):
        server_req = "nginx"
    else:
        server_req = "apache2 libapache2-mod-wsgi"
    # Initial setup and package install.
    sudo("aptitude update")
    sudo("aptitude -y install git-core python-dev python-setuptools "
                              "postgresql-dev postgresql-client build-essential "
                              "libpq-dev subversion mercurial "
                              "binutils proj gdal-bin %s "
                              "python-pip" % server_req)
    if hasattr(env, 'use_nginx'):
        _put_template("nginx/nginx_webserver.conf", "%(nginx_confdir)snginx.conf" % env, env, use_sudo=True)
        sudo("mkdir -p %(nginx_confdir)ssites-enabled" % env)
    else:
        with cd("/etc/apache2"):
            sudo("rm -rf apache2.conf conf.d/ httpd.conf magic mods-* sites-* ports.conf")
        _put_template("apache/apache2.conf", "/etc/apache2/apache2.conf", env, use_sudo=True)
        sudo("mkdir -m777 -p /var/www/.python-eggs")
        sudo("mkdir -p /etc/apache2/sites-enabled" % env)
def setup_webapp():
    """ Setup virtualenv/startup scripts/configs for webapp """
    sudo("pip install -U virtualenv")
    run("virtualenv /home/%(user)s/%(project_name)s --distribute" % env)
    run("mkdir -p /home/%(user)s/%(project_name)s" % env)
    run("mkdir -p /home/%(user)s/static" % env)
    if hasattr(env, 'use_nginx'):
        _put_template("nginx/nginx_webapp.conf", "%(nginx_confdir)ssites-enabled/%(project_name)s.conf" % env, env, use_sudo=True)
        _put_template("config/django_gunicorn", "/home/%(user)s/%(project_name)s/bin/django_gunicorn" % env, env)
        run("chmod gu+rx /home/%(user)s/%(project_name)s/bin/django_gunicorn" % env)
    else:
        _put_template("apache/site.conf", "/etc/apache2/sites-enabled/%(project_name)s.conf" % env, env, use_sudo=True)
def push_wsgi():
    _put_template("config/app.wsgi", "%(wsgipath)s" % env, env, use_sudo=True)
def push_django_settings():
    _put_template("config/local_settings.py",
        "%(root)s/django-carpool/carpool/local_settings.py" % env, env)