Skip to content

Development Environment

zz9pzza edited this page Nov 21, 2015 · 28 revisions

Vagrant is a tool that allows us to easily create and install a fully-configured development environment.

Table of Contents

System Requirements

Initial Setup

  • Install version 4.3.8 or greater of VirtualBox
  • Install version 1.5.0 or greater of Vagrant

First Run

In a command window, make a new directory for the project and in that directory in a command window.

    vagrant init aooo/ao3_1; vagrant up --provider virtualbox

This step will first download and then boot the virtual machine.

Note: The download can take a long time -- make sure to set your computer not to go to sleep or else the download will be interrupted.

The output will look something like:

    A `Vagrantfile` has been placed in this directory. You are now
    ready to `vagrant up` your first virtual environment! Please read
    the comments in the Vagrantfile as well as documentation on
    `vagrantup.com` for more information on using Vagrant.
    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Importing base box 'aooo/ao3_1'...
    ==> default: Matching MAC address for NAT networking...
    ==> default: Checking if box 'aooo/ao3_1' is up to date...
    ==> default: Setting the name of the VM: vagrant_test_default_1435761128429_81089
    ==> default: Fixed port collision for 22 => 2222. Now on port 2200.
    ==> default: Clearing any previously set network interfaces...
    ==> default: Preparing network interfaces based on configuration...
        default: Adapter 1: nat
    ==> default: Forwarding ports...
        default: 22 => 2200 (adapter 1)
    ==> default: Booting VM...
    ==> default: Waiting for machine to boot. This may take a few minutes...
        default: SSH address: 127.0.0.1:2200
        default: SSH username: vagrant
        default: SSH auth method: private key
        default: Warning: Connection timeout. Retrying...
        default:
        default: Vagrant insecure key detected. Vagrant will automatically replace
        default: this with a newly generated keypair for better security.
        default:
        default: Inserting generated public key within guest...
        default: Removing insecure key from the guest if its present...
        default: Key inserted! Disconnecting and reconnecting using new SSH key...
    ==> default: Machine booted and ready!
    ==> default: Checking for guest additions in VM...
    ==> default: Mounting shared folders...
        default: /vagrant => /Users/mac/Desktop/vagrant_test

Now you have downloaded the Vagrant virtual machine, you can connect to it using the following command:

    vagrant ssh

This will connect you to the Ubuntu virtual machine as the user "vagrant".

Keeping Up To Date (vagrant provision)

You can keep your Vagrant install up to date with fixes and improvements by running

    vagrant provision

in the same directory as the Vagrantfile, after running vagrant up.

Reset RVM

This step is temporarily needed until we update the vagrant box.

Connect to the virtual machine with

    vagrant ssh

Edit the file bin/reset_rvm2 and paste in the following script

#!/bin/bash
if [ "$1" != "force" ] 
 then

 echo "This script resets the rvm enviroment, downloading and reinstalling ruby and the gems to a known good state."

 read -p "Are you sure? " -n 1
 if [[ ! $REPLY =~ ^[Yy]$ ]]
 then
    echo
    exit 1
 fi
fi

if [ -d ~/.rvm ] ; then
  mv ~/.rvm ~/.rvm-`date +%F-%H-%M-%N`
fi

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -L https://get.rvm.io | bash -s stable
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

rvm autolibs read-fail
rvm install ruby-2.0.0-p643
rvm --default use 2.0.0-p643
rvm gemset use global 
gem install rvm-capistrano
gem install bundler
gem install god
gem install unicorn
rvm rvmrc warning ignore /home/ao3app/app/current/Gemfile

Save the script and make it runnable with

    chmod u+x bin/reset_rvm2

Run the script

    bin/reset_rvm2

This will set up Ruby 2.0 correctly.

Clone the GitHub Repository

Next, you need to check out a copy of the Archive from GitHub. If you don't have a GitHub account, see Get a GitHub account.

If you intend to edit the Archive files using tools on your host machine, then perform the following step in the folder where you downloaded the Vagrantfile. The files will then be mirrored in the /vagrant folder on the Vagrant guest machine (note the leading slash in /vagrant).

   git clone git@github.com:YOURUSERNAME/otwarchive.git
   Cloning into 'otwarchive'...
   remote: Reusing existing pack: 78163, done.
   remote: Counting objects: 15, done.
   remote: Compressing objects: 100% (12/12), done.
   remote: Total 78178 (delta 5), reused 7 (delta 3)
   Receiving objects: 100% (78178/78178), 41.37 MiB | 745 KiB/s, done.
   Resolving deltas: 100% (56038/56038), done.

If you are going to edit the Archive files directly on the guest virtual machine, you can place them wherever is convenient.

Once you have cloned the repository, add the release version of the Archive upstream so you can keep up to date.

  git remote add upstream git://github.com/otwcode/otwarchive.git
  git fetch upstream

bundle install

After connecting with vagrant ssh:

   cd /vagrant/otwarchive/
   bundle install

You will see:

   Using rake (10.1.0)
   ...[long list of gems]...
   Your bundle is complete!
   Use `bundle show [gemname]` to see where a bundled gem is installed.

Update your database

  bundle exec rake db:migrate

Start Your Unicorns

After connecting with vagrant ssh:

   start_my_unicorns

You will see:

starting new unicorn master
setting /home/vagrant/app/current to /vagrant/otwarchive
copying config database.yml local.yml redis.yml
  -=t`", /`-.___.
    (_/, `-._.r="
      /|/`--,`}
      \I'  ,//  
 
vagrant@ubuntu:/vagrant/otwarchive$

Cache the Skins

In order to have the Archive skins available, you have to cache them.

After connecting with vagrant ssh, start a console with:

    bundle exec rails c 

In the console paste the command:

   1.9.3-p545 :001 > Skin.where("skins.cached = true and not  ( `skins`.`id` = 887 or `skins`.`id` = 890 or id=928 or id=929 ) ").each{|skin| skin.cache!}

Filter counts

  bundle exec rake Tag:reset_filter_counts

Browse the Archive

Point your browser to http://127.0.0.1:8081/

If you get a "connection denied" message, check your Vagrantfile contains:

  config.vm.network "forwarded_port", guest: 8081, host: 8081

If you get a "connection denied" message, you may need to configure VirtualBox to forward your port. (This has been an issue under OS X Yosemite in at least one case.)

  • Open VirtualBox on your computer
  • Right-click on the AO3 box in the list of boxes and choose Settings
  • Click on Network
  • Click on the Port Forwarding button
  • Forward host port 8081 to guest port 8081 and OK
If you continue to have issues, make sure that your version of VirtualBox matches the version of the Guest Additions installed in the vagrant box (you should be notified when you run vagrant up if it doesn't). If not, download an older version of VirtualBox from their site.

Load Autocomplete Data

To load the autocomplete data into redis, you have to use the rake task. After connecting with vagrant ssh:

   bundle exec rake autocomplete:load_data

This will take some time to complete. You can continue working on the Archive in the meantime.

Starting and Stopping the App

Starting

  start_my_unicorns

You will see:

   vagrant@ubuntu:/vagrant/otwarchive$

Stopping

  kill_my_unicorns

Run Resque Jobs

After connecting with vagrant ssh:

  start_my_workers

And

  kill_my_workers

Note: The version of the Archive code being run by the Resque jobs will not automatically reload new code as you write. So if you change the way any background job works, you will have to kill your workers and start them again before the changes will be reflected in your results.

Tests

Run Cucumber Tests

After connecting with vagrant ssh:

To run cucumber tests, use:

  run_cucumber features

This will run all the tests, which might take a while. You can run individual (once the system is set up by the above command) tests in the /features folder using the following syntax:

  export RAILS_ENV=test
  bundle exec rake db:create:all --trace
  bundle exec rake db:schema:load --trace
  bundle exec rake db:migrate --trace
  bundle exec cucumber -f progress -r features features/<folder, or folder/filename, or folder/filename:line-number>

Troubleshooting

Elasticsearch Problems

If your Vagrant copy of Elasticsearch has problems and the Archive can't connect to it, you may need to restart Elasticsearch.

After connecting with vagrant ssh

  sudo /etc/init.d/elasticsearch restart

Clear Memcached

If you need to dump your Memcached store:

  rails c development

This will load your Rails console in the development environment. You then need to issue the command to dump the store:

  Rails.cache.clear

Share Your Work with Others

Show Your Copy of the Archive to Others

  • Make sure your Vagrant Archive is started and responding on port 8081 as explained above.
  • Create an account at vagrantcloud.com.
  • On the command line, go to your Archive Vagrant folder, type in vagrant login and enter the details you set up at step 2.
  • Type vagrant share --http 8081 to enable sharing. It will allocate a URL based on some random name and number to your box. You should be able to access your copy of the Archive over the Internet.
Notes: The share is actually running in the command line/Terminal window where you started it in step 4 -- the share will expire if you terminate the command or close the window. Otherwise, it expires after one hour. You can view any running shares on your account page at vagrantcloud.com and terminate them there.

Share Your Vagrant via SSH

Please see the SSH Sharing instructions at VagrantUp. Sharing your box via ssh will allow you to collaborate directly with other coders, or enable Systems to troubleshoot problems on your box.

Navigation

Clone this wiki locally