I'm trying to install bundler v2 in docker file and it installs successfully. when i run rubocop i get below error.
warn_for_outdated_bundler_version': You must use Bundler 2 or greater with this lockfile. (Bundler::LockfileError)
which still seem to think that I am using bundler older version, how can I fix this error
Dockerfile is
FROM ruby:2.3.4-alpine
WORKDIR "/var/www/app"
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN gem install bundler && bundle install
bin/bundle file is
require "rubygems"
m = Module.new do
module_function
def invoked_as_script?
File.expand_path($0) == File.expand_path(__FILE__)
end
def env_var_version
ENV["BUNDLER_VERSION"]
end
def gemfile
gemfile = ENV["BUNDLE_GEMFILE"]
return gemfile if gemfile && !gemfile.empty?
File.expand_path("../../Gemfile", __FILE__)
end
def bundler_version
#bundler_version ||= begin
env_var_version || cli_arg_version ||
lockfile_version || "#{Gem::Requirement.default}.a"
end
end
def load_bundler!
ENV["BUNDLE_GEMFILE"] ||= gemfile
# must dup string for RG < 1.8 compatibility
activate_bundler(bundler_version.dup)
end
def activate_bundler(bundler_version)
if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0")
bundler_version = "< 2"
end
gem_error = activation_error_handling do
gem "bundler", bundler_version
end
return if gem_error.nil?
require_error = activation_error_handling do
require "bundler/version"
end
return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION))
warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`"
exit 42
end
def activation_error_handling
yield
nil
rescue StandardError, LoadError => e
e
end
end
m.load_bundler!
if m.invoked_as_script?
load Gem.bin_path("bundler", "bundle")
end
and the bundler version inside Gemfile.lock is 2.0.2
set BUNDLER_VERSION environment variable inside Dockerfile:
ENV BUNDLER_VERSION=2.0.2
or if you have docker.env:
BUNDLER_VERSION=2.0.2
I faced this problem also but as well as defining the BUNDLER_VERSION environment variable, I also needed to install that version into my container.
My Dockerfile therefore would look like:
FROM ruby:2.3.4-alpine
WORKDIR "/var/www/app"
ENV BUNDLER_VERSION=2.0.2
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN gem install bundler -v "$BUNDLER_VERSION" && bundle install
Related
It's possible, for example, to use ActiveRecord inline in a Ruby script. I like this a lot to report bugs, test features and share gists.
I'm wondering if the same could be done for a Rails webservice? (I'm mainly interested in getting the controller layer to work, the rest should be easy to add on demand.) Something along these lines:
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem 'rails', '~> 6.0.0'
end
require 'rails/commands'
APP_PATH = File.expand_path('config/application', __dir__)
Rails::Command.invoke('server')
When toying around with this it did seem that an external entry point (APP_PATH) is required. So alternatively, an acceptable approach would be to cram all config into the single entry point. I couldn't get this to work so far.
The Rails Initialization Process is the best resource I found about this so far.
I produced a minimal Rails app to get started as follows:
rails new min-rails --skip-keeps --skip-action-mailer --skip-action-mailbox --skip-action-text --skip-active-record --skip-active-storage --skip-puma --skip-action-cable --skip-sprockets --skip-spring --skip-listen --skip-javascript --skip-turbolinks --skip-test --skip-system-test --skip-bootsnap --api
cd min-rails
rm -rf app/jobs app/models config/initializers config/locales lib log public tmp vendor config/environments/test.rb config/environments/production.rb config/credentials.yml.enc config/master.key bin/rake bin/setup bin/bundle
I ended up with the following script:
inline-rails.rb
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem 'rails', '~> 6.0.0'
end
require "action_controller/railtie"
class App < Rails::Application
routes.append do
get "/hello/world" => "hello#world"
end
config.consider_all_requests_local = true # display errors
end
class HelloController < ActionController::API
def world
render json: {hello: :world}
end
end
App.initialize!
Rack::Server.new(app: App, Port: 3000).start
Run it as:
ruby inline-rails.rb
I have the following ruby code :
require 'HTTPClient'
require 'rubygems'
require 'zip'
def self.unzip(data, dest_dir)
::Zip::File.open_buffer(data) do |fzip|
fzip.each do |entry|
path = File.join(dest_dir, entry.name)
puts "here"
FileUtils::mkdir_p(File.dirname(path))
fzip.extract(entry, path) unless File.exist?(path)
#fzip.close
end
end
end
def self.fetch_from_url(url, dest_dir)
response = HTTPClient.get(url, follows_redirect: true)
if response.status == 200
unzip(response.body, dest_dir)
else
raise 'Could not fetch files from 3scale'
end
end
url = 'link/artifactory/zip-release-local/djin/3Sroxy/1.0.5/configuration.zip'
fetch_from_url(url, "/Users/something/")
When I run this in Mac ruby 2.0.0p481 it works fine and unzips the folder, but when I run the same in centOS 6.6 in ruby 1.8.7 it fails with following :
[root#ip-10-201-90-206 ~]# sudo ruby test/ex.rb
test/ex.rb:7:in `unzip': uninitialized constant Zip::File (NameError)
from test/ex.rb:20:in `fetch_from_url'
from test/ex.rb:28
also, I've done :
gem install zip
Successfully installed zip-2.0.2
1 gem installed
Installing ri documentation for zip-2.0.2...
Installing RDoc documentation for zip-2.0.2...
As Prashant4224 stated, you need to install
gem install rubyzip
You seem to use the zip Gem...
In Gemfile you need to add. Everything else didn't work for me.
gem 'rubyzip', '>= 1.0.0'
gem 'zip-zip'
/home/palpandi/.rvm/gems/ruby-1.8.7-p374#fedena_zip/gems/rails-2.3.5/lib/rails/gem_dependency.rb:119:Warning:
Gem::Dependency#version_requirements is deprecated and will be removed
on or after August 2010. Use #requirement
/home/palpandi/.rvm/gems/ruby-1.8.7-p374#fedena_zip/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:105:in
`const_missing': uninitialized constant Rails::Boot::Bundler
(NameError)
Using Rails 2.3.5
Ruby 1.8.7
ubuntu 12.04
I had a similar problem. The correct way to solve this is to go to your project folder, then in config/boot.rb go to the very bottom, and just before the Rails.boot! line add the following :
begin
require "rubygems"
require "bundler"
rescue Bundler::GemNotFound
raise RuntimeError, "Bundler couldn't find some gems." + "Did you run bundle install?"
end
class Rails::Boot
def run
load_initializer
Rails::Initializer.class_eval do
def load_gems
#bundler_loaded ||= Bundler.require :default, Rails.env
end
end
Rails::Initializer.run(:set_load_path)
end
end
This will solve the "uninitialized constant Authorization" error.
put this line in your boot.rb
begin
require "rubygems"
require "bundler"
rescue Bundler::GemNotFound
raise RuntimeError, "Bundler couldn't find some gems." + "Did you run bundle install?"
end
or
gem install bundler
and after adding:
gem 'bundler'
in line 2 in config/boot.rb (just after require 'rubygems')
I am looking at this boot.rb file:
http://github.com/bestbuyremix/BBYIDX/blob/master/config/boot.rb
And after trying to understand it, it is as if I have learned nothing so far.
Can someone detail what is going on here?
I have no idea how someone could even come up with this?
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end
def booted?
defined? Rails::Initializer
end
def pick_boot
(vendor_rails? ? VendorBoot : GemBoot).new
end
def vendor_rails?
File.exist?("#{RAILS_ROOT}/vendor/rails")
end
def preinitialize
load(preinitializer_path) if File.exist?(preinitializer_path)
end
def preinitializer_path
"#{RAILS_ROOT}/config/preinitializer.rb"
end
end
class Boot
def run
load_initializer
Rails::Initializer.run(:set_load_path)
end
end
class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
end
end
class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
require 'initializer'
end
def load_rails_gem
if version = self.class.gem_version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
exit 1
end
class << self
def rubygems_version
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
end
def gem_version
if defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
ENV['RAILS_GEM_VERSION']
else
parse_gem_version(read_environment_rb)
end
end
def load_rubygems
require 'rubygems'
min_version = '1.1.1'
unless rubygems_version >= min_version
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end
rescue LoadError
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end
def parse_gem_version(text)
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
end
private
def read_environment_rb
File.read("#{RAILS_ROOT}/config/environment.rb")
end
end
end
end
# All that for this:
Rails.boot!
If you are learning Rails, this is not the place to do it. Perhaps you have the thought that in order to use it, you need to understand how the code flows from the beginning? Don't do that. :)
If you're learning rails, use any of the many guides and tutorials to build a basic site.
As for this bit of code, some concepts that it employs involve ruby's iconic class << self Here is a critical read on meta classes: http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/
The Rails.boot! at the bottom leads you to the conclusion "the method boot! is called on the object Rails ... going back up to the top, you see
module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end
...
Here you can see the magic behind class << self ... it created the boot! method on the module itself. from there you can trace the method call throughout the file, as it checks for the existence of a preinitializer file...
pick_boot returns an object, either VendorBoot or GemBoot depending on the result of vendor_rails? and then call the run method on it.
From there you have some standard class inheritance of the Boot classes, as it sets up the rest of the libraries. Hopefully that gets you started. :)
This is actually very good OO style... small methods and classes that all do a simple task. There's also OO inheritance and several common ruby idioms. All in all, a very good bit of ruby code. :)
Update
Here's a rough estimate of how it would look if coded in a more procedural style:
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
unless defined? Rails::Initializer
preinitializer_path = "#{RAILS_ROOT}/config/preinitializer.rb"
load() if File.exist?(preinitializer_path)
if File.exist?("#{RAILS_ROOT}/vendor/rails")
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
Rails::Initializer.run(:set_load_path)
else
begin
require 'rubygems'
min_version = '1.1.1'
rubygems_version = Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
unless rubygems_version >= min_version
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end
rescue LoadError
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end
begin
if defined? RAILS_GEM_VERSION
version = RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
version = ENV['RAILS_GEM_VERSION']
else
version = $1 if (File.read("#{RAILS_ROOT}/config/environment.rb")) =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~=]*\s*[\d.]+)["']/
end
if version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
exit 1
end
require 'initializer'
end
end
What this is really doing is a bunch of fancy footwork to determine where the rails source code is.
You have the ability to 'freeze' your current rails gem source code into the vendor directory. This is useful when you might have more than one version of rails installed and want to make sure that your application is developed and run with that version only.
This code is checking to see if a version of rails has been frozen into the vendor directory, and if so, use that. If a frozen version isn't available it tries to use the local gems, but first makes sure they meet a minimum version requirement.
For more information on 'freezing' your gems, look at the descriptions of the rake tasks availble to your project with "rake -T".
Ok, so I have a Rails app set up on DreamHost and I had it working a while ago and now it's broken. I don't know a lot about deployment environments or anything like that so please forgive my ignorance. Anyway, it looks like the app is crashing at this line in config/environment.rb:
require File.join(File.dirname(__FILE__), 'boot')
config/boot.rb is pretty much normal, but I'll include it here anyway.
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end
def booted?
defined? Rails::Initializer
end
def pick_boot
(vendor_rails? ? VendorBoot : GemBoot).new
end
def vendor_rails?
File.exist?("#{RAILS_ROOT}/vendor/rails")
end
def preinitialize
load(preinitializer_path) if File.exist?(preinitializer_path)
end
def preinitializer_path
"#{RAILS_ROOT}/config/preinitializer.rb"
end
end
class Boot
def run
load_initializer
Rails::Initializer.run(:set_load_path)
end
end
class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
end
end
class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
require 'initializer'
end
def load_rails_gem
if version = self.class.gem_version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
exit 1
end
class << self
def rubygems_version
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
end
def gem_version
if defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
ENV['RAILS_GEM_VERSION']
else
parse_gem_version(read_environment_rb)
end
end
def load_rubygems
require 'rubygems'
min_version = '1.1.1'
unless rubygems_version >= min_version
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end
rescue LoadError
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end
def parse_gem_version(text)
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
end
private
def read_environment_rb
File.read("#{RAILS_ROOT}/config/environment.rb")
end
end
end
end
# All that for this:
Rails.boot!
Does anyone have any ideas? I am not getting any errors in the log or on the page.
-fREW
I had the same problem on DreamHost. Freezing rails and unpacking all gems got me past it.
rake rails:freeze:gems
rake gems:unpack:dependencies
My guess would be that you're breaking because of a newer version of the Rails gems on Dreamhost. At least, that's been my issue when things have blown up in something like boot.rb.
Try freezing the gems from your development environment into your vendor/rails directory.
Ya - the problem is not really in boot.rb - it's just that boot.rb is where rails is actually loaded.
So you'll get an error like this if you've specified a version of Rails that just doesn't exist on your dreamhost slice. This can happen if you either upgrade your project, start a new project (and forget that you upgraded rails in the meanwhile) or if you're still using an old version of rails and it's now been removed from the dreamhost server that you're on.
To figure out which is is, look in config/environment.rb for the line that'll read something like:
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
Then ssh to your dreamhost server and type gem list and see if your version is in the list.
If not, you an try several options. Lets say the version you're using is 2.3.4
To begin with, try: gem install rails -v=2.3.4 then restart. That may be all that is required.
If that doesn't work, then try freezing and unpacking the gems (as per the other answer here).
There is also another possibility - that you're actually missing a gem that rails depends on but which is failing silently - eg a dependency on a certain version of rack caught me out once. But you may also have other gem dependencies
If you run rake gems you will be able to list all the gems that your project knows about that it needs - make sure they're installed to begin with.
Then, as a sort of rough smoke-test, try running script/console - if you're missing an important rails gem, script/console won't load and should fail, giving you a notice about the gem you need.
Update:
If you're trying to run v 2.3.5, you may also be suffering from this problem:
Bypassing rack version error using Rails 2.3.5
In which case you'll need to follow the instructions there.