-
Getting to know Ruby debugger
This is a guest-post by Cameron Dykes, about getting started with the ruby debugger.
A key step to debugging any program is replicating the environment to ensure you can consistently produce the bug. In my early Ruby days, to inspect the environment, I used a primitive method: placing
puts
lines in my code to print values to the console (let’s call them “inspection puts”).It may have looked something like this:
The case for ruby-debug
This method very easily gives me the information I need, but it has some downsides:
- To inspect the return value of
special_options
I have to add another inspection puts. - Every addition of a new
puts
requires that I restart the application to inspect the results. - To inspect how
special_options
andperform_operation
are handling the data, I have to add inspection puts inside of them. - I must remember to remove all of the inspection puts before I push the code.
If only there was a better way to do this.
ruby-debug to the rescue! By putting a breakpoint in our code, we have the ability to inspect environment state, check the return value of any methods, and step through our code one line at a time. This is much more versatile than inspection puts because the full environment is available to us. The “I wonder what the value of this is” problem is gone since we can inspect whatever we want. The step functionality the debugger gives us is useful as well, allowing us to step inside of a called method while maintaining the interactive environment.
Setting up ruby-debug
To get set up with the debugger, we’ll need to install the gem:
ruby-debug in action
Let’s update the example from above using a debugger breakpoint instead of inspection puts:
The next time the method is called, the debugger will stop and give us an interactive shell. We can inspect the values of each variable with the
eval
command:We can also see what the return value is of the invoked methods:
We can even use the step command to enter inside of
special_options
andperform_operation
to see what they do.Here are the various debugger commands that I most commonly use:
list, l
- show the code for the current breakpointeval, e
- evaluate expression and print the valuestep, s
- next line of code, moving within methodscontinue, c
- continue in the program until the program ends or reaches another breakpointquit, q
- abort the program
Many more commands are available, which can be seen by entering
help
in the debugger.Better debugging ftw!
With the ruby-debug gem, we have a better tool for diving in to our code than inspection puts. Using a debugger breakpoint, we can interactively step through our code with less cleanup.
Happy debugging!
Debugging references
- Debugging with ruby-debug in the Debugging Rails Applications guide
- ruby-debug19 gem
- Example code from this post
Who am I?
- To inspect the return value of
-
Deploy a private Github repository with whiskey_disk
whiskey_disk
makes it very easy to quickly deploy a new version of your site to one or more servers. It’s very efficient, as files are copied directly from your git repository to the server. This means you can very quickly make large deployments even when you are on a slow internet connection. You do need a secure way for your server to access the git repository while the deploy is going on, read on to see how this is easily done.For this example, I’m going to deploy a small static site from a private Github repository to a server via ssh using
whiskey_disk
. There are a few steps in getting this all setup, but once you have the ssh infrastructure playing nicely, a complete deployment of a small static site takes on the order of 2-5 seconds. Stay with me, and I’ll show how all the pieces fit together.To begin with, I’ll assume you have a site or application already pushed to a private Github reposity that you now want to do a deploy of. You also have ssh access to the server you are deploying to for this to work. For this example, I’m deploying the
master
branch of the following private git repository:git@github.com:rud/whiskey_disk-demo.git
Adding the configuration file
Make sure you have your appropriate code pushed to Github, then we’ll add the whiskey_disk config file
config/deploy.yml
to the project:This is a simple YAML file (indentation matters), you will need to adjust the domain and paths as appropriate. I use the name
demo
for the environment, other common examples would bestaging
,production
, etc. A lot more options are available, see the official whiskey_disk readme, but this all we need to get started.Running whiskey_disk setup
whiskey_disk has two commands: a
setup
command, for doing the initial repository clone, and thedeploy
command used for all subsequent deploys.With the
config/deploy.yml
file in place inside the project, we’re going to just try and run thesetup
command from the local computer, seeing what happens. For now, this errors out:Okay, we’re not quite ready to do deployments just yet.
Resolving all ssh issues
Github hangs up on us - what’s happening? Testing from my local machine as described in the github ssh guide:
That’s the kind of success we’re looking for on the server as well. Ssh to the server as the deploy user, and try the same command there:
Try the command again, now that the host key is trusted:
Notice how we get a new error later in the connection process, a
Permission denied (publickey)
instead ofHost key verification failed
.For the sake of completeness, this is what this situation looks like from whiskey_disk:
ssh-agent to the rescue
The root cause for this difference is Github knows my public key and has access to the corresponding private key, but this key is not available to my server, so it cannot authenticate against Github. The solution is fortunately simple. Do this on your local machine:
This prompts you for the password for your private key, then keeps the private key unlocked in memory for automated access. You need to do this after each time you restart before you can do new deployments, as the unlocked key is only stored in memory. We also need to enable ssh-agent support for the connection to the server - as ssh-agent gives a server the possibility to use our private key without further prompting, you need to trust the administrator of servers you deploy to. With that caveat out of the way, open your
~/.ssh/config
file, and add a config block like this:Adjust the hostname to taste, obviously. You can also add port-number here, if your admin moved ssh access from the default port. While you have that file open, also check out these extremely handy ssh productivity tips for a lot of extra ssh goodness.
Close the previous connection to your server, open a new one (so ssh-agent can do its magic), then re-run the test:
We have ssh-win!
Deploying
Time to re-try the deployment
setup
from the local machine:This means the repository was successfully cloned into place. We only have to do this once, from now on deployments are just a matter of:
I’m seeing a total time taken for a deployment in the area of 2-5 seconds. Remember to push to Github before running your deployment, as it is from Github your server is pulling versions.
This method for deployment is extremely awesome, and the amount of awesome grows with the size of your deployments, as server-server connections are most likely a lot faster than your own internet connection.
Next steps
There is a lot more to whiskey_disk that I’ve shown here. In particular I’d like to highlight the “Configuration Repository” section of the official whiskey_disk readme. Editing per-application configuration files on individual servers is effectively a thing of the past.
subscribe via RSS