In previous post I have wrote how to use Chef Solo to setup and deploy a Server with PHP stuff. Today I’d like to use Chef Server for storing Chef’s configurations (Cookbooks, Roles, Enviroments, Data Bags) and Bootstrap a server use Chef Server.
Setup Chef Server
The easiest way to get started with a Chef server is to use Opscode’s Hosted Chef–it’s free for up to 5 nodes. Once you exceed 5 nodes you can decide whether to pay a monthly fee or install your own Chef server.
https://github.com/code-for-food/php-cooking.git # fork from github
Move to php-cooking and copy client key and validation key to .chef folder.
Replace the knife.rb in .chef folder
123456789101112131415161718
current_dir=File.dirname(__FILE__)log_level:infolog_locationSTDOUTnode_name"codeforfoods"client_key"#{current_dir}/codeforfoods.pem"# replace by your keyvalidation_client_name"chicken-validator"validation_key"#{current_dir}/chicken-validator.pem"# replace by your keychef_server_url"https://api.opscode.com/organizations/chicken"cache_type'BasicFile'cache_options(:path=>"#{ENV['HOME']}/.chef/checksums")cookbook_path["cookbooks","site-cookbooks"]node_path"nodes"role_path"roles"environment_path"environments"data_bag_path"data_bags"encrypted_data_bag_secret"#{current_dir}/encrypted_data_bag_secret"
Upload cookbooks to Chef server
knife cookbook upload --all
View on Chef server
Upload Roles to Chef Server
knife role from file roles/myapp.json
knife role from file roles/myapp_deploy.json
View on Server
Create new enviroment called staging by create staging.json file in enviroments folder
# -*- mode: ruby -*-# vi: set ft=ruby :# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!VAGRANTFILE_API_VERSION="2"CHEF_PATH="."SYNC_PATH="./www"Vagrant.configure(VAGRANTFILE_API_VERSION)do|config|config.vm.box="ubuntu14.04"config.vm.box_url="https://oss-binaries.phusionpassenger.com/vagrant/boxes/latest/ubuntu-14.04-amd64-vbox.box"config.vm.network"private_network",ip:"192.168.34.100"config.vm.hostname="devphp.congdang.com"config.ssh.forward_agent=trueconfig.ssh.forward_x11=trueconfig.vm.provider"virtualbox"do|vb|vb.customize(["modifyvm",:id,"--natdnshostresolver1","off"])vb.customize(["modifyvm",:id,"--natdnsproxy1","off"])vb.customize(["modifyvm",:id,"--memory","1024"])endconfig.omnibus.chef_version='11.16.0'# Your organization name for hosted Chef orgname="chicken"# Set the Chef node ID based on environment variable NODE, if set. Otherwise default to vagrant-$USERnode=ENV['NODE']node||="vagrant-codeforfoods"config.vm.provision:chef_clientdo|chef|chef.chef_server_url="https://api.opscode.com/organizations/#{orgname}"chef.validation_key_path="#{CHEF_PATH}/.chef/#{orgname}-validator.pem"chef.validation_client_name="#{orgname}-validator"chef.encrypted_data_bag_secret_key_path="#{CHEF_PATH}/.chef/encrypted_data_bag_secret"chef.node_name="#{node}"chef.provisioning_path="/etc/chef"chef.log_level=:debug#chef.log_level = :infochef.environment="dev"chef.add_role("myapp")#chef.json.merge!({ :mysql_password => "foo" }) # You can do this to override any default attributes for this node.endconfig.vm.synced_folder("#{SYNC_PATH}","/vagrant",:owner=>"vagrant",:group=>"vagrant",:mount_options=>['dmode=777','fmode=777'])end