help - newbie trying to writing my own task

52 views
Skip to first unread message

Joel Taylor

unread,
May 5, 2015, 4:11:31 PM5/5/15
to capis...@googlegroups.com
Versions:
  • Ruby 2.0.0p576
  • Capistrano 3.4.0
I'm learning Ruby + Capistrano. I'm trying to write a task that will pull down a file of a remote server. A simple start so I thought. :)

Here's the task: http://pastie.org/10160640
I have it under: /lib/capistrano/tasks/sync_db.rb

I can see the task available when I run: cap -T

cap sync_db:to_local             # Download and import DB to local

However, I'm getting this output/error:

Downloading Staging DB

(Backtrace restricted to imported tasks)

cap aborted!

NoMethodError: undefined method `execute' for main:Object

/.../lib/capistrano/tasks/sync_db.rb:10:in `block (2 levels) in <top (required)>'

Tasks: TOP => sync_db:to_local

(See full trace by running task with --trace)


the 'download!' command isn't working either ... what am I missing???

Lee Hambley

unread,
May 5, 2015, 4:13:29 PM5/5/15
to Capistrano
You're missing the "on()" block, you need to wrap the task inside something like:

task :downloaddb do
  on roles(:db) do                           # <<<----------- you missed this bit!
    # everything from your task here
  end
end

--
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/e4d093c2-4160-4b16-ab5d-89bc0b0b9de1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joel Taylor

unread,
May 5, 2015, 6:53:44 PM5/5/15
to capis...@googlegroups.com
Thanks Lee,
I guess that makes sense - but is there documentation about this? Or do you just "know" it? :)

Thx!!

Lee Hambley

unread,
May 6, 2015, 1:05:59 AM5/6/15
to Capistrano

Capistranorb.com ?

Joel Taylor

unread,
May 6, 2015, 10:55:41 AM5/6/15
to capis...@googlegroups.com
Yeh, but that doesn't really tell me why the "on" block is needed. Or what it really does. Is that Rake DSL? 

Docs are def. not for newbies.

Lee Hambley

unread,
May 6, 2015, 11:14:45 AM5/6/15
to Capistrano
Sorry. Docs are the best we can do. The `on()` block is where Capistrano starts, and Rake ends, outside of `on()`… what server should things be run on?

Joel Taylor

unread,
May 6, 2015, 2:48:59 PM5/6/15
to capis...@googlegroups.com
I get it - docs are hard and time consuming.

Answer me this, how would you download a file from a remote (staging) server to your local machine? There's a "download!" method - but I can't seem to figure it out.

Should it live inside of a "on roles(:all) do" or "run_locally do".

desc "Export Stage DB to Local DB"
task :to_local do
#on roles(:all) do
run_locally do
puts 'Downloading Staging DB'
execute 'pwd'
date_path = Date.today.strftime("%Y/%m/%d")
download! "/var/mysql-backup/#{date_path}/oamm-wp-db_stage-00.sql.gz" "oamm-wp-db_stage-00.sql.gz"
end
end

cap staging sync_db:to_local

Downloading Staging DB

INFO [4c3b8d6b] Running /usr/bin/env pwd as myuser@localhost

DEBUG [4c3b8d6b] Command: /usr/bin/env pwd

DEBUG [4c3b8d6b] /Users/myuser/Sites/mysite.dev

INFO [4c3b8d6b] Finished in 0.015 seconds with exit status 0 (successful).

(Backtrace restricted to imported tasks)

cap aborted!

Errno::ENOENT: No such file or directory - /var/mysql-backup/2015/05/06/oamm-wp-db_stage-00.sql.gzoamm-wp-db_stage-00.sql.gz

/Users/myuser/Sites/mysite.dev/lib/capistrano/tasks/sync_db.rb:14:in `block (3 levels) in <top (required)>'

/Users/myuser/Sites/mysite.dev/lib/capistrano/tasks/sync_db.rb:10:in `block (2 levels) in <top (required)>'

Tasks: TOP => sync_db:to_local

(See full trace by running task with --trace)

--
You received this message because you are subscribed to a topic in the Google Groups "Capistrano" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/capistrano/RMmt-ZHWwfc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to capistrano+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/capistrano/CAN_%2BVLW%2B__NokKz%3Dctg5vT_yDz8RYh-4ZH5bDdbkQqHK97Hncw%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
-------------------------------------------------------------
Joel A. Taylor
(615) 480-2010
http://joelataylor.com

Lee Hambley

unread,
May 6, 2015, 3:40:14 PM5/6/15
to Capistrano

Where do you see download! () documented?

Joel Taylor

unread,
May 6, 2015, 3:41:59 PM5/6/15
to capis...@googlegroups.com

Lee Hambley

unread,
May 6, 2015, 3:44:18 PM5/6/15
to Capistrano

Right, so it should run in an on('someserver') block. Its confusing because run_locally is a legacy API you can also do on(:local) but that's not what you want either.

Joel Taylor

unread,
May 6, 2015, 3:59:21 PM5/6/15
to capis...@googlegroups.com
Ok, so I tried that as well - see the commented out 3rd line. But this doesn't work either, it runs the command on the remote server right? And that's not how SCP works.


For more options, visit https://groups.google.com/d/optout.

Lee Hambley

unread,
May 6, 2015, 4:12:37 PM5/6/15
to Capistrano
You missed a comma, Ruby will implicitly join two strings:

$ irb
irb(main):001:0> puts "i forgot" "how to write docs"
i forgothow to write docs
=> nil

Easy enough mistake to make, if you have the time a download!() example would be very welcome in the SSHKit EXAMPLES.md file. (We covered "upload!" and friends there)

Joel Taylor

unread,
May 6, 2015, 4:15:57 PM5/6/15
to capis...@googlegroups.com
holy crap - it worked. stupid lil' comma! :) I'll for sure add a PR for the docs - happy to contribute. Thanks for your help Lee.


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages