Last active
September 1, 2017 14:02
-
-
Save renatooliveira/7065365 to your computer and use it in GitHub Desktop.
Minha configuração atual de deploy, usada no projeto http://mobdoctor.me Otimizações são bem vindas :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from fabric.api import env, cd, run, sudo | |
from fabric.contrib.files import exists | |
from fabric.colors import green | |
from config import (HOST, USER, PASSWORD, APP_DIR, | |
ENV_HOME, PROJECT_NAME, VIRTUALENVS, REPOSITORY, BRANCH) | |
env.hosts = [HOST] | |
env.user = USER | |
env.password = PASSWORD | |
env.app_dir = APP_DIR | |
env.home = ENV_HOME | |
env.project_name = PROJECT_NAME | |
env.virtualenv_dir = VIRTUALENVS | |
def _create_ve(project_name): | |
if not exists(env.virtualenv_dir): | |
run('mkdir ~/.virtualenvs') | |
if not exists(env.virtualenv_dir + project_name): | |
with cd(env.virtualenv_dir): | |
mkvirtualenv = 'virtualenv {0}' | |
run(mkvirtualenv.format(project_name)) | |
else: | |
print 'Virtualenv {0} already exists, skipping...'.format( | |
env.project_name | |
) | |
def _run(command, pip=''): | |
run('{venv}{project_name}/bin/{target} {command}'.format( | |
venv=env.virtualenv_dir, | |
project_name=env.project_name, | |
command=command, target=pip or 'python')) | |
def _update_app(): | |
with cd(env.app_dir): | |
run('git pull origin {0}'.format(BRANCH)) | |
_run('install -r requirements.prod.txt', 'pip') | |
_run('manage.py syncdb') | |
_run('manage.py migrate') | |
_run('manage.py collectstatic') | |
def _restart_app(): | |
sudo('service nginx restart') | |
sudo('supervisorctl restart {project_name}'.format(**env)) | |
def deploy(): | |
_update_app() | |
_restart_app() | |
def setup_nginx(): | |
with cd('/etc/nginx/sites-enabled/'): | |
sudo('touch {0}'.format(env.project_name)) | |
with open('nginx') as nginx: | |
format_dict = { | |
'project_name': env.project_name, | |
'host': HOST, 'user': env.user | |
} | |
text = ''.join(nginx.readlines()) % format_dict | |
sudo("echo '{0}' > {1}". format(text, env.project_name)) | |
def setup_supervisor(): | |
with cd('/etc/supervisor/conf.d/'): | |
sudo('touch {0}.conf'.format(env.project_name)) | |
with open('supervisor') as supervisor: | |
text = ''.join(supervisor.readlines()).format( | |
project_name=env.project_name, user=env.user | |
) | |
sudo("echo '{0}' > {1}.conf". format(text, env.project_name)) | |
sudo("supervisorctl reload") | |
def bootstrap(): | |
_create_ve(env.project_name) | |
if not exists('~/{0}'.format(env.project_name)): | |
run('git clone {0}'.format(REPOSITORY)) | |
setup_nginx() | |
setup_supervisor() | |
_update_app() | |
_restart_app() | |
print green('Well done!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing | |
user = "user" | |
workers = multiprocessing.cpu_count() * 2 + 1 | |
bind = "localhost:some_port" | |
pidfile = "/tmp/mobdoctor.pid" | |
name = "mobdoctor" | |
backlog = 2048 | |
logfile = "home/user/logs/gunicorn.mobdoctor.log" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
listen 80; | |
server_name mobdoctor.me; | |
location /static/ { | |
alias /home/%(user)s/%(project_name)s/%(project_name)s/static_files/; | |
expires 30d; | |
} | |
location /media/ { | |
alias /home/%(user)s/%(project_name)s/%(project_name)s/media/; | |
expires 30d; | |
} | |
access_log /home/%(user)s/logs/access.%(project_name)s.log; | |
error_log /home/%(user)s/logs/error.%(project_name)s.log; | |
location / { | |
proxy_pass_header Server; | |
proxy_set_header Host $host; | |
proxy_redirect off; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Scheme $scheme; | |
proxy_pass http://localhost:9500/; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[program:{project_name}] | |
command=/home/{user}/.virtualenvs/{project_name}/bin/python /home/{user}/{project_name}/manage.py run_gunicorn -c /home/{user}/{project_name}/gunicorn.conf.py | |
user={user} | |
autostart=true | |
autorestart=true | |
stdout_logfile = /home/{user}/logs/supervisor.{project_name}.log | |
redirect_stderr=true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Eu prefiro disparar vários processos individuais ouvindo em portas TCP diferentes e coloca-los como upstream no nginx... algo +/- parecido com isso aqui: http://justcramer.com/2013/06/27/serving-python-web-applications/
Mas como disse num comentário em seu commit no projeto que vc criou para hospedar essas configurações eu pretendo testar outra pilha de software: http://nathancahill.github.io/circus/