Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute system command with Rake outside Bundle scope

Let say I have this Rake task:

namespace :db do
  namespace :dump do.
    desc 'Backup database dump to s3'
    task :backup => :environment do 
      cmd = ['backup', 'perform',  '-t project_backup',  "-c #{Rails.root.join 'lib', 'backup', 'config.rb'}"] 
      system(*cmd)                      # ...I've tried `` & exec() sa well, same thing
    end
  end
end

Backup gem is stand alone ruby gem application which dependencies needs to be isolated from application bundler. In other words it cannot be part of Gemfile. This gem is simply installed over gem install backup

When I run backup command over bash console, it successfully run:

$ backup perform -t validations_backup -c /home/equivalent/my_project/lib/backup/config.rb

When I execute rake db:dump:backup I will get

backup is not part of the bundle. Add it to Gemfile. (Gem::LoadError)

...which is the same thing when I run backup command with bundle exec from bash

$ bundle exec backup perform -t validations_backup -c /home/equivalent/my_project/lib/backup/config.rb

...meaning that the backup command is executed over bundler when run as part of rake task.

my question: How can I run rake db:dump:backup outsite the bundle scope, meaning that backup command won`t be executed over bundler?

Thank you

like image 957
equivalent8 Avatar asked Jun 18 '14 11:06

equivalent8


People also ask

What is rake command?

Rake is a software task management and build automation tool created by Jim Weirich. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace. It is similar in to SCons and Make.

What is bundle exec command?

bundle exec is a Bundler command to execute a script in the context of the current bundle (the one from your directory's Gemfile). rake db:migrate is the script where db is the namespace and migrate is the task name defined.

What is rake task in rails?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.

Does rake come with Ruby?

Rake is a Rubygem that automates many common functions required to build, test, package, and install programs; it is part of every modern Ruby installation, so you don't need to install it yourself. Here are some common Rake tasks that you may encounter: Set up required environment by creating directories and files.


1 Answers

I found a workaround for this problem here:

namespace :db do
  namespace :dump do
    desc 'Backup database dump to s3'
    task :backup do
      Bundler.with_clean_env do
        sh "backup perform -t project_backup -c #{Rails.root.join 'lib', 'backup', 'config.rb'}"
      end
    end
  end
end

The key here is to enclose the code that must not run under bundler's environment in a block like this:

Bundler.with_clean_env do
  # Code that needs to run without the bundler environment loaded
end
like image 132
Ernesto Avatar answered Oct 29 '22 15:10

Ernesto