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); __allow("/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
- #curl(*args) ⇒ Object
- #cvs(*args) ⇒ Object
- #date(*args) ⇒ Object
- #df(*args) ⇒ Object
-
#digest_md5(*files) ⇒ Object
-
files
An Array of file paths Returns an Array of MD5 digests for each of the given files.
-
-
#digest_sha1(*files) ⇒ Object
-
files
An Array of file paths Returns an Array of SH1 digests for each of the given files.
-
-
#digest_sha2(*files) ⇒ Object
-
files
An Array of file paths Returns an Array of SH2 digests for each of the given files.
-
-
#dir(*args) ⇒ Object
– WINDOWS ++.
-
#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
newcontent
to remotefilepath
. -
#file_download(*paths) ⇒ Object
Transfer files from a machine via Net::SCP.
-
#file_exists?(path) ⇒ Boolean
Does
path
from 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
path
match the knownexpected_digest
? This is useful for verifying downloaded files. -
#file_write(filepath, newcontent, backup = false) ⇒ Object
Write
newcontent
to remotefilepath
. - #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); __allow(‘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); __allow(‘cd’, args); end def rm(*args); __allow(‘rm’, args); end.
- #wget(*args) ⇒ Object
- #which(*args) ⇒ Object
Class Method Details
.add_command(meth, path = nil, *hard_args, &block) ⇒ Object
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.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/rye/cmd.rb', line 259 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 __allow(path, *local_args) end end end |
.can?(meth) ⇒ Boolean
248 249 250 |
# File 'lib/rye/cmd.rb', line 248 def Cmd.can?(meth) instance_methods.member?(meth) end |
.remove_command(meth) ⇒ Object
A helper for removing a command from Rye::Cmd.
-
meth
the method name
279 280 281 |
# File 'lib/rye/cmd.rb', line 279 def Cmd.remove_command(meth) remove_method(meth) end |
Instance Method Details
#awk(*args) ⇒ Object
41 |
# File 'lib/rye/cmd.rb', line 41 def awk(*args); __allow('awk', args); end |
#bash(*args) ⇒ Object
53 |
# File 'lib/rye/cmd.rb', line 53 def bash(*args); __allow('bash', args); end |
#bunzip2(*args) ⇒ Object
78 |
# File 'lib/rye/cmd.rb', line 78 def bunzip2(*args); __allow('bunzip2', args); end |
#bzip2(*args) ⇒ Object
70 |
# File 'lib/rye/cmd.rb', line 70 def bzip2(*args); __allow('bzip2', args); end |
#can ⇒ Object Also known as: commands, cmds
Returns an Array of system commands available over SSH
236 237 238 |
# File 'lib/rye/cmd.rb', line 236 def can Rye::Cmd.instance_methods end |
#can?(meth) ⇒ Boolean
242 243 244 |
# File 'lib/rye/cmd.rb', line 242 def can?(meth) self.can.member?(RUBY_VERSION =~ /1.9/ ? meth.to_sym : meth.to_s) end |
#cat(*args) ⇒ Object
42 |
# File 'lib/rye/cmd.rb', line 42 def cat(*args); __allow('cat', args); end |
#chmod(*args) ⇒ Object
67 |
# File 'lib/rye/cmd.rb', line 67 def chmod(*args); __allow('chmod', args); end |
#chown(*args) ⇒ Object
68 |
# File 'lib/rye/cmd.rb', line 68 def chown(*args); __allow('chown', args); end |
#cmd? ⇒ Boolean
246 247 248 |
# File 'lib/rye/cmd.rb', line 246 def can?(meth) self.can.member?(RUBY_VERSION =~ /1.9/ ? meth.to_sym : meth.to_s) end |
#command? ⇒ Boolean
245 246 247 |
# File 'lib/rye/cmd.rb', line 245 def can?(meth) self.can.member?(RUBY_VERSION =~ /1.9/ ? meth.to_sym : meth.to_s) end |
#configure(*args) ⇒ Object
86 |
# File 'lib/rye/cmd.rb', line 86 def configure(*args); __allow('./configure', args); end |
#cp(*args) ⇒ Object
26 |
# File 'lib/rye/cmd.rb', line 26 def cp(*args); __allow("cp", args); end |
#curl(*args) ⇒ Object
60 |
# File 'lib/rye/cmd.rb', line 60 def curl(*args); __allow('curl', args); end |
#cvs(*args) ⇒ Object
38 |
# File 'lib/rye/cmd.rb', line 38 def cvs(*args); __allow('cvs', args); end |
#date(*args) ⇒ Object
49 |
# File 'lib/rye/cmd.rb', line 49 def date(*args); __allow('date', args); end |
#df(*args) ⇒ Object
31 |
# File 'lib/rye/cmd.rb', line 31 def df(*args); __allow('df', args); end |
#digest_md5(*files) ⇒ Object
-
files
An Array of file paths
Returns an Array of MD5 digests for each of the given files
213 214 215 216 217 |
# File 'lib/rye/cmd.rb', line 213 def digest_md5(*files) files.flatten.collect { |file| File.exists?(file) ? Digest::MD5.hexdigest(File.read(file)) : nil } end |
#digest_sha1(*files) ⇒ Object
-
files
An Array of file paths
Returns an Array of SH1 digests for each of the given files
221 222 223 224 225 |
# File 'lib/rye/cmd.rb', line 221 def digest_sha1(*files) files.flatten.collect { |file| File.exists?(file) ? Digest::SHA1.hexdigest(File.read(file)) : nil } end |
#digest_sha2(*files) ⇒ Object
-
files
An Array of file paths
Returns an Array of SH2 digests for each of the given files
229 230 231 232 233 |
# File 'lib/rye/cmd.rb', line 229 def digest_sha2(*files) files.flatten.collect { |file| File.exists?(file) ? Digest::SHA2.hexdigest(File.read(file)) : nil } end |
#dir(*args) ⇒ Object
– WINDOWS ++
91 |
# File 'lib/rye/cmd.rb', line 91 def dir(*args); __allow('cmd', args); 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.
129 |
# File 'lib/rye/cmd.rb', line 129 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.
123 |
# File 'lib/rye/cmd.rb', line 123 def dir_upload(*paths); net_scp_transfer!(:upload, true, *paths); end |
#du(*args) ⇒ Object
32 |
# File 'lib/rye/cmd.rb', line 32 def du(*args); __allow('du', args); end |
#echo(*args) ⇒ Object
54 |
# File 'lib/rye/cmd.rb', line 54 def echo(*args); __allow('echo', args); end |
#env ⇒ Object
34 |
# File 'lib/rye/cmd.rb', line 34 def env; __allow "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.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rye/cmd.rb', line 152 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 content ||= StringIO.new 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) ⇒ Object
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.
118 |
# File 'lib/rye/cmd.rb', line 118 def file_download(*paths); net_scp_transfer!(:download, false, *paths); end |
#file_exists?(path) ⇒ Boolean
Does path
from the current working directory?
189 190 191 192 193 194 195 196 197 198 |
# File 'lib/rye/cmd.rb', line 189 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.
-
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.
105 |
# File 'lib/rye/cmd.rb', line 105 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
203 204 205 206 207 208 209 |
# File 'lib/rye/cmd.rb', line 203 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) ⇒ Object
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.
172 173 174 175 176 177 178 179 180 |
# File 'lib/rye/cmd.rb', line 172 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) ⇒ Object
79 |
# File 'lib/rye/cmd.rb', line 79 def getconf(*args); __allow('getconf', args); end |
#git(*args) ⇒ Object
39 |
# File 'lib/rye/cmd.rb', line 39 def git(*args); __allow('git', args); end |
#grep(*args) ⇒ Object
48 |
# File 'lib/rye/cmd.rb', line 48 def grep(*args); __allow('grep', args); end |
#gunzip(*args) ⇒ Object
76 |
# File 'lib/rye/cmd.rb', line 76 def gunzip(*args); __allow('gunzip', args); end |
#gzip(*args) ⇒ Object
57 |
# File 'lib/rye/cmd.rb', line 57 def gzip(*args); __allow('gzip', args); end |
#history(*args) ⇒ Object
80 |
# File 'lib/rye/cmd.rb', line 80 def history(*args); __allow('history', args); end |
#hostname(*args) ⇒ Object
83 |
# File 'lib/rye/cmd.rb', line 83 def hostname(*args); __allow('hostname', args); end |
#ls(*args) ⇒ Object
28 |
# File 'lib/rye/cmd.rb', line 28 def ls(*args); __allow('ls', args); end |
#make(*args) ⇒ Object
58 |
# File 'lib/rye/cmd.rb', line 58 def make(*args); __allow('make', args); end |
#mkdir(*args) ⇒ Object
64 |
# File 'lib/rye/cmd.rb', line 64 def mkdir(*args); __allow('mkdir', args); end |
#mkfs(*args) ⇒ Object
56 |
# File 'lib/rye/cmd.rb', line 56 def mkfs(*args); __allow('mkfs', args); end |
#mount(*args) ⇒ Object
62 |
# File 'lib/rye/cmd.rb', line 62 def mount(*args); __allow("mount", args); end |
#mv(*args) ⇒ Object
27 |
# File 'lib/rye/cmd.rb', line 27 def mv(*args); __allow("mv", args); end |
#perl(*args) ⇒ Object
52 |
# File 'lib/rye/cmd.rb', line 52 def perl(*args); __allow('perl', args); end |
#printenv(*args) ⇒ Object
82 |
# File 'lib/rye/cmd.rb', line 82 def printenv(*args); __allow('printenv', args); end |
#ps(*args) ⇒ Object
29 |
# File 'lib/rye/cmd.rb', line 29 def ps(*args); __allow('ps', args); end |
#pwd(*args) ⇒ Object
36 |
# File 'lib/rye/cmd.rb', line 36 def pwd(*args); __allow "pwd", args; end |
#python(*args) ⇒ Object
75 |
# File 'lib/rye/cmd.rb', line 75 def python(*args); __allow('python', args); end |
#rake(*args) ⇒ Object
def kill(*args); __allow(‘kill’, args); end
46 |
# File 'lib/rye/cmd.rb', line 46 def rake(*args); __allow('sudo', args); end |
#ruby(*args) ⇒ Object
50 |
# File 'lib/rye/cmd.rb', line 50 def ruby(*args); __allow('ruby', args); end |
#rudy(*args) ⇒ Object
51 |
# File 'lib/rye/cmd.rb', line 51 def rudy(*args); __allow('rudy', args); end |
#rudy_ec2(*args) ⇒ Object
84 |
# File 'lib/rye/cmd.rb', line 84 def rudy_ec2(*args); __allow('rudy-ec2', args); end |
#rudy_s3(*args) ⇒ Object
81 |
# File 'lib/rye/cmd.rb', line 81 def rudy_s3(*args); __allow('rudy-s3', args); end |
#rudy_sdb(*args) ⇒ Object
85 |
# File 'lib/rye/cmd.rb', line 85 def rudy_sdb(*args); __allow('rudy-sdb', args); end |
#rye(*args) ⇒ Object
35 |
# File 'lib/rye/cmd.rb', line 35 def rye(*args); __allow "rye", args; end |
#sed(*args) ⇒ Object
40 |
# File 'lib/rye/cmd.rb', line 40 def sed(*args); __allow('sed', args); end |
#sh(*args) ⇒ Object
30 |
# File 'lib/rye/cmd.rb', line 30 def sh(*args); __allow('sh', args); end |
#sleep(*args) ⇒ Object
63 |
# File 'lib/rye/cmd.rb', line 63 def sleep(*args); __allow("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.
135 136 137 |
# File 'lib/rye/cmd.rb', line 135 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
143 144 145 |
# File 'lib/rye/cmd.rb', line 143 def string_upload(str, remote_path) net_scp_transfer!(:upload, StringIO.new(str), remote_path) end |
#sudo(*args) ⇒ Object
47 |
# File 'lib/rye/cmd.rb', line 47 def sudo(*args); __allow('sudo', args); end |
#svn(*args) ⇒ Object
37 |
# File 'lib/rye/cmd.rb', line 37 def svn(*args); __allow('svn', args); end |
#tar(*args) ⇒ Object
43 |
# File 'lib/rye/cmd.rb', line 43 def tar(*args); __allow('tar', args); end |
#test(*args) ⇒ Object
55 |
# File 'lib/rye/cmd.rb', line 55 def test(*args); __allow('test', args); end |
#touch(*args) ⇒ Object
65 |
# File 'lib/rye/cmd.rb', line 65 def touch(*args); __allow('touch', args); end |
#umount(*args) ⇒ Object
73 |
# File 'lib/rye/cmd.rb', line 73 def umount(*args); __allow("umount", args); end |
#uname(*args) ⇒ Object
66 |
# File 'lib/rye/cmd.rb', line 66 def uname(*args); __allow('uname', args); end |
#unzip(*args) ⇒ Object
69 |
# File 'lib/rye/cmd.rb', line 69 def unzip(*args); __allow('unzip', args); end |
#uptime(*args) ⇒ Object
74 |
# File 'lib/rye/cmd.rb', line 74 def uptime(*args); __allow("uptime", args); end |
#useradd(*args) ⇒ Object
77 |
# File 'lib/rye/cmd.rb', line 77 def useradd(*args); __allow('useradd', args); end |
#wc(*args) ⇒ Object
NOTE: See Rye::Box for the implementation of cd def cd(*args); __allow(‘cd’, args); end def rm(*args); __allow(‘rm’, args); end
25 |
# File 'lib/rye/cmd.rb', line 25 def wc(*args); __allow('wc', args); end |
#wget(*args) ⇒ Object
59 |
# File 'lib/rye/cmd.rb', line 59 def wget(*args); __allow('wget', args); end |
#which(*args) ⇒ Object
71 |
# File 'lib/rye/cmd.rb', line 71 def which(*args); __allow('which', args); end |