Class: Vagrant::Provisioners::Shell

Inherits:
Base
  • Object
show all
Defined in:
lib/vagrant/provisioners/shell.rb

Defined Under Namespace

Classes: Config

Instance Attribute Summary

Attributes inherited from Base

#config, #env

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#cleanup, #initialize, #prepare

Constructor Details

This class inherits a constructor from Vagrant::Provisioners::Base

Class Method Details

.config_classObject



45
46
47
# File 'lib/vagrant/provisioners/shell.rb', line 45

def self.config_class
  Config
end

Instance Method Details

#provision!Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vagrant/provisioners/shell.rb', line 72

def provision!
  args = ""
  args = " #{config.args}" if config.args
  command = "chmod +x #{config.upload_path} && #{config.upload_path}#{args}"

  with_script_file do |path|
    # Upload the script to the VM
    env[:vm].channel.upload(path.to_s, config.upload_path)

    # Execute it with sudo
    env[:vm].channel.sudo(command) do |type, data|
      if [:stderr, :stdout].include?(type)
        # Output the data with the proper color based on the stream.
        color = type == :stdout ? :green : :red

        # Note: Be sure to chomp the data to avoid the newlines that the
        # Chef outputs.
        env[:ui].info(data.chomp, :color => color, :prefix => false)
      end
    end
  end
end

#with_script_fileObject

This method yields the path to a script to upload and execute on the remote server. This method will properly clean up the script file if needed.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vagrant/provisioners/shell.rb', line 52

def with_script_file
  if config.path
    # Just yield the path to that file...
    yield Pathname.new(config.path).expand_path(env[:root_path])
    return
  end

  # Otherwise we have an inline script, we need to Tempfile it,
  # and handle it specially...
  file = Tempfile.new('vagrant-shell')
  begin
    file.write(config.inline)
    file.fsync
    yield file.path
  ensure
    file.close
    file.unlink
  end
end