Created
September 27, 2014 05:18
-
-
Save jamesmoriarty/4ac960319d06a4823761 to your computer and use it in GitHub Desktop.
Vagrant, Ansible, Rbenv and Ruby on Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: all | |
vars: | |
app: rails-app | |
app_path: /vagrant | |
app_user: vagrant | |
ruby: 2.1.0 | |
packages: | |
- software-properties-common | |
- build-essential | |
- runit | |
- curl | |
- wget | |
- libsqlite3-dev | |
- nodejs | |
handlers: | |
- name: restart app | |
command: sv restart {{app}} | |
sudo: yes | |
- name: rbenv rehash | |
shell: rbenv rehash | |
environment: | |
PATH: /home/{{app_user}}/.rbenv/shims:/home/{{app_user}}/.rbenv/bin:{{lookup('env', 'PATH')}} | |
tasks: | |
- command: apt-get update | |
sudo: yes | |
- apt: name={{item}} state=present | |
sudo: yes | |
with_items: packages | |
- name: rbenv build dependencies | |
apt: pkg={{item}} state=present | |
sudo: yes | |
with_items: | |
- build-essential | |
- git-core | |
- patch | |
- zlib1g-dev | |
- libssl-dev | |
- libreadline-dev | |
- git: depth=1 update=no repo=git://github.com/sstephenson/rbenv.git dest=.rbenv version=v0.4.0 | |
- copy: src=files/rbenv.sh dest=/etc/profile.d/rbenv.sh owner=root group=root mode=0755 | |
sudo: yes | |
- command: rbenv versions | |
environment: | |
PATH: /home/{{app_user}}/.rbenv/shims:/home/{{app_user}}/.rbenv/bin:{{lookup('env', 'PATH')}} | |
register: ruby_installed | |
failed_when: "'{{ruby}}' not in ruby_installed.stdout" | |
changed_when: False | |
ignore_errors: yes | |
- command: mktemp -d | |
register: tempdir | |
when: ruby_installed|failed | |
- git: depth=1 repo=git://github.com/sstephenson/ruby-build.git dest={{tempdir.stdout}}/ruby-build | |
when: ruby_installed|failed | |
- command: ./install.sh chdir={{tempdir.stdout}}/ruby-build | |
when: ruby_installed|failed | |
sudo: yes | |
- file: path={{tempdir.stdout}} state=absent | |
when: ruby_installed|failed | |
- command: rbenv install {{ruby}} | |
environment: | |
PATH: /home/{{app_user}}/.rbenv/shims:/home/{{app_user}}/.rbenv/bin:{{lookup('env', 'PATH')}} | |
when: ruby_installed|failed | |
- command: rbenv global {{ruby}} | |
environment: | |
PATH: /home/{{app_user}}/.rbenv/shims:/home/{{app_user}}/.rbenv/bin:{{lookup('env', 'PATH')}} | |
when: ruby_installed|failed | |
- command: gem install bundler | |
environment: | |
PATH: /home/{{app_user}}/.rbenv/shims:/home/{{app_user}}/.rbenv/bin:{{lookup('env', 'PATH')}} | |
- shell: rbenv rehash | |
environment: | |
PATH: /home/{{app_user}}/.rbenv/shims:/home/{{app_user}}/.rbenv/bin:{{lookup('env', 'PATH')}} | |
- shell: chdir={{app_path}} bundle | |
environment: | |
PATH: /home/{{app_user}}/.rbenv/shims:/home/{{app_user}}/.rbenv/bin:{{lookup('env', 'PATH')}} | |
notify: | |
- rbenv rehash | |
- file: path="/etc/service/{{app}}" state=directory recurse=yes owner="{{app_user}}" | |
sudo: yes | |
- template: src=templates/run dest="/etc/service/{{app}}/run" mode="775" | |
sudo: yes | |
notify: | |
- restart app | |
- name: open browser | |
local_action: command open http://localhost:3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
export PATH=$HOME/.rbenv/bin:/usr/local/bin:$PATH | |
export RBENV_ROOT="$HOME/.rbenv" | |
eval "$(rbenv init -)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
PATH=/home/{{app_user}}/.rbenv/shims:/home/{{app_user}}/.rbenv/bin:$PATH | |
cd {{app_path}} | |
exec chpst -u {{app_user}} bundle exec rails server --binding 0.0.0.0 2>&1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! | |
VAGRANTFILE_API_VERSION = "2" | |
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| | |
config.vm.network :forwarded_port, guest: 3000, host: 3000 | |
config.vm.box = "precise" | |
config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box" | |
config.vm.synced_folder ".", "/vagrant" | |
config.vm.provision "ansible" do |ansible| | |
ansible.playbook = "ansible/rails-app.yml" | |
end | |
config.vm.provider :virtualbox do |vb| | |
vb.customize ["modifyvm", :id, "--memory", "1024"] | |
end | |
if Vagrant.has_plugin?("vagrant-cachier") | |
# Configure cached packages to be shared between instances of the same base box. | |
# More info on http://fgrehm.viewdocs.io/vagrant-cachier/usage | |
config.cache.scope = :box | |
# OPTIONAL: If you are using VirtualBox, you might want to use that to enable | |
# NFS for shared folders. This is also very useful for vagrant-libvirt if you | |
# want bi-directional sync | |
config.cache.synced_folder_opts = { | |
type: :nfs, | |
# The nolock option can be useful for an NFSv3 client that wants to avoid the | |
# NLM sideband protocol. Without this option, apt-get might hang if it tries | |
# to lock files needed for /var/cache/* operations. All of this can be avoided | |
# by using NFSv4 everywhere. Please note that the tcp option is not the default. | |
mount_options: ['rw', 'vers=3', 'tcp', 'nolock'] | |
} | |
# For more information please check http://docs.vagrantup.com/v2/synced-folders/basic_usage.html | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment