Skip to content

Instantly share code, notes, and snippets.

@dggodfrey
Last active October 9, 2015 21:07
Show Gist options
  • Save dggodfrey/8c81bb7ee61f3c1b460e to your computer and use it in GitHub Desktop.
Save dggodfrey/8c81bb7ee61f3c1b460e to your computer and use it in GitHub Desktop.
Notes on Chef/Vagrant

Install

  1. Virtual Box https://www.virtualbox.org/wiki/Downloads
  2. Vagrant https://www.vagrantup.com/downloads.html

Vagrant

Boxes https://atlas.hashicorp.com/boxes/search

Settings https://docs.vagrantup.com/v2/vagrantfile/machine_settings.html

Forward Ports config.vm.network "forwarded_port", guest: 8888, host: 8990

Provision

  config.vm.provision "chef_solo" do |chef|
    chef.install = true
    chef.add_recipe "basic"
  end

Try It

cd into your project structure and run:

  1. mkdir -p cookbooks/basic/recipes
  2. mkdir -p cookbooks/basic/files/default
  3. touch cookbooks/basic/recipes/default.rb

Open the default.rb file in a code editor

default.rb

#
# Cookbook Name:: basic
# Recipe:: default
#
# Copyright (c) 2015 The Authors, All Rights Reserved.

execute "apt-get-update" do
  command "apt-get update"
  ignore_failure true
  action :run
end

package 'apache2'

execute "apache-remove-www" do
	command "rm -rf /var/www"
end

execute "apache-link-www" do
	command "ln -fs /vagrant/ /var/www"
end

execute "apache-enable-rewrite" do
	command "a2enmod rewrite"
end

execute "apache-override-all" do
	command "sed -i '/AllowOverride None/c AllowOverride All' /etc/apache2/sites-available/*"
end

In the root of your project

vagrant init

Open Vagrantfile file in a code editor

Vagrantfile

config.vm.box = "hashicorp/precise32"

config.vm.network "forwarded_port", guest: 8888, host: 8990

config.vm.provision "chef_solo" do |chef|
  chef.install = true
  chef.add_recipe "basic"
end

More:

  1. npm init
  2. npm install express --save
  3. touch app.js
  4. Copy hello world example into app.js http://expressjs.com/starter/hello-world.html
  5. Add package nodejs to default.rb
  6. add to default.rb:
execute "update-node-package" do
    command "curl -sL https://deb.nodesource.com/setup | sudo bash -"
end

package 'nodejs'

execute "install-nodemon" do
    command "npm install -g nodemon"
end

execute "update-npm" do
    command "cd /vagrant/ && npm install"
end

execute "start-node-server" do
    command "nodemon /vagrant/app.js"
end

Common Chef

Update Packages

# update package list
execute "apt-get-update" do
  command "apt-get update"
  ignore_failure true
  action :run
end

Install Packages

package 'apache2'

Apache Stuff

execute "apache-remove-www" do
	command "rm -rf /var/www"
end

execute "apache-link-www" do
	command "ln -fs /vagrant/ /var/www"
end

execute "apache-enable-rewrite" do
	command "a2enmod rewrite"
end

execute "apache-override-all" do
	command "sed -i '/AllowOverride None/c AllowOverride All' /etc/apache2/sites-available/*"
end

Include Files

cookbook_file "/usr/share/nginx/www/index.html" do
  source "index.html" #found in cookbooks/basic/files/default
  mode "0644"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment