How to get the current Rack environment in Rake? - ruby

Is there a way to get information about the current Rack environment in Rake? For example, how can I tell whether Rack is running in development or production mode?
I understand that Rake is not Rack-aware. I'm trying to avoid replicating code in nearly-identical Rake tasks between production and dev environments.

Question is old but never fetched the best practice answer or a satisfying answer at all.
The real question is: How to go sure which environment is used in a Rake task in order to load the correct configuration / hitting into correct if-conditions.
Note: As Rake doesn't give much about Rack (Rake is not using HTTP) to
rely on the RACK_ENV is basically wrong but common and handy if a Rake
task loads your main Sinatra application (the RACK_ENV is required to let
Sinatras development? / test? / production? being set correctly).
The answer: Set the environment with each Rake task call.
Command line call:
/usr/bin/rake namespace:task_name RACK_ENV=production
Cronjob call (in crontab):
cd /into/your/app/root && /usr/bin/rake namespace:task_name RACK_ENV=production --silent
Note: To specify the path of the Rake bin is not necessary if you have it in your global system variables. Your path might differs from mine used in the examples, check on Unix systems with: whereis rake
You can check the RACK_ENV in your tasks via:
puts ENV["RACK_ENV"]

As other environment variable, you can retrieve it using:
ENV['RACK_ENV']
Considering it's a Sinatra application, and that you've set the environment into config/environment.rb, you can add the following to your Rakefile:
task :environment do
require File.expand_path('config/environment', File.dirname(__FILE__))
end
task :your_task => :environment do
# task
end
Then, you can retrieve the environment (depending how you set it up in your environment.rb) with ENV['RACK_ENV'] or Sinatra::Application.environment.
Considering there isn't a config/environment.rb config file, only the application file, for instance hello_world.rb, the following works:
hello_world.rb:
require 'sinatra'
set :environment, :production
get '/' do
'Hello World'
end
Rakefile:
task :environment do
require File.expand_path('hello_world', File.dirname(__FILE__)) # your Sinatra app
end
task :your_task => :environment do
puts Sinatra::Application.environment
end
When doing rake your_task you should obtain:
> rake your_task
production

After 2.5 years, I want to share what I've found to be the best solution.
Create a .env file in the root folder of the application, and add a flag specifying the application environment:
ENVIRONMENT=development
Then use Brandon Keepers' dotenv gem to load all environment variables from this file. Now you can use any environment variables specified in .env within Rake tasks.
Rake will rely on the explicit value set in .env, so you must create separate .env files for each environment you plan on using (e.g. dev, test, staging, production, etc).
Sample Rakefile:
require 'dotenv/tasks'
task :default => :help
desc 'Show this help menu'
task :help do
puts "Available rake tasks:"
system('rake --tasks')
end
# Will run in any environment
desc 'Demo task'
task :demo_task => :dotenv do
puts "Running demo task in '#{ENV['ENVIRONMENT']}' mode"
end
# Will only run if ENVIRONMENT value in .env file is set to 'production'
desc 'Production-only task'
task :production_task => :dotenv do
if ENV['ENVIRONMENT'] == 'production'
puts "Running 'Production-only' task"
else
puts "Won't run, because the environment is not set to PRODUCTION!"
end
end
# Will only run if ENVIRONMENT value in .env file is set to 'development'
desc 'Development-only task'
task :dev_task => :dotenv do
if ENV['ENVIRONMENT'] == 'development'
puts "Running 'Development-only' task"
else
puts "Won't run, because the environment is not set to DEVELOPMENT!"
end
end
If you want to use the environment variables within your Rack or Sinatra app (which you probably do), add the following to the application's config or bootstrap block:
require 'dotenv'
Dotenv.load

Related

capistrano aborts - SSHKit::Runner::ExecuteError and Devise.secret_key was not set

I'm trying to update an app (Rails 4.1.12; ruby 1.9.3p484) by deploying changes from development to production using capistrano. Below is log (I masked the IP address and suggested secret key).
Previously never had a problem running a deploy. Have searched and found posts with similar errors but have not found a solution. I did follow the instructions to add Devise.secret_key to the Devise initializer and restarted the application (dev and prod). Next attempt to deploy received same error with suggestion for different secret key.
All help, hints, suggestions are greatly appreciated. Thanks.
UPDATE:I just tried to deploy using
bundle exec cap production deploy
and did not get errors but still get errors when running
cap production deploy
UPDATE 2: I used Figaro to generate an application.yml file (and append to gitignore). In the devise.rb file I created a line config.secret_key = ENV["config.secret_key"] In the application.yml file I created a line config.secret_key: 'random_128_chr_string' I then restarted the development server and ran cap production deploy Unfortunately got same error.
cap production deploy
.
.
.
[deploy:migrate] Run `rake db:migrate`
[e0c2c251] Running bundle exec rake db:migrate as deployer#123.123.123.12
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deployer#123.123.123.12: rake exit status: 1
rake stdout: Nothing written
rake stderr: rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = 'looong_string_of_numbers_and_letters'
Please ensure you restarted your application after installing Devise or setting the key.
deploy.rb:
lock '>=3.2.1'
set :application, 'pepperslice'
set :repo_url, 'git#github.com:jeffaltman/pepperslice.git'
set :linked_dirs, %w{log tmp public/images/uploaded}
set :linked_files, %w{config/database.yml config/initializers/devise.rb config/initializers/smtp_settings.rb}
set :format, :pretty
set :log_level, :info
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute "sudo /usr/sbin/service unicorn restart"
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
capfile:
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
#
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
# require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
#require 'capistrano/ssh_doctor'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

