0

I need to deploy mailman with daemon and capistrano for my rails app. I came across this article and just followed the steps that he wrote.

But when I tried to deploy it, my local machined gave me this error:

The deploy has failed with an error: undefined local variable or method `mailman'.

I know the reason why this failed is because I am using the latest version capistrano (version 3.4.0) while the code is for the older version of it (probably version 2+)

Below is the code:

# Mailman configuration
namespace :mailman do
  desc "Mailman::Start"
  task :start, :roles => [:app] do
      run "cd #{current_path};RAILS_ENV=#{rails_env} script/mailman_daemon.rb start"
  end

  desc "Mailman::Stop" 
  task :stop, :roles => [:app] do
      run "cd #{current_path};RAILS_ENV=#{rails_env} script/mailman_daemon.rb stop"
  end 

  desc "Mailman::Restart" 
  task :restart, :roles => [:app] do
      mailman.stop mailman.start
  end
end 
before "deploy:cleanup", "mailman:restart"

So, may I know how could I rewrite the above code to follow the latest version of capistrano?

Thanks!

############################## UPDATE ########################

Below is my deploy.rb file:

server '178.62.16.69', port: 22, roles: [:web, :app, :db], primary: true

set :repo_url,        'git@github.com:ryzalyusoff/xxxx.git'
set :application,     'xxxx'
set :user,            'deploy'
set :puma_threads,    [4, 16]
set :puma_workers,    0

set :pty,             false
set :use_sudo,        false
set :stage,           :production
set :deploy_via,      :remote_cache
set :deploy_to,       "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :ssh_options,     { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true  # Change to false when not using ActiveRecord


set :whenever_identifier, ->{ "myapp_#{fetch(:stage)}" }

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
        exit
      end
    end
  end

  desc 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      invoke 'puma:restart'
    end
  end

  before :starting,     :check_revision
  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
  after  :finishing,    :restart

end


# Mailman configuration
namespace :mailman do
  desc "Mailman::Start"
  task :start do
    on roles(:app) do
      within release_path do
        with default_env: fetch(:default_env) do
          execute "script/mailman_daemon.rb", "start"
        end
      end
    end
  end

  desc "Mailman::Stop"
  task :stop do
    on roles(:app) do
      within release_path do
        with default_env: fetch(:default_env) do
          execute "script/mailman_daemon.rb", "stop"
        end
      end
    end
  end

  desc "Mailman::Restart"
  task :restart do
    on roles(:app) do
      invoke("mailman:stop")
      invoke("mailman:start")
    end
  end
end

before "deploy:cleanup", "mailman:restart"
Ryzal Yusoff
  • 957
  • 2
  • 22
  • 49

1 Answers1

2

Capistrano 3 syntax changed quite a lot, try this:

# Mailman configuration
namespace :mailman do
  desc "Mailman::Start"
  task :start do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute "script/mailman_daemon.rb", "start"
        end
      end
    end
  end

  desc "Mailman::Stop"
  task :stop do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute "script/mailman_daemon.rb", "stop"
        end
      end
    end
  end

  desc "Mailman::Restart"
  task :restart do
    on roles(:app) do
      invoke("mailman:stop")
      invoke("mailman:start")
    end
  end
end

before "deploy:cleanup", "mailman:restart"

See the Capistrano upgrade guide for more options.

Matouš Borák
  • 15,606
  • 1
  • 42
  • 53
  • Hey thanks for the answer! But I got this error when I tried it: `Don't know how to build task 'mailman:restart'` – Ryzal Yusoff Mar 09 '16 at 14:48
  • Hey...and now i got this error: `script/mailman_daemon.rb stderr: /usr/bin/env: ruby: No such file or directory` – Ryzal Yusoff Mar 09 '16 at 15:29
  • This is a problem with your environment setup. Are you using rvm? You might need to set the `PATH` variable in the `deploy.rb` script so that it contains path to where you have `ruby` installed. Something like `set :default_env, { path: "/opt/ruby/bin:$PATH" }` as explained here: http://capistranorb.com/documentation/getting-started/configuration/ – Matouš Borák Mar 09 '16 at 15:48
  • Hey, Yeah i am using rvm. But I am not sure about the whole path thing..I have posted my `deploy.rb` file above, could you help me to set it? So far, I try to set the path, but no luck. – Ryzal Yusoff Mar 09 '16 at 16:23
  • Perhaps using the `capistrano-rvm` gem will be sufficient. We are using it successfully with Capistrano 3 and RVM. And, as I look into our config, no PATH settings are needed. So try to install the gem and tell us what you get... https://github.com/capistrano/rvm – Matouš Borák Mar 09 '16 at 16:30
  • Hm, acually, I already am using capistrano-rvm right now...Is there any other way? – Ryzal Yusoff Mar 09 '16 at 20:19
  • There must be another way, but I'm unsure which. I think that it must be related to your rvm setup or you might need to update the header of the mailman script so that it is run by the correct ruby. I am not sure. I am quite sure though that the capistrano tasks definitions are correct. Take a look here for further info on capistrano with rvm, perhaps you'll get some ideas from there: http://stackoverflow.com/a/22515485/1544012. And perhaps place a new question to better target the problem? – Matouš Borák Mar 09 '16 at 20:37
  • I see. Thanks so far for your help! I will open a new thread for that, and mark this one as solved. Thanks! :) – Ryzal Yusoff Mar 09 '16 at 20:39
  • I am having some issue `script/mailman_daemon.rb stdout: /usr/bin/env: 'script/mailman_daemon.rb': Permission denied` Any thoughts ? – Dusht Mar 04 '17 at 15:03