"uninitialized constant Etc" when running Ruby application - ruby-on-rails

A few applications on my server relying on Ruby and Ruby On Rails seem to have stopped working. Or atleast partially so. I managed to get the web gui of the application in question to start functioning again by reinstalling different part of Ruby. But I most likley broke something else in the process.
I have managed to track it down using some tests and it seems like Etc is somehow not found by the program when it runs.
I tested by
ruby -e 'puts Etc.getpwnam("apache").uid'
and got
uninitialized constant Etc
If I instead tested
ruby -r etc -e 'puts Etc.getpwnam("apache").uid'
I get the correct answer
48
This is running on an Amahi6 server (running Fedora Core 14 64bit).
Example on the code in question in the actual application that fails is
#!/usr/bin/env ruby
# Amahi Home Server
# Copyright (C) 2007-2010 Amahi
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License v3
# (29 June 2007), as published in the COPYING file.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# file COPYING for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Amahi
# team at http://www.amahi.org/ under "Contact Us."
require File.dirname(__FILE__) + '/../config/boot'
#require 'commands/runner'
require 'optparse'
# switch to apache:users first
uid = Etc.getpwnam("apache").uid
gid = Etc.getgrnam("users").gid
Process.gid = gid
Process.egid = gid
Process.uid = uid
Process.euid = uid
...
this fails with the same
uninitialized constant Etc
Clarification: I have NOT changed this code at all and it has worked in it's current state before
What could it be that I have messed up? :)
UPDATE:
Ruby installed now is version 1.8.7
UPDATE 2:
If I try to manually add a require for rubygems and etc it fails, but complains about another package. So I think there is something fundamentally messed up with the core of my ruby.
/usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:55:
uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in
gem_original_require'
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:inrequire'
from
/usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support.rb:56
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in
gem_original_require'
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:inrequire'
from
/usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record.rb:25
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in
gem_original_require'
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:inrequire'
from /var/hda/platform/html/script/install-app:70

Ended up being that the versions of rails and gems were to new. Downgraded to gems 1.3.7 and rails 2.3.8.
gem update --system 1.3.7
gem uninstall rails
gem install rails --version 2.3.8

You seem to be missing require 'rubygems' and require 'etc' (put them below require 'optparse')

remember about 'rubygems',
commandline:
ruby -rubygems -e 'puts Etc.getpwnam("apache").uid'
script:
require 'rubygems'
puts Etc.getpwnam("apache").uid

Related

Why is too hard to create a simple proyect with rails?

Only i want to create a simple proyect with ruby and rails, but i think its harder than it looked.
I have a version of ruby 2.6.8, i created a new project with this command rails new blog, after that cd to blog
i applied this command: bin/rails server
i am facing this error: bin/rails:4:in require_relative': cannot load such file -- .../rb/blog/config/boot (LoadError) from bin/rails:4:in '
check with command:
ruby -v
that you ruby version same as in gemfile file, also as mention before don't forget run "bundle" after creating new project or add new gem to gemfile.
If this didn't help and you rails version is 6 you can manually fix problem - create boilerplate config/boot.rb:
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
i am only start learning rails but i think if you only beginner then try fix problem in another way such reinstall rails, ruby etc. bc in future it can cause problem, its not normal when you just create new project and it already dont work, something wrong.

Can not run rails server for my application

