Ignore IDN decoding errors.
[reddit.git] / Vagrantfile
blob6461b36016eb5e6904d80f6ed79d6648d5ef3cb6
1 # -*- mode: ruby -*-
2 # vi: set ft=ruby :
4 # This assumes that the host machine has r2 and all the reddit plugins checked
5 # out and in the correct directories--pay attention to both name and position
6 # relative to the r2 code:
8 # r2:         {ROOTDIR}/reddit
10 # plugins:
11 # about:      {ROOTDIR}/about
12 # gold:       {ROOTDIR}/gold
13 # liveupdate: {ROOTDIR}/liveupdate
14 # private:    {ROOTDIR}/private
16 # All plugins are optional. A plugin will only be installed if it is listed
17 # in `plugins` AND it is located in a directory that both follows the plugin
18 # naming convention and is correctly located on the host machine. The general
19 # rule for naming each plugin directory is that "reddit-plugin-NAME" should be
20 # in the directory {ROOTDIR}/NAME.
22 # This VagrantFile allows for the creation of two VMs:
23 #   * default: the primary VM, with all services necessary to run reddit
24 #              locally against the local codebase.
25 #   * travis:  Testing-only VM suitable for running `nosetests` and debugging
26 #              issues encountered without having to wait for travis-ci to pick
27 #              up the build.  This will *not* be the same environment as
28 #              travis, but it should be useful for repairing broken tests.
30 # To start your vagrant box simply enter `vagrant up` from {ROOTDIR}/reddit.
31 # You can then ssh into it with `vagrant ssh`.
33 # avahi-daemon is installed on the guest VM so you can access your local install
34 # at https://reddit.local. If that fails you'll need to update your host
35 # machine's hosts file (/etc/hosts) to include the line:
36 # 192.168.56.111 reddit.local
38 # If you want to create additional vagrant boxes you can copy this file
39 # elsewhere, but be sure to update `code_share_host_path` to be the absolute
40 # path to {ROOTDIR}.
42 vagrant_user = "vagrant"
44 # code directories
45 this_path = File.absolute_path(__FILE__)
46 reddit_dir = File.expand_path("..", this_path)
47 code_share_host_path = File.expand_path("..", reddit_dir)
48 code_share_guest_path = "/media/reddit_code"
49 plugins = [
50   "about",
51   "gold",
52   "liveupdate",
53   "private",
56 # overlayfs directories
57 overlay_mount = "/home/#{vagrant_user}/src"
58 overlay_lower = code_share_guest_path
59 overlay_upper = "/home/#{vagrant_user}/.overlay"
61 # "default" vm config
62 guest_ip = "192.168.56.111"
63 guest_mem = "4096"
64 guest_swap = "4096"
65 hostname = "reddit.local"
68 Vagrant.configure(2) do |config|
69   config.vm.box = "trusty-cloud-image"
70   config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
72   # mount the host shared folder
73   config.vm.synced_folder code_share_host_path, code_share_guest_path, mount_options: ["ro"]
75   config.vm.provider "virtualbox" do |vb|
76     vb.memory = guest_mem
77   end
79   # ubuntu cloud image has no swapfile by default, set one up
80   config.vm.provision "shell", inline: <<-SCRIPT
81     if ! grep -q swapfile /etc/fstab; then
82       echo 'swapfile not found. Adding swapfile.'
83       fallocate -l #{guest_swap}M /swapfile
84       chmod 600 /swapfile
85       mkswap /swapfile
86       swapon /swapfile
87       echo '/swapfile none swap defaults 0 0' >> /etc/fstab
88     else
89       echo 'swapfile found. No changes made.'
90     fi
91   SCRIPT
93   # set up the overlay filesystem
94   config.vm.provision "shell", inline: <<-SCRIPT
95     if [ ! -d #{overlay_mount} ]; then
96       echo "creating overlay mount directory #{overlay_mount}"
97       sudo -u #{vagrant_user} mkdir #{overlay_mount}
98     fi
100     if [ ! -d #{overlay_upper} ]; then
101       echo "creating overlay upper directory #{overlay_upper}"
102       sudo -u #{vagrant_user} mkdir #{overlay_upper}
103     fi
105     echo "mounting overlayfs (lower: #{overlay_lower}, upper: #{overlay_upper}, mount: #{overlay_mount})"
106     mount -t overlayfs overlayfs -o lowerdir=#{overlay_lower},upperdir=#{overlay_upper} #{overlay_mount}
107   SCRIPT
109   # NOTE: This VM exists solely to assist in writing tests.  It does not actually
110   # install travis but rather builds a minimal vm with only the services
111   # available under a travis build to aid in test debugging (via `nosetests`)
112   # To use:
113   #     $ vagrant up travis
114   #     $ vagrant ssh travis
115   #     vagrant@travis$ cd src/reddit/r2 && nosetests
116   config.vm.define "travis", autostart: false do |travis|
117       travis.vm.hostname = "travis"
118       # run install script
119       travis.vm.provision "shell", inline: <<-SCRIPT
120         if [ ! -f /var/local/reddit_installed ]; then
121           echo "running install script"
122           cd /home/#{vagrant_user}/src/reddit
123           ./install/travis.sh vagrant
124           touch /var/local/reddit_installed
125         else
126           echo "install script already run"
127         fi
128       SCRIPT
129   end
131   # NB: this is the primary VM. To build run
132   #    $ vagrant up
133   # [though 'vagrant up default' will also work, the 'default' is redudnant]
134   # Once built, avahi-daemon should guarantee the instance will be accessible
135   # from https://reddit.local/
136   config.vm.define "default", primary: true do |redditlocal|
137       redditlocal.vm.hostname = hostname
138       # host-only network interface
139       redditlocal.vm.network "private_network", ip: guest_ip
141       # rabbitmq web interface
142       config.vm.network "forwarded_port", guest: 15672, host: 15672
144       # run install script
145       plugin_string = plugins.join(" ")
146       redditlocal.vm.provision "shell", inline: <<-SCRIPT
147         if [ ! -f /var/local/reddit_installed ]; then
148           echo "running install script"
149           cd /home/#{vagrant_user}/src/reddit
150           REDDIT_PLUGINS="#{plugin_string}" REDDIT_DOMAIN="#{hostname}" ./install/reddit.sh
151           touch /var/local/reddit_installed
152         else
153           echo "install script already run"
154         fi
155       SCRIPT
157       # inject test data
158       redditlocal.vm.provision "shell", inline: <<-SCRIPT
159         if [ ! -f /var/local/test_data_injected ]; then
160           cd /home/#{vagrant_user}/src/reddit
161           sudo -u #{vagrant_user} reddit-run scripts/inject_test_data.py -c 'inject_test_data()'
162           touch /var/local/test_data_injected
163         else
164           echo "inject test data already run"
165         fi
167         # HACK: stop and start everything (otherwise sometimes there's an issue with
168         # ports being in use?)
169         reddit-stop
170         reddit-start
171       SCRIPT
173       # additional setup
174       redditlocal.vm.provision "shell", inline: <<-SCRIPT
175         if [ ! -f /var/local/additional_setup ]; then
176           apt-get install -y ipython avahi-daemon
177           touch /var/local/additional_setup
178         else
179           echo "additional setup already run"
180         fi
181       SCRIPT
183       # DONE: let this run whenever provision is run so that the user can see
184       # how to proceed.
185       redditlocal.vm.provision "shell", inline: <<-SCRIPT
186         cd /home/#{vagrant_user}/src/reddit
187         REDDIT_DOMAIN="#{hostname}" ./install/done.sh
188       SCRIPT
189   end