## devops.rb
require "sysadmin/1.0"
require "sysadmin/2.0/virtualization"
require "sysadmin/2.0/automation"
Vagrant usa VirtualBox come provider multipiattaforma:
$ sudo apt-get install virtualbox
NOTA: virtualbox e kvm non vanno molto daccordo ;-)
Su Linux è possibile far uso di un provider non ufficiale basato su LXC:
$ sudo apt-get install lxc
...
$ vagrant plugin install vagrant-lxc
...
Vagrant.configure("2") do |config|
config.vm.define :raring64 do |raring_config|
raring_config.vm.box = 'raring64'
raring_config.vm.hostname = "lab-raring64"
raring_config.vm.provider 'virtualbox' do |vm|
vm.box_url = "http://BOXURL.box"
vm.customize ['modifyvm', :id, '--memory', '512']
# vm.gui = true
end
end
end
Vagrant.configure("2") do |config|
config.vm.define :raring64 do |raring_config|
raring_config.vm.box = 'raring64'
raring_config.vm.hostname = "lab-raring64"
raring_config.vm.provider 'lxc' do |vm|
vm.box_url = "http://BOXURL.box"
vm.customize 'cgroup.memory.limit_in_bytes', '512M'
end
end
end
$ vagrant box add raring64 VBOX_BOX_URL
... # add base raring64 box (virtualbox)
$ vagrant box add raring64 LXC_BOX_URL
... # add base raring64 box (lxc)
$ vagrant up raring64 [--provider=lxc]
... # create vm, run provision
$ vagrant ssh raring64
... # connect to the defined vm
Vagrant.configure("2") do |config|
config.vm.define :raring64 do |raring_config|
...
end
config.vm.provision :shell, :path => "./prepare.sh"
end
$ vagrant provision raring64
... # rerun defined provisioners
$ vagrant reload raring64
... # restart vm and rerun defined provisioners
which chef-solo || (sudo true && \
curl -L https://www.opscode.com/chef/install.sh | \
sudo bash)
which chef-solo || (sudo true && \
curl -L https://www.opscode.com/chef/install.sh | \
sudo bash)
which chef-solo || (sudo true && \
curl -L https://www.opscode.com/chef/install.sh | \
sudo bash)
name "my_project"
maintainer "Luca Greco"
maintainer_email "luca.greco@alcacoop.it"
license "GPLv3"
description "my project cookbook"
long_description "..."
version "0.0.1"
http://docs.opscode.com/chef/resources.html
package, script, template, file, cookbook_file, template_file, remote_file, directory, scm, git, subversion, service, mount, cron, ...
# Cookbook Name:: my_project
# Recipe:: www_dependencies
package 'ruby'
package 'rubygems'
gem_package 'bundler'
# Cookbook Name:: my_project
# Recipe:: www_deploy
bash 'my_project_bundle_install' do
user 'root'
pwd '/srv/app_name'
code <<-EOC
bundle install
EOC
action :nothing
end
...
...
git "/srv/app_name" do
repository node[:app_name][:git_repository]
reference node[:app_name][:git_revision]
action :sync
notifies :run, "bash[my_project_bundle_install]"
end
...
...
cookbook_file "/etc/myapp-monitor/notifications.yml" do
source "notifications.yml"
end
...
...
template_file "/srv/app_name/config/database.yml" do
source "database.yml.erb"
variables(
:db_address => node[:app_name][:db_address]
)
end
...
production:
adapter: mysql
database: my_project
host: <%= @db_address %>
username: user
password: ****
pool: 5
timeout: 5000
default[:app_name][:db_address] = '127.0.0.1'
default[:app_name][:git_repository] = '...'
default[:app_name][:git_revision] = '...'
...
config.vm.provision :chef_solo do |chef|
chef.provisioning_path = '/etc/vagrant-chef'
# chef.log_level = :info
chef.run_list = %w[
my_project::www
my_project::db
my_project::email
]
...
...
chef.json = {
app_name: {
git_repository: '...',
git_revision: '...'
}
}
...
name "my_project"
maintainer "Luca Greco"
maintainer_email "luca.greco@alcacoop.it"
license "GPLv3"
description "my project cookbook"
long_description "..."
version "0.0.1"
depends 'apache2', "= 1.6.2"
site :opscode
### dependencies auto-recognized from metadata and
### downloaded by opscode community cookbooks site.
metadata
### or from a path / git repository:
# cookbook "apache2",
# :path => '../my_temporary_apache2_fork'
# cookbook "apache2",
# :git => "GIT REPO URL",
# :branch => GIT_BRANCH_NAME"
$ vagrant plugin install vagrant-berkshelf
...
$ vagrant up --provider=lxc
[sudo] password for rpl:
Bringing machine 'raring64' up with 'lxc' provider...
[raring64] Importing base box 'raring64'...
[Berkshelf] Updating Vagrant's berkshelf: '~/.berkshelf...
[Berkshelf] Using minitest-handler (0.2.0)
[Berkshelf] Using my_project (0.0.1) at path: '/my_project'
[Berkshelf] Using chef_handler (1.1.4)
[Berkshelf] Using openssh (1.1.4)
...
...
[raring64] Setting up mount entries for shared folders...
[raring64] -- /vagrant
[raring64] -- /etc/vagrant-chef/chef-solo-1/cookbooks
[raring64] Starting container...
[raring64] Waiting for container to start. This should not...
...