vagrant-lxc-ng/setup-vagrant-dev-box

135 lines
3.8 KiB
Text
Raw Normal View History

2013-02-25 04:58:04 +00:00
#!/usr/bin/env ruby
raise 'You should not run this script from the dev box' if ENV['USER'] == 'vagrant'
require 'bundler'
require 'json'
IMAGE_ROOT = 'https://cloud-images.ubuntu.com/releases/quantal/release-20130206'
IMAGE_NAME = 'ubuntu-12.10-server-cloudimg-amd64-root.tar.gz'
VAGRANT_REPO = 'https://raw.github.com/mitchellh/vagrant/master'
def download(source, destination)
destination = "#{File.dirname __FILE__}/cache/#{destination}"
return if File.exists?(destination)
sh "wget #{source} -O #{destination}"
end
def sh(cmd)
Bundler.with_clean_env do
puts cmd
raise 'Errored!' unless system cmd
end
end
def restore_snapshot!
sh 'vagrant halt -f'
conf = JSON.parse File.read('.vagrant')
id = conf['active']['default']
sh "VBoxManage snapshot '#{id}' restore ready-to-rock"
sh 'vagrant up'
exit 0
end
2013-02-25 05:22:18 +00:00
def vagrant_ssh(cmd)
2013-02-26 03:35:56 +00:00
sh "vagrant ssh -c '#{cmd}'"
2013-02-25 05:22:18 +00:00
end
2013-02-25 04:58:04 +00:00
Bundler.with_clean_env do
# Ensure box has not been created yet
unless `vagrant status` =~ /not created/
print 'Vagrant box already created, do you want to [r]ecreate it, restore [s]napshot or [A]bort? '
answer = gets.chomp
exit 0 if answer.empty? || answer =~ /^a/i
case
when answer =~ /^s/i
restore_snapshot!
when answer =~ /^r/i
sh 'vagrant destroy -f'
else
puts 'Invalid option!'
exit 1
end
end
end
# Cache development dependencies
`mkdir -p cache`
# Fetches vagrant submodule
`git submodule update --init`
2013-02-25 04:58:04 +00:00
# Cache container image between vagrant box destructions
download "#{IMAGE_ROOT}/#{IMAGE_NAME}", IMAGE_NAME
# Start vagrant
sh 'vagrant up'
# Because I'm lazy ;)
2013-02-26 03:35:56 +00:00
vagrant_ssh 'echo "cd /vagrant" >> ~/.bashrc'
vagrant_ssh 'echo "alias be=\"bundle exec\"" >> ~/.bashrc'
2013-02-25 04:58:04 +00:00
# "be" archive is too slow for me
2013-02-26 03:35:56 +00:00
vagrant_ssh 'sudo sed -i -e "s/be.archive/br.archive/g" /etc/apt/sources.list'
2013-02-25 04:58:04 +00:00
# Ensure we have the latest packages around
2013-02-25 05:22:18 +00:00
vagrant_ssh "sudo apt-get update && sudo apt-get upgrade -y"
2013-02-25 04:58:04 +00:00
2013-02-25 22:48:23 +00:00
# Install dependencies
2013-02-25 23:49:33 +00:00
vagrant_ssh "sudo apt-get install lxc rinetd libffi-dev libffi-ruby ruby1.9.1-dev htop git virtualbox virtualbox-ose-dkms linux-headers-generic linux-headers-3.5.0-17-generic -y && sudo gem install bundler --no-ri --no-rdoc -v 1.2.5"
vagrant_ssh "sudo dkms install virtualbox/4.1.18"
vagrant_ssh "sudo service virtualbox start"
2013-02-25 04:58:04 +00:00
2013-02-26 03:35:56 +00:00
# Ensure the machine can boot properly after upgrades and dependencies have been installed
sh 'vagrant reload'
# Allow gems to be installed on vagrant user home avoiding "sudo"s
# Tks to http://wiki.railsplayground.com/railsplayground/show/How+to+install+gems+and+non+root+user
vagrant_ssh 'mkdir -p ~/gems'
vagrant_ssh "cat << EOF >> ~/.profile
export GEM_HOME=$HOME/gems
export GEM_PATH=$HOME/gems:/var/lib/gems/1.9.1
export PATH=$PATH:$HOME/gems/bin
EOF"
vagrant_ssh "cat << EOF > .gemrc
---
:verbose: true
gem: --no-ri --no-rdoc
:update_sources: true
:sources:
- http://gems.rubyforge.org
- http://gems.github.com
:backtrace: false
:bulk_threshold: 1000
:benchmark: false
gemhome: $HOME/gems
gempath:
- $HOME/gems
- /usr/local/lib/ruby/gems/1.8
EOF"
2013-02-25 04:58:04 +00:00
# Backup rinetd config
2013-02-25 05:22:18 +00:00
vagrant_ssh "cp /etc/rinetd.conf /vagrant/cache/rinetd.conf"
2013-02-25 04:58:04 +00:00
# Make rinetd writable by vagrant user
2013-02-25 05:22:18 +00:00
vagrant_ssh 'sudo chown vagrant:vagrant /etc/rinetd.conf'
2013-02-25 04:58:04 +00:00
# Bundle!
vagrant_ssh 'cd /vagrant && bundle && cd /vagrant/dev && bundle'
2013-02-25 04:58:04 +00:00
# Setup vagrant default ssh key
vagrant_keys_path = '/vagrant/vagrant/keys'
vagrant_ssh "mkdir -p ~/.ssh && cd /vagrant && cp #{vagrant_keys_path}/vagrant ~/.ssh/id_rsa && cp #{vagrant_keys_path}/vagrant.pub ~/.ssh/id_rsa.pub && chmod 600 ~/.ssh/id_rsa"
2013-02-25 04:58:04 +00:00
# Setup lxc cache
2013-02-25 05:22:18 +00:00
vagrant_ssh "sudo mkdir -p /var/cache/lxc/cloud-quantal && sudo cp /vagrant/cache/#{IMAGE_NAME} /var/cache/lxc/cloud-quantal/#{IMAGE_NAME}"
2013-02-25 04:58:04 +00:00
# Click
sh 'vagrant halt'
conf = JSON.parse File.read('.vagrant')
id = conf['active']['default']
sh "VBoxManage snapshot '#{id}' take ready-to-rock"
sh 'vagrant up'