Advertisement
Guest User

Finally...

a guest
Nov 24th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.77 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Credit where it's due.  I started with this: http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/
  4. #I hate prompts in stuff I should script with a holy burning passion...
  5. #It's all commented pretty well, so here:
  6.  
  7. WEBAPPDIR='/opt/webapps'
  8. VIRTUALENVNAME='torhiddenservice'
  9. PROJECTNAME='abraham'
  10. SITENAME='abraham'
  11. DB_PASS='insertyourpassword'
  12.  
  13. #This will change in a bit...
  14. LINUX_UNAME=`whoami`
  15. WEBAPPGROUP='webapps'
  16.  
  17. #TODO move the streamline files in to string blobs I echo to files, so it's a monolithic bash script
  18. #Everything that says sed rules will probably be bash variable string echos
  19.  
  20. function goto_venv(){
  21.     cd $WEBAPPDIR/$VIRTUALENVNAME
  22. }
  23.  
  24. function config_file_dropper(){
  25.     #Config file dropper
  26.     echo 'Not implemented'
  27. }
  28.  
  29. function install_all_the_things(){
  30. :'
  31.    #Install all the things...
  32.  
  33.    #Install xargs for later
  34.    sudo apt-get install xargs
  35.  
  36.    #Get ready... we are assuming we will use this as a scaffold to do production deployments
  37.    sudo apt-get install -y git
  38.  
  39.    #Install postgres
  40.    sudo apt-get install -y postgresql postgresql-contrib
  41.  
  42.    #Install virtualenv and binary dependencies for psycopg
  43.    sudo apt-get install -y python-virtualenv
  44.    sudo apt-get install -y libpq-dev python-dev
  45.  
  46.    #Install supervisord
  47.    sudo apt-get install -y supervisor
  48.  
  49.    #Install nginx
  50.    sudo apt-get install -y nginx
  51.    sudo service nginx start
  52.  
  53.    #Memcached with python wrapper...just in case
  54.    sudo apt-get install -y python-memcache
  55.  
  56.    #Streamlined...left the explanation as a comment.  It will be removed when I add this to version control
  57.    #To reduce "LoC"... I also squashed the nginx restart since it is superfluous and happens again later anyway
  58.    I will pull comments then too
  59. '
  60.     sudo apt-get install -y xargs
  61.     echo 'git postgresql postgresql-contrib python-virtualenv libpq-dev python-dev supervisor nginx python-memcache' > ../installer/base-apt-deps
  62.     cat ../installer/base-apt-deps | xargs sudo apt-get install -y
  63.     }
  64.  
  65. function configure_postgres(){
  66.     #Configure postgres
  67.     ##Create user
  68.     sudo su postgres -c "createuser -DIRSw $VIRTUALENVNAME"
  69.  
  70.    ##Set password for this user
  71.     sudo su postgres -c "psql -c \"ALTER USER $VIRTUALENVNAME PASSWORD '$DB_PASS'\""
  72.  
  73.     ##Assign a new database to this user
  74.     sudo su postgres -c "psql -c \"DROP DATABASE $VIRTUALENVNAME\""
  75.     sudo su postgres -c "createdb $VIRTUALENVNAME -O $VIRTUALENVNAME"
  76.     }
  77.  
  78. function install_site_deps_global(){
  79.     echo 'python-geoip' > ../install/site-deps-python-global
  80.     cat ../installer/site-deps-python-global | xargs sudo apt-get install -y
  81. }
  82.  
  83. function adduser_django(){
  84.     sudo groupadd --system $WEBAPPGROUP
  85.     sudo useradd --system --gid $WEBAPPGROUP --home $WEBAPPDIR/$VIRTUALENVNAME $VIRTUALENVNAME
  86. }
  87.  
  88. function create_virtualenv(){
  89.     #Create a virtualenv
  90.     cd $WEBAPPDIR
  91.     sudo virtualenv --system-site-packages $VIRTUALENVNAME
  92.     sudo chown -R $LINUX_UNAME:$WEBAPPGROUP $VIRTUALENVNAME
  93.     cd $VIRTUALENVNAME
  94.     source bin/activate
  95.     }
  96.  
  97. function pip_installs(){
  98. :'
  99.    #Get django
  100.    pip install django
  101.  
  102.    #Get the database adapter
  103.    pip install psycopg2
  104.  
  105.    #Get setproctitle so you can ps for a not "python" process
  106.    pip install setproctitle
  107.  
  108.    #Get gunicorn
  109.    pip install gunicorn
  110.  
  111.    #Could streamline this with a requirements file...
  112.    #Read more here: http://www.pip-installer.org/en/latest/logic.html#requirements-file-format
  113.    #Streamlined
  114. '
  115.     goto_venv
  116.     echo 'django
  117. psycopg2
  118. setproctitle
  119. gunicorn' > installer/base-pip-deps
  120.     pip install -r installer/base-pip-deps
  121.     }
  122.  
  123.  
  124. function get_your_stuff(){
  125.     goto_venv
  126.     #git clone something
  127.     sudo chown -R $LINUX_UNAME:$WEBAPPGROUP .
  128.     #insert a line in here to get an initial user table and put it in opt/webapps/venvname/projname/someapp/fixtures/initial_data.json
  129.     echo 'Not implemented'
  130. }
  131.  
  132. function install_site_deps_local(){
  133.     goto_venv
  134.     echo 'django-chronograph' > installer/site-deps-python-local
  135.     pip install -r installer/site-deps-python-local
  136.  
  137.     #fix busticated django-chronograph that is incompatible with 1.6
  138.     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'
  139.     #there really sould be a version checking if clause, but i'm too big of an asshat to submit a patch...yet
  140. }
  141.  
  142. function config_db_connector(){
  143.     #TODO insert sed rules after it works manually
  144.     echo 'Not implemented...and might not be.  This is getting boooooring...'
  145. }
  146.  
  147. function prepare_django_stuff(){
  148.     goto_venv
  149.     cd $PROJECTNAME
  150.     #Sync up your database schema...flush is not necessary for new installs, but helped during testing
  151.     config_db_connector
  152.     python manage.py syncdb --noinput
  153.     #Purge the DB in case you're testing...
  154.     python manage.py flush --noinput
  155.     #Do this to get app specific static crap in your site static-root...  if it clobbers, file a bug.
  156.     #They're doing it wrong and nee to fix their urls.
  157.     python manage.py collectstatic --noinput
  158.  
  159.     #Don't forget to create an admin user and set its password
  160. }
  161.  
  162. function gunicorn_launcher(){
  163.     #TODO insert sed rules after it works manually
  164.     goto_venv
  165.     mkdir run
  166.     cp installer/gunicorn_start.bash bin/gunicorn_start.bash
  167.     chmod +x bin/gunicorn_start.bash
  168. }
  169.  
  170. function supervisor_prep(){
  171.     #TODO insert sed rules after it works manually
  172.     goto_venv
  173.     sudo cp installer/supervisor_template.conf /etc/supervisor/conf.d/torhiddenservice.conf
  174.     sudo chown -R $VIRTUALENVNAME:$WEBAPPGROUP .
  175.     sudo supervisorctl reread
  176.     sudo supervisorctl update
  177.     sudo supervisorctl restart $VIRTUALENVNAME
  178. }
  179.  
  180. function nginx_prep(){
  181.     #TODO insert sed rules after it works manually
  182.     #Possibly shim in disabling Apache if you're on a hosting provider that uses it by default
  183.     #Possibly shim in linking in as a non-default site template.
  184.     goto_venv
  185.     sudo cp installer/nginx_template.conf /etc/nginx/sites-available/default
  186.     sudo service nginx restart
  187. }
  188.  
  189.  
  190. ###############
  191. #Thoughts at the moment:
  192. #Do your sudo and postgre stuff first.
  193. #Create a dedicated user
  194. #su to that user before you activate the virtualenv
  195. #Move along
  196. #I did it in a workflow steps kind of way initially.
  197. #Now that I'm hitting permissions and other walls, I need to move things around in order of logic precedence.
  198. #It's getting easier and harder at the same time.
  199. #We'll see.
  200.  
  201.  
  202. install_all_the_things
  203. configure_postgres
  204. install_site_deps_global
  205.  
  206. adduser_django
  207.  
  208. create_virtualenv
  209. get_your_stuff
  210. pip_installs
  211.  
  212. install_site_deps_local
  213.  
  214. prepare_django_stuff
  215.  
  216.  
  217. gunicorn_launcher
  218. supervisor_prep
  219. nginx_prep
  220.  
  221. echo "Done..."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement