How to do rbenv Ruby upgrades?
This is not really a bug, but more of a convention that we should figure out for posterity.
I just ran into this problem with Barkeep. The issue occurs when you have a setup script that looks like this:
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), "terraform_dsl"))
include Terraform::DSL
# ...
ensure_rbenv_ruby("some-version")
Then you'll have something like this in your deploy script:
run "cd #{deploy_path} && ./setup.rb"
This works fine when you're running the setup script for the first time, but if you ran it before and now you've updated the .rbenv-version file, you'll have an error. This is because when you go to that directory and run a ruby script, rbenv will notice that you don't have the new version of Ruby and fail.
My workaround in Barkeep (hacky, but very quick) was to do this in the deploy script instead:
run "mv #{deploy_path}/.rbenv-version #{deploy_path}/.rbenv_version.bak"
run "cd #{deploy_path} && ./setup.rb"
run "mv #{deploy_path}/.rbenv-version.bak #{deploy_path}/.rbenv_version"
Other things I thought of:
- If we're using rbenv, set a version with
rbenv shellbefore we cd to the directory - Don't add rbenv into the
.bashrc, instead manually adding the shims/bin directories to our path in the run scripts (not ideal for debugging) - Find a system ruby by inspecting
$PATHand then run$RUBY ./setup.rbto get rbenv out of the picture while we're running the setup
Any better ideas?
Scratch that, my workaround doesn't work. Later in the same setup script, it tries to install gems. If the script was run with the wrong Ruby, then gem will be wrong.
My current workaround is to check, in the deploy script, whether rbenv in installed and the Ruby version is not installed. In this case, I manually install the new Ruby version before running the deploy script.
See 5ba607 in the Barkeep repo to see exactly what I did.
Obviously, this is an undesirable hack.
We need to figure out a way to reconcile the fact that (a) we run the setup script using any old Ruby that happens to be on the box with (b) some steps in the setup script (notably installing gems) rely on the correct Ruby being used.