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"
Methods
Public Class
Public Instance
- ab
- aptitude
- awk
- bash
- bunzip2
- bzip2
- can
- can?
- cat
- chmod
- chown
- configure
- cp
- curl
- cvs
- date
- df
- digest_md5
- digest_sha1
- digest_sha2
- dir
- dir_download
- dir_upload
- dpkg
- du
- echo
- env
- file_append
- file_download
- file_exists?
- file_modify
- file_upload
- file_verified?
- file_write
- getconf
- git
- grep
- gunzip
- gzip
- hg
- history
- hostname
- irb
- ldconfig
- ln
- ls
- make
- mkdir
- mkfs
- mount
- mv
- perl
- printenv
- ps
- pwd
- python
- rake
- ruby
- rudy
- rudy_ec2
- rudy_s3
- rudy_sdb
- rye
- sed
- sh
- siege
- sleep
- stella
- string_append
- string_download
- string_upload
- su
- svn
- tail
- tar
- template_upload
- template_write
- test
- touch
- try
- umount
- uname
- unxz
- unzip
- uptime
- useradd
- wc
- wget
- which
- whoami
- xz
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
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
# File lib/rye/cmd.rb, line 333 def Cmd.can?(meth) instance_methods.member?(meth) end
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
# File lib/rye/cmd.rb, line 123 def aptitude(*args) run_command('aptitude', args) end
# File lib/rye/cmd.rb, line 74 def awk(*args) run_command('awk', args) end
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
# File lib/rye/cmd.rb, line 118 def bunzip2(*args) run_command('bunzip2', args) end
# File lib/rye/cmd.rb, line 105 def bzip2(*args) run_command('bzip2', args) end
Returns an Array of system commands available over SSH
# File lib/rye/cmd.rb, line 321 def can Rye::Cmd.instance_methods end
# File lib/rye/cmd.rb, line 327 def can?(meth) self.can.member?(RUBY_VERSION =~ /1.9/ ? meth.to_sym : meth.to_s) end
# File lib/rye/cmd.rb, line 75 def cat(*args) run_command('cat', args) end
# File lib/rye/cmd.rb, line 102 def chmod(*args) run_command('chmod', args) end
# File lib/rye/cmd.rb, line 103 def chown(*args) run_command('chown', args) end
# File lib/rye/cmd.rb, line 130 def configure(*args) run_command('./configure', args) end
# File lib/rye/cmd.rb, line 92 def curl(*args) run_command('curl', args) end
# File lib/rye/cmd.rb, line 71 def cvs(*args) run_command('cvs', args) end
# File lib/rye/cmd.rb, line 82 def date(*args) run_command('date', args) end
-
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
-
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
-
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
# File lib/rye/cmd.rb, line 135 def dir(*args); run_command('cmd', args); end
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
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
# File lib/rye/cmd.rb, line 93 def dpkg(*args) run_command('dpkg', args) end
# File lib/rye/cmd.rb, line 86 def echo(*args) run_command('echo', args) end
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
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
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 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
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
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
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
# File lib/rye/cmd.rb, line 119 def getconf(*args) run_command('getconf', args) end
# File lib/rye/cmd.rb, line 72 def git(*args) run_command('git', args) end
# File lib/rye/cmd.rb, line 81 def grep(*args) run_command('grep', args) end
# File lib/rye/cmd.rb, line 114 def gunzip(*args) run_command('gunzip', args) end
# File lib/rye/cmd.rb, line 89 def gzip(*args) run_command('gzip', args) end
# File lib/rye/cmd.rb, line 120 def history(*args) run_command('history', args) end
# File lib/rye/cmd.rb, line 125 def hostname(*args) run_command('hostname', args) end
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
# File lib/rye/cmd.rb, line 126 def ldconfig(*args) run_command('ldconfig', args) end
# File lib/rye/cmd.rb, line 90 def make(*args) run_command('make', args) end
# File lib/rye/cmd.rb, line 99 def mkdir(*args) run_command('mkdir', args) end
# File lib/rye/cmd.rb, line 88 def mkfs(*args) run_command('mkfs', args) end
# File lib/rye/cmd.rb, line 97 def mount(*args) run_command("mount", args) end
# File lib/rye/cmd.rb, line 84 def perl(*args) run_command('perl', args) end
# File lib/rye/cmd.rb, line 124 def printenv(*args) run_command('printenv', args) end
# File lib/rye/cmd.rb, line 69 def pwd(*args) run_command('pwd', args) end
# File lib/rye/cmd.rb, line 113 def python(*args) run_command('python', args) end
def kill(*args) run_command('kill', args) end
# File lib/rye/cmd.rb, line 80 def rake(*args) run_command('rake', args) end
# File lib/rye/cmd.rb, line 85 def ruby(*args) run_command('ruby', args) end
# File lib/rye/cmd.rb, line 83 def rudy(*args) run_command('rudy', args) end
# File lib/rye/cmd.rb, line 127 def rudy_ec2(*args) run_command('rudy-ec2', args) end
# File lib/rye/cmd.rb, line 121 def rudy_s3(*args) run_command('rudy-s3', args) end
# File lib/rye/cmd.rb, line 128 def rudy_sdb(*args) run_command('rudy-sdb', args) end
# File lib/rye/cmd.rb, line 68 def rye(*args) run_command('rye', args) end
# File lib/rye/cmd.rb, line 73 def sed(*args) run_command('sed', args) end
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
# File lib/rye/cmd.rb, line 107 def siege(*args) run_command("siege", args) end
# File lib/rye/cmd.rb, line 98 def sleep(*args) run_command("sleep", args) end
# File lib/rye/cmd.rb, line 109 def stella(*args) run_command("stella", args) end
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
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, *paths).string end
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, StringIO.new(str), remote_path) end
# File lib/rye/cmd.rb, line 70 def svn(*args) run_command('svn', args) end
# File lib/rye/cmd.rb, line 94 def tail(*args) run_command('tail', args) end
# File lib/rye/cmd.rb, line 76 def tar(*args) run_command('tar', args) end
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
# File lib/rye/cmd.rb, line 236 def template_write(filepath, template) template_upload template, filepath end
# File lib/rye/cmd.rb, line 87 def test(*args) run_command('test', args) end
# File lib/rye/cmd.rb, line 100 def touch(*args) run_command('touch', args) end
# File lib/rye/cmd.rb, line 77 def try(*args) run_command('tar', args) end
# File lib/rye/cmd.rb, line 110 def umount(*args) run_command("umount", args) end
# File lib/rye/cmd.rb, line 101 def uname(*args) run_command('uname', args) end
# File lib/rye/cmd.rb, line 95 def unxz(*args) run_command('unxz', args) end
# File lib/rye/cmd.rb, line 104 def unzip(*args) run_command('unzip', args) end
# File lib/rye/cmd.rb, line 112 def uptime(*args) run_command("uptime", args) end
# File lib/rye/cmd.rb, line 117 def useradd(*args) run_command('useradd', args) end
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
# File lib/rye/cmd.rb, line 91 def wget(*args) run_command('wget', args) end
# File lib/rye/cmd.rb, line 106 def which(*args) run_command('which', args) end
# File lib/rye/cmd.rb, line 115 def whoami(*args) run_command('whoami', args) end