Ruby sshkit
From wikinotes
SSHKit is a ruby framework for executing SSH commands.
It uses a HashMap of available commands/absolute-paths on the server.
Generally, the available commandmap is extended to ruby capistrano in capistrano plugins (rather than at the top of your project).
Documentation
github https://github.com/capistrano/sshkit cookbook https://github.com/capistrano/sshkit/blob/master/EXAMPLES.md api docs https://www.rubydoc.info/github/capistrano/sshkit/toplevel
Example
require 'sshkit' require 'sshkit/dsl' include SSHKit::DSL on ["docs.example.com", "api.example.com"] do |fqdn| # on this server within "/usr/local/www/#{fqdn}" do # in this dir execute(:rake, "assets:precompile") # execute command execute(:rails, "runner") end end
Configuration
CommandMap
When you pass a symbol as the first param to
execute()
,
the command is looked up in the commandmap.
The commandmap should contain a mapping of executables to their absolute path on the server, or a${PATH}
environment variable to use in.SSHKit.config.command_map[:rake] = "/usr/local/rbenv/shims/rake" SSHKit.config.command_map.prefix[:rake].push("bundle exec") # only executables in '/usr/local/rbenv/shims/*' are available SSHKit.config.command_map = Hash.new do |hash, command| hash[command] = "/usr/local/rbenv/shims/#{command}" end
Syntax
Where/How
on(["host1.com", "host2.com"], in: :parallel) # command on remote server (and sequence/parallel/...) as(user: "me", group: "wheel") { ... } # command as user/group within("/var/tmp") { ... } # command in directory with(VARIABLE: "VALUE") { ... } # command with environment variableExecute
execute(:cp, '/var/tmp/a.txt', '/var/tmp/b.txt') capture(:ls, '-l') upload!("/var/tmp/foo.txt", "/usr/local/www/app/foo.txt") download!("/var/tmp/foo.txt", "/usr/local/www/app/foo.txt")