noooooob
noooooob

Reputation: 241

bundler: command not found. Cannot start puma or even open rails console

I am using capistrano to deploy a rails 5.2 application. Everything goes on well except the fact that puma doesn't start.

Result of tail -f /var/www/tuma/shared/log/puma_error.log when I execute sudo systemctl start puma_tuma_production

bundler: command not found: puma
Install missing gem executables with `bundle install`

I have noticed that when I cc into the current rails folder, I cannot even open the rails console too

bundle exec rails c production returns:

bundler: command not found: rails
Install missing gem executables with `bundle install`

Here is the output of gem list bundler when performed on /var/www/tuma/current

*** LOCAL GEMS ***

bundler (2.3.17, default: 2.1.4)
bundler-unload (1.0.2)
rubygems-bundler (1.4.5)

When gem list bundler is ran outside the current folder, it returns: /home/ubuntu/.rvm/gems/ruby-2.7.1/bin/bundler

Here is the output of sudo systemctl status puma_tuma_production

× puma_tuma_production.service - Puma HTTP Server for tuma (production)
     Loaded: loaded (/etc/systemd/system/puma_tuma_production.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Tue 2022-07-12 15:22:28 UTC; 24s ago
    Process: 320774 ExecStart=/home/ubuntu/.rvm/bin/rvm default do bundle exec --keep-file-descriptors puma -C /var/www/tuma/shared/puma.rb (code=exited, status=127)
   Main PID: 320774 (code=exited, status=127)
        CPU: 558ms

Jul 12 15:22:26 ip-172-31-42-199 systemd[1]: puma_tuma_production.service: Main process exited, code=exited, status=127/n/a
Jul 12 15:22:26 ip-172-31-42-199 systemd[1]: puma_tuma_production.service: Failed with result 'exit-code'.
Jul 12 15:22:28 ip-172-31-42-199 systemd[1]: puma_tuma_production.service: Scheduled restart job, restart counter is at 5.
Jul 12 15:22:28 ip-172-31-42-199 systemd[1]: Stopped Puma HTTP Server for tuma (production).
Jul 12 15:22:28 ip-172-31-42-199 systemd[1]: puma_tuma_production.service: Start request repeated too quickly.
Jul 12 15:22:28 ip-172-31-42-199 systemd[1]: puma_tuma_production.service: Failed with result 'exit-code'.
Jul 12 15:22:28 ip-172-31-42-199 systemd[1]: Failed to start Puma HTTP Server for tuma (production).

Here is the result of sudo systemctl cat puma_tuma_production

# /etc/systemd/system/puma_tuma_production.service
[Unit]
Description=Puma HTTP Server for tuma (production)
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/var/www/tuma/current
# Support older bundler versions where file descriptors weren't kept
# See https://github.com/rubygems/rubygems/issues/3254
ExecStart=/home/ubuntu/.rvm/bin/rvm default do bundle exec --keep-file-descriptors puma -C /var/www/tuma/shared/puma.rb
ExecReload=/bin/kill -USR1 $MAINPID
StandardOutput=append:/var/www/tuma/shared/log/puma_access.log
StandardError=append:/var/www/tuma/shared/log/puma_error.log

Restart=always
RestartSec=1
SyslogIdentifier=puma
[Install]
WantedBy=multi-user.target

Here is my puma.rb file

#!/usr/bin/env puma

directory '/var/www/tuma/current'
rackup "/var/www/tuma/current/config.ru"
environment 'production'

tag ''

pidfile "/var/www/tuma/shared/tmp/pids/puma.pid"
state_path "/var/www/tuma/shared/tmp/pids/puma.state"

threads 0,16
bind 'unix:///var/www/tuma/shared/tmp/sockets/puma.sock'
workers 2

restart_command 'bundle exec puma'
prune_bundler

on_restart do
  puts 'Refreshing Gemfile'
  ENV["BUNDLE_GEMFILE"] = ""
end

Here is my PATH.

/home/ubuntu/.rvm/gems/ruby-2.7.1/bin:/home/ubuntu/.rvm/gems/ruby-2.7.1@global/bin:/home/ubuntu/.rvm/rubies/ruby-2.7.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ubuntu/.rvm/bin:/home/ubuntu/.rvm/bin

Here is my GemFile

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.1'

gem 'rails', '~> 5.2.8'
gem 'pg'
# Use Puma as the app server
gem 'puma', '~> 5.6.0'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'terser'

gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
gem 'redis', '~> 4.0'
gem 'sidekiq'                 # Background Jobs Processing
gem 'devise'
gem 'business_time'
gem 'phony_rails'
gem 'friendly_id', '~> 5.4.0', require: "friendly_id"
gem 'meta-tags'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'annotate'
  gem 'net-ssh', '~> 7.0.0.beta1'
  ## Deployment
  gem 'capistrano'
  gem 'capistrano-rails'
  gem 'capistrano-bundler'
  gem 'capistrano-rvm'
  gem 'capistrano3-puma', github: 'seuros/capistrano-puma'
  gem 'capistrano3-nginx'
  gem 'capistrano-db-tasks', require: false
  gem 'capistrano-sidekiq', github: 'seuros/capistrano-sidekiq'
  gem 'pry'
  gem 'pry-rails'

  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Result for /home/ubuntu/.rvm/bin/rvm obtained from which rvm

#!/usr/bin/env bash

if (( ${rvm_ignore_rvmrc:=0} == 0 ))
then
  declare rvmrc

  rvm_rvmrc_files=("/etc/rvmrc" "$HOME/.rvmrc")
  if [[ -n "${rvm_prefix:-}" ]] && ! [[ "$HOME/.rvmrc" -ef "${rvm_prefix}/.rvmrc" ]]
     then rvm_rvmrc_files+=( "${rvm_prefix}/.rvmrc" )
  fi

  for rvmrc in "${rvm_rvmrc_files[@]}"
  do
    if [[ -f "$rvmrc" ]]
    then
      if GREP_OPTIONS="" \grep '^\s*rvm .*$' "$rvmrc" >/dev/null 2>&1
      then
        printf "%b" "
  Error:
    $rvmrc is for rvm settings only.
    rvm CLI may NOT be called from within $rvmrc.
    Skipping the loading of $rvmrc
"
        exit 1
      else
        source "$rvmrc"
      fi
    fi
  done
  unset rvm_rvmrc_files
  unset rvmrc
fi

# duplication marker jdgkjnfnkjdngjkfnd4fd
export rvm_path
if [[ -z "${rvm_path:-}" ]]
then
  if [[ -d "${0%/bin/rvm}" ]]
  then rvm_path="$( \cd "${0%/bin/rvm}">/dev/null; pwd )"
  elif (( UID == 0 )) && [[ -d "/usr/local/rvm" ]]
  then rvm_path="/usr/local/rvm"
  elif [[ -d "${HOME}/.rvm" ]]
  then rvm_path="${HOME}/.rvm"
  elif [[ -d "/usr/local/rvm" ]]
  then rvm_path="/usr/local/rvm"
  else echo "Can't find rvm install!" 1>&2 ; exit 1
  fi
fi

# allow disabling check temporary
: rvm_is_not_a_shell_function:${rvm_is_not_a_shell_function:=1}
export rvm_is_not_a_shell_function

# if to prevent fork-bomb
if source "${rvm_scripts_path:="$rvm_path/scripts"}/rvm"
then
  typeset -f rvm >/dev/null 2>&1 || {
    echo "RVM not loaded, aborting." >&2
    exit 1
  }
  rvm "$@"
else
  echo "Error sourcing RVM!"  1>&2
  exit 1
fi

Result of config/deploy.rb

# config valid for current version and patch releases of Capistrano
lock "~> 3.17.0"
require 'capistrano-db-tasks'

set :application, "tuma"
set :repo_url, "[email protected]:<MYGITLABUSERNAME>/tuma.git"

set :use_sudo, false

# if you want to remove the local dump file after loading
set :db_local_clean, false

# Default branch is :master
ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
set :use_sudo, false
set :bundle_binsstubs, nil

append :rvm_map_bins, 'puma', 'pumactl', 'sidekiq', 'sidekiqctl'

set :app_path, "#{deploy_to}/current"
set :pty, false

append :linked_files, 'config/database.yml', 'config/secrets.yml', 'config/application.yml', 'config/sidekiq.yml'

append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'public/system', 'public/uploads'

set :disallow_pushing, false

set :assets_dir, %w(public/uploads)
set :local_assets_dir, %w(public/uploads)

### Puma Configuration ###
set :nginx_use_ssl, true
set :puma_workers, 2

### Sidekiq Configuration ###
SSHKit.config.command_map[:sidekiq] = "bundle exec sidekiq"
SSHKit.config.command_map[:sidekiqctl] = "bundle exec sidekiqctl"

set :sidekiq_default_hooks, true
set :sidekiq_pid, File.join(shared_path, 'tmp', 'pids', 'sidekiq.pid') # ensure this path exists in production before deploying.
set :sidekiq_env, :production
set :sidekiq_log, File.join(shared_path, 'log', 'sidekiq.log')
set :sidekiq_config, File.join(shared_path, 'config', 'sidekiq.yml') 

Here is my Capfile

# Load DSL and set up stages
require 'capistrano/setup'

require 'capistrano/deploy'

require 'capistrano/scm/git'
install_plugin Capistrano::SCM::Git

require 'capistrano/rvm'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/puma'
require 'capistrano/sidekiq'
require 'capistrano/nginx'

install_plugin Capistrano::Puma  # Default puma tasks
install_plugin Capistrano::Puma::Workers  # if you want to control the workers (in cluster mode)
install_plugin Capistrano::Puma::Nginx  # if you want to upload a nginx site template
install_plugin Capistrano::Sidekiq::Systemd
install_plugin Capistrano::Puma::Systemd  # if you use SystemD
install_plugin Capistrano::Nginx

Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

I have changed set :rvm_bin_stubs to append :rvm_bin_stubs with no luck

Upvotes: 2

Views: 1445

Answers (2)

i0x539
i0x539

Reputation: 5363

would you run which bundle and make sure you are pointed at the correct rvm build?

Also just to confirm have you ran bundle update ?

Upvotes: 0

John N.
John N.

Reputation: 91

I suspect that systemctl needs your $PATH variable set properly - it seems that bundle cannot find the gem bin paths set by RVM. You may have a valid $PATH set in your shell but, perhaps not in the context of systemctl. I believe there are two ways you can achieve this.

First, you can set a $PATH in the systemctl config for the service, like this answer outlines https://stackoverflow.com/a/37341810/261940

# /etc/systemd/system/nagios.service.d/env.conf
[Service]
Environment="PATH=/home/ubuntu/.rvm/gems/ruby-2.7.1/bin:/home/ubuntu/.rvm/gems/ruby-2.7.1@global/bin:/home/ubuntu/.rvm/rubies/ruby-2.7.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ubuntu/.rvm/bin:/home/ubuntu/.rvm/bin"

Second, you can try editing your ExecStart to use bash, which I think will load your $PATH properly, like this:

ExecStart=/bin/bash -lc '/home/ubuntu/.rvm/bin/rvm default do bundle exec --keep-file-descriptors puma -C /var/www/tuma/shared/puma.rb'

I am happy to provide more follow-up if you try these and let me know what happens.

Upvotes: 1

Related Questions