Keyword cleanup
[oweals/karmaworld.git] / Vagrantfile
1 # -*- mode: ruby -*-
2 # vi: set ft=ruby :
3
4 # This file is for use by Vagrant (http://www.vagrantup.com/).
5 # It will establish a debian-based (Ubuntu) virtual machine for development.
6
7 # The virtual machine environment attempts to match the production environment 
8 # as closely as possible.
9
10 # This file was generated by `vagrant up` and consequently modified.
11
12 # Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
13 VAGRANTFILE_API_VERSION = "2"
14
15 # Copy the vagrant SSH key into the VM so vagrant can SSH to localhost within
16 # the VM. Continued in the shell script below.
17 # http://serverfault.com/questions/491343/how-can-i-move-my-deploy-key-into-vagrant#comment549259_491345
18 git_ssh_key = File.read(ENV['HOME'] + '/.vagrant.d/insecure_private_key');
19
20 # build a shell script that installs prereqs, copies over the host secrets,
21 # configures the database, sets up the user/group associations, creates a self
22 # signed SSL cert, pulls in the code from the host machine, sets up some
23 # external dependency configs, and then runs fabric.
24 shellscript = <<SCRIPT
25 cat >>/home/vagrant/.ssh/insecure_private_key <<EOF
26 #{git_ssh_key}
27 EOF
28 chown vagrant:vagrant /home/vagrant/.ssh/insecure_private_key
29 chmod 600 /home/vagrant/.ssh/insecure_private_key
30 cat >>/home/vagrant/.ssh/config <<EOF
31 Host localhost
32     User vagrant
33     IdentityFile ~/.ssh/insecure_private_key
34
35 Host 127.0.0.1
36     User vagrant
37     IdentityFile ~/.ssh/insecure_private_key
38 EOF
39 chmod 644 /home/vagrant/.ssh/config
40
41 cat >/home/vagrant/localhost.conf <<EOF
42 [req]
43 default_keyfile=localhost.key.pem
44 encrypt_key=no
45 default_bits=512
46 prompt=no
47 utf8=yes
48 distinguished_name=dn
49
50 [dn]
51 C=US
52 ST=Massachusetts
53 L=Cambridge
54 O=FinalsClub Foundation
55 CN=localhost
56 emailAddress=info@karmanotes.org
57 EOF
58 cd /home/vagrant
59 openssl req -new -config localhost.conf -out localhost.csr.pem
60 openssl x509 -req -in localhost.csr.pem -signkey localhost.key.pem -out localhost.cert.pem
61 chown vagrant:vagrant localhost*
62 cd -
63
64 export DEBIAN_FRONTEND=noninteractive
65
66 add-apt-repository -y ppa:coolwanglu/pdf2htmlex # pdf2htmlex
67
68 apt-get update
69 apt-get upgrade -y
70 apt-get install -y python-pip postgresql python-virtualenv libffi-dev \
71                    git nginx postgresql-server-dev-9.1 libxslt1-dev \
72                    libxml2-dev libmemcached-dev python-dev rabbitmq-server \
73                    p7zip-full pdf2htmlex
74
75 cat >> `dpkg -L pdf2htmlex | grep pdf2htmlEX.js` <<PDF2HTMLEXHACK
76 Viewer.prototype['rescale'] = Viewer.prototype.rescale;
77 Viewer.prototype['scroll_to'] = Viewer.prototype.scroll_to;
78 PDF2HTMLEXHACK
79
80 echo "CREATE USER vagrant WITH CREATEROLE CREATEDB LOGIN; CREATE DATABASE karmaworld OWNER vagrant;" | su postgres -c "psql"
81
82 mkdir -m 775 -p /var/www
83 chown -R :www-data /var/www
84 usermod -a -G www-data vagrant
85
86 su vagrant -c "git clone /vagrant karmaworld"
87
88 SECRETPATH="karmaworld/secret"
89
90 su vagrant -c "cp /vagrant/$SECRETPATH/* karmaworld/$SECRETPATH/"
91
92 CFILE="karmaworld/$SECRETPATH/db_settings.py"
93 cat > $CFILE <<CONFIG
94 #!/usr/bin/env python
95 # -*- coding:utf8 -*-
96 # Copyright (C) 2012  FinalsClub Foundation
97 """
98 DO NOT check this file into source control.
99 """
100 PROD_DB_NAME = 'karmaworld'
101 PROD_DB_USERNAME = 'vagrant'
102 PROD_DB_PASSWORD = ''
103 CONFIG
104 chown vagrant:vagrant karmaworld/$SECRETPATH/*.py
105
106 cat > /etc/nginx/sites-available/karmaworld <<CONFIG
107 server {
108     listen 80;
109     server_name localhost;
110     return 301 https://\\\$host:6659\\\$request_uri;
111 }
112
113 server {
114     listen 443;
115     ssl on;
116     # don't do virtual hosting, handle all requests regardless of header
117     server_name localhost;
118     client_max_body_size 20M;
119     ssl_certificate     /home/vagrant/localhost.cert.pem;
120     ssl_certificate_key /home/vagrant/localhost.key.pem;
121
122     location / {
123         # pass traffic through to gunicorn
124         proxy_pass http://127.0.0.1:8000;
125         # pass HTTP(S) status through to Django
126         proxy_set_header X-Forwarded-SSL \\\$https;
127         proxy_set_header X-Forwarded-Protocol \\\$scheme;
128         proxy_set_header X-Forwarded-Proto \\\$scheme;
129         # pass nginx site back to Django
130         proxy_set_header Host \\\$http_host;
131     }
132 }
133 CONFIG
134 rm /etc/nginx/sites-enabled/default
135 ln -s /etc/nginx/sites-available/karmaworld /etc/nginx/sites-enabled/karmaworld
136 sudo service nginx restart
137
138 cp karmaworld/confs/prod/supervisor /etc/init.d
139 chmod 755 /etc/init.d/supervisor
140 update-rc.d supervisor defaults
141
142 pip install fabric
143 SCRIPT
144 # end of script
145
146 Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
147   # All Vagrant configuration is done here. The most common configuration
148   # options are documented and commented below. For a complete reference,
149   # please see the online documentation at vagrantup.com.
150
151   # Every Vagrant virtual environment requires a box to build off of.
152   config.vm.box = "Official Ubuntu 12.04 daily Cloud Image i386"
153   #config.vm.box = "Official Ubuntu 12.04 daily Cloud Image amd64"
154   #config.vm.box = "Official Ubuntu 12.10 daily Cloud Image i386"
155   #config.vm.box = "Official Ubuntu 12.10 daily Cloud Image amd64"
156   #config.vm.box = "Official Ubuntu 13.04 daily Cloud Image i386"
157   #config.vm.box = "Official Ubuntu 13.04 daily Cloud Image amd64"
158   #config.vm.box = "Official Ubuntu 13.10 daily Cloud Image i386"
159   #config.vm.box = "Official Ubuntu 13.10 daily Cloud Image amd64"
160
161   # The url from where the 'config.vm.box' box will be fetched if it
162   # doesn't already exist on the user's system.
163   config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-i386-vagrant-disk1.box"
164   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box"
165   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/quantal/current/quantal-server-cloudimg-i386-vagrant-disk1.box"
166   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/quantal/current/quantal-server-cloudimg-amd64-vagrant-disk1.box"
167   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/raring/current/raring-server-cloudimg-i386-vagrant-disk1.box"
168   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/raring/current/raring-server-cloudimg-amd64-vagrant-disk1.box"
169   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/saucy/current/saucy-server-cloudimg-i386-vagrant-disk1.box"
170   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/saucy/current/saucy-server-cloudimg-amd64-vagrant-disk1.box"
171
172   # Create a forwarded port mapping which allows access to a specific port
173   # within the machine from a port on the host machine. In the example below,
174   # accessing "localhost:8080" will access port 80 on the guest machine.
175   # config.vm.network :forwarded_port, guest: 80, host: 8080
176
177   # OM (sanskrit) KW (KarmaWorld) on a phone: 66 59
178   config.vm.network :forwarded_port, guest: 443, host: 6659, auto_correct: true
179   config.vm.network :forwarded_port, guest: 80, host: 16659, auto_correct: true
180
181   # Create a private network, which allows host-only access to the machine
182   # using a specific IP.
183   #config.vm.network :private_network, ip: "192.168.66.59"
184
185   # Create a public network, which generally matched to bridged network.
186   # Bridged networks make the machine appear as another physical device on
187   # your network.
188   # Used to directly access the internet for downloading updates and so forth.
189   config.vm.network :public_network
190
191   # If true, then any SSH connections made will enable agent forwarding.
192   # Default value: false
193   # config.ssh.forward_agent = true
194
195   # Share an additional folder to the guest VM. The first argument is
196   # the path on the host to the actual folder. The second argument is
197   # the path on the guest to mount the folder. And the optional third
198   # argument is a set of non-required options.
199   # config.vm.synced_folder "../data", "/vagrant_data"
200
201   # Setup scripts
202   config.vm.provision "shell", inline: shellscript
203
204   # Provider-specific configuration so you can fine-tune various
205   # backing providers for Vagrant. These expose provider-specific options.
206   # Example for VirtualBox:
207   #
208   # config.vm.provider :virtualbox do |vb|
209   #   # Don't boot with headless mode
210   #   vb.gui = true
211   #
212   #   # Use VBoxManage to customize the VM. For example to change memory:
213   #   vb.customize ["modifyvm", :id, "--memory", "1024"]
214   # end
215   #
216   # View the documentation for the provider you're using for more
217   # information on available options.
218
219   # Enable provisioning with Puppet stand alone.  Puppet manifests
220   # are contained in a directory path relative to this Vagrantfile.
221   # You will need to create the manifests directory and a manifest in
222   # the file base.pp in the manifests_path directory.
223   #
224   # An example Puppet manifest to provision the message of the day:
225   #
226   # # group { "puppet":
227   # #   ensure => "present",
228   # # }
229   # #
230   # # File { owner => 0, group => 0, mode => 0644 }
231   # #
232   # # file { '/etc/motd':
233   # #   content => "Welcome to your Vagrant-built virtual machine!
234   # #               Managed by Puppet.\n"
235   # # }
236   #
237   # config.vm.provision :puppet do |puppet|
238   #   puppet.manifests_path = "manifests"
239   #   puppet.manifest_file  = "site.pp"
240   # end
241
242   # Enable provisioning with chef solo, specifying a cookbooks path, roles
243   # path, and data_bags path (all relative to this Vagrantfile), and adding
244   # some recipes and/or roles.
245   #
246   # config.vm.provision :chef_solo do |chef|
247   #   chef.cookbooks_path = "../my-recipes/cookbooks"
248   #   chef.roles_path = "../my-recipes/roles"
249   #   chef.data_bags_path = "../my-recipes/data_bags"
250   #   chef.add_recipe "mysql"
251   #   chef.add_role "web"
252   #
253   #   # You may also specify custom JSON attributes:
254   #   chef.json = { :mysql_password => "foo" }
255   # end
256
257   # Enable provisioning with chef server, specifying the chef server URL,
258   # and the path to the validation key (relative to this Vagrantfile).
259   #
260   # The Opscode Platform uses HTTPS. Substitute your organization for
261   # ORGNAME in the URL and validation key.
262   #
263   # If you have your own Chef Server, use the appropriate URL, which may be
264   # HTTP instead of HTTPS depending on your configuration. Also change the
265   # validation key to validation.pem.
266   #
267   # config.vm.provision :chef_client do |chef|
268   #   chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
269   #   chef.validation_key_path = "ORGNAME-validator.pem"
270   # end
271   #
272   # If you're using the Opscode platform, your validator client is
273   # ORGNAME-validator, replacing ORGNAME with your organization name.
274   #
275   # If you have your own Chef Server, the default validation client name is
276   # chef-validator, unless you changed the configuration.
277   #
278   #   chef.validation_client_name = "ORGNAME-validator"
279 end