Skip to content

Instantly share code, notes, and snippets.

@sanjmen
Last active December 20, 2015 23:48
Show Gist options
  • Save sanjmen/6214692 to your computer and use it in GitHub Desktop.
Save sanjmen/6214692 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""Management utilities."""
from fabric.contrib.console import confirm
from fabric.api import abort, env, local, settings, task, run, roles, put, cd, lcd, execute, sudo
# GLOBALS:
env.run = 'python manage.py'
env.settings = 'settings'
@task
def production():
env.hosts = ["user@ip"]
env.passwords = {"user@ip": "password"}
env.roledefs = {
'web': ['user@ip'],
}
# HELPERS:
def cont(cmd, message):
"""Given a command, ``cmd``, and a message, ``message``, allow a user to
either continue or break execution if errors occur while executing ``cmd``.
:param str cmd: The command to execute on the local system.
:param str message: The message to display to the user on failure.
.. note::
``message`` should be phrased in the form of a question, as if ``cmd``'s
execution fails, we'll ask the user to press 'y' or 'n' to continue or
cancel exeuction, respectively.
Usage::
cont('heroku run ...', "Couldn't complete %s. Continue anyway?" % cmd)
"""
with settings(warn_only=True):
result = local(cmd, capture=True)
if message and result.failed and not confirm(message):
abort('Stopped execution per user request.')
# DATABASE MANAGEMENT:
@task
def syncdb():
"""Run a syncdb."""
local('{run} syncdb --noinput'.format(**env))
@task
def migrate(app=None):
"""Apply one (or more) migrations. If no app is specified, fabric will
attempt to run a site-wide migration.
:param str app: Django app name to migrate.
"""
if app:
local('{run} migrate {app} --noinput'.format(run=env.run, app=app))
else:
local('{run} migrate --noinput'.format(**env))
# FILE MANAGEMENT:
@task
def collectstatic():
"""Collect all static files, and copy them to S3 for production usage."""
local('{run} collectstatic --noinput'.format(**env))
# TESTS:
@task
def test():
"""Run tests"""
local('{run} test events --settings={settings}'.format(**env))
# MANAGEMENT:
# TODO:
@task
def clean():
""""""
local('{run} clean_pyc --settings={settings}'.format(**env))
local('rm -Rf .coverage')
@task
@roles('web')
def status():
sudo("supervisorctl status")
@task
@roles('web')
def restart():
sudo("supervisorctl restart orcheeder")
@task
@roles('web')
def deploy():
try:
result = local("ls fabfile.py", capture=True)
except:
abort('Please run this command from repository root directory!')
local("python manage.py clean_pyc")
with lcd("../"):
local("tar czf /tmp/orcheeder.tar.gz --exclude=backups --exclude=.git --exclude=local_settings.py --exclude=fabfile.py fase2/")
put('/tmp/orcheeder.tar.gz', '/tmp')
with cd('/home/orcheeder'):
run('tar xzf /tmp/orcheeder.tar.gz')
run("rm /tmp/orcheeder.tar.gz")
local("rm /tmp/orcheeder.tar.gz")
execute(status)
execute(restart)
execute(status)
@task
@roles('web')
def restart_service(service="memcached"):
sudo("/etc/init.d/{service} restart".format(service=service))
@task
def reindex():
"""Run a syncdb."""
local('{run} rebuild_index --noinput'.format(**env))
ser www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
server {
server_name www.orcheeder.com orcheeder.com;
if ($host != 'www.orcheeder.com' ) {
rewrite ^/(.*)$ http://www.orcheeder.com/$1 permanent;
}
# Serve an empty 1x1 gif _OR_ an error 204 (No Content) for favicon.ico
location = /favicon.ico {
empty_gif;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 60;
proxy_read_timeout 300;
proxy_send_timeout 90;
proxy_pass http://localhost:8081/;
}
# what to serve if upstream is not available or crashes
#error_page 500 502 503 504 /media/50x.html;
}
[program:orcheeder]
command=/home/orcheeder/env/bin/python /home/orcheeder/fase2/manage.py run_gunicorn -b localhost:8081 -w 3 -t 300
directory=/home/orcheeder/fase2/
user=nobody
autostart=true
autorestart=true
redirect_stderr=True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment