Zen Rails Development Environment
Summary
Development environment for Ruby on Rails based on Vagrant, VirtualBox and Ubuntu 16.04 LTS (Xenial Xerus).
The Vagrant provisioning scripts do the following:
- Set the VM locale and time zone.
- Configure the VM for SSH access with a custom SSH key.
- Use RVM to install any version of Ruby and Rails in the VM.
- Install PostgreSQL, configure it to listen on 0.0.0.0, create a new user
called
ubuntu
with passwordlocaldbpass
and grant it CREATEDB permissions. - Install MySQL, configure it to listen on 0.0.0.0 and set the password of
the
root
user tolocaldbpass
. - Install Yarn, Rails' official Frontend package manager as of Rails 5.1.
- Install and configure Git at the VM.
- Install Elastic Beanstalk CLI at the VM.
- Install Heroku CLI at the VM.
- Install Selenium Webdriver, ChromeDriver, and Xvfb. These allow the execution of JavaScript-enabled tests on Minitest or RSpec (with Capybara) using headless Google Chrome.
Other scripts run at shell startup to set environment variables and Bash shortcuts (aliases and functions).
Installation
-
Install the latest version of Vagrant and VirtualBox on the host machine.
-
Choose a directory for your VirtualBox shared folder (in the host machine). Download this repository and unzip it inside the chosen directory.
-
Create an SSH key pair, name the files
id_rsa
(private key),id_rsa.pub
(public key), and place them in thevm_provisioning
directory. -
Edit the
Vagrantfile
in order to customize the values of the variables located in the "User Configurations" section. -
Optional: If you require any custom environment variables to be available at the guest VM, create a file named
environment_variables.sh
within thevm_provisioning
directory and set your environment vars using the standard bash syntax: oneexport VAR_NAME='var value'
declaration per line. Those vars will be loaded automatically every time the VM boots. That is a good place to store your API keys and other secrets. -
Optional: If you wish to use any custom bash aliases or functions, add them to
bash_shortcuts.sh
-
Optional: If you intend to use Selenium and Chrome headless with Capybara and RSpec, add the following to
spec/rails_helper.rb
:require 'selenium-webdriver' Capybara.register_driver :selenium do |app| options = Selenium::WebDriver::Chrome::Options.new # The window size is important for screenshots options.add_argument "--window-size=1366,768" Selenium::WebDriver::Chrome.driver_path = "/usr/local/bin/chromedriver" Capybara::Selenium::Driver.new(app, browser: :chrome, options: options) end
Files and their purposes
File | Contents and/or purpose | Executed at | Called by |
---|---|---|---|
bash_shortcuts.sh | Custom bash aliases and function definitions. | "Login" (shell startup) | Symlink at /etc/profile.d |
environment_variables.sh | Custom environment variable definitions. You may store your API keys and other secrets here, as long as you keep this file outside any Git repositories. | "Login" (shell startup) | Symlink at /etc/profile.d |
boot.sh | Custom startup commands. These commands were not inserted in rc.local because: a) we prefer Bash over Bourne Shell; b) the -e flag used in rc.local's shebang line causes undesired behavior. | VM boot | /etc/rc.local |
custom_rc_local.sh | Custom rc.local. Waits for the Vagrant shared folder to be mounted and executes the custom boot script (boot.sh ). |
VM boot | Installed by provision.sh |
provision.sh | Installs and configures everything in the VM, except Ruby and Rails. | VM provisioning | Vagrantfile |
install_ruby.sh | Installs Ruby and Rails in the VM at the end of provisioning. | VM provisioning | Vagrantfile |
Vagrantfile | General configurations. Set up the VirtualBox VM and the provisioning process. | vagrant up, halt, etc | the user |
Usage
cd to the directory where the Vagrantfile is (in the host machine) and run:
vagrant up
to start the VM;vagrant ssh
to access the VM via SSH;vagrant halt
to turn off the VM;
You can also access the VM via SSH by connecting to port 2222 of the host
machine with your favorite SSH client, using the private key specified in the
ssh_private_key
variable of the Vagrantfile.
Shortcuts
All shortcuts except jcurl
only work when executed within a Rails app
directory.
rs
rails s -b 0.0.0.0
rc
rails console
br
bin/rspec. Use within the Rails application folder to run RSpec with
Spring.
brfd
bin/rspec --format documentation
tdl
tail -f development log
ttl
tail -f test log
ctl
clear test log
cdl
clear development log
jcurl
runs curl -s and pretty prints JSON output with the awesome_print gem.
Takes a URL as an argument.
bi
Run bundle install in multiple parallel threads (faster).
gpl
git pull
ga
git add
gc
git commit. Takes a commit message as an argument.
gp
git push. Takes a remote name and a branch name as optional arguments
(e.g., origin master)
gs
git status
gl
git log
Contributing
Bug reports
Please use the issue tracker to report any bugs.
Developing
- Create an issue and describe your idea
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -m 'Add some feature')
- Publish the branch (git push origin my-new-feature)
- Create a Pull Request
License
Released under the MIT License.