Class: Vagrant::Easy::Operations
- Inherits:
-
Object
- Object
- Vagrant::Easy::Operations
- Defined in:
- lib/vagrant/easy/operations.rb
Overview
This class contains all the "operations" that easy commands are able to run. An instance of this is class is what is sent into the callback for all easy commands.
Direct Known Subclasses
Instance Method Summary (collapse)
-
- (String) download(from, to = nil)
Download a file from the remote virtual machine.
-
- (Operations) initialize(vm)
constructor
A new instance of Operations.
-
- (Object) local(command, options = nil)
Runs a command on the local machine.
-
- (Object) run(command, options = nil)
Run a shell command within the VM.
-
- (Object) sudo(command, options = nil)
Same as run except runs the command with superuser privileges via
sudo. -
- (Object) upload(from, to)
Uploads a file to the virtual machine.
Constructor Details
- (Operations) initialize(vm)
A new instance of Operations
14 15 16 17 |
# File 'lib/vagrant/easy/operations.rb', line 14 def initialize(vm) @logger = Log4r::Logger.new("vagrant::easy::operations") @vm = vm end |
Instance Method Details
- (String) download(from, to = nil)
Download a file from the remote virtual machine. If to is nil, then
the contents are returned as a string.
25 26 27 28 29 30 |
# File 'lib/vagrant/easy/operations.rb', line 25 def download(from, to=nil) raise Errors::VMNotRunningError if @vm.state != :running @logger.info("download: #{from} => #{to}") @vm.channel.download(from, to) end |
- (Object) local(command, options = nil)
Runs a command on the local machine. This will return an object where
you can access the exit_code, stdout, and stderr easiy:
output = local("echo foo")
puts "Output was #{output.stdout}"
(Likewise, exit_code and stderr are attributes on the return value)
It is recommended you use this local method rather than trying to
manually use Ruby's underlying subprocess tools because this will use
the Vagrant Subprocess class which has been refined over the years
to work equally well on Windows, Mac OS X, Linux as well as on many
runtimes such as CRuby and JRuby.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/vagrant/easy/operations.rb', line 48 def local(command, =nil) @logger.info("local: #{command}") block = nil = { :echo => true }.merge( || {}) if [:echo] # If we're echoing data, then setup the block that sends the # data to the UI. block = Proc.new do |type, data| if type == :stdout || type == :stderr @vm.ui.info(data.to_s, :prefix => false, :new_line => false) end end end Vagrant::Util::Subprocess.execute(command, :notify => [:stdout, :stderr], &block) end |
- (Object) run(command, options = nil)
Run a shell command within the VM. The command will run within a
shell environment, and the output and exit code will be returned
as an object with attributes: exit_code,stdout, andstderr`.
Example:
output = run("echo foo")
puts "Output was #{output.stdout}"
79 80 81 82 83 |
# File 'lib/vagrant/easy/operations.rb', line 79 def run(command, =nil) @logger.info("run: #{command}") = { :echo => true }.merge( || {}) remote_command(:execute, command, ) end |
- (Object) sudo(command, options = nil)
Same as run except runs the command with superuser privileges
via sudo.
90 91 92 93 94 |
# File 'lib/vagrant/easy/operations.rb', line 90 def sudo(command, =nil) @logger.info("sudo: #{command}") = { :echo => true }.merge( || {}) remote_command(:sudo, command, ) end |
- (Object) upload(from, to)
Uploads a file to the virtual machine.
Note that the to path must have proper permissions setup so that the
SSH user can upload to it. If it does not, then you should upload
to a location you do have permission to, then use #sudo to move
it.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/vagrant/easy/operations.rb', line 105 def upload(from, to) raise Errors::VMNotRunningError if @vm.state != :running # If we're dealing with an IO object, then save it to a temporary # file and upload that. We define `temp = nil` here so that it # doesn't go out of scope and get GC'd until after the method # closes. temp = nil if from.is_a?(IO) temp = Tempfile.new("vagrant") temp.write(from) temp.close from = temp.path end # Perform the upload @logger.info("upload: #{from} => #{to}") @vm.channel.upload(from, to) end |