Skip to content

Instantly share code, notes, and snippets.

@kazuph
Created July 21, 2014 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazuph/693496806f0c94810257 to your computer and use it in GitHub Desktop.
Save kazuph/693496806f0c94810257 to your computer and use it in GitHub Desktop.
capistrano 3 をできるだけシンプルにサーバーにコマンドを流し込むツールとして使いこなす ref: http://qiita.com/kazuph/items/deeaa7d3f9889674d7fe
lock '3.2.1'
set :pty, true
namespace :utils do
task :ls do
on roles(:app) do
execute "ls"
end
end
task :perl_version do
on roles(:app) do
execute %[perl -v]
end
end
end
$ bundle init
$ echo gem "capistrano" >> Gemfile
$ bundle install
$ cap install
$ cap -V
Capistrano Version: 3.2.1 (Rake Version: 10.3.2)
$ bundle exec cap dev utils:perl_version
INFO[444094bb] Running /usr/bin/env perl -v on +HFOWFLWKEJF+WLEHF
DEBUG[444094bb] Command: perl -v
DEBUG[444094bb]
DEBUG[444094bb] This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
DEBUG[444094bb]
DEBUG[444094bb] Copyright 1987-2009, Larry Wall
DEBUG[444094bb]
DEBUG[444094bb] Perl may be copied only under the terms of either the Artistic License or the
DEBUG[444094bb] GNU General Public License, which may be found in the Perl 5 source kit.
DEBUG[444094bb]
DEBUG[444094bb] Complete documentation for Perl, including FAQ lists, should be found on
DEBUG[444094bb] this system using "man perl" or "perldoc perl". If you have access to the
DEBUG[444094bb] Internet, point your browser at http://www.perl.org/, the Perl Home Page.
DEBUG[444094bb]
INFO[444094bb] Finished in 1.874 seconds with exit status 0 (successful).
$ bundle exec cap dev utils:perl_version
INFO[444094bb] Running /usr/bin/env perl -v on LHFWLKEFJWEFJ
DEBUG[444094bb] Command: perl -v
DEBUG[444094bb]
DEBUG[444094bb] This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
DEBUG[444094bb]
DEBUG[444094bb] Copyright 1987-2009, Larry Wall
DEBUG[444094bb]
DEBUG[444094bb] Perl may be copied only under the terms of either the Artistic License or the
DEBUG[444094bb] GNU General Public License, which may be found in the Perl 5 source kit.
DEBUG[444094bb]
DEBUG[444094bb] Complete documentation for Perl, including FAQ lists, should be found on
DEBUG[444094bb] this system using "man perl" or "perldoc perl". If you have access to the
DEBUG[444094bb] Internet, point your browser at http://www.perl.org/, the Perl Home Page.
DEBUG[444094bb]
INFO[444094bb] Finished in 1.874 seconds with exit status 0 (successful).
bundle exec cap dev utils:echo user_name=kazuph
$ bundle exec cap dev utils:echo
Please enter user_name (): kazuph
INFO[62046886] Running /usr/bin/env echo kazuph on example.com
DEBUG[62046886] Command: echo kazuph
INFO[62046886] Finished in 2.086 seconds with exit status 0 (successful).
DEBUG[62046886] kazuph
INFO[62046886] Finished in 2.086 seconds with exit status 0 (successful).
INFO[29ab7cc8] Running /usr/bin/env sudo whoami on +LHKFlkL+FHLEWJF
DEBUG[29ab7cc8] Command: sudo whoami
DEBUG[29ab7cc8] sudo
DEBUG[29ab7cc8] :
DEBUG[29ab7cc8] sudo を実行するには tty がなければいけません。すみません
DEBUG[29ab7cc8]
cap aborted!
$ tree
.
├── Capfile
├── Gemfile
├── Gemfile.lock
├── config
│   ├── deploy
│   │   ├── production.rb
│   │   └── staging.rb
│   └── deploy.rb
└── lib
└── capistrano
└── tasks
# 最初のexecute
/usr/bin/env whoami
# 2つ目のexecute
/usr/bin/env if ! sudo -u fuga whoami > /dev/null; then echo "You cannot switch to user 'fuga' using sudo, please check the sudoers file" 1>&2; false; fi on example.com
└─- config
└── deploy
├── production.rb
├── staging.rb
└── dev.rb
$ cap dev YOUR_TASK_NAME
$ bundle exec cap dev ls
INFO[6a60919c] Running /usr/bin/env ls on example.com
DEBUG[6a60919c] Command: /usr/bin/env ls
DEBUG[6a60919c] <<ここにファイル一覧が表示される>>
INFO[6a60919c] Finished in 1.851 seconds with exit status 0 (successful).
task :upload_dir_all do
on roles(:app) do
execute :mkdir, '-p', '/home/hoge/cap_sample'
upload! '.', '/home/hoge/cap_sample', recursive: true
execute :tree, '-L 2 /home/hoge/cap_sample'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment