capistrano - davidegrayson.com · “capistrano is a utility and framework for executing commands...
TRANSCRIPT
![Page 1: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/1.jpg)
Capistrano
David GraysonLas Vegas Ruby Meetup
2012-11-7
![Page 2: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/2.jpg)
“Capistrano is a utility and framework for executing commands
in parallel on multiple remote machines, via SSH.”
server1
server2
server3
server4
Local machine,running capistrano
ssh
ssh
ssh
ssh
![Page 3: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/3.jpg)
This talk will be about:
1) Capistrano in general2) Deploying Rails
![Page 4: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/4.jpg)
Before Capistrano
● Logging in to multiple servers :(● Run commands with ssh utility.
![Page 5: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/5.jpg)
Simple Capfile #1: tasks
task "uptime" do run "uptime"end
![Page 6: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/6.jpg)
Simple Capfile #2: hosts
task "uptime", :hosts => "strontium" do run "uptime"end
![Page 7: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/7.jpg)
Simple Capfile #3: servers and roles
server "strontium", :app
task "uptime" do run "uptime"end
![Page 8: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/8.jpg)
Simple Capfile #4: desc
server "strontium", :app
desc "Runs the uptime commmand"task "uptime" do run "uptime"end
![Page 9: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/9.jpg)
“invoke” task
● Invokes a command on remote servers
![Page 10: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/10.jpg)
Compositiontask "uptime" do run "uptime"end
task "disk_usage" do run %{df | awk '$6=="/"{print $5}'}end
task "status" do uptime disk_usageend
![Page 11: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/11.jpg)
![Page 12: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/12.jpg)
Before and after hooks
before 'status', 'uptime'after 'status', 'disk_usage'
![Page 13: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/13.jpg)
server "strontium", :app
namespace "status" do task "default" do uptime disk_usage end
task "uptime" do run "uptime" end task "disk_usage" do run %{df | awk '$6=="/"{print $5}'} end end
Namespaces
status:uptime
status:disk_usage
status
![Page 14: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/14.jpg)
File transfer
download "/etc/crontab", "$CAPISTRANO:HOST$.crontab"
● upload● download● put● get
![Page 15: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/15.jpg)
capture
● Important: only runs on ONE server!
version = capture("cat current/REVISION")
![Page 16: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/16.jpg)
stream
stream "tail -f shared/log/production.log"
![Page 17: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/17.jpg)
run_locally
run_locally "git push"
![Page 18: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/18.jpg)
Variables
● Normal:
● Deferred:
set(:root_password) do Capistrano::CLI.password_prompt("Root password: ")end
task :backup_database do run "bkup --user=root --password=#{root_password}"end
set(:root_password, "f00")
![Page 19: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/19.jpg)
Variables (continued)
● set● unset● exist?● fetch
![Page 20: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/20.jpg)
Special Significant Variables
● https://github.com/capistrano/capistrano/wiki/2.x-Significant-Configuration-Variables
● default_environment● shared_children
![Page 21: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/21.jpg)
Errors● Based on command return code
task "foo" do run "ls missing_file"end
![Page 22: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/22.jpg)
Transactions
task "update" do transaction do copy start endend
task "copy" do on_rollback { run "rm -fv b d" } run "cp a b" run "cp c d"end
![Page 23: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/23.jpg)
![Page 24: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/24.jpg)
Deploying Rails
● Online tutorials● Check out
https://github.com/capistrano/capistrano/blob/master/lib/capistrano/recipes/deploy.rb
● For assets, check out:https://github.com/capistrano/capistrano/blob/master/lib/capistrano/recipes/deploy/assets.rb
● WARNING: deploy:rollback doesn't roll back assets.
![Page 25: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/25.jpg)
![Page 26: Capistrano - davidegrayson.com · “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.” server1 server2 server3 server4](https://reader034.vdocuments.mx/reader034/viewer/2022042407/5f229c9ee99f33678c22c1e5/html5/thumbnails/26.jpg)
The End