module Rye::Cmd

Rye::Cmd

This class contains all of the shell command methods available to an instance of Rye::Box. For security and general safety, Rye only permits this whitelist of commands by default. However, you're free to add methods with mixins.

require 'rye'
module Rye::Cmd
  def special(*args); run_command("/your/special/command", args); end
end

rbox = Rye::Box.new('somehost')
rbox.special        # => "special on somehost"

Public Class Methods

add_command(meth, path=nil, *hard_args, &block) click to toggle source

A helper for adding a command to Rye::Cmd.

  • meth the method name

  • path (optional) filesystem path for the given method

  • hard_args (optional) hardcoded arguments which are prepended to the argument list every time the method is called

An optional block can be provided which will be called instead of calling a system command.

# File lib/rye/cmd.rb, line 344
def Cmd.add_command(meth, path=nil, *hard_args, &block)
  if block
    hard_args.unshift(path) unless path.nil? # Don't lose an argument
    define_method(meth) do |*args|
      local_args = hard_args.clone
      local_args += args
      block.call(*local_args)
    end
  else
    path ||= meth.to_s
    define_method(meth) do |*args|
      local_args = hard_args.clone
      local_args += args
      run_command(path, *local_args)
    end        
  end
end
can?(meth) click to toggle source
# File lib/rye/cmd.rb, line 333
def Cmd.can?(meth)
  instance_methods.member?(meth)
end
remove_command(meth) click to toggle source

A helper for removing a command from Rye::Cmd.

  • meth the method name

# File lib/rye/cmd.rb, line 364
def Cmd.remove_command(meth)
  remove_method(meth)
end

Public Instance Methods

ab(*args) click to toggle source
# File lib/rye/cmd.rb, line 63
def ab(*args) run_command('ab', args) end
aptitude(*args) click to toggle source
# File lib/rye/cmd.rb, line 123
def aptitude(*args) run_command('aptitude', args) end
awk(*args) click to toggle source
# File lib/rye/cmd.rb, line 74
def awk(*args) run_command('awk', args) end
bash(*args, &blk) click to toggle source

When called without a block this will open an interactive shell session.

# File lib/rye/cmd.rb, line 33
def bash(*args, &blk)
  setenv('PS1', "(rye) \\h:\\w \\u\\$\ ")
  __shell 'bash', *args, &blk
end
bunzip2(*args) click to toggle source
# File lib/rye/cmd.rb, line 118
def bunzip2(*args) run_command('bunzip2', args) end
bzip2(*args) click to toggle source
# File lib/rye/cmd.rb, line 105
def bzip2(*args) run_command('bzip2', args) end
can() click to toggle source

Returns an Array of system commands available over SSH

# File lib/rye/cmd.rb, line 321
def can
  Rye::Cmd.instance_methods
end
Also aliased as: commands, cmds
can?(meth) click to toggle source
# File lib/rye/cmd.rb, line 327
def can?(meth)
  self.can.member?(RUBY_VERSION =~ /1.9/ ? meth.to_sym : meth.to_s)
end
Also aliased as: command?, cmd?
cat(*args) click to toggle source
# File lib/rye/cmd.rb, line 75
def cat(*args) run_command('cat', args) end
chmod(*args) click to toggle source
# File lib/rye/cmd.rb, line 102
def chmod(*args) run_command('chmod', args) end
chown(*args) click to toggle source
# File lib/rye/cmd.rb, line 103
def chown(*args) run_command('chown', args) end
cmd?(meth)
Alias for: can?
cmds()
Alias for: can
command?(meth)
Alias for: can?
commands()
Alias for: can
configure(*args) click to toggle source
# File lib/rye/cmd.rb, line 130
def configure(*args) run_command('./configure', args) end
cp(*args) click to toggle source
# File lib/rye/cmd.rb, line 55
def cp(*args) run_command("cp", args) end
curl(*args) click to toggle source
# File lib/rye/cmd.rb, line 92
def curl(*args) run_command('curl', args) end
cvs(*args) click to toggle source
# File lib/rye/cmd.rb, line 71
def cvs(*args) run_command('cvs', args) end
date(*args) click to toggle source
# File lib/rye/cmd.rb, line 82
def date(*args) run_command('date', args) end
df(*args) click to toggle source
# File lib/rye/cmd.rb, line 59
def df(*args) run_command('df', args) end
digest_md5(*files) click to toggle source
  • files An Array of file paths

Returns an Array of MD5 digests for each of the given files

# File lib/rye/cmd.rb, line 298
def digest_md5(*files)
  files.flatten.collect { |file| 
    File.exists?(file) ? Digest::MD5.hexdigest(File.read(file)) : nil
  }
end
digest_sha1(*files) click to toggle source
  • files An Array of file paths

Returns an Array of SH1 digests for each of the given files

# File lib/rye/cmd.rb, line 306
def digest_sha1(*files)
  files.flatten.collect { |file| 
    File.exists?(file) ? Digest::SHA1.hexdigest(File.read(file)) : nil
  }
end
digest_sha2(*files) click to toggle source
  • files An Array of file paths

Returns an Array of SH2 digests for each of the given files

# File lib/rye/cmd.rb, line 314
def digest_sha2(*files)
  files.flatten.collect { |file| 
    File.exists?(file) ? Digest::SHA2.hexdigest(File.read(file)) : nil
  }
end
dir(*args) click to toggle source
# File lib/rye/cmd.rb, line 135
def dir(*args); run_command('cmd', args); end
dir_download(*paths) click to toggle source

Same as #file_download except directories are processed recursively. If any supplied paths are directories you need to use this method and not file_download.

# File lib/rye/cmd.rb, line 172
def dir_download(*paths); net_scp_transfer!(:download, true, *paths); end
Also aliased as: directory_download
dir_upload(*paths) click to toggle source

Same as #file_upload except directories are processed recursively. If any supplied paths are directories you need to use this method and not file_upload.

# File lib/rye/cmd.rb, line 166
def dir_upload(*paths); net_scp_transfer!(:upload, true, *paths); end
Also aliased as: directory_upload
directory_download(*paths)
Alias for: dir_download
directory_upload(*paths)
Alias for: dir_upload
dpkg(*args) click to toggle source
# File lib/rye/cmd.rb, line 93
def dpkg(*args) run_command('dpkg', args) end
du(*args) click to toggle source
# File lib/rye/cmd.rb, line 60
def du(*args) run_command('du', args) end
echo(*args) click to toggle source
# File lib/rye/cmd.rb, line 86
def echo(*args) run_command('echo', args) end
env() click to toggle source
# File lib/rye/cmd.rb, line 67
def env()      run_command('env')       end
file_append(filepath, newcontent, backup=false) click to toggle source

Append newcontent to remote filepath. If the file doesn't exist it will be created. If backup is specified, filepath will be copied to filepath-previous before appending.

NOTE: Not recommended for large files. It downloads the contents.

# File lib/rye/cmd.rb, line 204
def file_append(filepath, newcontent, backup=false)
  content = StringIO.new
  
  if self.file_exists?(filepath)
    self.cp filepath, "#{filepath}-previous" if backup
    content = self.file_download filepath
  end
  
  if newcontent.is_a?(StringIO)
    newcontent.rewind
    content.puts newcontent.read
  else
    content.puts newcontent
  end
  
  self.file_upload content, filepath
end
file_download(*paths) click to toggle source

Transfer files from a machine via Net::SCP.

  • paths is an Array of files to download. The last element must be the

local directory to download to. If downloading a single file the last element can be a file path. The target can also be a StringIO object. The target directory will be created if it does not exist, but only when multiple files are being transferred. This method will fail early if there are obvious problems with the input parameters. An exception is raised and no files are transferred. Return nil or a StringIO object, if specified as the target.

NOTE: Changes to current working directory with cd or +[]+ are ignored.

# File lib/rye/cmd.rb, line 161
def file_download(*paths); net_scp_transfer!(:download, false, *paths); end
file_exists?(path) click to toggle source

Does path from the current working directory?

# File lib/rye/cmd.rb, line 274
def file_exists?(path)
  begin
    ret = self.quietly { ls(path) }
  rescue Rye::Err => ex
    ret = ex.rap
  end
  # "ls" returns a 0 exit code regardless of success in Linux
  # But on OSX exit code is 1. This is why we look at STDERR. 
  !(ret.exit_status > 0) || ret.stderr.empty?
end
file_modify(filepath, regexp, replace=nil) click to toggle source
# File lib/rye/cmd.rb, line 268
def file_modify(filepath, regexp, replace=nil)
  raise "File not found: #{filepath}" unless file_exists?(filepath)
  sed :i, :r, "s/#{regexp}/#{replace}/", filepath
end
file_upload(*paths) click to toggle source

Transfer files to a machine via Net::SCP.

  • paths is an Array of files to upload. The last element is the

directory to upload to. If uploading a single file, the last element can be a file path. The list of files can also include StringIO objects. The target directory will be created if it does not exist, but only when multiple files are being transferred. This method will fail early if there are obvious problems with the input parameters. An exception is raised and no files are transferred. Always return nil.

NOTE: Changes to current working directory with cd or +[]+ are ignored.

# File lib/rye/cmd.rb, line 148
def file_upload(*paths); net_scp_transfer!(:upload, false, *paths); end
file_verified?(path, expected_digest, digest_type=:md5) click to toggle source

Does the calculated digest of path match the known expected_digest? This is useful for verifying downloaded files. digest_type must be one of: :md5, :sha1, :sha2

# File lib/rye/cmd.rb, line 288
def file_verified?(path, expected_digest, digest_type=:md5)
  return false unless file_exists?(path)
  raise "Unknown disgest type: #{digest_type}" unless can?("digest_#{digest_type}")
  digest = self.send("digest_#{digest_type}", path).first
  info "#{digest_type} (#{path}) = #{digest}"
  digest.to_s == expected_digest.to_s
end
file_write(filepath, newcontent, backup=false) click to toggle source

Write newcontent to remote filepath. If the file exists it will be overwritten. If backup is specified, filepath will be copied to filepath-previous before appending.

# File lib/rye/cmd.rb, line 225
def file_write(filepath, newcontent, backup=false)
  if self.file_exists?(filepath)
    self.cp filepath, "#{filepath}-previous" if backup
  end
  
  content = StringIO.new
  content.puts newcontent
  self.file_upload content, filepath
end
getconf(*args) click to toggle source
# File lib/rye/cmd.rb, line 119
def getconf(*args) run_command('getconf', args) end
git(*args) click to toggle source
# File lib/rye/cmd.rb, line 72
def git(*args) run_command('git', args) end
grep(*args) click to toggle source
# File lib/rye/cmd.rb, line 81
def grep(*args) run_command('grep', args) end
gunzip(*args) click to toggle source
# File lib/rye/cmd.rb, line 114
def gunzip(*args) run_command('gunzip', args) end
gzip(*args) click to toggle source
# File lib/rye/cmd.rb, line 89
def gzip(*args) run_command('gzip', args) end
hg(*args) click to toggle source
# File lib/rye/cmd.rb, line 64
def hg(*args) run_command('hg', args) end
history(*args) click to toggle source
# File lib/rye/cmd.rb, line 120
def history(*args) run_command('history', args) end
hostname(*args) click to toggle source
# File lib/rye/cmd.rb, line 125
def hostname(*args) run_command('hostname', args) end
irb(*args, &blk) click to toggle source

When called without a block this will open an interactive shell session.

# File lib/rye/cmd.rb, line 40
def irb(*args, &blk)
    __shell 'irb', *args, &blk
end
ldconfig(*args) click to toggle source
# File lib/rye/cmd.rb, line 126
def ldconfig(*args) run_command('ldconfig', args) end
ln(*args) click to toggle source
# File lib/rye/cmd.rb, line 62
def ln(*args) run_command('ln', args) end
ls(*args) click to toggle source
# File lib/rye/cmd.rb, line 57
def ls(*args) run_command('ls', args) end
make(*args) click to toggle source
# File lib/rye/cmd.rb, line 90
def make(*args) run_command('make', args) end
mkdir(*args) click to toggle source
# File lib/rye/cmd.rb, line 99
def mkdir(*args) run_command('mkdir', args) end
mkfs(*args) click to toggle source
# File lib/rye/cmd.rb, line 88
def mkfs(*args) run_command('mkfs', args) end
mount(*args) click to toggle source
# File lib/rye/cmd.rb, line 97
def mount(*args) run_command("mount", args) end
mv(*args) click to toggle source
# File lib/rye/cmd.rb, line 56
def mv(*args) run_command("mv", args) end
perl(*args) click to toggle source
# File lib/rye/cmd.rb, line 84
def perl(*args) run_command('perl', args) end
printenv(*args) click to toggle source
# File lib/rye/cmd.rb, line 124
def printenv(*args) run_command('printenv', args) end
ps(*args) click to toggle source
# File lib/rye/cmd.rb, line 58
def ps(*args) run_command('ps', args) end
pwd(*args) click to toggle source
# File lib/rye/cmd.rb, line 69
def pwd(*args) run_command('pwd', args) end
python(*args) click to toggle source
# File lib/rye/cmd.rb, line 113
def python(*args) run_command('python', args) end
rake(*args) click to toggle source

def kill(*args) run_command('kill', args) end

# File lib/rye/cmd.rb, line 80
def rake(*args) run_command('rake', args) end
ruby(*args) click to toggle source
# File lib/rye/cmd.rb, line 85
def ruby(*args) run_command('ruby', args) end
rudy(*args) click to toggle source
# File lib/rye/cmd.rb, line 83
def rudy(*args) run_command('rudy', args) end
rudy_ec2(*args) click to toggle source
# File lib/rye/cmd.rb, line 127
def rudy_ec2(*args) run_command('rudy-ec2', args) end
rudy_s3(*args) click to toggle source
# File lib/rye/cmd.rb, line 121
def rudy_s3(*args) run_command('rudy-s3', args) end
rudy_sdb(*args) click to toggle source
# File lib/rye/cmd.rb, line 128
def rudy_sdb(*args) run_command('rudy-sdb', args) end
rye(*args) click to toggle source
# File lib/rye/cmd.rb, line 68
def rye(*args) run_command('rye', args) end
sed(*args) click to toggle source
# File lib/rye/cmd.rb, line 73
def sed(*args) run_command('sed', args) end
sh(*args, &blk) click to toggle source

When called without a block this will open an interactive shell session.

# File lib/rye/cmd.rb, line 46
def sh(*args, &blk)
  setenv('PS1', "(rye) $\ ")
  __shell 'sh', *args, &blk
end
siege(*args) click to toggle source
# File lib/rye/cmd.rb, line 107
def siege(*args) run_command("siege", args) end
sleep(*args) click to toggle source
# File lib/rye/cmd.rb, line 98
def sleep(*args) run_command("sleep", args) end
stella(*args) click to toggle source
# File lib/rye/cmd.rb, line 109
def stella(*args) run_command("stella", args) end
str_download(*paths)
Alias for: string_download
str_upload(str, remote_path)
Alias for: string_upload
string_append(filepath, newcontent, backup=false) click to toggle source

Shorthand for +file_append('remote/path', StringIO.new('file content'))+

Appends the content of the String str to remote_path. Returns nil

# File lib/rye/cmd.rb, line 194
def string_append(filepath, newcontent, backup=false)
  file_append(remote_path, StringIO.new(str), backup)
end
string_download(*paths) click to toggle source

Shorthand for +file_download('remote/path').string+

Returns a String containing the content of all remote paths.

# File lib/rye/cmd.rb, line 178
def string_download(*paths)
  net_scp_transfer!(:download, false, *paths).string
end
Also aliased as: str_download
string_upload(str, remote_path) click to toggle source

Shorthand for +file_upload(StringIO.new('file content'), 'remote/path')+

Uploads the content of the String str to remote_path. Returns nil

# File lib/rye/cmd.rb, line 186
def string_upload(str, remote_path)
  net_scp_transfer!(:upload, false, StringIO.new(str), remote_path)
end
Also aliased as: str_upload, str_upload
su(*args) click to toggle source
# File lib/rye/cmd.rb, line 61
def su(*args) run_command('su', args) end
svn(*args) click to toggle source
# File lib/rye/cmd.rb, line 70
def svn(*args) run_command('svn', args) end
tail(*args) click to toggle source
# File lib/rye/cmd.rb, line 94
def tail(*args) run_command('tail', args) end
tar(*args) click to toggle source
# File lib/rye/cmd.rb, line 76
def tar(*args) run_command('tar', args) end
template_upload(*paths) click to toggle source

Parse a template and upload that as a file to remote_path.

# File lib/rye/cmd.rb, line 241
def template_upload(*paths)
  remote_path = paths.pop
  templates = []
  paths.collect! do |path|      
    if StringIO === path
      path.rewind
      template = Rye::Tpl.new(path.read, "inline-template")
    elsif String === path
      raise "No such file: #{Dir.pwd}/#{path}" unless File.exists?(path)
      template = Rye::Tpl.new(File.read(path), File.basename(path))
    end
    template.result!(binding)
    templates << template
    template.path
  end
  paths << remote_path
  ret = self.file_upload *paths
  templates.each { |template| 
    tmp_path = File.join(remote_path, File.basename(template.path))
    if file_exists?(tmp_path)
      mv tmp_path, File.join(remote_path, template.basename)
    end
    template.delete 
  }
  ret
end
template_write(filepath, template) click to toggle source
# File lib/rye/cmd.rb, line 236
def template_write(filepath, template)
  template_upload template, filepath
end
test(*args) click to toggle source
# File lib/rye/cmd.rb, line 87
def test(*args) run_command('test', args) end
touch(*args) click to toggle source
# File lib/rye/cmd.rb, line 100
def touch(*args) run_command('touch', args) end
try(*args) click to toggle source
# File lib/rye/cmd.rb, line 77
def try(*args) run_command('tar', args) end
umount(*args) click to toggle source
# File lib/rye/cmd.rb, line 110
def umount(*args) run_command("umount", args) end
uname(*args) click to toggle source
# File lib/rye/cmd.rb, line 101
def uname(*args) run_command('uname', args) end
unxz(*args) click to toggle source
# File lib/rye/cmd.rb, line 95
def unxz(*args) run_command('unxz', args) end
unzip(*args) click to toggle source
# File lib/rye/cmd.rb, line 104
def unzip(*args) run_command('unzip', args) end
uptime(*args) click to toggle source
# File lib/rye/cmd.rb, line 112
def uptime(*args) run_command("uptime", args) end
useradd(*args) click to toggle source
# File lib/rye/cmd.rb, line 117
def useradd(*args) run_command('useradd', args) end
wc(*args) click to toggle source
NOTE: See Rye::Box for the implementation of cd

def cd(*args) run_command('cd', args) end def rm(*args) run_command('rm', args) end

# File lib/rye/cmd.rb, line 54
def wc(*args) run_command('wc', args) end
wget(*args) click to toggle source
# File lib/rye/cmd.rb, line 91
def wget(*args) run_command('wget', args) end
which(*args) click to toggle source
# File lib/rye/cmd.rb, line 106
def which(*args) run_command('which', args) end
whoami(*args) click to toggle source
# File lib/rye/cmd.rb, line 115
def whoami(*args) run_command('whoami', args) end
xz(*args) click to toggle source
# File lib/rye/cmd.rb, line 65
def xz(*args) run_command('xz', args) end

Private Instance Methods

__shell(cmd, *args, &blk) click to toggle source
# File lib/rye/cmd.rb, line 23
def __shell(cmd, *args, &blk)
  self.rye_shell = true
  rap = run_command cmd, *args, &blk
  self.rye_shell = false
  rap
end