Chef solo the beginning

1,918 views
1,784 views

Published on

A basic intro to chef solo

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,918
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
30
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Chef solo the beginning

  1. 1. Chef soloThe beginning
  2. 2. BackgroundIt is efficient to automate things thatwe do again and again, even it takessome time
  3. 3. BackgroundServer setup is one of the most tedious andboring task, which we have to do again andagain.Look at your screen to see if the mysqlpassword request has come or not.
  4. 4. Why chef● Idempotent: Safe to re run the script● Thick Clients, Thin Server● A level of platform independencies● Rich collection of recipes (800+)● Integration with Leading Cloud Providers
  5. 5. TerminologyChef: Cooks a recipe for you,they way you wantRecipe: A set of instructions for preparinga particular dishCookbooks: Manages your recipeNode: Configuration of your recipeKnife: A tool to help you cook
  6. 6. Getting startedRoot /-- cookbooks-- node.json-- run_recipies.rbRun the script$ sudo chef-solo -c run_recipies.rb -j node.json
  7. 7. Getting started● cookbooks○ mysql■ recipes● default.rb● another_recipie.rb■ templates● my.cnf■ files
  8. 8. Using existing repoTarget: setup mysql with chef solo● Pull the mysql cookbook in the cookbooksfolder● Change the node.json to serve our need
  9. 9. Using existing repoSample node.json{ "run_list": [ "recipe[mysql::server]","recipe[mysql::client]" ],"mysql": { "server_root_password": "","server_repl_password": "","server_debian_password": "","bind_address": "0.0.0.0","allow_remote_root": true}}
  10. 10. Using existing repoRun the script$ sudo chef-solo -c run_recipies.rb -j node.jsonRE Run the script any number of time$ sudo chef-solo -c run_recipies.rb -j node.jsonAnd it is safe :D
  11. 11. Creating own recipeCreate folder structure as followschef/├── cookbooks| └── mychefrepo| ├── recipes| | └── default.rb| ├── templates| └── files└── solo.rb
  12. 12. default.rb1 begin2 require mysql3 rescue LoadError4 Chef::Log.info("Missing gem mysql")5 end67 ruby_block "Create replication user and database" do8 block do9 m = Mysql.new(localhost, "root", node[:mysql][:server_root_password])1011 command = %Q{12 GRANT ALL ON *.*13 TO #{node[:mysql][:server_replication][:database_user]}@%14 IDENTIFIED BY15 #{node[:mysql][:server_replication][:database_password]};16 }17 m.query(command)18 m.query(FLUSH PRIVILEGES)19 end20 # only execute if mysql is running21 only_if "pgrep mysqld$"22 end
  13. 13. default.rb1 gem_package "bundler"2 begin3 require mysql4 rescue LoadError5 Chef::Log.info("Missing gem mysql")6 end
  14. 14. default.rb7 ruby_block "Create replication user and database" do8 block do9 m = Mysql.new(localhost, "root", node[:mysql][:server_root_password])1011 command = %Q{12 GRANT ALL ON *.*13 TO #{node[:mysql][:server_replication][:database_user]}@%14 IDENTIFIED BY15 #{node[:mysql][:server_replication][:database_password]};16 }17 m.query(command)18 m.query(FLUSH PRIVILEGES)19 end20 # only execute if mysql is running21 only_if "pgrep mysqld$"22 end
  15. 15. node.json{ "run_list": [ "recipe[mysql::server]","recipe[mysql::client]","recipe[mychefrepo]" ],"mysql": { "server_root_password": "","server_repl_password": "","server_debian_password": "","bind_address": "0.0.0.0","allow_remote_root": true,"server_replication": {"database": "moteel_production","database_user": "repl","database_password": "repl"}}}
  16. 16. Run the scriptRun the script$ sudo chef-solo -c run_recipies.rb -j node.json
  17. 17. Commonly used resources%w{rover fido bubbers}.each do |pet_name|execute "feed_pet_#{pet_name}" docommand "echo Feeding: #{pet_name}; touch /tmp/#{pet_name}"not_if { ::File.exists?("/tmp/#{pet_name}")}endendgem_package "bundler" dooptions(:prerelease => true, :format_executable => false)endpackage "tar" doversion "1.16.1-1"action :installend
  18. 18. Commonly used resources%w{rover fido bubbers}.each do |pet_name|execute "feed_pet_#{pet_name}" docommand "echo Feeding: #{pet_name}; touch /tmp/#{pet_name}"not_if { ::File.exists?("/tmp/#{pet_name}")}endendgem_package "bundler" dooptions(:prerelease => true, :format_executable => false)endpackage "tar" doversion "1.16.1-1"action :installend
  19. 19. Commonly used resourcesdirectory "/tmp/folder" doowner "root"group "root"mode 0755action :createendcookbook_file "/usr/local/bin/apache2_module_conf_generate.pl" dosource "apache2_module_conf_generate.pl"mode 0755owner "root"endtemplate "/tmp/somefile" domode 00644source "somefile.erb"not_if { node[:some_value] }end
  20. 20. Deploying to multiple serversProblems● Different servers might need different node.json file to configure them● You have to ssh into different servers andand set up the prerequisites for chef
  21. 21. Deploying to multiple serversSolution1. Use chef server○ Need to buy chef server serviceOR○ Maintain a chef server of your ownOR2. Use scripts like capistrano and some ruby todeploy from development machine
  22. 22. Using Capistranoset :user, ...set :password, ........set :main_server, ***.**.***.**server main_server, :app, :primary => truenamespace :setup dotask :env, :depends => ["setup:install_prerequisite"], :roles => :app dosetup.install_prerequisitecd_run "ruby change_local.rb #{user} #{main_server}"cd_run "#{try_sudo} ./setup_base.sh"cd_run "#{try_sudo} chef-solo -c run_recipies.rb -j local.json"endend
  23. 23. Using Capistranonamespace :setup dotask :install_prerequisite, :roles => :app dorun "mkdir -p /home/#{user}/.ssh"upload "deploy_keys/id_rsa", "/home/#{user}/.ssh/id_rsa"upload "deploy_keys/id_rsa.pub", "/home/#{user}/.ssh/id_rsa.pub"run "#{try_sudo} apt-get update"run "#{try_sudo} apt-get -y install aptitude git unzip"beginrun "cd /home/#{user} && git clone git@git.tasawr.com:ashraf/moteel-setup.git"rescuecd_run "git pull origin master"endrun "#{try_sudo} aptitude install -y ruby1.9.1 ruby1.9.1-dev make"run "#{try_sudo} gem install --no-rdoc --no-ri chef"endenddef cd_run(cmd)run "cd /home/#{user}/moteel-setup && #{cmd}"end
  24. 24. Using Capistranoruby_block "share the torrent file" doblock dof = File.open(node[bittorrent][torrent],rb)#read the .torrent file and base64 encode itenc = Base64.encode64(f.read)data = {id=>bittorrent_item_id(node[bittorrent][file]),seed=>node.ipaddress,torrent=>enc}item = Chef::DataBagItem.newitem.data_bag(bittorrent)item.raw_data = dataitem.saveendaction :nothingsubscribes :create, resources(:bittorrent_torrent => node[bittorrent][torrent])end
  25. 25. Deploying to multiple servers1. Configure capistrano with the serverinformation2. Install prerequisites with capistrano3. Run a ruby or any program to update thenode.json file from the capistranoinformation4. Go get yourself a cup of coffee
  26. 26. Wait for the script to complete
  27. 27. References● Quick start● Chef DSL● Chef resources● Some cookbooks
  28. 28. AuthorA.K.M. AshrafuzzamanLead Software Engineer,Tasawr Interactive.www.ashrafuzzaman.comashraf@tasawr.com

×