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.
-
-
#dir_download(*paths) ⇒ Object
(also: #directory_download)
Same as file_download except directories are processed recursively.
-
#dir_upload(*paths) ⇒ Object
(also: #directory_upload)
Same as file_upload except directories are processed recursively.
- #du(*args) ⇒ Object
- #echo(*args) ⇒ Object
- #env ⇒ Object
-
#file_append(filepath, newcontent, backup = false) ⇒ Object
Append
newcontentto remotefilepath. -
#file_download(*paths) ⇒ Object
Transfer files from a machine via Net::SCP.
-
#file_exists?(path) ⇒ Boolean
Does
pathfrom the current working directory?. -
#file_upload(*paths) ⇒ 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
- #pwd(*args) ⇒ Object
- #python(*args) ⇒ Object
-
#rake(*args) ⇒ Object
def kill(*args); cmd(‘kill’, args); end.
- #ruby(*args) ⇒ Object
- #rudy(*args) ⇒ Object
- #rudy_ec2(*args) ⇒ Object
- #rudy_s3(*args) ⇒ Object
- #rudy_sdb(*args) ⇒ Object
- #rye(*args) ⇒ Object
- #sed(*args) ⇒ Object
- #sh(*args) ⇒ Object
- #sleep(*args) ⇒ Object
-
#string_download(*paths) ⇒ 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
- #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 def rm(*args); cmd(‘rm’, 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.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/rye/cmd.rb', line 242 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
231 232 233 |
# File 'lib/rye/cmd.rb', line 231 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
262 263 264 |
# File 'lib/rye/cmd.rb', line 262 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
57 |
# File 'lib/rye/cmd.rb', line 57 def bash(*args); cmd('bash', args); end |
#bunzip2(*args) ⇒ Object
80 |
# File 'lib/rye/cmd.rb', line 80 def bunzip2(*args); cmd('bunzip2', args); end |
#bzip2(*args) ⇒ Object
72 |
# File 'lib/rye/cmd.rb', line 72 def bzip2(*args); cmd('bzip2', args); end |
#can ⇒ Object Also known as: commands, cmds
Returns an Array of system commands available over SSH
219 220 221 |
# File 'lib/rye/cmd.rb', line 219 def can Rye::Cmd.instance_methods end |
#can?(meth) ⇒ Boolean
225 226 227 |
# File 'lib/rye/cmd.rb', line 225 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
69 |
# File 'lib/rye/cmd.rb', line 69 def chmod(*args); cmd('chmod', args); end |
#chown(*args) ⇒ Object
70 |
# File 'lib/rye/cmd.rb', line 70 def chown(*args); cmd('chown', args); end |
#cmd? ⇒ Boolean
229 230 231 |
# File 'lib/rye/cmd.rb', line 229 def can?(meth) self.can.member?(RUBY_VERSION =~ /1.9/ ? meth.to_sym : meth.to_s) end |
#command? ⇒ Boolean
228 229 230 |
# File 'lib/rye/cmd.rb', line 228 def can?(meth) self.can.member?(RUBY_VERSION =~ /1.9/ ? meth.to_sym : meth.to_s) end |
#configure(*args) ⇒ Object
88 |
# File 'lib/rye/cmd.rb', line 88 def configure(*args); cmd('./configure', args); end |
#cp(*args) ⇒ Object
30 |
# File 'lib/rye/cmd.rb', line 30 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
53 |
# File 'lib/rye/cmd.rb', line 53 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
196 197 198 199 200 |
# File 'lib/rye/cmd.rb', line 196 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
204 205 206 207 208 |
# File 'lib/rye/cmd.rb', line 204 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
212 213 214 215 216 |
# File 'lib/rye/cmd.rb', line 212 def digest_sha2(*files) files.flatten.collect { |file| File.exists?(file) ? Digest::SHA2.hexdigest(File.read(file)) : nil } end |
#dir_download(*paths) ⇒ Object Also known as: directory_download
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.
125 |
# File 'lib/rye/cmd.rb', line 125 def dir_download(*paths); net_scp_transfer!(:download, true, *paths); end |
#dir_upload(*paths) ⇒ Object Also known as: directory_upload
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.
119 |
# File 'lib/rye/cmd.rb', line 119 def dir_upload(*paths); net_scp_transfer!(:upload, true, *paths); end |
#du(*args) ⇒ Object
36 |
# File 'lib/rye/cmd.rb', line 36 def du(*args); cmd('du', args); end |
#echo(*args) ⇒ Object
58 |
# File 'lib/rye/cmd.rb', line 58 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.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/rye/cmd.rb', line 148 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(*paths) ⇒ Object
Transfer files from a machine via Net::SCP.
-
pathsis 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.
114 |
# File 'lib/rye/cmd.rb', line 114 def file_download(*paths); net_scp_transfer!(:download, false, *paths); end |
#file_exists?(path) ⇒ Boolean
Does path from the current working directory?
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/rye/cmd.rb', line 172 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(*paths) ⇒ Object
Transfer files to a machine via Net::SCP.
-
pathsis 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.
101 |
# File 'lib/rye/cmd.rb', line 101 def file_upload(*paths); net_scp_transfer!(:upload, false, *paths); 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
186 187 188 189 190 191 192 |
# File 'lib/rye/cmd.rb', line 186 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
81 |
# File 'lib/rye/cmd.rb', line 81 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
52 |
# File 'lib/rye/cmd.rb', line 52 def grep(*args); cmd('grep', args); end |
#gunzip(*args) ⇒ Object
78 |
# File 'lib/rye/cmd.rb', line 78 def gunzip(*args); cmd('gunzip', args); end |
#gzip(*args) ⇒ Object
61 |
# File 'lib/rye/cmd.rb', line 61 def gzip(*args); cmd('gzip', args); end |
#history(*args) ⇒ Object
82 |
# File 'lib/rye/cmd.rb', line 82 def history(*args); cmd('history', args); end |
#hostname(*args) ⇒ Object
85 |
# File 'lib/rye/cmd.rb', line 85 def hostname(*args); cmd('hostname', args); end |
#ls(*args) ⇒ Object
32 |
# File 'lib/rye/cmd.rb', line 32 def ls(*args); cmd('ls', args); end |
#make(*args) ⇒ Object
62 |
# File 'lib/rye/cmd.rb', line 62 def make(*args); cmd('make', args); end |
#mkdir(*args) ⇒ Object
66 |
# File 'lib/rye/cmd.rb', line 66 def mkdir(*args); cmd('mkdir', args); end |
#mkfs(*args) ⇒ Object
60 |
# File 'lib/rye/cmd.rb', line 60 def mkfs(*args); cmd('mkfs', args); end |
#mount(*args) ⇒ Object
64 |
# File 'lib/rye/cmd.rb', line 64 def mount(*args); cmd("mount", args); end |
#mv(*args) ⇒ Object
31 |
# File 'lib/rye/cmd.rb', line 31 def mv(*args); cmd("mv", args); end |
#perl(*args) ⇒ Object
56 |
# File 'lib/rye/cmd.rb', line 56 def perl(*args); cmd('perl', args); end |
#printenv(*args) ⇒ Object
84 |
# File 'lib/rye/cmd.rb', line 84 def printenv(*args); cmd('printenv', args); end |
#ps(*args) ⇒ Object
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
77 |
# File 'lib/rye/cmd.rb', line 77 def python(*args); cmd('python', args); end |
#rake(*args) ⇒ Object
def kill(*args); cmd(‘kill’, args); end
50 |
# File 'lib/rye/cmd.rb', line 50 def rake(*args); cmd('sudo', args); end |
#ruby(*args) ⇒ Object
54 |
# File 'lib/rye/cmd.rb', line 54 def ruby(*args); cmd('ruby', args); end |
#rudy(*args) ⇒ Object
55 |
# File 'lib/rye/cmd.rb', line 55 def rudy(*args); cmd('rudy', args); end |
#rudy_ec2(*args) ⇒ Object
86 |
# File 'lib/rye/cmd.rb', line 86 def rudy_ec2(*args); cmd('rudy-ec2', args); end |
#rudy_s3(*args) ⇒ Object
83 |
# File 'lib/rye/cmd.rb', line 83 def rudy_s3(*args); cmd('rudy-s3', args); end |
#rudy_sdb(*args) ⇒ Object
87 |
# File 'lib/rye/cmd.rb', line 87 def rudy_sdb(*args); cmd('rudy-sdb', 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
65 |
# File 'lib/rye/cmd.rb', line 65 def sleep(*args); cmd("sleep", args); end |
#string_download(*paths) ⇒ Object Also known as: str_download
Shorthand for file_download(‘remote/path’).string
Returns a String containing the content of all remote paths.
131 132 133 |
# File 'lib/rye/cmd.rb', line 131 def string_download(*paths) net_scp_transfer!(:download, *paths).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
139 140 141 |
# File 'lib/rye/cmd.rb', line 139 def string_upload(str, remote_path) net_scp_transfer!(:upload, StringIO.new(str), remote_path) end |
#sudo(*args) ⇒ Object
51 |
# File 'lib/rye/cmd.rb', line 51 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
59 |
# File 'lib/rye/cmd.rb', line 59 def test(*args); cmd('test', args); end |
#touch(*args) ⇒ Object
67 |
# File 'lib/rye/cmd.rb', line 67 def touch(*args); cmd('touch', args); end |
#umount(*args) ⇒ Object
75 |
# File 'lib/rye/cmd.rb', line 75 def umount(*args); cmd("umount", args); end |
#uname(*args) ⇒ Object
68 |
# File 'lib/rye/cmd.rb', line 68 def uname(*args); cmd('uname', args); end |
#unzip(*args) ⇒ Object
71 |
# File 'lib/rye/cmd.rb', line 71 def unzip(*args); cmd('unzip', args); end |
#uptime(*args) ⇒ Object
76 |
# File 'lib/rye/cmd.rb', line 76 def uptime(*args); cmd("uptime", args); end |
#useradd(*args) ⇒ Object
79 |
# File 'lib/rye/cmd.rb', line 79 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 def rm(*args); cmd(‘rm’, args); end
29 |
# File 'lib/rye/cmd.rb', line 29 def wc(*args); cmd('wc', args); end |
#which(*args) ⇒ Object
73 |
# File 'lib/rye/cmd.rb', line 73 def which(*args); cmd('which', args); end |