Extend Capistrano V3 DSL

127 views
Skip to first unread message

Maxim Dobryakov

unread,
Dec 26, 2013, 6:55:08 AM12/26/13
to capis...@googlegroups.com
Hi their,

I want to add several commands to Capistrano DSL for manage init.d files. For example:

  task :init_d do
    on roles(:app), in: :sequence, wait: 5 do
      install_initd('handler', 'bin/handler', nil, start_env: 'PATH=/opt/ruby/shims:$PATH RUBY_ENV=production')
    end
  end

As you can see I want to add several methods with arguments and call them from tasks. I can't use tasks because they don't have ability to pass parameters.

Could you please recommend a way how can I extend Capistrano DSL and add own method to it?

Thanks

Lee Hambley

unread,
Dec 26, 2013, 7:35:23 AM12/26/13
to capistrano
Hi Maxim,

Everything in the `on()` block is evaluated in the context of a SSHKit handler, by default this is the Net::SSH handler. If you really need to do what you are doing, you can create your own class with more methods, and force Capistrano to use that runner for the backend.

Better would be to write your methods in a way that they return a SSHKit::Command, so that you could do something like:

   execute InitdHandler.new('handler', 'bin/handler', nil, start_env: 'PATH=/opt/ruby/shims:$PATH RUBY_ENV=production').install

Where the #install method returns the aforementioned SSHKit::Command.

If you go this way, then you'll be able to unit test that InitdHandler#install returns the correct command, and it allows you to keep your code isolated.

The Ruby autoloading code will not care if your modules are defined in our namespaces or not, so it should "just work".

--
You received this message because you are subscribed to the Google Groups "Capistrano" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capistrano+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/capistrano/43d7d1e1-23b0-4fd3-8485-a16f32148faa%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages