alexis-j
11/13/2012 - 9:26 AM

vagrant setup

vagrant setup

# Run apt-get update to create the stamp file
execute "apt-get-update" do
  command "apt-get update"
  ignore_failure true
  not_if do ::File.exists?('/var/lib/apt/periodic/update-success-stamp') end
end

# For other recipes to call to force an update
execute "apt-get update" do
  command "apt-get update"
  ignore_failure true
  action :nothing
end

# provides /var/lib/apt/periodic/update-success-stamp on apt-get update
package "update-notifier-common" do
  notifies :run, resources(:execute => "apt-get-update"), :immediately
end

execute "apt-get-update-periodic" do
  command "apt-get update"
  ignore_failure true
  only_if do
    File.exists?('/var/lib/apt/periodic/update-success-stamp') &&
      File.mtime('/var/lib/apt/periodic/update-success-stamp') < Time.now - 86400
  end
end

# install the software we need
%w(
openjdk-6-jre-headless
curl
tmux
vim
emacs23-nox
git
libapache2-mod-php5
postgresql
php5-cli
php5-curl
php5-sqlite
php5-intl
php5-pgsql
build-essential
php5-dev
php-pear
php5-xdebug
chrpath
libssl-dev
libfontconfig1-dev
).each { | pkg | package pkg }

execute "add phar to suhosin.ini whitelist so composer works in the box" do
  not_if "grep phar /etc/php5/conf.d/suhosin.ini"
  command "echo 'suhosin.executor.include.whitelist = phar' >> /etc/php5/conf.d/suhosin.ini"
end

template "/home/vagrant/.bash_aliases" do
  user "vagrant"
  mode "0644"
  source ".bash_aliases.erb"
end

file "/etc/apache2/sites-enabled/000-default" do
  action :delete
end

template "/etc/apache2/sites-enabled/vhost.conf" do
  user "root"
  mode "0644"
  source "vhost.conf.erb"
  notifies :reload, "service[apache2]"
end

template "/etc/php5/apache2/php.ini" do
  user "root"
  mode "0644"
  source "php.ini.erb"
  notifies :reload, "service[apache2]"
end

execute "give access to postgres user for everyone with unix sockets" do
  user "root"
  not_if "egrep 'trust$' /etc/postgresql/9.1/main/pg_hba.conf"
  command "sed -i 's/peer$/trust/' /etc/postgresql/9.1/main/pg_hba.conf"
  notifies :reload, "service[postgresql]"
end


service "postgresql" do
  supports :restart => true, :reload => true, :status => true
  action [ :enable, :start ]
end

bash "postgres: import the database " do
  not_if "psql --list | grep symfonyreal", :user => "postgres"
  user "postgres"
  code <<-EOH
      set -e
      createdb symfonyreal
      EOH
end

bash "postgres: set up test database " do
  not_if "psql --list | grep symfonytest", :user => "postgres"
  user "postgres"
  code <<-EOH
      set -e
      createdb symfonytesttest
      EOH
end

execute "a2enmod rewrite"

service "apache2" do
  supports :restart => true, :reload => true, :status => true
  action [ :enable, :start ]
end

execute "date.timezone = UTC in php.ini?" do
 user "root"
 not_if "grep 'date.timezone = UTC' /etc/php5/cli/php.ini"
 command "echo -e '\ndate.timezone = UTC\n' >> /etc/php5/cli/php.ini"
end

bash "install phantom.js " do
  not_if "which phantomjs"
  code <<-EOH
      set -e
      wget http://phantomjs.googlecode.com/files/phantomjs-1.7.0-linux-i686.tar.bz2
      tar jxf phantomjs-1.7.0-linux-i686.tar.bz2
      rm -rf phantomjs-1.7.0-linux-i686.tar.bz2
      ln -sf /phantomjs-1.7.0-linux-i686/bin/phantomjs /usr/local/bin/phantomjs
      EOH
end

bash "install casper.js " do
  not_if "which casperjs"
  code <<-EOH
      set -e
      git clone git://github.com/n1k0/casperjs.git
      cd casperjs
      git checkout tags/1.0.0-RC4
      ln -sf /casperjs/bin/casperjs /usr/local/bin/casperjs
      EOH
end

