Home > Enterprise >  Ruby on Rails app runs only as a root user and errors out when run as ec2-user
Ruby on Rails app runs only as a root user and errors out when run as ec2-user

Time:12-25

Background: I have a ruby application in an EC2 instance running Amazon Linux 2. My application is using Ruby 2.7.6. I am trying to run the application as ec2-user. When I run bundle install the packages load fine but error our when I run rails s command.

Traceback (most recent call last):
        40: from bin/rails:10:in `<main>'
        39: from bin/rails:10:in `require'
        38: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/railties-5.2.8.1/lib/rails/commands.rb:18:in `<top (required)>'
        37: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/railties-5.2.8.1/lib/rails/command.rb:46:in `invoke'
        36: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/railties-5.2.8.1/lib/rails/command/base.rb:69:in `perform'
        35: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
        34: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
        33: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
        32: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/railties-5.2.8.1/lib/rails/commands/server/server_command.rb:142:in `perform'
        31: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/railties-5.2.8.1/lib/rails/commands/server/server_command.rb:142:in `tap'
        30: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/railties-5.2.8.1/lib/rails/commands/server/server_command.rb:145:in `block in perform'
        29: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/railties-5.2.8.1/lib/rails/commands/server/server_command.rb:145:in `require'
        28: from /home/ec2-user/test/mvs_web/config/application.rb:9:in `<top (required)>'
        27: from /opt/rubies/ruby-2.7.6/lib/ruby/site_ruby/2.7.0/bundler.rb:187:in `require'
        26: from /opt/rubies/ruby-2.7.6/lib/ruby/site_ruby/2.7.0/bundler/runtime.rb:44:in `require'
        25: from /opt/rubies/ruby-2.7.6/lib/ruby/site_ruby/2.7.0/bundler/runtime.rb:44:in `each'
        24: from /opt/rubies/ruby-2.7.6/lib/ruby/site_ruby/2.7.0/bundler/runtime.rb:55:in `block in require'
        23: from /opt/rubies/ruby-2.7.6/lib/ruby/site_ruby/2.7.0/bundler/runtime.rb:55:in `each'
        22: from /opt/rubies/ruby-2.7.6/lib/ruby/site_ruby/2.7.0/bundler/runtime.rb:60:in `block (2 levels) in require'
        21: from /opt/rubies/ruby-2.7.6/lib/ruby/site_ruby/2.7.0/bundler/runtime.rb:60:in `require'
        20: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/savon-2.13.1/lib/savon.rb:27:in `<top (required)>'
        19: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
        18: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:257:in `load_dependency'
        17: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `block in require'
        16: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
        15: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/savon-2.13.1/lib/savon/client.rb:2:in `<top (required)>'
        14: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
        13: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:257:in `load_dependency'
        12: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `block in require'
        11: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
        10: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/savon-2.13.1/lib/savon/operation.rb:9:in `<top (required)>'
         9: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
         8: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:257:in `load_dependency'
         7: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `block in require'
         6: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
         5: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/mail-2.8.0/lib/mail.rb:3:in `<top (required)>'
         4: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/mail-2.8.0/lib/mail.rb:14:in `<module:Mail>'
         3: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
         2: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:257:in `load_dependency'
         1: from /opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `block in require'
/opt/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require': cannot load such file -- mail/indifferent_hash (

But when I run the application as root I don't face this error and the application is running fine.

But I need this to run for ec2-user and webapp users because during the deployment the pipeline is executing the command with webapp user and it errors out with a similar error.

2022/12/16 21:32:38.034224 [INFO] Running command /bin/su webapp -c bundle exec /opt/elasticbeanstalk/config/private/checkforraketask.rb db:migrate
2022/12/16 21:32:42.299253 [INFO] rake aborted!
LoadError: cannot load such file -- mail/indifferent_hash
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `block in require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:257:in `load_dependency'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/mail-2.8.0/lib/mail.rb:14:in `<module:Mail>'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/mail-2.8.0/lib/mail.rb:3:in `<top (required)>'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `block in require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:257:in `load_dependency'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/savon-2.13.1/lib/savon/operation.rb:9:in `<top (required)>'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `block in require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:257:in `load_dependency'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/savon-2.13.1/lib/savon/client.rb:2:in `<top (required)>'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `block in require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:257:in `load_dependency'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/activesupport-5.2.8.1/lib/active_support/dependencies.rb:291:in `require'
/var/app/staging/vendor/bundle/ruby/2.7.0/gems/savon-2.13.1/lib```

Please advice on how to bypass this.

CodePudding user response:

This issue is a known bug in the mail gem (see here).

You should make sure to use the previous version - 2.7.1.

  • Related