Automate a cross-environment db restore in Heroku - api

I have two heroku environments and I'd like to copy the latest backup of the Production database to my Development environment.
With the CLI, this is easy:
heroku pgbackups:restore DATABASE `heroku pgbackups:url --app Production` --app Development --confirm Development
But I'd like to automate this. So I made it a rake task that uses the heroku_api gem. This should allow to call it from the Heroku Scheduler.
task :auto_refresh do
#heroku = Heroku::API.new(...)
#heroku.post_ps('Development', 'heroku pgbackups:restore DATABASE `heroku pgbackups:url --app Production` --app Development --confirm Development')
end
This does not work. All it does is refresh Development from the latest Development backup.
Any ideas how I can automate this?

Using the 'heroku' gem (not 'heroku-api' gem), you can manipulate the list of backups from one app to another
When you install the pgbackups addon, it creates a config variable PGBACKUPS_URL. You'll need to use this value in your Dev app like so:
# Get the latest backup
client = Heroku::Client::Pgbackups.new('https://bigmessything#pgbackups.heroku.com/client')
backup = client.get_latest_backup
backup['public_url] should contain the URL you want to restore from using the code you have above

Related

Uable to run migration on heroku on a postgre database

I need to add a property/column to a table in a production database(Postgre) on Heroku.com (Rails app) by doing migration.
When I do the migration it looks ok, but when I view the columns on the table it has has not added the column!
My development db is sqlite3 and the production db is postgre
I do the following:
heroku run rails generate migration AddUtc_OffsetToEvents utc_offset:integer RAILS_ENV=production --app app-name-1111
and it returns:
invoke active_record
create db/migrate/20130304070946_add_utc_offset_to_events.rb
And then I run the migration
heroku run rake db:migrate RAILS_ENV=production --app app-name-1111
And then:
heroku restart
When I run
heroku pg:psql HEROKU_POSTGRESQL_GRAY_URL --app app-name-1111
and check the columns of the table:
\d+ events
It still does not have the utc_offset column, and no errors are displyed while doing the previous cmds.
Any ideas or hints?
It looks like you are doing several calls to heroku run
Each time you do heroku run it spins up a completely new dyno with your latest code, and when run is over that dyno is destroyed. So the second heroku run does not have the migration filed created in the first.
Since you are already familiar with psql you can just use ALTER TABLE directly. Otherwise you'll need to check your migration into your code and git push heroku master it to heroku, then run it.
Why not just download the code, add the migration and push the changes ?
After, just run the heroku run rake db:migrate on the app.

How can I run oink in heroku?

I'm having a problem running the oink gem on my app in Heroku. I've included it in my gemfile and gemfile.lock, uploaded those, and it installs. It even creates the oink.log (which I have no way of viewing, unfortunately). When I run
heroku run bundle exec oink --threshold=0 log/* --app my_app
I get
Running bundle exec oink --threshold=0
log/delayed_job.log log/development.log log/oink.log log/production.log log/test.log attached to terminal... up, run.3
/app/.bundle/gems/ruby/1.8/gems/oink-0.9.3/bin/../lib/oink/cli.rb:88:in get_file_listing':
Could not find "log/delayed_job.log" (RuntimeError)
from /app/.bundle/gems/ruby/1.8/gems/oink-0.9.3/bin/../lib/oink/cli.rb:86:ineach'
from /app/.bundle/gems/ruby/1.8/gems/oink-0.9.3/bin/../lib/oink/cli.rb:86:in get_file_listing'
from /app/.bundle/gems/ruby/1.8/gems/oink-0.9.3/bin/../lib/oink/cli.rb:59:inprocess'
from /app/.bundle/gems/ruby/1.8/gems/oink-0.9.3/bin/oink:4
from /app/.bundle/gems/ruby/1.8/bin/oink:19:in 'load'
from /app/.bundle/gems/ruby/1.8/bin/oink:19
I've tried running each of the individual files, too, and get the same result. This command runs fine on my local machine.
In my production.rb file, I have
config.logger = Hodel3000CompliantLogger.new(config.paths.log.first)
config.middleware.use( Oink::Middleware )
as configuration.
Can you enlighten me on what I'm doing wrong here? My understanding is that the logs are read only, but I don't know if that means they're only accessible through the heroku logs command. If there's a way I can see the oink.log file, too--knowing how to do that is also appreciated, or knowing how to see it in the actual Heroku log using heroku logs.
UPDATE: The configuration for oink shown above allows the commands to be run successfully on my localhost.
Thanks!
-Andrew

How to create an RVM environment on a server through net-ssh?

I have a script which is installing Ruby/RVM (and more) on my server to get an environment ready to deploy a Rails project. This script is in ruby and use the net-ssh lib to do the job.
After having installed RVM, Ruby, I would like to create the project Gemset:
connection do |conn|
logger("Create RVM environment #{ruby_version}##{project_name}")
conn.exec!("#{rvmsudo_path} #{rvm_path} gemset create #{project_name}")
end
I get my gemset created under, all good:
/usr/local/rvm/gems/ruby-1.9.3-p286#my_project_name
Here is my Capistrano setting for rvm/ruby:
set :rvm_type, :system
set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"")
When I'm trying to deploy using capistrano, I get:
/usr/local/rvm/environments/ruby-1.9.3-p286#my_project_name: Permission denied
The environment file 'ruby-1.9.3-p286#my_project_name' is actually missing in that folder. I need to log into the server and navigate to my project so the .rvmrc file to trigger the creation of the environment (rvm --create my_project_name). I would like to avoid this last step. Do you know how to trigger the creation of this environment? (I though it would create it when I have created the Gemset)
RVM has support for installing itself via capistrano, to install rubies and to create gemsets, you should make use of it:
https://rvm.io/integration/capistrano/#install_rvm
https://rvm.io/integration/capistrano/#install_ruby
Basically the RVM environment need to be loaded to be able to script it remotely.
https://rvm.io/workflow/scripting/
You can run the following command with net-ssh to do so:
conn.exec!("source "/usr/local/rvm/scripts/rvm; rvm --create ruby-1.9.3-p286#my_project_name")
It will create your environment and associated gemset.

Queue_Classic: How to run the rake task automatically on Heroku without Procfile

i need to know how to run the queue_classic (rake qc:work) rake task automatically on Heroku. I tried with Procfile, but i am using Bamboo and i get the next error: "Heroku push rejected, Procfile is not supported on the Bamboo stack"
any idea??
Thanks.
On heroku there is a addon viz heroku-scheduler, its free. Add this addon to your application. It will appear in your application addon lists, click it, and you will lead to worker dashboard where you can add job for example
bundle exec rake task_name
and schedule the task.

How to display RoR app code version on heroku?

For my Rails apps I normally deploy to production from a tagged version, and then display the tag in the user interface assigning the output of git describe --always to a variable in config/application.rb.
Now I'm moving an app over to Heroku, and deployment to heroku only happens using the master branch, so this trick won't work any more.
Are there any other ways to assign a version number to my code and display it on the UI when I've deployed to heroku?
Thanks,
Stewart
You can add a variable to the Heroku configuration by running this command locally whenever you push new changes to Heroku:
heroku config:add GIT_TAG=`git describe --always`
Then you can access this in your app's configuration:
version = ENV['GIT_TAG'] || `git describe --always`
When the app is running on Heroku, it will pick up the config variable (ENV['GIT_TAG']) and when it's running locally in development it will fall back to running git describe --always.
You will need to update the Heroku config variable each time you deploy, but I generally add this kind of thing to a deploy script or rake task (along with useful things like creating a new tag marking the deploy and running any new database migrations on Heroku).
Doesn't git tag fit your needs?
And why wouldn't the old trick work anymore?
If you want to display it on the UI then a git SHA output probably isn't particularly useful - you have two options, set a Heroku config variable with a user friendly version number in or a set a version number in your code that you increment when you deploy from master. You could probably wrap the deploy up in a rake task that incremented the version number either a file (and then readded it to git and commits it) or simply increments a value in a config variable.
Also, don't forget Heroku release management http://blog.heroku.com/archives/2010/11/17/releases/ which you may also be able to employ here to get the version number from that perhaps.