Jak uruchomić zadanie grabie z Capistrano?

Już mamrb, który może wdrożyć moją aplikację na moim serwerze produkcyjnym.

Moja aplikacja zawiera niestandardowe zadanie rake (a .plik rake w katalogu lib / tasks).

Chciałbym stworzyć zadanie cap, które zdalnie uruchomi to zadanie rake.

Author: Richard Poirier, 2008-11-23

16 answers

Trochę bardziej wyraźne, w \config\deploy.rb, Dodaj poza dowolnym zadaniem lub przestrzenią nazw:

namespace :rake do  
  desc "Run a task on a remote server."  
  # run like: cap staging rake:invoke task=a_certain_task  
  task :invoke do  
    run("cd #{deploy_to}/current; /usr/bin/env rake #{ENV['task']} RAILS_ENV=#{rails_env}")  
  end  
end

Następnie z /rails_root/ można uruchomić:

cap staging rake:invoke task=rebuild_table_abc
 60
Author: Coward,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-20 10:47:08

Capistrano 3 Generic Version (Uruchom dowolne zadanie rake)

[[3]}budowanie ogólnej wersji odpowiedzi Mirka Rusina:
desc 'Invoke a rake command on the remote server'
task :invoke, [:command] => 'deploy:set_rails_env' do |task, args|
  on primary(:app) do
    within current_path do
      with :rails_env => fetch(:rails_env) do
        rake args[:command]
      end
    end
  end
end

Przykładowe użycie: cap staging "invoke[db:migrate]"

Zauważ, że deploy:set_rails_env wymaga]}

 46
Author: marinosb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-23 06:18:59

...kilka lat później...

Spójrz na wtyczkę rails capistrano, możesz zobaczyć na https://github.com/capistrano/rails/blob/master/lib/capistrano/tasks/migrations.rake#L5-L14 może wyglądać tak:

desc 'Runs rake db:migrate if migrations are set'
task :migrate => [:set_rails_env] do
  on primary fetch(:migration_role) do
    within release_path do
      with rails_env: fetch(:rails_env) do
        execute :rake, "db:migrate"
      end
    end
  end
end
 44
Author: Mirek Rusin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-10 21:59:34
run("cd #{deploy_to}/current && /usr/bin/env rake `<task_name>` RAILS_ENV=production")

Znalazłem to w Google -- http://ananelson.com/said/on/2007/12/30/remote-rake-tasks-with-capistrano/

The RAILS_ENV=production was a gotcha -- I didn ' t think of it at first and could not figure out why the task was nothing.

 41
Author: Richard Poirier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-21 00:38:03

Użyj wywoływania grabi w stylu Capistrano

Jest wspólny sposób, który "po prostu działa" z require 'bundler/capistrano' i innymi rozszerzeniami, które modyfikują rake. Będzie to również działać w środowiskach przedprodukcyjnych, jeśli używasz wielostopniowych. Sedno? Użyj varów konfiguracyjnych, jeśli możesz.

desc "Run the super-awesome rake task"
task :super_awesome do
  rake = fetch(:rake, 'rake')
  rails_env = fetch(:rails_env, 'production')

  run "cd '#{current_path}' && #{rake} super_awesome RAILS_ENV=#{rails_env}"
end
 20
Author: captainpete,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-08 01:37:32

Użyj capistrano-rake gem

Po prostu zainstaluj gem bez mieszania się z niestandardowymi przepisami capistrano i wykonaj żądane zadania rake na zdalnych serwerach, Jak to:

cap production invoke:rake TASK=my:rake_task

pełne ujawnienie: napisałem to

 16
Author: Sheharyar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-07 04:58:35

Ja osobiście używam w produkcji metody pomocniczej takiej jak Ta:

def run_rake(task, options={}, &block)
  command = "cd #{latest_release} && /usr/bin/env bundle exec rake #{task}"
  run(command, options, &block)
end

Który pozwala na uruchomienie zadania rake podobnego do użycia metody run (command).


Uwaga: jest to podobne do tego, co zaproponowałDuke , Ale ja:

  • użyj latest_release zamiast current_release - z mojego doświadczenia wynika, że jest to bardziej to, czego oczekujesz podczas uruchamiania polecenia rake;
  • postępuj zgodnie z konwencją nazewnictwa Grabie i Capistrano (zamiast: cmd - > task i grabie - > run_rake)
  • nie ustawiaj RAILS_ENV= # {rails_env} , ponieważ właściwym miejscem do jej ustawienia jest zmienna default_run_options. E. g default_run_options [: env] = {'RAILS_ENV' = > 'production'} # - > DRY!
 7
Author: Szymon Jeż,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:47:19

Istnieje ciekawy klejnot Przylądek , który sprawia, że Twoje zadania rake są dostępne jako zadania Capistrano, dzięki czemu możesz je uruchamiać zdalnie. {[2] } jest dobrze udokumentowane, ale oto krótki przegląd jak skonfigurować i.

Po zainstalowaniu gem, wystarczy dodać to do pliku config/deploy.rb.

# config/deploy.rb
require 'cape'
Cape do
  # Create Capistrano recipes for all Rake tasks.
  mirror_rake_tasks
end
Teraz możesz uruchamiać wszystkie zadania lokalnie lub zdalnie przez cap.

Jako dodatkowy bonus, cape pozwala ustawić, jak chcesz uruchomić zadanie rake lokalnie i zdalnie (nie więcej bundle exec rake), wystarczy dodać to do pliku config/deploy.rb:

# Configure Cape to execute Rake via Bundler, both locally and remotely.
Cape.local_rake_executable  = '/usr/bin/env bundle exec rake'
Cape.remote_rake_executable = '/usr/bin/env bundle exec rake'
 5
Author: yacc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-30 02:04:17
namespace :rake_task do
  task :invoke do
    if ENV['COMMAND'].to_s.strip == ''
      puts "USAGE: cap rake_task:invoke COMMAND='db:migrate'" 
    else
      run "cd #{current_path} && RAILS_ENV=production rake #{ENV['COMMAND']}"
    end
  end                           
end 
 3
Author: Darme,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-06 14:13:29

To mi pomogło:

task :invoke, :command do |task, args|
  on roles(:app) do
    within current_path do
      with rails_env: fetch(:rails_env) do
        execute :rake, args[:command]
      end
    end
  end
end

Następnie po prostu uruchom cap production "invoke[task_name]"

 3
Author: Abram,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-23 07:18:00

Oto, co umieściłem w mojej misji.rb, aby uprościć uruchamianie zadań rake. Jest to prosty wrapper wokół metody run () capistrano.

def rake(cmd, options={}, &block)
  command = "cd #{current_release} && /usr/bin/env bundle exec rake #{cmd} RAILS_ENV=#{rails_env}"
  run(command, options, &block)
end

Potem uruchamiam dowolne zadanie rake ' a tak:

rake 'app:compile:jammit'
 2
Author: Duke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-14 23:31:34

Większość pochodzi z powyżej odpowiedzi z drobnym ulepszeniem, aby uruchomić każde zadanie rake z capistrano

Uruchom dowolne zadanie grabie z capistrano

$ cap rake -s rake_task=$rake_task

# Capfile     
task :rake do
  rake = fetch(:rake, 'rake')
  rails_env = fetch(:rails_env, 'production')

  run "cd '#{current_path}' && #{rake} #{rake_task} RAILS_ENV=#{rails_env}"
end
 1
Author: Sairam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 10:31:30

To również działa:

run("cd #{release_path}/current && /usr/bin/rake <rake_task_name>", :env => {'RAILS_ENV' => rails_env})

Więcej informacji: Capistrano Run

 1
Author: acw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-08 15:37:56

Jeśli chcesz być w stanie przekazać wiele argumentów, spróbuj tego (na podstawie odpowiedzi marinosberna):

task :invoke, [:command] => 'deploy:set_rails_env' do |task, args|
  on primary(:app) do
    within current_path do
      with :rails_env => fetch(:rails_env) do
        execute :rake, "#{args.command}[#{args.extras.join(",")}]"
      end
    end
  end
end

Następnie można uruchomić zadanie tak: cap production invoke["task","arg1","arg2"]

 1
Author: Robin Clowers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-24 01:21:34

Pracowałam nad tym. szwy działają dobrze. Jednak potrzebujesz formatera, aby naprawdę wykorzystać kod.

Jeśli nie chcesz używać formatera ustaw poziom dziennika na tryb debugowania. Te SEMA do h

SSHKit.config.output_verbosity = Logger::DEBUG

Cap Stuff

namespace :invoke do
  desc 'Run a bash task on a remote server. cap environment invoke:bash[\'ls -la\'] '
  task :bash, :execute do |_task, args|
    on roles(:app), in: :sequence do
      SSHKit.config.format = :supersimple
      execute args[:execute]
    end
  end

  desc 'Run a rake task on a remote server. cap environment invoke:rake[\'db:migrate\'] '
  task :rake, :task do |_task, args|
    on primary :app do
      within current_path do
        with rails_env: fetch(:rails_env) do
          SSHKit.config.format = :supersimple
          rake args[:task]
        end
      end
    end
  end
end

To jest formatter, który zbudowałem do pracy z powyższym kodem. Jest on oparty na: textsimple wbudowanym w sshkit, ale nie jest to zły sposób na wywoływanie niestandardowych zadań. Och, to wiele nie działa z najnowszą wersją sshkit gem. Wiem, że działa z 1.7.1. Mówię to, ponieważ gałąź master zmieniła dostępne metody poleceń SSHKit::.

module SSHKit
  module Formatter
    class SuperSimple < SSHKit::Formatter::Abstract
      def write(obj)
        case obj
        when SSHKit::Command    then write_command(obj)
        when SSHKit::LogMessage then write_log_message(obj)
        end
      end
      alias :<< :write

      private

      def write_command(command)
        unless command.started? && SSHKit.config.output_verbosity == Logger::DEBUG
          original_output << "Running #{String(command)} #{command.host.user ? "as #{command.host.user}@" : "on "}#{command.host}\n"
          if SSHKit.config.output_verbosity == Logger::DEBUG
            original_output << "Command: #{command.to_command}" + "\n"
          end
        end

        unless command.stdout.empty?
          command.stdout.lines.each do |line|
            original_output << line
            original_output << "\n" unless line[-1] == "\n"
          end
        end

        unless command.stderr.empty?
          command.stderr.lines.each do |line|
            original_output << line
            original_output << "\n" unless line[-1] == "\n"
          end
        end

      end

      def write_log_message(log_message)
        original_output << log_message.to_s + "\n"
      end
    end
  end
end
 0
Author: newdark-it,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-12 00:44:34

Poprzednie odpowiedzi mi nie pomogły i znalazłem to: Od http://kenglish.co/run-rake-tasks-on-the-server-with-capistrano-3-and-rbenv/

namespace :deploy do
  # ....
  # @example
  #   bundle exec cap uat deploy:invoke task=users:update_defaults
  desc 'Invoke rake task on the server'
  task :invoke do
    fail 'no task provided' unless ENV['task']

    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, ENV['task']
        end
      end
    end
  end

end

Aby uruchomić zadanie użyj

bundle exec cap uat deploy:invoke task=users:update_defaults

Może się komuś przyda

 0
Author: A.Miroshnichenko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-13 14:37:33