Skip to content

Instantly share code, notes, and snippets.

@reoring
Last active December 20, 2015 01:19
Show Gist options
  • Save reoring/6047763 to your computer and use it in GitHub Desktop.
Save reoring/6047763 to your computer and use it in GitHub Desktop.

VirtualBox

4.2.16

Vagrant

http://files.vagrantup.com/packages/0219bb87725aac28a97c0e924c310cc97831fd9d/Vagrant-1.2.4.dmg

Box

MD5 (wheezy.box) = 8611a6e40daf56779134feb783a7f897

curl https://www.dropbox.com/s/ps447hpvfmdjwoy/wheezy.box

vagrant box add wheezy wheezy.box

Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.hostname = "vagrant"
  config.vm.box = "wheezy"

  config.vm.network :private_network, ip: "33.33.33.33"

  config.ssh.max_tries = 40
  config.ssh.timeout   = 120
  config.ssh.forward_agent = true
end

起動

vagrant up

終了

vagrant halt

破棄

vagrant destroy

vagrantの組み込みrubyにpathを通す

echo 'PATH="$PATH:/Applications/Vagrant/embedded/bin"' >> ~/.bashrc
source ~/.bashrc

# rbenvとかRVMとかが入っている場合は、先頭に追加してください。

berkshelのインストール

gem install berkshelf

# パーミッションのエラーがでるときはsudoで

berkshelfプラグインのインストール

vagrant plugin install vagrant-berkshelf

berk cookbook

berks cookbook handson
cd handson

Vagrantfile update

Vagrant.configure("2") do |config|
  config.vm.hostname = "vagrant"
  config.vm.box = "wheezy"

  config.vm.network :private_network, ip: "33.33.33.33"

  config.ssh.max_tries = 40
  config.ssh.timeout   = 120
  config.ssh.forward_agent = true

  config.vm.provision :chef_solo do |chef|
    chef.run_list = [
        "recipe[handson::default]"
    ]
  end
end

Apache2

編集 recipes/default.rb

include_recipe "apache2"

編集 metadata.rb

depends "apache2", "~> 1.6.0"

プロビジョン

vagrant up

or

vagrant provision

表示してみる

http://33.33.33.33/

静的ページを配置

templates/default/apache2.conf.erb

# Managed by Chef for <%= node[:hostname] %>
<VirtualHost *:80>
    ServerAdmin <%= node[:apache][:contact] %>

    DocumentRoot <%= node[:handson][:document_root] %>
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory <%= node[:handson][:document_root] %>>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog <%= node[:apache][:log_dir] %>/error.log

    LogLevel warn

    CustomLog <%= node[:apache][:log_dir] %>/access.log combined
    ServerSignature Off
</VirtualHost>

attributes/default.rb

default[:handson][:config] = "static.conf"
default[:handson][:app]    = "/srv/static"
default[:handson][:document_root] = "/srv/static"

recipes/default.rb

#
# Cookbook Name:: handson
# Recipe:: default
#
# Copyright (C) 2013 YOUR_NAME
# 
# All rights reserved - Do Not Redistribute
#
include_recipe "apache2"

apache_site "000-default" do
	enable false
end

directory "/srv/static" do
    owner "www-data"
    group "www-data"
    action :create
    mode 00700
end

template "#{node[:handson][:document_root]}/index.html" do
    source "index.html.erb"
    owner "www-data"
    group "www-data"
    mode 00766
end
template "#{node[:apache][:dir]}/sites-available/#{node[:handson][:config]}" do
	source "apache2.conf.erb"
	notifies :restart, 'service[apache2]'
end

apache_site node[:handson][:config] do
	enable true
end

templates/default/index.html.erb

hello vagrant

MySQL

Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.hostname = "vagrant"
  config.vm.box = "wheezy"

  config.vm.network :private_network, ip: "33.33.33.33"

  config.ssh.max_tries = 40
  config.ssh.timeout   = 120
  config.ssh.forward_agent = true

  config.berkshelf.enabled = true

  config.vm.provision :chef_solo do |chef|
    chef.json = {
      :mysql => {
        :server_root_password   => 'root',
        :server_debian_password => 'root',
        :server_repl_password   => 'root',
        :bind_address => '0.0.0.0'
      }
    }

    chef.run_list = [
        "recipe[handson::default]"
    ]
  end
end

recipes/default.rb

include_recipe "handson::database"

recipes/database.rb

include_recipe "mysql::server"
include_recipe "database::mysql"

mysql_database "handson" do
  connection(
    :host => "localhost",
    :username => "root",
    :password => node[:mysql][:server_root_password]
  )
  action :create
end

mysql_database_user "handson" do
  connection(
    :host => "localhost",
    :username => "root",
    :password => node[:mysql][:server_root_password]
  )
  password "handson"
  database_name "handson"
  host "localhost"
  action [:create, :grant]
end

metadata.rb

depends "mysql",       "~> 3.0.0"
depends "database",    "~> 1.3.0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment