Capistrano : SSHKit : Introduction

SSHKit is an amazing tool to run any commands on your VPS or docker images as any user. Capistrano uses it to all the server related stuffs. In other words you can build your own deployer software or server maintenance tools using SSHKit.

$ gem install sshkit

Simple Introduction

Lets create a ruby script to create a file in my VPS’s home directory

require 'sshkit'
require 'sshkit/dsl'
on ['deployer@4X.79.XXX.XX1'], in: :sequence, wait: 1 do |host|
  within '~' do # or '/home/deployer/'
    as :deployer do
      execute 'echo', 'My name is shiva > shiva.txt'
    end
  end
end

Go and check in your VPS, you see the file

Run a command as a different user

 

Some examples

run_locally do
  within '~/projects/jc' do
    execute :touch, 'new_file.rb'
    info 'File successfully created'
  end
end

Output

INFO [e4df5c2e] Running /usr/bin/env touch new_file.rb as john@localhost
INFO [e4df5c2e] Finished in 0.003 seconds with exit status 0 (successful).
INFO File successfully created
 => #<IO:<STDOUT>>

Sources:

https://github.com/capistrano/sshkit/blob/master/EXAMPLES.md

Capistrano Variables

2 thoughts on “Capistrano : SSHKit : Introduction

  1. Jason B says:

    You’re a life saver! Thanks so much for this post! I lost two days of my life trying to get a bash command to run properly in Cap3 and this post was the one that made the difference

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s