# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
-# Install fabric so that the KarmaWorld fabfile may be run.
+# Copy the vagrant SSH key into the VM so vagrant can SSH to localhost within
+# the VM. Continued in the shell script below.
+# http://serverfault.com/questions/491343/how-can-i-move-my-deploy-key-into-vagrant#comment549259_491345
+git_ssh_key = File.read(ENV['HOME'] + '/.vagrant.d/insecure_private_key');
-# build a shell script that installs prereqs, configures the database, sets up
-# the user/group associations, pulls in the code from the host machine, sets up
-# some external dependency configs, and then runs fabric.
+# build a shell script that installs prereqs, copies over the host secrets,
+# configures the database, sets up the user/group associations, creates a self
+# signed SSL cert, pulls in the code from the host machine, sets up some
+# external dependency configs, and then runs fabric.
shellscript = <<SCRIPT
+cat >>/home/vagrant/.ssh/insecure_private_key <<EOF
+#{git_ssh_key}
+EOF
+chown vagrant:vagrant /home/vagrant/.ssh/insecure_private_key
+chmod 600 /home/vagrant/.ssh/insecure_private_key
+cat >>/home/vagrant/.ssh/config <<EOF
+Host localhost
+ User vagrant
+ IdentityFile ~/.ssh/insecure_private_key
+
+Host 127.0.0.1
+ User vagrant
+ IdentityFile ~/.ssh/insecure_private_key
+EOF
+chmod 644 /home/vagrant/.ssh/config
+
+cat >/home/vagrant/localhost.conf <<EOF
+[req]
+default_keyfile=localhost.key.pem
+encrypt_key=no
+default_bits=512
+prompt=no
+utf8=yes
+distinguished_name=dn
+
+[dn]
+C=US
+ST=Massachusetts
+L=Cambridge
+O=FinalsClub Foundation
+CN=localhost
+emailAddress=info@karmanotes.org
+EOF
+cd /home/vagrant
+openssl req -new -config localhost.conf -out localhost.csr.pem
+openssl x509 -req -in localhost.csr.pem -signkey localhost.key.pem -out localhost.cert.pem
+chown vagrant:vagrant localhost*
+cd -
+
+export DEBIAN_FRONTEND=noninteractive
+
+add-apt-repository -y ppa:coolwanglu/pdf2htmlex # pdf2htmlex
+
apt-get update
apt-get upgrade -y
-apt-get install -y python-pip postgresql python-virtualenv virtualenvwrapper \
+apt-get install -y python-pip postgresql python-virtualenv libffi-dev \
git nginx postgresql-server-dev-9.1 libxslt1-dev \
- libxml2-dev libmemcached-dev python-dev rabbitmq-server
+ libxml2-dev libmemcached-dev python-dev rabbitmq-server \
+ p7zip-full pdf2htmlex
+
+cat >> `dpkg -L pdf2htmlex | grep pdf2htmlEX.js` <<PDF2HTMLEXHACK
+Viewer.prototype['rescale'] = Viewer.prototype.rescale;
+Viewer.prototype['scroll_to'] = Viewer.prototype.scroll_to;
+PDF2HTMLEXHACK
-echo "CREATE USER vagrant WITH CREATEROLE LOGIN; CREATE DATABASE karmaworld OWNER vagrant;" | su postgres -c "psql"
+echo "CREATE USER vagrant WITH CREATEROLE CREATEDB LOGIN; CREATE DATABASE karmaworld OWNER vagrant;" | su postgres -c "psql"
mkdir -m 775 -p /var/www
chown -R :www-data /var/www
su vagrant -c "git clone /vagrant karmaworld"
-SECRETPATH="karmaworld/karmaworld/secret"
-CFILE="$SECRETPATH/db_settings.py"
+SECRETPATH="karmaworld/secret"
+
+su vagrant -c "cp /vagrant/$SECRETPATH/* karmaworld/$SECRETPATH/"
+
+CFILE="karmaworld/$SECRETPATH/db_settings.py"
cat > $CFILE <<CONFIG
#!/usr/bin/env python
# -*- coding:utf8 -*-
PROD_DB_USERNAME = 'vagrant'
PROD_DB_PASSWORD = ''
CONFIG
-cp $SECRETPATH/filepicker.py.example $SECRETPATH/filepicker.py
-cp $SECRETPATH/static_s3.py.example $SECRETPATH/static_s3.py
-chown vagrant:vagrant $SECRETPATH/*.py
+chown vagrant:vagrant karmaworld/$SECRETPATH/*.py
+
+cat > /etc/nginx/sites-available/karmaworld <<CONFIG
+server {
+ listen 80;
+ server_name localhost;
+ return 301 https://\\\$host:6659\\\$request_uri;
+}
+
+server {
+ listen 443;
+ ssl on;
+ # don't do virtual hosting, handle all requests regardless of header
+ server_name localhost;
+ client_max_body_size 20M;
+ ssl_certificate /home/vagrant/localhost.cert.pem;
+ ssl_certificate_key /home/vagrant/localhost.key.pem;
+
+ location / {
+ # pass traffic through to gunicorn
+ proxy_pass http://127.0.0.1:8000;
+ # pass HTTP(S) status through to Django
+ proxy_set_header X-Forwarded-SSL \\\$https;
+ proxy_set_header X-Forwarded-Protocol \\\$scheme;
+ proxy_set_header X-Forwarded-Proto \\\$scheme;
+ # pass nginx site back to Django
+ proxy_set_header Host \\\$http_host;
+ }
+}
+CONFIG
+rm /etc/nginx/sites-enabled/default
+ln -s /etc/nginx/sites-available/karmaworld /etc/nginx/sites-enabled/karmaworld
+sudo service nginx restart
+
+cp karmaworld/confs/prod/supervisor /etc/init.d
+chmod 755 /etc/init.d/supervisor
+update-rc.d supervisor defaults
pip install fabric
-#su vagrant -c "cd karmaworld; fab here first_deploy"
SCRIPT
# end of script
# config.vm.network :forwarded_port, guest: 80, host: 8080
# OM (sanskrit) KW (KarmaWorld) on a phone: 66 59
- config.vm.network :forwarded_port, guest: 80, host: 6659, auto_correct: true
+ config.vm.network :forwarded_port, guest: 443, host: 6659, auto_correct: true
+ config.vm.network :forwarded_port, guest: 80, host: 16659, auto_correct: true
# Create a private network, which allows host-only access to the machine
# using a specific IP.