Skip to content

Instantly share code, notes, and snippets.

@dergachev
Last active December 12, 2015 02:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dergachev/4698240 to your computer and use it in GitHub Desktop.
Save dergachev/4698240 to your computer and use it in GitHub Desktop.
Vagrant Talk (Drupalcamp NJ & Drupalcamp Ottawa)

Vagrant tutorial

Gist with notes from the BoF this afternoon: https://gist.github.com/4699228

Ruby for Chef

Just enough not to freak out

Methods

def double(num)
  num * 2
end
double 33 # => 66

Ruby Strings

:bob == 'bob'.to_sym
%w{git apache2 mysql} == ['git', 'apache2', 'mysql']
"head #{variable} tail" == "head " +  variable + " tail"

Ruby dictionaries

options = {
  :deploy_drupal => { 
    :apache_group => 'vagrant', 
    :sql_load_file => '/vagrant/db/fga.sql.gz',
    :source =>  "/vagrant/public/fga/www",
  },
  :mysql => {
    :server_root_password => "root",
  },
}

DSL

template "/etc/mysql/drupal-grants.sql" do
  path "/etc/mysql/drupal-grants.sql"
  source "grants.sql.erb"
  owner "root"
  group "root"
  mode "0600"
  notifies :run, "execute[mysql-install-drupal-privileges]", :immediately
end

In javascript:

template("/etc/mysql/drupal-grants.sql", function() {  
  this.path("/etc/mysql/drupal-grants.sql");
  this.source("grants.sql.erb");
  this.owner("root");
  this.group("root");
  this.mode("0600");
  this.notifies('run', "execute[mysql-install-drupal-privileges]", 'immediately');
});

Chef Basics

What can recipes do?

Install a user

user "sam" do
     home "/home/sam"
     shell "/bin/zsh"
     comment "Sam loves DevOps"
     action :create
     password :$1$pfFfDG3M$22vsMsPnn93ZnuodI86Ec0
end

Install a template

template '/var/www/sites/default/settings.php' do
  # action :create
  # action :create_if_missing
  source "settings.php.erb"
  variables ( {
    :user => 'root',
    :pass => node[:mysql]['root_password'],
    :name => 'drupalsite_v1',
  })
end

Install packages

package "tar" do
  action :install
end
package %w{vim git}
php_pear "uploadprogress"
gem_package "syntax"

Execute bash

execute "download drupal" do
  cwd '/var/www'
  command "drush dl drupal-7.20"
  creates '/var/www/drupal-7.20/index.php'
end

Deploy from git

deploy "/my/deploy/dir" do
  repo "git@github.com/mycompany/project"
  user "www-data"
  group "www-data"
  revision "master"
  action :sync # default value, also :checkout
end

defining and controlling service daemons

# runs /etc/init.d/apache2 (start|stop|restart), etc.
service "apache2" do
  supports :status => true, :restart => true, :reload => true
  action [ :enable, :start ]
end

apache cookbook: webapp LWRP

web_app 'drupalcampottawa' do
  template "web_app.conf.erb"
  port 80
  server_name 'drupalcampottawa.com'
  docroot '/var/www/dcampottawa'
  notifies :restart, resources("service[apache2]"), :delayed
end

Cookbook structure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment