A framework for gradual system automation
See our blog post for the philosophy behind Runbook and an overview of its features.
Runbook provides a DSL for specifying a series of steps to execute an operation. Once your runbook is specified, you can use it to generate a formatted representation of the book or to execute the runbook interactively. For example, you can export your runbook to markdown or use the same runbook to execute commands on remote servers.
Runbook has two modes for evaluating your runbook. The first mode, view mode, allows you to export your runbook into various formats such as markdown. The second mode, run mode, allows you to execute behavior based on the statements in your runbook.
Runbook can be integrated into existing infrastructure in many different ways. It can be integrated into existing projects to add orchestration functionality, installed on systems as a stand-alone executable, or runbooks can be defined as self-executable scripts. In addition to being useful for automating common tasks, runbooks are a perfect bridge for providing operations teams with step-by-step instructions to handle common issues (especially when solutions cannot be easily automated).
Lastly, Runbook provides an extendable interface for augmenting the DSL and defining your own behavior.
Though Runbook can solve a myriad of problems, it is best used for removing the need for repeated, rote developer operations. Runbook allows developers to execute processes at a higher level than that of individual command-line commands. Additionally, Runbook provides features to simply and safely execute operations in mission-critical environments.
Runbook is not intended to replace more special-purpose automation solutions such as configuration management solutions (Puppet, Chef, Ansible, Salt), deployment solutions (Capistrano, Kubernetes, Docker Swarm), monitoring solutions (Nagios, Datadog), or local command execution (shell scripts, Rake tasks, Make). Instead Runbook is best used as a glue when needing to accomplish a task that cuts across these domains.
Add this line to your application's Gemfile:
gem 'runbook'And then execute:
$ bundleOr install it yourself as:
$ gem install runbookGenerate a runbook using the Runbook Generator:
$ runbook generate runbook my_first_runbookExecute the runbook:
$ runbook exec my_first_runbook.rbWhen setting up Runbook, you can install it at a system level, create a dedicated runbook project, or incorporate Runbook into an existing project.
Install runbook at a system level using gem:
$ gem install runbookSet any Runbook configuration in /etc/runbook.conf.
Generate runbook files using runbook generate runbook. Execute runbook generate help runbook for more details.
Installing Runbook at a system level can be useful for executing runbooks on remote hosts or within docker containers. One disadvantage of installing Runbook at a system level is that there is no built-in solution for dependency management.
Install runbook using gem:
$ gem install runbookGenerate a new runbook project:
$ runbook generate project This will generate a new runbook project. Cd into your project directory and initialize its dependencies:
$ cd && bin/setupAdd this line to your project's Gemfile:
gem 'runbook'Install the Runbook gem:
$ bundle installInitialize Runbook in your project:
$ bundle exec runbook initBelow is an example of a runbook:
Runbook.book "Restart Nginx" do
description 25
`echo 'Help! failure rate at #{failure_rate}' | mail -s 'High failure rate!' [email protected]`
else
`echo "Experienced failure rate of #{failure_rate}" | mail -s 'Help me eventually' not-urgent@my_site.com`
end
notice "Email sent!"
endMetadata at execution time is structured as follows:
…Additional methods that the ruby_command block has access to are:
metadata[:toolbox].prompt: A TTY::Prompt for retrieving input from the usermetadata[:toolbox].ask(msg): retrieve user inputmetadata[:toolbox].yes?(msg): provide the user with a yes/no promptmetadata[:toolbox].output(msg): output text to the usermetadata[:toolbox].warn(msg): output warning text to the usermetadata[:toolbox].error(msg): output error text to the usermetadata[:toolbox].exit(return_value): exit the process with the specified response codeRuns the provided cmd in the specified pane.
tmux_command "tail -Fn 100 /var/log/nginx.log", :monitor_1Uploads the specified file to to. An optional ssh_config can be specified to configure how the upload command gets run, for example specifying the remote host and remote directory to upload to. Optional options can be specified that get passed down to the underlying sshkit implementation
upload my_secrets.yml, to: secrets.yml, ssh_config: {servers: ["host1.prod"]}, options: {log_percent: 10}Sleeps for the specified amount of time (in seconds)
wait 5Runbook provides native support for defining tmux layouts and executing commands in separate tmux panes. Layouts are specified by passing an array or hash to the layout statement in book blocks.
Runbook.book "My Book" do
layout [
:left,
{name: :middle, directory: "/var/log", command: "tail -Fn 100 auth.log"},
[:top_right, {name: :bottom_right, runbook_pane: true}]
]
endWhen layout is passed as an array, each element of the array represents a pane stacked side-by-side with the other elements. Elements of the array can be symbols, hashes, or arrays.
Symbols and hashes represent panes. Hash keys for a pane include name, directory, command, and runbook_pane. name is the identifier used for the pane. This is used when specifying what pane you want to execute tmux commands in. directory indicates the starting directory of the pane. command is the initial command to execute in the pane when it is created. runbook_pane indicates which pane in the layout should hold the executing runbook. Only one pane should be designated as the runbook_pane and the runbook pane should not have a directory or command specified.
Arrays nested underneath the initial array split the pane from top to bottom. Arrays nested under these arrays split the pane from side to side, ad infinitum. You can start spliting panes from top to bottom as opposed to side-by-side by immediately nesting an array.
Runbook.book "Stacked Layout" do
layout [[
:top,
:middle,
:bottom,
]]
endWhen a hash is passed to layout, the keys of the hash represent window names and the values represent pane layouts.
Runbook.book "Multi Window Layout" do
layout({
:web_monitor => [
:left, :middle, :right,
],
:db_monitor => [[
:top, :middle, :bottom,
]]
})
endNotice in the example that parenthesis are used to wrap the hash. Ruby will raise a syntax error if layout's argument is not wrapped in parenthesis when passing a hash. Runbook expects that it is running in the last window in a tmux session. If you are running a runbook that uses a multi-window layout, the layout will not work unless runbook is running in the last window in the session.
If you want panes to be un-evenly spaced, you can replace the array of panes with a hash where the keys are panes and the values are numbers. The panes will be spaced according to the specified numbers.
Runbook.book "Uneven Layout" do
layout [[
{:left => 20, {name: :middle, runbook_pane: true} => 60No open issues yet, or sync has not completed.