I am trying to run a project downloaded from internet, it is ruby on rails, I am getting the following error
Traceback (most recent call last):
4: from bin/rails:3:in `<main>'
3: from bin/rails:3:in `load'
2: from C:/Users/Amira/canvas/bin/spring:10:in `<top (required)>'
1: from C:/Users/Amira/canvas/bin/spring:10:in `read'
C:/Users/Amira/canvas/bin/spring:10:in `read': No such file or directory # rb_sysopen - C:/Users/Amira/canvas/Gemfile.lock (Errno::ENOENT)
The code written in the file is as following
#!/usr/bin/env ruby
# This file loads spring without using Bundler, in order to be fast.
# It gets overwritten when you run the `spring binstub` command.
unless defined?(Spring)
require 'rubygems'
require 'bundler'
if (match = Bundler.default_lockfile.read.match(/^GEM$.*?^ (?: )*spring \((.*?)\)$.*?^$/m))
Gem.paths = { 'GEM_PATH' => [Bundler.bundle_path.to_s, *Gem.path].uniq.join(Gem.path_separator) }
gem 'spring', match[1]
require 'spring/binstub'
end
end
It is an opensource project and I am new on rails
can anyone please help me ?
EDIT
Gemfile content
# What have they done to the Gemfile???
#
# Relax. Breathe deep. All the gems are still there; they're just loaded in various files in Gemfile.d/
# This allows us to require gems locally that we might not want to commit to our public repo. We can maintain
# a customized list of gems for development and debuggery, without affecting our ability to merge with canvas-lms
#
# NOTE: this file has to use 1.8.7 hash syntax to not raise a parser exception on 1.8.7
#
# NOTE: some files in Gemfile.d/ will have certain required gems indented. While this may seem arbitrary,
# it actually has semantic significance. An indented gem required in Gemfile is a gem that is NOT
# directly used by Canvas, but required by a gem that is used by Canvas. We lock into specific versions of
# these gems to prevent regression, and the indentation serves to alert us to the relationship between the gem and canvas-lms
source 'https://rubygems.org/'
Dir[File.join(File.dirname(__FILE__), 'gems/plugins/*/Gemfile.d/_before.rb')].each do |file|
eval(File.read(file), nil, file)
end
require File.expand_path("../config/canvas_rails_switcher", __FILE__)
Dir.glob(File.join(File.dirname(__FILE__), 'Gemfile.d', '*.rb')).sort.each do |file|
eval(File.read(file), nil, file)
end
The Gemfile usually specifies the dependencies (other code the application needs). The project you look into seems to use a slightly more complex setup.
Normally it works roughly like this:
Once the dependencies are installed via bundler (the command bundle or the more explicit bundle install), a Gemfile.lock is placed next to it to list the actually used versions of the installed dependencies.
Two takeaways:
use bundle (the gem is called bundler) to install dependencies.
read the README of the project carefully. If it does not have instructions on installation, create an issue if the projects has a method to do so. If you are hypernice or normal and the project is open and healthy, create a Pull Request for the README in which you describe how you managed to install the app.
Edit this might have sounded a bit arrogant, and I did not peek into the details. I assume you followed https://github.com/instructure/canvas-lms/wiki/Quick-Start . The canvas-lms project uses a setup slightly more complex than your typical Rails application. I believe you should ask specifically for help with canvas installation (edit your question).

What can be done to fix the following error when we run any rails command: " `require_relative': cannot load such file "

Any rails command doesn't work for me. I have several versions of ruby installed through rvm. I tried installing rails with all the versions, they do install successfully but with all of them I face the following error whenever I run any rails command in my project directory:
~ rails new blog
Traceback (most recent call last):
1: from bin/rails:3:in `<main>'
bin/rails:3:in `require_relative': cannot load such file -- /Users/Am33d/Documents/config/boot (LoadError)
I tried looking up for the error but didn't find any solutions.
What can be done to fix this? I am using macOS Mojave (10.14.6)
This error would indicate that you do not have a boot.rb file in your config directory for some reason. When running rails commands -- regardless of if you run them as bin/rails [command] or bundle exec rails [command], runs the bin/rails file. This file typically has a line require_relative '../config/boot. The boilerplate bin/rails file in a new Rails 6 app is:
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
To simply fix this you can create a blank file by running touch config/boot.rb from the root directory of your application and that would itself suppress the error. Having said that, you'd be better off creating a config/boot.rb file that contains useful information. The boilerplate config/boot.rb file is this
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
Without this file you are not necessarily appropriately loading the gems from your gemfile or caching operations with bootsnap.
When I ran into this problem, I also received the same error when trying to use rails -s.
If the same is happening for you, its because your version of ruby isn't compatible with your rails version.
After upgrading to the latest rails version the error stopped and everything worked well.
A little bit weird.
rails new blog should not need to find boot file, neither need bundler. Actually, you are trying to create a new project, so it is expected there is not Gemfile, boot, or bundler created for your project.
So I would advise you to find what rails command is being executed. Sometimes one rails executable created by bundler is taking precedence in the $PATH environment variable.
Ex: some incorrect bin/rails executable is on your $HOME directory.
i was trying to build an app using 'rails server' and this error was showing. You have to make sure that the rails version matches the ruby version and even if you do what an answer above said (create the config/boot.rb paste) doesnt work, than you have to change the version of rails to the stable. I'll let the link of this problem here, there's an issue closed in github for this problem https://github.com/rails/rails/pull/43951
to solve the problem you have to replace the gem rails line on the gemfile to this:
gem "rails", github: "rails/rails", branch: "7-0-stable"
Sorry about my english.

rails 3.2.8 ruby 1.9.3 on DreamHost shared hosting fcgi RMagic issue

