Module: Rye::Cmd
- Included in:
- Box
- Defined in:
- lib/rye/cmd.rb
Overview
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); cmd("/your/special/command", args); end
end
rbox = Rye::Box.new('somehost')
rbox.special # => "special on somehost"
Class Method Summary collapse
-
.add_command(meth, path = nil, *hard_args, &block) ⇒ Object
A helper for adding a command to Rye::Cmd.
- .can?(meth) ⇒ Boolean
-
.remove_command(meth) ⇒ Object
A helper for removing a command from Rye::Cmd.
Instance Method Summary collapse
- #awk(*args) ⇒ Object
- #bash(*args) ⇒ Object
- #bunzip2(*args) ⇒ Object
- #bzip2(*args) ⇒ Object
-
#can ⇒ Object
(also: #commands, #cmds)
Returns an Array of system commands available over SSH.
- #can?(meth) ⇒ Boolean
- #cat(*args) ⇒ Object
- #chmod(*args) ⇒ Object
- #chown(*args) ⇒ Object
- #cmd? ⇒ Boolean
- #command? ⇒ Boolean
- #configure(*args) ⇒ Object
- #cp(*args) ⇒ Object
- #cvs(*args) ⇒ Object
- #date(*args) ⇒ Object
- #df(*args) ⇒ Object
-
#digest_md5(*files) ⇒ Object
-
filesAn Array of file paths Returns an Array of MD5 digests for each of the given files.
-
-
#digest_sha1(*files) ⇒ Object
-
filesAn Array of file paths Returns an Array of SH1 digests for each of the given files.
-
-
#digest_sha2(*files) ⇒ Object
-
filesAn Array of file paths Returns an Array of SH2 digests for each of the given files.
-
- #du(*args) ⇒ Object
- #echo(*args) ⇒ Object
- #env ⇒ Object
-
#file_append(filepath, newcontent, backup = false) ⇒ Object
Append
newcontentto remotefilepath. -
#file_download(*files) ⇒ Object
Transfer files from a machine via Net::SCP.
-
#file_exists?(path) ⇒ Boolean
Does
pathfrom the current working directory?. -
#file_upload(*files) ⇒ Object
Transfer files to a machine via Net::SCP.
-
#file_verified?(path, expected_digest, digest_type = :md5) ⇒ Boolean
Does the calculated digest of
pathmatch the knownexpected_digest? This is useful for verifying downloaded files. - #getconf(*args) ⇒ Object
- #git(*args) ⇒ Object
- #grep(*args) ⇒ Object
- #gunzip(*args) ⇒ Object
- #gzip(*args) ⇒ Object
- #history(*args) ⇒ Object
- #hostname(*args) ⇒ Object
- #ls(*args) ⇒ Object
- #make(*args) ⇒ Object
- #mkdir(*args) ⇒ Object
- #mkfs(*args) ⇒ Object
- #mount(*args) ⇒ Object
- #mv(*args) ⇒ Object
- #perl(*args) ⇒ Object
- #printenv(*args) ⇒ Object
-
#ps(*args) ⇒ Object
def rm(*args); cmd(‘rm’, args); end.
- #pwd(*args) ⇒ Object
- #python(*args) ⇒ Object
- #ruby(*args) ⇒ Object
- #rudy(*args) ⇒ Object
- #rudy_ec2(*args) ⇒ Object
- #rudy_edb(*args) ⇒ Object
- #rudy_s3(*args) ⇒ Object
- #rye(*args) ⇒ Object
- #sed(*args) ⇒ Object
- #sh(*args) ⇒ Object
- #sleep(*args) ⇒ Object
-
#string_download(*files) ⇒ Object
(also: #str_download)
Shorthand for file_download(‘remote/path’).string.
-
#string_upload(str, remote_path) ⇒ Object
(also: #str_upload)
Shorthand for file_upload(StringIO.new(‘file content’), ‘remote/path’).
-
#sudo(*args) ⇒ Object
def kill(*args); cmd(‘kill’, args); end.
- #svn(*args) ⇒ Object
- #tar(*args) ⇒ Object
- #test(*args) ⇒ Object
- #touch(*args) ⇒ Object
- #umount(*args) ⇒ Object
- #uname(*args) ⇒ Object
- #unzip(*args) ⇒ Object
- #uptime(*args) ⇒ Object
- #useradd(*args) ⇒ Object
-
#wc(*args) ⇒ Object
NOTE: See Rye::Box for the implementation of cd def cd(*args); cmd(‘cd’, args); end.
- #which(*args) ⇒ Object
Class Method Details
.add_command(meth, path = nil, *hard_args, &block) ⇒ Object
A helper for adding a command to Rye::Cmd.
-
meththe 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.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/rye/cmd.rb', line 229 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 cmd(path, *local_args) end end end |
.can?(meth) ⇒ Boolean
218 219 220 |
# File 'lib/rye/cmd.rb', line 218 def Cmd.can?(meth) instance_methods.member?(meth) end |
.remove_command(meth) ⇒ Object
A helper for removing a command from Rye::Cmd.
-
meththe method name
249 250 251 |
# File 'lib/rye/cmd.rb', line 249 def Cmd.remove_command(meth) remove_method(meth) end |
Instance Method Details
#awk(*args) ⇒ Object
45 |
# File 'lib/rye/cmd.rb', line 45 def awk(*args); cmd('awk', args); end |
#bash(*args) ⇒ Object
56 |
# File 'lib/rye/cmd.rb', line 56 def bash(*args); cmd('bash', args); end |
#bunzip2(*args) ⇒ Object
79 |
# File 'lib/rye/cmd.rb', line 79 def bunzip2(*args); cmd('bunzip2', args); end |
#bzip2(*args) ⇒ Object
71 |
# File 'lib/rye/cmd.rb', line 71 def bzip2(*args); cmd('bzip2', args); end |
#can ⇒ Object Also known as: commands, cmds
Returns an Array of system commands available over SSH
206 207 208 |
# File 'lib/rye/cmd.rb', line 206 def can Rye::Cmd.instance_methods end |
#can?(meth) ⇒ Boolean
212 213 214 |
# File 'lib/rye/cmd.rb', line 212 def can?(meth) self.can.member?(RUBY_VERSION =~ /1.9/ ? meth.to_sym : meth.to_s) end |
#cat(*args) ⇒ Object
46 |
# File 'lib/rye/cmd.rb', line 46 def cat(*args); cmd('cat', args); end |
#chmod(*args) ⇒ Object
68 |
# File 'lib/rye/cmd.rb', line 68 def chmod(*args); cmd('chmod', args); end |
#chown(*args) ⇒ Object
69 |
# File 'lib/rye/cmd.rb', line 69 def chown(*args); cmd('chown', args); end |
#cmd? ⇒ Boolean
216 217 218 |
# File 'lib/rye/cmd.rb', line 216 def can?(meth) self.can.member?(RUBY_VERSION =~ /1.9/ ? meth.to_sym : meth.to_s) end |
#command? ⇒ Boolean
215 216 217 |
# File 'lib/rye/cmd.rb', line 215 def can?(meth) self.can.member?(RUBY_VERSION =~ /1.9/ ? meth.to_sym : meth.to_s) end |
#configure(*args) ⇒ Object
87 |
# File 'lib/rye/cmd.rb', line 87 def configure(*args); cmd('./configure', args); end |
#cp(*args) ⇒ Object
29 |
# File 'lib/rye/cmd.rb', line 29 def cp(*args); cmd("cp", args); end |
#cvs(*args) ⇒ Object
42 |
# File 'lib/rye/cmd.rb', line 42 def cvs(*args); cmd('cvs', args); end |
#date(*args) ⇒ Object
52 |
# File 'lib/rye/cmd.rb', line 52 def date(*args); cmd('date', args); end |
#df(*args) ⇒ Object
35 |
# File 'lib/rye/cmd.rb', line 35 def df(*args); cmd('df', args); end |
#digest_md5(*files) ⇒ Object
-
filesAn Array of file paths
Returns an Array of MD5 digests for each of the given files
183 184 185 186 187 |
# File 'lib/rye/cmd.rb', line 183 def digest_md5(*files) files.flatten.collect { |file| File.exists?(file) ? Digest::MD5.hexdigest(File.read(file)) : nil } end |
#digest_sha1(*files) ⇒ Object
-
filesAn Array of file paths
Returns an Array of SH1 digests for each of the given files
191 192 193 194 195 |
# File 'lib/rye/cmd.rb', line 191 def digest_sha1(*files) files.flatten.collect { |file| File.exists?(file) ? Digest::SHA1.hexdigest(File.read(file)) : nil } end |
#digest_sha2(*files) ⇒ Object
-
filesAn Array of file paths
Returns an Array of SH2 digests for each of the given files
199 200 201 202 203 |
# File 'lib/rye/cmd.rb', line 199 def digest_sha2(*files) files.flatten.collect { |file| File.exists?(file) ? Digest::SHA2.hexdigest(File.read(file)) : nil } end |
#du(*args) ⇒ Object
36 |
# File 'lib/rye/cmd.rb', line 36 def du(*args); cmd('du', args); end |
#echo(*args) ⇒ Object
57 |
# File 'lib/rye/cmd.rb', line 57 def echo(*args); cmd('echo', args); end |
#env ⇒ Object
38 |
# File 'lib/rye/cmd.rb', line 38 def env; cmd "env"; end |
#file_append(filepath, newcontent, backup = false) ⇒ Object
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.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/rye/cmd.rb', line 135 def file_append(filepath, newcontent, backup=false) if self.file_exists?(filepath) self.cp filepath, "#{filepath}-previous" if backup file_content = self.file_download filepath end file_content ||= StringIO.new if newcontent.is_a?(StringIO) newcontent.rewind file_content.puts newcontent.read else file_content.puts newcontent end self.file_upload file_content, filepath end |
#file_download(*files) ⇒ Object
Transfer files from a machine via Net::SCP.
-
filesis 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.
113 |
# File 'lib/rye/cmd.rb', line 113 def file_download(*files); net_scp_transfer!(:download, *files); end |
#file_exists?(path) ⇒ Boolean
Does path from the current working directory?
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/rye/cmd.rb', line 159 def file_exists?(path) begin ret = self.quietly { ls(path) } rescue Rye::CommandError => 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.stderr.empty? end |
#file_upload(*files) ⇒ Object
Transfer files to a machine via Net::SCP.
-
filesis 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.
100 |
# File 'lib/rye/cmd.rb', line 100 def file_upload(*files); net_scp_transfer!(:upload, *files); end |
#file_verified?(path, expected_digest, digest_type = :md5) ⇒ Boolean
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
173 174 175 176 177 178 179 |
# File 'lib/rye/cmd.rb', line 173 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 |
#getconf(*args) ⇒ Object
80 |
# File 'lib/rye/cmd.rb', line 80 def getconf(*args); cmd('getconf', args); end |
#git(*args) ⇒ Object
43 |
# File 'lib/rye/cmd.rb', line 43 def git(*args); cmd('git', args); end |
#grep(*args) ⇒ Object
51 |
# File 'lib/rye/cmd.rb', line 51 def grep(*args); cmd('grep', args); end |
#gunzip(*args) ⇒ Object
77 |
# File 'lib/rye/cmd.rb', line 77 def gunzip(*args); cmd('gunzip', args); end |
#gzip(*args) ⇒ Object
60 |
# File 'lib/rye/cmd.rb', line 60 def gzip(*args); cmd('gzip', args); end |
#history(*args) ⇒ Object
81 |
# File 'lib/rye/cmd.rb', line 81 def history(*args); cmd('history', args); end |
#hostname(*args) ⇒ Object
84 |
# File 'lib/rye/cmd.rb', line 84 def hostname(*args); cmd('hostname', args); end |
#ls(*args) ⇒ Object
31 |
# File 'lib/rye/cmd.rb', line 31 def ls(*args); cmd('ls', args); end |
#make(*args) ⇒ Object
61 |
# File 'lib/rye/cmd.rb', line 61 def make(*args); cmd('make', args); end |
#mkdir(*args) ⇒ Object
65 |
# File 'lib/rye/cmd.rb', line 65 def mkdir(*args); cmd('mkdir', args); end |
#mkfs(*args) ⇒ Object
59 |
# File 'lib/rye/cmd.rb', line 59 def mkfs(*args); cmd('mkfs', args); end |
#mount(*args) ⇒ Object
63 |
# File 'lib/rye/cmd.rb', line 63 def mount(*args); cmd("mount", args); end |
#mv(*args) ⇒ Object
30 |
# File 'lib/rye/cmd.rb', line 30 def mv(*args); cmd("mv", args); end |
#perl(*args) ⇒ Object
55 |
# File 'lib/rye/cmd.rb', line 55 def perl(*args); cmd('perl', args); end |
#printenv(*args) ⇒ Object
83 |
# File 'lib/rye/cmd.rb', line 83 def printenv(*args); cmd('printenv', args); end |
#ps(*args) ⇒ Object
def rm(*args); cmd(‘rm’, args); end
33 |
# File 'lib/rye/cmd.rb', line 33 def ps(*args); cmd('ps', args); end |
#pwd(*args) ⇒ Object
40 |
# File 'lib/rye/cmd.rb', line 40 def pwd(*args); cmd "pwd", args; end |
#python(*args) ⇒ Object
76 |
# File 'lib/rye/cmd.rb', line 76 def python(*args); cmd('python', args); end |
#ruby(*args) ⇒ Object
53 |
# File 'lib/rye/cmd.rb', line 53 def ruby(*args); cmd('ruby', args); end |
#rudy(*args) ⇒ Object
54 |
# File 'lib/rye/cmd.rb', line 54 def rudy(*args); cmd('rudy', args); end |
#rudy_ec2(*args) ⇒ Object
85 |
# File 'lib/rye/cmd.rb', line 85 def rudy_ec2(*args); cmd('rudy-ec2', args); end |
#rudy_edb(*args) ⇒ Object
86 |
# File 'lib/rye/cmd.rb', line 86 def rudy_edb(*args); cmd('rudy-sdb', args); end |
#rudy_s3(*args) ⇒ Object
82 |
# File 'lib/rye/cmd.rb', line 82 def rudy_s3(*args); cmd('rudy-s3', args); end |
#rye(*args) ⇒ Object
39 |
# File 'lib/rye/cmd.rb', line 39 def rye(*args); cmd "rye", args; end |
#sed(*args) ⇒ Object
44 |
# File 'lib/rye/cmd.rb', line 44 def sed(*args); cmd('sed', args); end |
#sh(*args) ⇒ Object
34 |
# File 'lib/rye/cmd.rb', line 34 def sh(*args); cmd('sh', args); end |
#sleep(*args) ⇒ Object
64 |
# File 'lib/rye/cmd.rb', line 64 def sleep(*args); cmd("sleep", args); end |
#string_download(*files) ⇒ Object Also known as: str_download
Shorthand for file_download(‘remote/path’).string
Returns a String containing the content of all remote files.
118 119 120 |
# File 'lib/rye/cmd.rb', line 118 def string_download(*files) net_scp_transfer!(:download, *files).string end |
#string_upload(str, remote_path) ⇒ Object Also known as: str_upload
Shorthand for file_upload(StringIO.new(‘file content’), ‘remote/path’)
Uploads the content of the String str to remote_path. Returns nil
126 127 128 |
# File 'lib/rye/cmd.rb', line 126 def string_upload(str, remote_path) net_scp_transfer!(:upload, StringIO.new(str), remote_path) end |
#sudo(*args) ⇒ Object
def kill(*args); cmd(‘kill’, args); end
50 |
# File 'lib/rye/cmd.rb', line 50 def sudo(*args); cmd('sudo', args); end |
#svn(*args) ⇒ Object
41 |
# File 'lib/rye/cmd.rb', line 41 def svn(*args); cmd('svn', args); end |
#tar(*args) ⇒ Object
47 |
# File 'lib/rye/cmd.rb', line 47 def tar(*args); cmd('tar', args); end |
#test(*args) ⇒ Object
58 |
# File 'lib/rye/cmd.rb', line 58 def test(*args); cmd('test', args); end |
#touch(*args) ⇒ Object
66 |
# File 'lib/rye/cmd.rb', line 66 def touch(*args); cmd('touch', args); end |
#umount(*args) ⇒ Object
74 |
# File 'lib/rye/cmd.rb', line 74 def umount(*args); cmd("umount", args); end |
#uname(*args) ⇒ Object
67 |
# File 'lib/rye/cmd.rb', line 67 def uname(*args); cmd('uname', args); end |
#unzip(*args) ⇒ Object
70 |
# File 'lib/rye/cmd.rb', line 70 def unzip(*args); cmd('unzip', args); end |
#uptime(*args) ⇒ Object
75 |
# File 'lib/rye/cmd.rb', line 75 def uptime(*args); cmd("uptime", args); end |
#useradd(*args) ⇒ Object
78 |
# File 'lib/rye/cmd.rb', line 78 def useradd(*args); cmd('useradd', args); end |
#wc(*args) ⇒ Object
NOTE: See Rye::Box for the implementation of cd def cd(*args); cmd(‘cd’, args); end
28 |
# File 'lib/rye/cmd.rb', line 28 def wc(*args); cmd('wc', args); end |
#which(*args) ⇒ Object
72 |
# File 'lib/rye/cmd.rb', line 72 def which(*args); cmd('which', args); end |