Skip to content
Browse files

Add ansible stuff, requirements and Vagrantfile

  • Loading branch information...
1 parent 64c0929 commit 94baee24ab7228b80a26d4a3a2ff0b842a92d372 @mattupstate committed Aug 29, 2012
View
32 Vagrantfile
@@ -0,0 +1,32 @@
+require 'vagrant-ansible'
+
+Vagrant::Config.run do |config|
+ config.vm.define :web do |web_config|
+ web_config.vm.box = "lucid64"
+ web_config.vm.box_url = "http://files.vagrantup.com/lucid64.box"
+
+ web_config.vm.forward_port 80, 8080
+ web_config.vm.network :bridged
+ web_config.vm.network :hostonly, "192.168.100.10"
+
+ web_config.vm.provision :ansible do |ansible|
+ ansible.playbook = "devops/webserver.yml"
+ ansible.hosts = "webservers"
+ end
+ end
+
+ config.vm.define :db do |db_config|
+ db_config.vm.box = "lucid64"
+ db_config.vm.box_url = "http://files.vagrantup.com/lucid64.box"
+
+ db_config.vm.forward_port 5432, 54322
+ db_config.vm.network :bridged
+ db_config.vm.network :hostonly, "192.168.100.20"
+
+ db_config.vm.provision :ansible do |ansible|
+ ansible.playbook = "devops/dbserver.yml"
+ ansible.hosts = "dbservers"
+ end
+ end
+
+end
View
4 app.py
@@ -4,11 +4,10 @@
app = Flask(__name__)
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = \
- 'postgres://postgres@192.168.100.11/ansible_vagrant_test'
+ 'postgres://postgres@192.168.100.20/vagrant_ansible_tutorial'
db = SQLAlchemy(app)
-
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
@@ -18,7 +17,6 @@ def __init__(self, username, email):
self.username = username
self.email = email
-
@app.route('/')
def index():
return render_template('index.html', users=User.query.all())
View
34 devops/dbserver.yml
@@ -0,0 +1,34 @@
+---
+
+- hosts: dbservers
+ sudo: True
+ user: vagrant
+
+ tasks:
+ - name: install python-software-properties
+ action: apt pkg=python-software-properties state=present
+
+ - name: update apt repo
+ action: command /usr/bin/apt-get update
+
+ - name: install postgres packages
+ action: apt pkg=$item state=present
+ with_items:
+ - build-essential
+ - python2.6-dev
+ - postgresql
+ - libpq-dev
+ - python-setuptools
+ - python-psycopg2
+
+ - name: upgrade psycopg2
+ action: easy_install name=psycopg2==2.4.5
+
+- hosts: dbservers
+ sudo: True
+ user: vagrant
+ sudo_user: postgres
+
+ tasks:
+ - name: create test database
+ action: postgresql_db name=vagrant_ansible_tutorial
View
3 devops/deploy.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+ansible-playbook devops/deploy.yml -i devops/hosts --private-key=$HOME/.vagrant.d/insecure_private_key
View
47 devops/deploy.yml
@@ -0,0 +1,47 @@
+---
+
+- hosts: webservers
+ user: vagrant
+ sudo: True
+
+ vars:
+ app_name: hello_flask
+ repo_url: https://github.com/mattupstate/vagrant-ansible-tutorial.git
+ repo_remote: origin
+ repo_version: master
+ webapps_dir: /srv/webapps
+ wsgi_file: wsgi.py
+ wsgi_callable: app
+
+ tasks:
+ - name: ensure log directory
+ action: file dest=${webapps_dir}/${app_name}/log state=directory
+
+ - name: deploy code from repository
+ action: git repo=$repo_url dest=${webapps_dir}/${app_name}/src remote=$repo_remote version=$repo_version
+
+ - name: install dependencies into virtualenv
+ action: pip requirements=${webapps_dir}/${app_name}/src/requirements.txt virtualenv=${webapps_dir}/${app_name}/venv state=present
+
+ - name: create supervisor program config
+ action: template src=templates/supervisor.ini dest=/etc/supervisor/${app_name}.ini
+ notify:
+ - restart app
+
+ - name: create nginx site config
+ action: template src=templates/nginx_site.conf dest=/etc/nginx/sites-available/${app_name}.conf
+ notify:
+ - restart nginx
+
+ - name: link nginx config
+ action: file src=/etc/nginx/sites-available/${app_name}.conf dest=/etc/nginx/sites-enabled/${app_name}.conf state=link
+
+ - name: start app
+ action: supervisorctl name=${app_name} state=started
+
+ handlers:
+ - name: restart app
+ action: supervisorctl name=${app_name} state=restarted
+
+ - name: restart nginx
+ action: service name=nginx state=restarted
View
5 devops/hosts
@@ -0,0 +1,5 @@
+[webservers]
+192.168.100.10
+
+[dbservers]
+192.168.100.20
View
30 devops/templates/nginx.conf
@@ -0,0 +1,30 @@
+worker_processes 1;
+worker_rlimit_nofile 8192;
+
+events {
+ worker_connections 3000;
+}
+
+error_log /var/log/nginx/error.log warn;
+pid /var/run/nginx.pid;
+
+http {
+ include /etc/nginx/mime.types;
+ default_type application/octet-stream;
+
+ log_format main '$remote_addr - $remote_user [$time_local] "$request" '
+ '$status $body_bytes_sent "$http_referer" '
+ '"$http_user_agent" "$http_x_forwarded_for"';
+
+ access_log /var/log/nginx/access.log main;
+
+ sendfile on;
+ #tcp_nopush on;
+
+ keepalive_timeout 65;
+
+ #gzip on;
+
+ include /etc/nginx/conf.d/*.conf;
+ include /etc/nginx/sites-enabled/*;
+}
View
19 devops/templates/nginx_site.conf
@@ -0,0 +1,19 @@
+server {
+ listen 80;
+ server_name localhost;
+
+ access_log {{ webapps_dir }}/{{ app_name }}/log/nginx-access.log;
+ error_log {{ webapps_dir }}/{{ app_name }}/log/nginx-error.log;
+
+ location / {
+ include uwsgi_params;
+ uwsgi_pass unix:/tmp/{{ app_name }}.sock;
+ }
+
+ location /static {
+ alias {{ webapps_dir }}/{{ app_name }}/src/{{ app_name }}/static;
+ expires 1y;
+ add_header Cache-Control "public";
+ }
+
+}
View
15 devops/templates/supervisor.ini
@@ -0,0 +1,15 @@
+[program:{{ app_name }}]
+command=/usr/local/bin/uwsgi
+ --socket /tmp/{{ app_name }}.sock
+ --logto {{ webapps_dir }}/{{ app_name }}/log/application.log
+ --home {{ webapps_dir }}/{{ app_name }}/venv
+ --pythonpath {{ webapps_dir }}/{{ app_name }}/src
+ --wsgi-file {{ webapps_dir }}/{{ app_name }}/src/{{ wsgi_file }}
+ --callable {{ wsgi_callable }}
+ --max-requests 1000
+ --master
+ --processes 1
+ --chmod
+directory={{ webapps_dir }}/{{ app_name }}/src
+autostart=true
+autorestart=true
View
129 devops/templates/supervisord.conf
@@ -0,0 +1,129 @@
+; Sample supervisor config file.
+
+[unix_http_server]
+file=/tmp/supervisor.sock ; (the path to the socket file)
+;chmod=0700 ; socket file mode (default 0700)
+;chown=nobody:nogroup ; socket file uid:gid owner
+;username=user ; (default is no username (open server))
+;password=123 ; (default is no password (open server))
+
+;[inet_http_server] ; inet (TCP) server disabled by default
+;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
+;username=user ; (default is no username (open server))
+;password=123 ; (default is no password (open server))
+
+[supervisord]
+logfile=/var/log/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)
+;umask=022 ; (process file creation umask;default 022)
+;user=chrism ; (default is current user, required if root)
+;identifier=supervisor ; (supervisord identifier, default is 'supervisor')
+;directory=/tmp ; (default is not to cd during start)
+;nocleanup=true ; (don't clean up tempfiles at start;default false)
+;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
+;environment=KEY=value ; (key value pairs to add to environment)
+;strip_ansi=false ; (strip ansi escape codes in logs; def. false)
+
+; the below section must remain in the config file for RPC
+; (supervisorctl/web interface) to work, additional interfaces may be
+; added by defining them in separate rpcinterface: sections
+[rpcinterface:supervisor]
+supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
+
+[supervisorctl]
+serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
+;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
+;username=chris ; should be same as http_username if set
+;password=123 ; should be same as http_password if set
+;prompt=mysupervisor ; cmd line prompt (default "supervisor")
+;history_file=~/.sc_history ; use readline history if available
+
+; The below sample program section shows all possible program subsection values,
+; create one or more 'real' program: sections to be able to control them under
+; supervisor.
+
+;[program:theprogramname]
+;command=/bin/cat ; the program (relative uses PATH, can take args)
+;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
+;numprocs=1 ; number of processes copies to start (def 1)
+;directory=/tmp ; directory to cwd to before exec (def no cwd)
+;umask=022 ; umask for process (default None)
+;priority=999 ; the relative start priority (default 999)
+;autostart=true ; start at supervisord start (default: true)
+;autorestart=true ; retstart at unexpected quit (default: true)
+;startsecs=10 ; number of secs prog must stay running (def. 1)
+;startretries=3 ; max # of serial start failures (default 3)
+;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
+;stopsignal=QUIT ; signal used to kill process (default TERM)
+;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
+;user=chrism ; setuid to this UNIX account to run the program
+;redirect_stderr=true ; redirect proc stderr to stdout (default false)
+;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
+;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
+;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
+;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
+;stdout_events_enabled=false ; emit events on stdout writes (default false)
+;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
+;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
+;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
+;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
+;stderr_events_enabled=false ; emit events on stderr writes (default false)
+;environment=A=1,B=2 ; process environment additions (def no adds)
+;serverurl=AUTO ; override serverurl computation (childutils)
+
+; The below sample eventlistener section shows all possible
+; eventlistener subsection values, create one or more 'real'
+; eventlistener: sections to be able to handle event notifications
+; sent by supervisor.
+
+;[eventlistener:theeventlistenername]
+;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
+;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
+;numprocs=1 ; number of processes copies to start (def 1)
+;events=EVENT ; event notif. types to subscribe to (req'd)
+;buffer_size=10 ; event buffer queue size (default 10)
+;directory=/tmp ; directory to cwd to before exec (def no cwd)
+;umask=022 ; umask for process (default None)
+;priority=-1 ; the relative start priority (default -1)
+;autostart=true ; start at supervisord start (default: true)
+;autorestart=unexpected ; restart at unexpected quit (default: unexpected)
+;startsecs=10 ; number of secs prog must stay running (def. 1)
+;startretries=3 ; max # of serial start failures (default 3)
+;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
+;stopsignal=QUIT ; signal used to kill process (default TERM)
+;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
+;user=chrism ; setuid to this UNIX account to run the program
+;redirect_stderr=true ; redirect proc stderr to stdout (default false)
+;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
+;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
+;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
+;stdout_events_enabled=false ; emit events on stdout writes (default false)
+;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
+;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
+;stderr_logfile_backups ; # of stderr logfile backups (default 10)
+;stderr_events_enabled=false ; emit events on stderr writes (default false)
+;environment=A=1,B=2 ; process environment additions
+;serverurl=AUTO ; override serverurl computation (childutils)
+
+; The below sample group section shows all possible group values,
+; create one or more 'real' group: sections to create "heterogeneous"
+; process groups.
+
+;[group:thegroupname]
+;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
+;priority=999 ; the relative start priority (default 999)
+
+; The [include] section can just contain the "files" setting. This
+; setting can list multiple files (separated by whitespace or
+; newlines). It can also contain wildcards. The filenames are
+; interpreted as relative to this file. Included files *cannot*
+; include files themselves.
+
+[include]
+files = /etc/supervisor/*
View
130 devops/templates/supervisord.sh
@@ -0,0 +1,130 @@
+# Supervisord auto-start
+#
+# description: Auto-starts supervisord
+# processname: supervisord
+# pidfile: /var/run/supervisord.pid
+
+PATH=/sbin:/usr/sbin:/bin:/usr/bin
+NAME=supervisord
+DESC="supervisod is a system for controlling process state"
+SUPERVISORD=/usr/local/bin/supervisord
+SUPERVISORCTL=/usr/local/bin/supervisorctl
+SCRIPTNAME=/etc/init.d/$NAME
+
+
+# Read configuration variable file if it is present
+[ -r /etc/default/$NAME ] && . /etc/default/$NAME
+
+# Load the VERBOSE setting and other rcS variables
+. /lib/init/vars.sh
+
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
+# and status_of_proc is working.
+. /lib/lsb/init-functions
+
+
+do_start()
+{
+ # Return
+ # 0 if daemon has been started
+ # 1 if daemon was already running
+ # 2 if daemon could not be started
+
+ if $SUPERVISORCTL status | grep -q "unix:///tmp/supervisor.sock no such file"; then
+ $SUPERVISORD
+ echo "supervisord started successfully"
+ return 0
+ fi
+
+ if $SUPERVISORCTL status | grep -qv "unix:///tmp/supervisor.sock no such file"; then
+ echo "supervisord is already running"
+ return 1
+ fi
+
+ echo "could not start supervisord"
+ return 2
+}
+
+do_stop() {
+ # Return
+ # 0 if daemon has been stopped
+ # 1 if daemon was already stopped
+ # 2 if daemon could not be stopped
+ # other if a failure occurred
+ if $SUPERVISORCTL status | grep -q "unix:///tmp/supervisor.sock no such file"; then
+ echo "supervisord already stopped"
+ return 1
+ else
+ if $SUPERVISORCTL shutdown | grep -q "Shut down"; then
+ while $SUPERVISORCTL shutdown | grep -q "already shutting down"
+ do
+ sleep 1
+ done
+ echo 'supervisor shutdown successfully'
+ return 0
+ else
+ echo "could not stop supervisord"
+ return 2
+ fi
+ fi
+}
+
+do_status() {
+ if $SUPERVISORCTL status; then
+ $SUPERVISORCTL status
+ echo
+ return 3
+ fi
+}
+
+case "$1" in
+ start)
+ [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
+ do_start
+ case "$?" in
+ 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+ 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+ esac
+ ;;
+ stop)
+ [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
+ do_stop
+ case "$?" in
+ 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+ 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+ esac
+ ;;
+ status)
+ do_status
+ exit $?
+ ;;
+ restart|force-reload)
+ #
+ # If the "reload" option is implemented then remove the
+ # 'force-reload' alias
+ #
+ log_daemon_msg "Restarting $NAME"
+ do_stop
+ case "$?" in
+ 0|1)
+ do_start
+ case "$?" in
+ 0) log_end_msg 0 ;;
+ 1) log_end_msg 1 ;; # Old process is still running
+ *) log_end_msg 1 ;; # Failed to start
+ esac
+ ;;
+ *)
+ # Failed to stop
+ log_end_msg 1
+ ;;
+ esac
+ ;;
+ *)
+ echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
+ exit 3
+ ;;
+esac
+
+:
View
68 devops/webserver.yml
@@ -0,0 +1,68 @@
+---
+
+- hosts: webservers
+ user: vagrant
+ sudo: True
+
+ tasks:
+ - name: install python-software-properties
+ action: apt pkg=python-software-properties state=present
+
+ - name: add nginx ppa
+ action: apt_repository repo=ppa:nginx/stable state=present
+
+ - name: update apt repo
+ action: command /usr/bin/apt-get update
+
+ - name: install common packages needed for python application development
+ action: apt pkg=$item state=installed
+ with_items:
+ - build-essential
+ - python2.6-dev
+ - libpq-dev
+ - libmysqlclient-dev
+ - libxml2-dev
+ - libjpeg62
+ - libjpeg62-dev
+ - libfreetype6
+ - libfreetype6-dev
+ - zlib1g-dev
+ - mysql-client
+ - python-dev
+ - python-setuptools
+ - python-imaging
+ - python-mysqldb
+ - python-psycopg2
+ - git-core
+ - nginx
+
+ - name: install pip
+ action: easy_install name=pip
+
+ - name: install various libraries with pip
+ action: pip name=$item state=present
+ with_items:
+ - virtualenv
+ - supervisor
+ - uwsgi
+
+ - name: remove default nginx site
+ action: file path=/etc/nginx/sites-enabled/default state=absent
+
+ - name: write nginx.conf
+ action: template src=templates/nginx.conf dest=/etc/nginx/nginx.conf
+
+ - name: create supervisord config folder
+ action: file dest=/etc/supervisor state=directory owner=root
+
+ - name: create supervisord config
+ action: template src=templates/supervisord.conf dest=/etc/supervisord.conf
+
+ - name: create supervisord init script
+ action: template src=templates/supervisord.sh dest=/etc/init.d/supervisord mode=0755
+
+ - name: start supervisord service and have it run during system startup
+ action: service name=supervisord state=started enabled=yes
+
+ - name: create webapps directory
+ action: file dest=/srv/webapps state=directory
View
1 requirements.txt
@@ -0,0 +1 @@
+Flask==0.9

0 comments on commit 94baee2

Please sign in to comment.
Something went wrong with that request. Please try again.