decreased memory footprint
[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, pulls in the
22 # code from the host machine, sets up some external dependency configs, and
23 # 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 export DEBIAN_FRONTEND=noninteractive
42
43 add-apt-repository -y ppa:coolwanglu/pdf2htmlex # pdf2htmlex
44
45 apt-get update
46 apt-get upgrade -y
47 apt-get install -y python-pip postgresql python-virtualenv virtualenvwrapper \
48                    git nginx postgresql-server-dev-9.1 libxslt1-dev \
49                    libxml2-dev libmemcached-dev python-dev rabbitmq-server \
50                    p7zip-full pdf2htmlex
51
52 cat >> `dpkg -L pdf2htmlex | grep pdf2htmlEX.js` <<PDF2HTMLEXHACK
53 Viewer.prototype['rescale'] = Viewer.prototype.rescale;
54 Viewer.prototype['scroll_to'] = Viewer.prototype.scroll_to;
55 PDF2HTMLEXHACK
56
57 echo "CREATE USER vagrant WITH CREATEROLE CREATEDB LOGIN; CREATE DATABASE karmaworld OWNER vagrant;" | su postgres -c "psql"
58
59 mkdir -m 775 -p /var/www
60 chown -R :www-data /var/www
61 usermod -a -G www-data vagrant
62
63 su vagrant -c "git clone /vagrant karmaworld"
64
65 SECRETPATH="karmaworld/secret"
66
67 su vagrant -c "cp /vagrant/$SECRETPATH/* karmaworld/$SECRETPATH/"
68
69 CFILE="karmaworld/$SECRETPATH/db_settings.py"
70 cat > $CFILE <<CONFIG
71 #!/usr/bin/env python
72 # -*- coding:utf8 -*-
73 # Copyright (C) 2012  FinalsClub Foundation
74 """
75 DO NOT check this file into source control.
76 """
77 PROD_DB_NAME = 'karmaworld'
78 PROD_DB_USERNAME = 'vagrant'
79 PROD_DB_PASSWORD = ''
80 CONFIG
81 chown vagrant:vagrant $SECRETPATH/*.py
82
83 cat > /etc/nginx/sites-available/karmaworld <<CONFIG
84 server {
85     listen 80;
86     # don't do virtual hosting, handle all requests regardless of header
87     server_name "";
88     client_max_body_size 20M;
89
90     location / {
91         # pass traffic through to gunicorn
92         proxy_pass http://127.0.0.1:8000;
93     }
94 }
95 CONFIG
96 rm /etc/nginx/sites-enabled/default
97 ln -s /etc/nginx/sites-available/karmaworld /etc/nginx/sites-enabled/karmaworld
98 sudo service nginx restart
99
100 cp karmaworld/confs/prod/supervisor /etc/init.d
101 chmod 755 /etc/init.d/supervisor
102 update-rc.d supervisor defaults
103
104 pip install fabric
105 SCRIPT
106 # end of script
107
108 Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
109   # All Vagrant configuration is done here. The most common configuration
110   # options are documented and commented below. For a complete reference,
111   # please see the online documentation at vagrantup.com.
112
113   # Every Vagrant virtual environment requires a box to build off of.
114   config.vm.box = "Official Ubuntu 12.04 daily Cloud Image i386"
115   #config.vm.box = "Official Ubuntu 12.04 daily Cloud Image amd64"
116   #config.vm.box = "Official Ubuntu 12.10 daily Cloud Image i386"
117   #config.vm.box = "Official Ubuntu 12.10 daily Cloud Image amd64"
118   #config.vm.box = "Official Ubuntu 13.04 daily Cloud Image i386"
119   #config.vm.box = "Official Ubuntu 13.04 daily Cloud Image amd64"
120   #config.vm.box = "Official Ubuntu 13.10 daily Cloud Image i386"
121   #config.vm.box = "Official Ubuntu 13.10 daily Cloud Image amd64"
122
123   # The url from where the 'config.vm.box' box will be fetched if it
124   # doesn't already exist on the user's system.
125   config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-i386-vagrant-disk1.box"
126   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box"
127   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/quantal/current/quantal-server-cloudimg-i386-vagrant-disk1.box"
128   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/quantal/current/quantal-server-cloudimg-amd64-vagrant-disk1.box"
129   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/raring/current/raring-server-cloudimg-i386-vagrant-disk1.box"
130   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/raring/current/raring-server-cloudimg-amd64-vagrant-disk1.box"
131   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/saucy/current/saucy-server-cloudimg-i386-vagrant-disk1.box"
132   #config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/saucy/current/saucy-server-cloudimg-amd64-vagrant-disk1.box"
133
134   # Create a forwarded port mapping which allows access to a specific port
135   # within the machine from a port on the host machine. In the example below,
136   # accessing "localhost:8080" will access port 80 on the guest machine.
137   # config.vm.network :forwarded_port, guest: 80, host: 8080
138
139   # OM (sanskrit) KW (KarmaWorld) on a phone: 66 59
140   config.vm.network :forwarded_port, guest: 80, host: 6659, auto_correct: true
141
142   # Create a private network, which allows host-only access to the machine
143   # using a specific IP.
144   #config.vm.network :private_network, ip: "192.168.66.59"
145
146   # Create a public network, which generally matched to bridged network.
147   # Bridged networks make the machine appear as another physical device on
148   # your network.
149   # Used to directly access the internet for downloading updates and so forth.
150   config.vm.network :public_network
151
152   # If true, then any SSH connections made will enable agent forwarding.
153   # Default value: false
154   # config.ssh.forward_agent = true
155
156   # Share an additional folder to the guest VM. The first argument is
157   # the path on the host to the actual folder. The second argument is
158   # the path on the guest to mount the folder. And the optional third
159   # argument is a set of non-required options.
160   # config.vm.synced_folder "../data", "/vagrant_data"
161
162   # Setup scripts
163   config.vm.provision "shell", inline: shellscript
164
165   # Provider-specific configuration so you can fine-tune various
166   # backing providers for Vagrant. These expose provider-specific options.
167   # Example for VirtualBox:
168   #
169   # config.vm.provider :virtualbox do |vb|
170   #   # Don't boot with headless mode
171   #   vb.gui = true
172   #
173   #   # Use VBoxManage to customize the VM. For example to change memory:
174   #   vb.customize ["modifyvm", :id, "--memory", "1024"]
175   # end
176   #
177   # View the documentation for the provider you're using for more
178   # information on available options.
179
180   # Enable provisioning with Puppet stand alone.  Puppet manifests
181   # are contained in a directory path relative to this Vagrantfile.
182   # You will need to create the manifests directory and a manifest in
183   # the file base.pp in the manifests_path directory.
184   #
185   # An example Puppet manifest to provision the message of the day:
186   #
187   # # group { "puppet":
188   # #   ensure => "present",
189   # # }
190   # #
191   # # File { owner => 0, group => 0, mode => 0644 }
192   # #
193   # # file { '/etc/motd':
194   # #   content => "Welcome to your Vagrant-built virtual machine!
195   # #               Managed by Puppet.\n"
196   # # }
197   #
198   # config.vm.provision :puppet do |puppet|
199   #   puppet.manifests_path = "manifests"
200   #   puppet.manifest_file  = "site.pp"
201   # end
202
203   # Enable provisioning with chef solo, specifying a cookbooks path, roles
204   # path, and data_bags path (all relative to this Vagrantfile), and adding
205   # some recipes and/or roles.
206   #
207   # config.vm.provision :chef_solo do |chef|
208   #   chef.cookbooks_path = "../my-recipes/cookbooks"
209   #   chef.roles_path = "../my-recipes/roles"
210   #   chef.data_bags_path = "../my-recipes/data_bags"
211   #   chef.add_recipe "mysql"
212   #   chef.add_role "web"
213   #
214   #   # You may also specify custom JSON attributes:
215   #   chef.json = { :mysql_password => "foo" }
216   # end
217
218   # Enable provisioning with chef server, specifying the chef server URL,
219   # and the path to the validation key (relative to this Vagrantfile).
220   #
221   # The Opscode Platform uses HTTPS. Substitute your organization for
222   # ORGNAME in the URL and validation key.
223   #
224   # If you have your own Chef Server, use the appropriate URL, which may be
225   # HTTP instead of HTTPS depending on your configuration. Also change the
226   # validation key to validation.pem.
227   #
228   # config.vm.provision :chef_client do |chef|
229   #   chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
230   #   chef.validation_key_path = "ORGNAME-validator.pem"
231   # end
232   #
233   # If you're using the Opscode platform, your validator client is
234   # ORGNAME-validator, replacing ORGNAME with your organization name.
235   #
236   # If you have your own Chef Server, the default validation client name is
237   # chef-validator, unless you changed the configuration.
238   #
239   #   chef.validation_client_name = "ORGNAME-validator"
240 end