How can I load ActiveRecord database tasks on a Ruby project outside Rails?

ActiveRecord 3.2.14
I want to use ActiveRecord in a non-Rails Ruby project. I want to have available the rake tasks that are defined by ActiveRecord. How can I do that?
rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load # Load fixtures into the current environment's database
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false)
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
rake db:structure:dump # Dump the database structure to db/structure.sql
rake db:version # Retrieves the current schema version number
The above list is the list of tasks that I want to be able to use on my non-Rails Ruby project that uses ActiveRecord. What do I have to write in my Rakefile?
Thanks in advance
The easiest thing to do is to load the tasks already defined in databases.rake. Here is a GIST of how it was done.
Inspired by this GIST by Drogus
Rakefile.rb
require 'yaml'
require 'logger'
require 'active_record'
include ActiveRecord::Tasks
class Seeder
def initialize(seed_file)
#seed_file = seed_file
end
def load_seed
raise "Seed file '#{#seed_file}' does not exist" unless File.file?(#seed_file)
load #seed_file
end
end
root = File.expand_path '..', __FILE__
DatabaseTasks.env = ENV['ENV'] || 'development'
DatabaseTasks.database_configuration = YAML.load(File.read(File.join(root, 'config/database.yml')))
DatabaseTasks.db_dir = File.join root, 'db'
DatabaseTasks.fixtures_path = File.join root, 'test/fixtures'
DatabaseTasks.migrations_paths = [File.join(root, 'db/migrate')]
DatabaseTasks.seed_loader = Seeder.new File.join root, 'db/seeds.rb'
DatabaseTasks.root = root
task :environment do
ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
ActiveRecord::Base.establish_connection DatabaseTasks.env.to_sym
end
load 'active_record/railties/databases.rake'
You could try the standalone-migrations gem:
https://github.com/thuss/standalone-migrations
For Rails 3.x:
You need to manually create the tasks. As example here is how to add them (this example uses the environment variables like Rails):
namespace :db do
desc "Drop and create the current database"
task :recreate => :environment do
abcs = ActiveRecord::Base.configurations
ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
ActiveRecord::Base.connection.recreate_database(ActiveRecord::Base.connection.current_database)
end
end
and you'll have the task rake db:recreate available
For Rails 4.x:
If you want to have the ActiveRecord rake tasks available in your ruby app, take a look at the documentation.
Example usage of DatabaseTasks outside Rails could look as such:
include ActiveRecord::Tasks
DatabaseTasks.database_configuration = YAML.load(File.read('my_database_config.yml'))
DatabaseTasks.db_dir = 'db'
# other settings...
DatabaseTasks.create_current('production')
Also you have here an example on how to use ActiveRecord in your ruby aplication.
Create your own!
Reference the Rails one though:
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake
Create a Rake Task file. To use Rake, generally you want a tasks folder filled with Rake task files. These files have the ".task" extension.
Study the file to link given.
Take parts of that file, or even the entire contents of the file, and add it to your new Rake task file.
Make sure your Rakefile loads those task files. Your Rakefile should have something like this
-
Dir[File.join(PROJECT_ROOT, 'tasks', '**', '*.rake')].each do |file|
load file
end
I believe you can use the sinatra-activerecord gem even if you're not using Sinatra. I just solved this problem by requiring that gem and then adding
require 'sinatra/activerecord/rake'
to my rakefile.
Once I added that require line the db tasks showed up in my rake -T!
If you are using Sinatra, you can use this gem:
https://github.com/janko-m/sinatra-activerecord
However, if you don't use it either, the source code inside provides a good example on how to implement AR rake tasks.

My Ruby install \bin\ruby.exe is not getting found when running rake_task for rspec

I can run rspec directly but with a Rakefile as simple as
require 'rspec/core/rake_task'
task :default => [:spec]
RSpec::Core::RakeTask.new do |task|
end
I get something like this output
C:\Ruby193 -S rspec ./spec/hello_spec.rb
You can see that it has not found the ruby executable.
How does rspec know where it is?
I shut off my environment variable for Ruby and it still does this.
I had to set environment variables for two types so I could still find binaries in the main Ruby install.
First for user I put the entire path to ruby\bin\ruby.exe and for system I put just the path to the bin directory. This seemed to solve my problem and I can still run other binaries.
USER:
%RUBY% = C:\Ruby193\bin\ruby.exe and append to PATH
SYSTEM:
%RUBY% = C:\Ruby193 and append this to Path as %RUBY%\bin
I use ansicon.exe plus the dll from root where ever as long as the same drive.
require 'rspec/core/rake_task'
task :default => [:spec]
RSpec::Core::RakeTask.new do |task|
task.pattern = "./spec/*_spec.rb"
task.rspec_opts = ['-c']# for ansi colors
end
Note you do not need default in the block. task is the default which is test. Just set some options with it.

Sinatra configuring environments on the fly

I have successfull written a little Sinatra application and already successfully deployed it on heroku.
However I want to run that application in development mode on my local computer and I want to have it production mode on heroku once I push it to the remote repository.
Currently I can achieve either one of thos options. When I change my config.ru to the following values:
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
require "./calc.rb"
enable :logging
set :environment, :development
set :port, 4567
I'm able to run it locally (on port 4567) via ruby config.ru. When I change the config.ru to this:
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
require "./calc.rb"
enable :logging
set :environment, :production
set :port, 4567
run Sinatra::Application
I'm able to get it to run on Heroku (on port 80).
But I can not use the same configuration for both development and production.
I would like to have something like:
ruby config.ru dev for development and ruby config.ru for production.
Additional information:
When I try to run the production config.ru on my local machine I get:
$ ruby config.ru
(eval):2:in `method_missing': undefined method `run' for main:Object (NoMethodError)
from (eval):4:in `__send__'
from (eval):4:in `method_missing'
from config.ru:10
C:\>type tmp.ru
require 'sinatra'
configure(:production){ p "I'm production" }
configure(:development){ p "I'mma dev mode" }
configure(:sassycustom){ p "I'mma own mode" }
exit!
C:\>rackup tmp.ru
"I'mma dev mode"
C:\>rackup -E development tmp.ru
"I'mma dev mode"
C:\>rackup -E production tmp.ru
"I'm production"
C:\>rackup -E sassycustom tmp.ru
"I'mma own mode"
C:\>rackup -E notdefined tmp.ru
If you don't specify an environment, development is used by default. You can specify any environment name you want, though 'production' is very common. If you specify an environment that you don't configure, no configuration block will match. (It might be a mistake on your part, but it's not an error caught by the code.)
Note that the Sinatra documentation says that setting RACK_ENV environment variable will be used if available. This used to not work, but some time in the last few years it has been fixed!
If, for example, you can set an environment variable for your service, you can control the mode.
You can also grab ENV['RACK_ENV'] in your config.ru and use that configure your app differently. On Heroku it should run in production by default and if you rackup to fire up your server it will be development by default. Here's some sample code from one of my apps that runs in both environments with the same config file:
#\ -p 4567
require 'bundler' # gem requires
Bundler.require(:default, ENV['RACK_ENV'].to_sym) # only loads environment specific gems
if ENV['RACK_ENV'] == 'production' # production config / requires
require './lib/middleware/exceptionmailer'
use Rack::ExceptionMailer,
:to => ['me#example.com'],
:from => 'service#example.com',
:subject => 'Error Occurred on Rack Application'
else # development or testing only
use Rack::ShowExceptions
end
This way, Thin or Passenger or whatever will pick it up and the right modules will get loaded in production but you can do other configuration for development.
Look at the Heroku documentation:
http://devcenter.heroku.com/articles/rack#frameworks
That's basically what I use for my app, when I start it locally it runs on port 4567.

Is there a better way to run a capistrano task from within rake?

I have a set of rake tasks where I need to invoke capistrano at some point. Edwin Goei's blog suggests shelling out to capistrano via "sh".
Is there a simpler way? It would seem you should be able to call the appropriate tasks programmatically. Thanks in advance.
Yes, Capistrano has programmatic access to the command-line components. If you want to call them from a rake task, though, you need to do a little extra work.
task :deploy
require 'rubygems'
require 'capistrano'
require 'capistrano/cli'
parameters = ["deploy"] # this is an array of the strings that come after
# cap on the command line. e.g.,
# ["deploy", "-S", "revision=1024"] gives you local var
# revision in your deploy.rb.
# The following is required ONLY when you run Capistrano 2+ from Rake,
# because Rake adds the methods from FileUtils to Object. FileUtils includes
# a method called symlink which interferes with Capistrano's symlink task.
Capistrano::Configuration::Namespaces::Namespace.class_eval { undef :symlink }
Capistrano::CLI.parse(parameters).execute!
end
For capistrano 3:
http://capistranorb.com/documentation/advanced-features/capistrano-pure-ruby/
require 'capistrano/all'
stages = "production"
set :application, 'my_app_name'
set :repo_url, 'git#github.com:capistrano/capistrano.git'
set :deploy_to, '/var/www/'
set :stage, :production
role :app, %w{}
require 'capistrano/setup'
require 'capistrano/deploy'
Dir.glob('capistrano/tasks/*.cap').each { |r| import r }
Capistrano::Application.invoke("production")
Capistrano::Application.invoke("deploy")
Jonathan, your mileage may vary by doing something like set(:shell, false) to stop capistrano running tasks in a sub-sh-shell.
Just a thought, feel free to ping me if you need a hand though.

Resources