什么是 Capistrano?

Capistrano 是一款远程服务器自动化工具。

它支持脚本编写和执行任意任务,并包含一组合理的默认部署工作流程。

Capistrano 可用于

  • 可靠地将 Web 应用程序同时部署到任意数量的机器,按顺序或作为滚动集
  • 自动化任意数量机器的审计(检查登录日志、枚举正常运行时间和/或应用安全补丁)
  • 通过 SSH 编写任意工作流程脚本
  • 自动化软件团队中的常见任务。
  • 驱动基础设施配置工具,如 chef-soloAnsible 或类似工具。

Capistrano 也是非常可脚本化的,可以与任何其他 Ruby 软件集成,成为更大工具的一部分。

它看起来像什么?

Capistrano 3.5 / Airbrussh formatter screenshot

盒子里还有什么?

Capistrano 工具箱里有很多很酷的东西

  • 可互换的输出格式化程序(进度、漂亮、html 等)
  • 轻松添加对其他源代码管理软件的支持。
  • 一个基本的用于交互式运行 Capistrano 的多控制台。
  • 主机和角色过滤器,用于部分部署或部分集群维护。
  • 用于 Rails 资产管道和数据库迁移的食谱。
  • 支持复杂的环境。
  • 一个理智且富有表现力的 API
desc "Show off the API"
task :ditty do

  on roles(:all) do |host|
    # Capture output from the remote host, and re-use it
    # we can reflect on the `host` object passed to the block
    # and use the `info` logger method to benefit from the
    # output formatter that is selected.
    uptime = capture('uptime')
    if host.roles.include?(:web)
      info "Your webserver #{host} has uptime: #{uptime}"
    end
  end

  on roles(:app) do
    # We can set environmental variables for the duration of a block
    # and move the process into a directory, executing arbitrary tasks
    # such as letting Rails do some heavy lifting.
    with({:rails_env => :production}) do
      within('/var/www/my/rails/app') do
        execute :rails, :runner, 'MyModel.something'
      end
    end
  end

  on roles(:db) do
    # We can even switch users, provided we have support on the remote
    # server for switching to that user without being prompted for a
    # passphrase.
    as 'postgres' do
      widgets = capture "echo 'SELECT * FROM widgets;' | psql my_database"
      if widgets.to_i < 50
        warn "There are fewer than 50 widgets in the database on #{host}!"
      end
    end
  end

  on roles(:all) do
    # We can even use `test` the way the Unix gods intended
    if test("[ -d /some/directory ]")
      info "Phew, it's ok, the directory exists!"
    end
  end
end
Fork me on GitHub