On Github javivelasco / vagrant-chef-presentation
var speaker = { name: 'Javi Velasco', twitter: '@javivelasco', worksAt: 'imixme.com', github: 'javivelasco', blog: 'blog.javivelas.co' }
Former front-end developer turned to fullstack. I pleasently worked with Floqq, Mindster and now @ Imixme
A software that provides a portable and reproducible environment using virtual machines. Automation tools are used for provisioning. All is set up in your project repository
$ vagrant init imixme/precise32 $ vagrant up $ vagrant ssh
VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "http://domain.com/path/to/above.box" config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.network "private_network", ip: "33.33.33.10" config.ssh.forward_agent = true config.vm.synced_folder "apps", "/apps" end
But here there is no configuration for software right?... Yep, but we can use shell scripts to run on vagrant up
You can always use scripts to provision your vagrant machine but if you have a complex stack or configuration... it's kind of messy.
# provision inline config.vm.provision "shell", inline: "ls -la /vagrant" # provision by script config.vm.provision "shell", path: "script.sh"
Solution: use an IT automation tool
Without provisioners Vagrant wouldn't be the same. Constructing boxes and distribute them is as heavy as classic VM images. Vagrant supports, along with Shell scripts:
Let's give a try to Vagrant command line and explore some options
Chef is a Ruby framework for automating, reusing and documenting server configuration. Two versions:
Chef Server & Client
Chef Solo
How can we install and run nginx
Chef defines some diretories to structure your cookbooks.
├── main │ └── recipes │ └── default.rb └── nginx ├── attributes │ └── nginx.rb ├── recipes │ └── default.rb └── templates └── default └── nginx.conf.erb
The code below installs nginx with the package built-in in the system, defines nginx as a service and sets a template to configure it.
# nginx/recipes/default.rb package "nginx" service "nginx" do supports :status => true, :restart => true, :reload => true action [:enable, :start] end template "/etc/nginx/nginx.conf" do source "nginx.conf.erb" notifies :reload, "service[nginx]" end
It's not mandatory but always recommended. Useful for specific setup, for example, defining your specific recipe dependencies and update your package manager.
# update your package manager execute "apt-get update" do command "apt-get update" end # dependencies for the proyect include_recipe "nginx"
$ gem install chef $ echo cookbooks_path "/cookbooks" > /etc/chef/solo.rb $ echo {"run-list": ["recipe[nginx]"]} > /etc/chef/node.json $ chef-solo -j /etc/chef/node.json
... and in the VagrantFile ...
# VagrantFile config.vm.provision :chef_solo do |chef| chef.add_recipe "default" end
and other complex examples?
Yep, you can get similar results with other tools, for example:
Docker is an open-source project to easily create lightweight, portable, self-sufficient containers from any application. The same container that a developer builds and tests on a laptop can run at scale, in production, on VMs, bare metal, OpenStack clusters, public clouds and more.
Any questions?