Skip to content

Instantly share code, notes, and snippets.

@brutus
Created October 12, 2014 23:19
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 brutus/98e5a3fd275cd29119b0 to your computer and use it in GitHub Desktop.
Save brutus/98e5a3fd275cd29119b0 to your computer and use it in GitHub Desktop.
A collection of tasks for *Invoke* to set up a Django project with Gunicorn on Uberspace.
# -*- coding: UTF-8 -*-
"""
A collection of tasks for `Invoke`_ to help setting up a `Django`_ project
with `Gunicorn`_ on `Uberspace`_ accounts.
All information for this script has to be set in a JSON file
called ``settings.json`` in the same directory like this::
{
"project": "name of the project",
"settings": {
"stageing": {
"host": "uberspace.host.url",
"user": "username",
"py_path": "path for project and apps",
"py_files": [
"path/to/project", "app1", "app2"
],
"django_port": 63557,
"django_url": "django.project.url",
"django_settings": "project.settings.stageing",
"app": "project.wsgi:application"
}
}
}
More informations: https://wiki.uberspace.de/cool:django
.. _Invoke: http://docs.pyinvoke.org/en/latest/
.. _Django: https://docs.djangoproject.com/en/stable/
.. _Gunicorn: http://gunicorn.org/
.. _Uberspace: https://uberspace.de/
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import json
from invoke import run, task
with open('settings.json') as fh:
SETTINGS = json.load(fh)
def ssh_cmd(command, user, host, echo=True, pty=True):
"""
Runs *command* trough ssh with *user* and *host*.
"""
ssh = "ssh '{user}@{host}' '{cmd}'".format(
user=user, host=host, cmd=command
)
run(ssh, echo=echo, pty=pty)
def get_service_name(project, config):
"""
Returns the name for the gunicorn service.
Given the current *project* and the chosen *config*.
"""
return 'gunicorn-{project}-{config}'.format(
project=project, config=config
)
@task()
def copy(config='stageing'):
"""
Copies your python files to the host.
"""
args = SETTINGS['settings'][config]
dest = args['py_path']
user, host = args['user'], args['host']
for path in args['py_files']:
cmd = "rsync -arv --delete --exclude '*.pyc' '{src}' '{user}@{host}:{dest}'"
cmd = cmd.format(user=user, host=host, src=path, dest=dest)
run(cmd)
@task
def service(config='stageing'):
"""
Creates the *gunicorn* service for ``svc``.
"""
args = SETTINGS['settings'][config]
user, host = args['user'], args['host']
service_name = get_service_name(SETTINGS['project'], config)
# check for `service` dir:
cmd = "test -d ~/service || uberspace-setup-svscan"
ssh_cmd(cmd, user, host)
# install service:
cmd = 'uberspace-setup-service {service} ~/bin/gunicorn --error-logfile - --chdir {path} --bind 127.0.0.1:{port} {app}'
cmd = cmd.format(
service=service_name, path=args['py_path'], port=args['django_port'], app=args['app']
)
cmd = "test -d ~/service/{service} || {cmd}".format(
cmd=cmd, service=service_name
)
ssh_cmd(cmd, user, host)
# set export for DJANGO_SETTINGS_MODULE:
cmd = "sed -i 's#\(\. $HOME/\.bash_profile\)#\1\n\nexport DJANGO_SETTINGS_MODULE={settings}#' ~/service/{service}/run"
cmd.format(
settings=args['django_settings'], service=service_name
)
ssh_cmd(cmd, user, host)
@task
def rmservice(config='stageing'):
"""
Removes the *gunicorn* service.
"""
args = SETTINGS['settings'][config]
service_name = get_service_name(SETTINGS['project'], config)
cmd = "cd ~/service/{service} && rm ~/service/{service} && svc -dx . log; cd && rm -rf ~/etc/run-{service}/".format(
service=service_name
)
ssh_cmd(cmd, args['user'], args['host'])
@task
def http(config='stageing'):
"""
Creates HTTP rewrite rules for Apache.
"""
args = SETTINGS['settings'][config]
cmd = 'cat <<__EOF__> ~/html/{url}/.htaccess\n'.format(
url=args['django_url']
)
cmd += 'RewriteEngine On\n'
cmd += 'RewriteCond %{REQUEST_FILENAME} !-f\n'
cmd += 'RewriteBase /\n'
cmd += 'RewriteRule ^(.*)$ http://127.0.0.1:{port}/\$1 [P]\n'.format(
port=args['django_port']
)
cmd += 'RequestHeader set X-Forwarded-Proto https env=HTTPS\n'
cmd += '__EOF__\n'
ssh_cmd(cmd, args['user'], args['host'])
@task
def rmhttp(config='stageing'):
"""
Removes HTTP rewrite rules.
"""
args = SETTINGS['settings'][config]
cmd = 'rm ~/html/{url}/.htaccess'.format(
url=args['django_url']
)
ssh_cmd(cmd, args['user'], args['host'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment