module Rye::Cmd
  1. lib/rye/cmd.rb
Parent: Rye

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::Box::Cmd
  def special(*args); run_command("/your/special/command", args); end
end
rbox = Rye::Box.new('somehost')
rbox.special        # => "special on somehost"

Public Instance Aliases

cmd? -> can?
cmds -> can
command? -> can?
commands -> can
directory_download -> dir_download
directory_upload -> dir_upload
str_download -> string_download
str_upload -> string_upload

Public Class methods

add_command (meth, path=nil, *hard_args, &block)

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.

[show source]
# 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)
[show source]
# File lib/rye/cmd.rb, line 333
def Cmd.can?(meth)
  instance_methods.member?(meth)
end
remove_command (meth)

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

  • meth the method name

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

Public Instance methods

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

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

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

Returns an Array of system commands available over SSH

[show source]
# File lib/rye/cmd.rb, line 321
def can
  Rye::Cmd.instance_methods
end
can? (meth)
[show 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
cat (*args)
[show source]
# File lib/rye/cmd.rb, line 75
def cat(*args) run_command('cat', args) end
chmod (*args)
[show source]
# File lib/rye/cmd.rb, line 102
def chmod(*args) run_command('chmod', args) end
chown (*args)
[show source]
# File lib/rye/cmd.rb, line 103
def chown(*args) run_command('chown', args) end
configure (*args)
[show source]
# File lib/rye/cmd.rb, line 130
def configure(*args) run_command('./configure', args) end
cp (*args)
[show source]
# File lib/rye/cmd.rb, line 55
def cp(*args) run_command("cp", args) end
curl (*args)
[show source]
# File lib/rye/cmd.rb, line 92
def curl(*args) run_command('curl', args) end
cvs (*args)
[show source]
# File lib/rye/cmd.rb, line 71
def cvs(*args) run_command('cvs', args) end
date (*args)
[show source]
# File lib/rye/cmd.rb, line 82
def date(*args) run_command('date', args) end
df (*args)
[show source]
# File lib/rye/cmd.rb, line 59
def df(*args) run_command('df', args) end
digest_md5 (*files)
  • files An Array of file paths

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

[show source]
# 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)
  • files An Array of file paths

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

[show source]
# 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)
  • files An Array of file paths

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

[show source]
# 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)
[show source]
# File lib/rye/cmd.rb, line 135
def dir(*args); run_command('cmd', args); end
dir_download (*paths)

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.

[show source]
# File lib/rye/cmd.rb, line 172
def dir_download(*paths); net_scp_transfer!(:download, true, *paths); end
dir_upload (*paths)

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.

[show source]
# File lib/rye/cmd.rb, line 166
def dir_upload(*paths); net_scp_transfer!(:upload, true, *paths); end
dpkg (*args)
[show source]
# File lib/rye/cmd.rb, line 93
def dpkg(*args) run_command('dpkg', args) end
du (*args)
[show source]
# File lib/rye/cmd.rb, line 60
def du(*args) run_command('du', args) end
echo (*args)
[show source]
# File lib/rye/cmd.rb, line 86
def echo(*args) run_command('echo', args) end
env ()
[show source]
# File lib/rye/cmd.rb, line 67
def env()      run_command('env')       end
file_append (filepath, newcontent, backup=false)

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.

[show source]
# 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)

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.

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

Does path from the current working directory?

[show source]
# 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)
[show 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)

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.

[show source]
# 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)

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

[show source]
# 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)

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.

[show source]
# 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)
[show source]
# File lib/rye/cmd.rb, line 119
def getconf(*args) run_command('getconf', args) end
git (*args)
[show source]
# File lib/rye/cmd.rb, line 72
def git(*args) run_command('git', args) end
grep (*args)
[show source]
# File lib/rye/cmd.rb, line 81
def grep(*args) run_command('grep', args) end
gunzip (*args)
[show source]
# File lib/rye/cmd.rb, line 114
def gunzip(*args) run_command('gunzip', args) end
gzip (*args)
[show source]
# File lib/rye/cmd.rb, line 89
def gzip(*args) run_command('gzip', args) end
hg (*args)
[show source]
# File lib/rye/cmd.rb, line 64
def hg(*args) run_command('hg', args) end
history (*args)
[show source]
# File lib/rye/cmd.rb, line 120
def history(*args) run_command('history', args) end
hostname (*args)
[show source]
# File lib/rye/cmd.rb, line 125
def hostname(*args) run_command('hostname', args) end
irb (*args, &blk)

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

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

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

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

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

[show source]
# File lib/rye/cmd.rb, line 46
def sh(*args, &blk)
  setenv('PS1', "(rye) $\ ")
  __shell 'sh', *args, &blk
end
siege (*args)
[show source]
# File lib/rye/cmd.rb, line 107
def siege(*args) run_command("siege", args) end
sleep (*args)
[show source]
# File lib/rye/cmd.rb, line 98
def sleep(*args) run_command("sleep", args) end
stella (*args)
[show source]
# File lib/rye/cmd.rb, line 109
def stella(*args) run_command("stella", args) end
string_append (filepath, newcontent, backup=false)

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

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

[show source]
# 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)

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

Returns a String containing the content of all remote paths.

[show source]
# File lib/rye/cmd.rb, line 178
def string_download(*paths)
  net_scp_transfer!(:download, *paths).string
end
string_upload (str, remote_path)

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

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

[show source]
# File lib/rye/cmd.rb, line 186
def string_upload(str, remote_path)
  net_scp_transfer!(:upload, StringIO.new(str), remote_path)
end
su (*args)
[show source]
# File lib/rye/cmd.rb, line 61
def su(*args) run_command('su', args) end
svn (*args)
[show source]
# File lib/rye/cmd.rb, line 70
def svn(*args) run_command('svn', args) end
tail (*args)
[show source]
# File lib/rye/cmd.rb, line 94
def tail(*args) run_command('tail', args) end
tar (*args)
[show source]
# File lib/rye/cmd.rb, line 76
def tar(*args) run_command('tar', args) end
template_upload (*paths)

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

[show source]
# 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)
[show source]
# File lib/rye/cmd.rb, line 236
def template_write(filepath, template)
  template_upload template, filepath
end
test (*args)
[show source]
# File lib/rye/cmd.rb, line 87
def test(*args) run_command('test', args) end
touch (*args)
[show source]
# File lib/rye/cmd.rb, line 100
def touch(*args) run_command('touch', args) end
try (*args)
[show source]
# File lib/rye/cmd.rb, line 77
def try(*args) run_command('tar', args) end
umount (*args)
[show source]
# File lib/rye/cmd.rb, line 110
def umount(*args) run_command("umount", args) end
uname (*args)
[show source]
# File lib/rye/cmd.rb, line 101
def uname(*args) run_command('uname', args) end
unxz (*args)
[show source]
# File lib/rye/cmd.rb, line 95
def unxz(*args) run_command('unxz', args) end
unzip (*args)
[show source]
# File lib/rye/cmd.rb, line 104
def unzip(*args) run_command('unzip', args) end
uptime (*args)
[show source]
# File lib/rye/cmd.rb, line 112
def uptime(*args) run_command("uptime", args) end
useradd (*args)
[show source]
# File lib/rye/cmd.rb, line 117
def useradd(*args) run_command('useradd', args) end
wc (*args)
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

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