bash "install elasticsearch" do
    not_if do ::File.exists?('/usr/local/share/elasticsearch') end
    code <<-EOH
        set -e

        wget https://github.com/downloads/elasticsearch/elasticsearch/elasticsearch-0.19.0.tar.gz -O elasticsearch.tar.gz
        tar -xf elasticsearch.tar.gz
        rm elasticsearch.tar.gz
        sudo mv elasticsearch-* elasticsearch
        sudo mv elasticsearch /usr/local/share

        curl -L http://github.com/elasticsearch/elasticsearch-servicewrapper/tarball/master | tar -xz
        mv *servicewrapper*/service /usr/local/share/elasticsearch/bin/
        rm -Rf *servicewrapper*
        sudo /usr/local/share/elasticsearch/bin/service/elasticsearch install
        sudo ln -s `readlink -f /usr/local/share/elasticsearch/bin/service/elasticsearch` /usr/local/bin/rcelasticsearch

        sudo service elasticsearch start
        EOH
end

bash "install elasticsearch-head " do
    not_if do ::File.exists?('/var/www/elasticsearch-head') end
    code <<-EOH
        set -e
        sudo git clone https://github.com/mobz/elasticsearch-head.git /var/www/elasticsearch-head
        sudo sed -i "s/new es.ElasticSearchHead/base_uri='http:\\/\\/elastic.lo:9200'; new es.ElasticSearchHead/" /var/www/elasticsearch-head/index.html
        EOH
end

template "/etc/apache2/sites-enabled/elasticsearch-head.conf" do
  user "root"
  mode "0644"
  source "elasticsearch-head.conf.erb"
  notifies :reload, "service[apache2]"
end

bash "install phpunit " do
  not_if "which phpunit"
  user "root"
  code <<-EOH
      pear channel-discover pear.phpunit.de
      pear channel-discover pear.symfony.com
      pear install --alldeps phpunit/PHPUnit
      EOH
end

execute "re-run composer to get the post-install things right" do
    user "vagrant"
    cwd "/vagrant"
    command "php composer.phar install --dev --prefer-source"
end
# Override the Apache User (vhosts are included at the end of apache2.conf)
User vagrant
Group vagrant

<VirtualHost *:80>
  Servername clem.lo
  DocumentRoot /vagrant/web
  <Directory "/vagrant/web">
    AllowOverride All
  </Directory>
</VirtualHost>

# -*- mode: ruby -*-
# vi: set ft=ruby :

this_dir = File.dirname(__FILE__) + "/"
require this_dir + "vagrant/hostmaster.rb"

Vagrant::Config.run do |config|

  # define some colors for our output
  def colorize(text, color_code) "#{color_code}#{text}\033[0m" end
  def red(text); colorize(text, "\033[31m"); end
  def green(text); colorize(text, "\033[32m"); end
  def yellow(text); colorize(text, "\033[33m"); end

  Dir.chdir this_dir

  if ARGV.include?("up") || ARGV.include?("provision")
    unless File.exists?(this_dir + "composer.phar")
      `curl -s https://getcomposer.org/installer | php`
      (puts red("Getting composer failed. Exiting..."); exit) unless $?.success?
    else
      IO.popen("php composer.phar self-update")
    end
    file = this_dir + "app/config/parameters.yml"
    FileUtils.cp(file + ".dist", file) unless File.exists?(file)
    puts "Running composer install..."
    IO.popen("php composer.phar install --dev --prefer-source") { |handle| handle.each { |line| puts line } }
    (puts red("Composer install failed. Exiting..."); exit) unless $?.success?
  end

  config.vm.box = "precise32"
  config.vm.customize ["modifyvm", :id, "--memory", 768]

  # The url from where the 'config.vm.box' box will be fetched if it
  # doesn't already exist on the user's system.
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"

  # Boot with a GUI so you can see the screen. (Default is headless)
  # config.vm.boot_mode = :gui

  # NFS only necessary if performance too slow
  config.vm.share_folder "v-root", "/vagrant", "." , :nfs => true
  # whithout this symlinks can't be created on the shared folder
  config.vm.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]

  # the hostmaster plugin populates /etc/hosts
  config.vm.host_name = "symfony.lo"
  config.hosts.aliases = %w(elastic.lo)

  # the ip address where the vm can be accessed from the host
  config.vm.network :hostonly, "172.172.172.172"

  # chef solo configuration
  config.vm.provision :chef_solo do |chef|

    chef.cookbooks_path = "./"
    # chef debug level, start vagrant like this to debug:
    # $ CHEF_LOG_LEVEL=debug vagrant <provision or up>
    chef.log_level = ENV['CHEF_LOG'] || "info"

    # chef recipes/roles
    chef.add_recipe("vagrant")
  end
end