Using the following guide
http://spontaneousderivation.com/2012/09/30/rails-3-2-on-a-shared-dreamhost-server/
I managed to get my Rails 3.2.8 application with Ruby 1.9.3 up and running on Dreamhost shared hosting using fcgi (i know it is not the best solution, but I don't want to downgrade my applications to 3.0 and I lack skill and money for a VPS ).
Following the guides on DH wiki:
http://wiki.dreamhost.com/RMagick
http://wiki.dreamhost.com/Image_Magick#Compiling_ImageMagick_on_your_DreamHost_account
I installed my own version of ImageMagic and RMagic as a local gem.
However when I try to run application with RMagic functionality I get the following error in error.log:
'Premature end of script headers: dispatch.fcgi'.
I found out it is occurring when I uncomment the following line in my uploader:
include CarrierWave::RMagick
Running dispatch.fcgi from shell does not report any errors.
I assume that the problem might be caused by LD_LIBRARY_PATH or other env variables (listed in DH wiki) but tried including them in my .bashrc, .bash_profile and dispatch.fcgi but without effect.
This solution is valid as of 3/7/2013. I just used it to deploy a new app to DH.
In your gemfile...
gem 'paperclip'
Then run "bundle install" from your app directory.
In your public/dispatch.fcgi...copy the code below and replace APPNAME with the name listed in your config/application.rb module, USER with your DH username, and RUBY with the specific ruby version listed in the output from "which ruby" on the command line on your DH ssh session.
#!/home/USER/.rvm/rubies/RUBY/bin/ruby
ENV['RAILS_ENV'] ||= 'production'
ENV['HOME'] ||= `echo ~`.strip
ENV['GEM_HOME'] = File.expand_path('~/.rvm/gems/RUBY')
ENV['GEM_PATH'] = File.expand_path('~/.rvm/gems/RUBY') + ":" +
File.expand_path('~/.rvm/gems/RUBY#global')
require 'fcgi'
require File.join(File.dirname(__FILE__), '../config/environment.rb')
class Rack::PathInfoRewriter
def initialize(app)
#app = app
end
def call(env)
env.delete('SCRIPT_NAME')
parts = env['REQUEST_URI'].split('?')
env['PATH_INFO'] = parts[0]
env['QUERY_STRING'] = parts[1].to_s
#app.call(env)
end
end
Rack::Handler::FastCGI.run Rack::PathInfoRewriter.new(APPNAME::Application)
No special processing is needed to get paperclip working assuming you have a valid install of RVM. Aside from the specifics I mentioned above (specifically the dispatch.fcgi is slightly changed), you can follow this guide, http://spontaneousderivation.com/2012/09/30/rails-3-2-on-a-shared-dreamhost-server/.
RMagick and ImageMagick are notorious for having compile / runtime issues. I recommend ditching RMagick in favor of something like MiniMagick. It looks like CarrierWave has a MiniMagick processor you can use instead: https://github.com/jnicklas/carrierwave/blob/master/lib/carrierwave/processing/mini_magick.rb
MiniMagick just shells out directly to the ImageMagick processes so you avoid the issues of linking to compiled binaries. In the past, RMagick was also known to be the source of memory leaks etc.
Also, you should seriously consider Heroku for deployment. A single dyno is free and I bet your experience will be much better.

Rails/Passenger: no such file to load -- money (MissingSourceFile)

I am attempting to deploy a Rails application (which works fine in development) onto a production server. I have installed and configured Apache, Passenger, and the necessary gems. After I restart apache and navigate to the server, I get the following error:
Exception PhusionPassenger::UnknownError in PhusionPassenger::Railz::ApplicationSpawner (no such file to load -- money (MissingSourceFile))
gem list money shows:
*** LOCAL GEMS ***
money (3.0.5)
An irb session on the server:
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'money'
=> true
The gem is required in config/environment.rb:
require 'money'
What's going on here?
Edit: The gem was installed as root:
# ls -l /usr/lib/ruby/gems/1.8/gems/money-3.0.5/lib
total 8
drwx------ 2 root root 4096 Sep 2 10:26 money
-rw-r--r-- 1 root root 1284 Sep 2 10:26 money.rb
Edit 2: After having no success with that roadblock, I realized that Passenger is sort of made to go along with Ruby Enterprise Edition, so I tried installing that. Afterwards, I used Ruby Enterprise's version of gem to install all the required gems. Now when I restart apache and navigate to my server, I get:
Exception LoadError in PhusionPassenger::Railz::ApplicationSpawner (no such file to load -- logger)
I have no problem requiring logger in either the old or new versions of Ruby:
# which irb
/usr/bin/irb
# irb
irb(main):001:0> require 'logger'
=> true
irb(main):002:0> exit
# /opt/ruby-enterprise-1.8.7-2010.02/bin/irb
irb(main):001:0> require 'logger'
=> true
Something must be going on with paths or permissions, but I followed the instructions in both the Passenger and Ruby Enterprise Edition installers exactly. Any other ideas?
Apache/Passenger runs the ruby process as root (or www-data), make sure you didn't install the Gem in a local repository.
You probably need to become superuser before installing the gem.
Answer 2:
The correct way to require a Gem is not using require, but using
config.gem "money"
are you using the same version of ruby for both?
Thanks all for your help. We investigated some more and found some of the required files (the gems themselves, I believe) were installed with the wrong permissions (not readable by the user that runs Apache/Passenger). I'm not sure why that happened, as they were installed the normal way as root (maybe some non-standard umask was the issue?), but changing permissions on those files fixed the problem.

Resources