Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Credit where it's due. I started with this: http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/
- #I hate prompts in stuff I should script with a holy burning passion...
- #It's all commented pretty well, so here:
- WEBAPPDIR='/opt/webapps'
- VIRTUALENVNAME='torhiddenservice'
- PROJECTNAME='abraham'
- SITENAME='abraham'
- DB_PASS='insertyourpassword'
- #This will change in a bit...
- LINUX_UNAME=`whoami`
- WEBAPPGROUP='webapps'
- #TODO move the streamline files in to string blobs I echo to files, so it's a monolithic bash script
- #Everything that says sed rules will probably be bash variable string echos
- function goto_venv(){
- cd $WEBAPPDIR/$VIRTUALENVNAME
- }
- function config_file_dropper(){
- #Config file dropper
- echo 'Not implemented'
- }
- function install_all_the_things(){
- :'
- #Install all the things...
- #Install xargs for later
- sudo apt-get install xargs
- #Get ready... we are assuming we will use this as a scaffold to do production deployments
- sudo apt-get install -y git
- #Install postgres
- sudo apt-get install -y postgresql postgresql-contrib
- #Install virtualenv and binary dependencies for psycopg
- sudo apt-get install -y python-virtualenv
- sudo apt-get install -y libpq-dev python-dev
- #Install supervisord
- sudo apt-get install -y supervisor
- #Install nginx
- sudo apt-get install -y nginx
- sudo service nginx start
- #Memcached with python wrapper...just in case
- sudo apt-get install -y python-memcache
- #Streamlined...left the explanation as a comment. It will be removed when I add this to version control
- #To reduce "LoC"... I also squashed the nginx restart since it is superfluous and happens again later anyway
- I will pull comments then too
- '
- sudo apt-get install -y xargs
- echo 'git postgresql postgresql-contrib python-virtualenv libpq-dev python-dev supervisor nginx python-memcache' > ../installer/base-apt-deps
- cat ../installer/base-apt-deps | xargs sudo apt-get install -y
- }
- function configure_postgres(){
- #Configure postgres
- ##Create user
- sudo su postgres -c "createuser -DIRSw $VIRTUALENVNAME"
- ##Set password for this user
- sudo su postgres -c "psql -c \"ALTER USER $VIRTUALENVNAME PASSWORD '$DB_PASS'\""
- ##Assign a new database to this user
- sudo su postgres -c "psql -c \"DROP DATABASE $VIRTUALENVNAME\""
- sudo su postgres -c "createdb $VIRTUALENVNAME -O $VIRTUALENVNAME"
- }
- function install_site_deps_global(){
- echo 'python-geoip' > ../install/site-deps-python-global
- cat ../installer/site-deps-python-global | xargs sudo apt-get install -y
- }
- function adduser_django(){
- sudo groupadd --system $WEBAPPGROUP
- sudo useradd --system --gid $WEBAPPGROUP --home $WEBAPPDIR/$VIRTUALENVNAME $VIRTUALENVNAME
- }
- function create_virtualenv(){
- #Create a virtualenv
- cd $WEBAPPDIR
- sudo virtualenv --system-site-packages $VIRTUALENVNAME
- sudo chown -R $LINUX_UNAME:$WEBAPPGROUP $VIRTUALENVNAME
- cd $VIRTUALENVNAME
- source bin/activate
- }
- function pip_installs(){
- :'
- #Get django
- pip install django
- #Get the database adapter
- pip install psycopg2
- #Get setproctitle so you can ps for a not "python" process
- pip install setproctitle
- #Get gunicorn
- pip install gunicorn
- #Could streamline this with a requirements file...
- #Read more here: http://www.pip-installer.org/en/latest/logic.html#requirements-file-format
- #Streamlined
- '
- goto_venv
- echo 'django
- psycopg2
- setproctitle
- gunicorn' > installer/base-pip-deps
- pip install -r installer/base-pip-deps
- }
- function get_your_stuff(){
- goto_venv
- #git clone something
- sudo chown -R $LINUX_UNAME:$WEBAPPGROUP .
- #insert a line in here to get an initial user table and put it in opt/webapps/venvname/projname/someapp/fixtures/initial_data.json
- echo 'Not implemented'
- }
- function install_site_deps_local(){
- goto_venv
- echo 'django-chronograph' > installer/site-deps-python-local
- pip install -r installer/site-deps-python-local
- #fix busticated django-chronograph that is incompatible with 1.6
- find . -type f -print0 | xargs -0 sed -i 's/from django.conf.urls.defaults import patterns, url/from django.conf.urls import patterns, url, include/g'
- #there really sould be a version checking if clause, but i'm too big of an asshat to submit a patch...yet
- }
- function config_db_connector(){
- #TODO insert sed rules after it works manually
- echo 'Not implemented...and might not be. This is getting boooooring...'
- }
- function prepare_django_stuff(){
- goto_venv
- cd $PROJECTNAME
- #Sync up your database schema...flush is not necessary for new installs, but helped during testing
- config_db_connector
- python manage.py syncdb --noinput
- #Purge the DB in case you're testing...
- python manage.py flush --noinput
- #Do this to get app specific static crap in your site static-root... if it clobbers, file a bug.
- #They're doing it wrong and nee to fix their urls.
- python manage.py collectstatic --noinput
- #Don't forget to create an admin user and set its password
- }
- function gunicorn_launcher(){
- #TODO insert sed rules after it works manually
- goto_venv
- mkdir run
- cp installer/gunicorn_start.bash bin/gunicorn_start.bash
- chmod +x bin/gunicorn_start.bash
- }
- function supervisor_prep(){
- #TODO insert sed rules after it works manually
- goto_venv
- sudo cp installer/supervisor_template.conf /etc/supervisor/conf.d/torhiddenservice.conf
- sudo chown -R $VIRTUALENVNAME:$WEBAPPGROUP .
- sudo supervisorctl reread
- sudo supervisorctl update
- sudo supervisorctl restart $VIRTUALENVNAME
- }
- function nginx_prep(){
- #TODO insert sed rules after it works manually
- #Possibly shim in disabling Apache if you're on a hosting provider that uses it by default
- #Possibly shim in linking in as a non-default site template.
- goto_venv
- sudo cp installer/nginx_template.conf /etc/nginx/sites-available/default
- sudo service nginx restart
- }
- ###############
- #Thoughts at the moment:
- #Do your sudo and postgre stuff first.
- #Create a dedicated user
- #su to that user before you activate the virtualenv
- #Move along
- #I did it in a workflow steps kind of way initially.
- #Now that I'm hitting permissions and other walls, I need to move things around in order of logic precedence.
- #It's getting easier and harder at the same time.
- #We'll see.
- install_all_the_things
- configure_postgres
- install_site_deps_global
- adduser_django
- create_virtualenv
- get_your_stuff
- pip_installs
- install_site_deps_local
- prepare_django_stuff
- gunicorn_launcher
- supervisor_prep
- nginx_prep
- echo "Done..."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement