Class: VagrantRake::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-rake/middleware.rb

Overview

A Vagrant middleware which executes a rake task on a given VM. This task will run “rake” on the VM that this action sequence was run on. If an env variable “rake.command” is populated, then this command will be executed by rake.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Middleware

Returns a new instance of Middleware.


7
8
9
10
# File 'lib/vagrant-rake/middleware.rb', line 7

def initialize(app, env)
  @app = app
  @env = env
end

Instance Method Details

#call(env) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vagrant-rake/middleware.rb', line 12

def call(env)
  if env["vm"].created? && env["vm"].vm.running?
    command = "rake #{env["rake.command"]}".strip

    env["vm"].ssh.execute do |ssh|
      env.ui.info I18n.t("vagrant.plugins.rake.executing", :command => command)

      ssh.exec!("cd #{working_directory}; #{command}") do |channel, type, data|
        # Print the data directly to STDOUT, not doing any newlines
        # or any extra formatting of our own
        $stdout.print(data) if type != :exit_status
      end

      # Puts out an ending newline just to make sure we end on a new
      # line.
      $stdout.puts
    end
  end

  @app.call(env)
end