Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# 1. lanza instancia
# 2. ssh instancia (click en ec2 instance, click en boton [connect] para ver info de conexion.
# update ubuntu
> export LANGUAGE=en_US.UTF-8
> export LANG=en_US.UTF-8
> export LC_ALL=en_US.UTF-8
> sudo apt-get update
> sudo apt-get upgrade
> sudo dpkg-reconfigure tzdata
# instalar dependencias
> sudo apt-get install build-essential python-dev libpcre3-dev libssl-dev python-setuptools git
# estructura de directorios
> mkdir -p ~/www/dq.com/src ~/www/dq.com/logs ~/server/logs
# git pull en src
# generate ssh key
> ssh-keygen -t rsa -b 4096 -C "thepanchi@gmail.com"
> vim /home/ubuntu/.ssh/id_rsa.pub # github / accounts / ssh keys
> git clone git@github.com:XXX/XXX.git
> sudo easy_install pip
> sudo pip install virtualenvwrapper
> source /usr/local/bin/virtualenvwrapper.sh
> mkvirtualenv XXX
# install requirements
> pip install -r deploy/requirements.txt
# link symbolico en ~/www/dq.com para el django project
# runserver y probar en browser con public DNS de la instancia, puerto 8000
> python manage.py runserver 0.0.0.0:8000
# as supervisord process, instalar global, osea fuera del env del proyecto
> sudo pip install supervisor
> sudo vim /etc/supervisord.conf
# agrega el contenido de supervisord.conf
# inicia supervisor
# que arranque cuando la instancia arranque
> sudo su
> crontab -e
# @reboot unlink /tmp/supervisor.sock; /usr/local/bin/supervisord -c /etc/supervisord.conf
# install uwsgi
> pip install uwsgi
> /home/ubuntu/.virtualenvs/dq/bin/uwsgi --master --http 0.0.0.0:8000 --processes 1 --wsgi-file /home/ubuntu/www/dq.com/dq/xtest/wsgi.py --pidfile /home/ubuntu/uwsgi.pid --home /home/ubuntu/.virtualenvs/dq --pythonpath /home/ubuntu/www/dq.com/dq
# tiempo para nginx? if True:
> wget 'http://nginx.org/download/nginx-1.0.0.tar.gz'
> tar -xzvf nginx-1.0.0.tar.gz
> cd nginx-1.0.0
> ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
> make
> sudo make install
> sudo vim /etc/init.d/nginx
# paste the contents of etc.init.d.nginx
# permisos de ejecucion y que arranque al inicio
> sudo chmod +x /etc/init.d/nginx
> sudo /usr/sbin/update-rc.d -f nginx defaults
> sudo vim /usr/local/nginx/conf/nginx.conf
# replace contents with usr.local.nginx.conf.nginx.conf
---------
# imprime algo que indique cual es el server:
# crea un view y imprima el valor de http://169.254.169.254/latest/meta-data/instance-id
---------
# cosas de AWS
# crea un AMI
# crea aws-cli en developer pc
# login con credenciales del developer: aws configure --profile test
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/home/ubuntu/server/logs/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
[program:dq.com]
command=/home/ubuntu/.virtualenvs/dq/bin/uwsgi --master --http 0.0.0.0:8000 --processes 1 --wsgi-file /home/ubuntu/www/dq.com/dq/xtest/wsgi.py --pidfile /home/ubuntu/uwsgi.pid --home /home/ubuntu/.virtualenvs/dq --pythonpath /home/ubuntu/www/dq.com/dq
directory=/home/ubuntu/www/dq.com/dq
user=ubuntu
autostart=true
autorestart=true
stdout_logfile=/home/ubuntu/www/dq.com/logs/supervisord.log
redirect_stderr=true
environment=HOME=/home/ubuntu,USER=ubuntu
stopsignal=INT
from contextlib import contextmanager as _contextmanager
from fabric.context_managers import prefix
from fabric.operations import get, run, sudo
from fabric.state import env
from fabric.contrib import django
import boto3
django.project('xtest')
from django.conf import settings
running_instances = []
s = boto3.session.Session(profile_name='xtest')
ec2 = s.resource("ec2")
def get_ec2_instances():
instances = ec2.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]
)
for i in instances:
ssh_access = "ubuntu@{0}".format(i.public_dns_name)
print 'servers >', ssh_access
running_instances.append(ssh_access)
get_ec2_instances()
environments = {
'production': {
'hosts': running_instances,
'source_code': '/home/ubuntu/www/dq.com/dq',
'supervisor_commands': [
'supervisorctl restart dq.com'
],
'virtualenv': {
'virtualenv_name': 'dq',
'virtualenv_sh': '/usr/local/bin/virtualenvwrapper.sh',
},
'git': {
'parent': 'origin',
'branch': 'master',
}
}
}
# Utils
@_contextmanager
def virtualenv():
""" Wrapper to run commands in the virtual env context """
environment = environments['default']
workon_home = environment['virtualenv'].get('workon_home',
'~/.virtualenvs')
with prefix('export WORKON_HOME={0}'.format(workon_home)):
virtualenv_sh = environment['virtualenv'].get('virtualenv_sh',
'/etc/bash_completion.d/virtualenvwrapper')
with prefix('source {0}'.format(virtualenv_sh)):
virtualenv_name = environment['virtualenv'].get('virtualenv_name')
with prefix('workon {0}'.format(virtualenv_name)):
source_code = environment['source_code']
with prefix('cd {0}'.format(source_code)):
yield
def django(command):
with virtualenv():
full_command = 'python manage.py {0}'.format(command)
run(full_command)
# setup
def production():
environments['default'] = environments['production']
env.hosts = environments['production']['hosts']
env.key_filename = 'cmhost-oregon-tests2.pem'
#tasks
def test_connection():
run('free -m')
def git_pull():
with virtualenv():
run('git pull %s %s' % (environments['default']['git']['parent'],
environments['default']['git']['branch']))
#run('git pull')
def pip_install():
with virtualenv():
run('pip install -r requirements.txt')
def pyclean():
with virtualenv():
run('find . -type f -name "*.py[co]" -exec rm -f \{\} \;')
def supervisor_restart():
for supervisor in environments['default']['supervisor_commands']:
sudo(supervisor)
def deploy():
git_pull()
pyclean()
supervisor_restart()
"""
Filters=[
{'Name': 'tag-key', 'Values': ['env']},
{'Name': 'tag-value', 'Values': ['qa']},
]
"""
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
DESC=nginx
PIDFILE=/usr/local/nginx/logs/nginx.pid
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
. /lib/lsb/init-functions
test_nginx_config() {
if nginx -t $DAEMON_OPTS
then
return 0
else
return $?
fi
}
case "$1" in
start)
echo -n "Starting $DESC: "
test_nginx_config
start-stop-daemon --start --quiet --pidfile $PIDFILE \
--exec $DAEMON -- $DAEMON_OPTS || true
echo "nginx."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile $PIDFILE \
--exec $DAEMON || true
echo "nginx."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
$PIDFILE --exec $DAEMON || true
sleep 1
test_nginx_config
start-stop-daemon --start --quiet --pidfile \
$PIDFILE --exec $DAEMON -- $DAEMON_OPTS || true
echo "nginx."
;;
reload)
echo -n "Reloading $DESC configuration: "
test_nginx_config
start-stop-daemon --stop --signal HUP --quiet --pidfile $PIDFILE \
--exec $DAEMON || true
echo "nginx."
;;
configtest)
echo -n "Testing $DESC configuration: "
if test_nginx_config
then
echo "nginx."
else
exit $?
fi
;;
status)
status_of_proc -p $PIDFILE "$DAEMON" nginx && exit 0 || exit $?
;;
*)
echo "Usage: nginx {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 1
;;
esac
exit 0
# /home/admin/www/<project.com>/conf/nginx.conf
upstream app_server_<project_name> {
#server unix:/tmp/gunicorn.sock fail_timeout=0;
# For a TCP configuration:
server 127.0.0.1:8000 fail_timeout=0;
}
server {
listen 80;
server_name <project.com>;
charset utf-8;
access_log /home/admin/www/<project.com>/logs/nginx-access.log;
error_log /home/admin/www/<project.com>/logs/nginx-error.log;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
# Redirect www subdomain
if ($host = 'www.<project.com>' ) {
rewrite ^/(.*)$ http://<project.com>/$1 permanent;
}
# Django admin media.
location /media/ {
alias /home/admin/.virtualenvs/<project.com>/lib/python2.6/site-packages/django/contrib/admin/media/;
}
# Site media
location /static/ {
alias /home/admin/www/<project.com>/waffil/trunk/static/;
}
# Finally, send all non-media requests to the Django server.
location / {
#auth_basic "Restricted";
#auth_basic_user_file /home/admin/www/<project.com>/conf/htpasswd;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server_<project_name>;
break;
}
}
}
user www-data;
worker_processes 1;
error_log /home/admin/server/logs/nginx-error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /usr/local/nginx/conf/mime.types;
access_log /home/admin/server/logs/nginx-access.log;
default_type application/octet-stream;
keepalive_timeout 10;
tcp_nodelay on;
client_max_body_size 20m;
sendfile on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
# Directories
client_body_temp_path /tmp/client_body/ 2 2;
fastcgi_temp_path /tmp/fastcgi/;
proxy_temp_path /tmp/proxy/;
uwsgi_temp_path /tmp/uwsgi/;
include /etc/nginx/conf.d/*.conf;
include /home/admin/server/sites-enabled/*;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment