Class: Vagrant::Commands

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/vagrant/commands.rb

Overview

Contains all the command-line commands invoked by the binaries. Having them all in one location assists with documentation and also takes the commands out of some of the other classes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#error_and_exit, included, #logger, #wrap_output

Constructor Details

#initialize(env) ⇒ Commands

Initialize a new Vagrant::Commands instance for the given environment. This merely prepares the #env variable.



14
15
16
# File 'lib/vagrant/commands.rb', line 14

def initialize(env)
  @env = env
end

Instance Attribute Details

#envObject (readonly)

The environment which these commands will act on



10
11
12
# File 'lib/vagrant/commands.rb', line 10

def env
  @env
end

Class Method Details

.execute(command, *args) ⇒ Object

Runs a command in the current environment by loading the environment of the current working directory prior to executing.



213
214
215
216
# File 'lib/vagrant/commands.rb', line 213

def execute(command, *args)
  env = Environment.load!
  env.commands.send(command, *args)
end

.init(default_box = nil) ⇒ Object

Initializes a directory for use with vagrant. This command copies an initial ‘Vagrantfile` into the current working directory so you can begin using vagrant. The configuration file contains some documentation to get you started.



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/vagrant/commands.rb', line 198

def init(default_box=nil)
  rootfile_path = File.join(Dir.pwd, Environment::ROOTFILE_NAME)
  if File.exist?(rootfile_path)
    error_and_exit(:rootfile_already_exists)
  end

  # Copy over the rootfile template into this directory
  default_box ||= "base"
  File.open(rootfile_path, 'w+') do |f|
    f.write(TemplateRenderer.render(Environment::ROOTFILE_NAME, :default_box => default_box))
  end
end

Instance Method Details

#box(argv) ⇒ Object

Manages the ‘vagrant box` command, allowing the user to add and remove boxes. This single command, given an array, determines which action to take and calls the respective action method (see #box_add and #box_remove)



110
111
112
113
114
115
116
117
118
# File 'lib/vagrant/commands.rb', line 110

def box(argv)
  sub_commands = ["list", "add", "remove"]

  if !sub_commands.include?(argv[0])
    error_and_exit(:command_box_invalid)
  end

  send("box_#{argv[0]}", env, *argv[1..-1])
end

#box_add(env, name, path) ⇒ Object

Adds a box to the local filesystem, given a URI.



137
138
139
# File 'lib/vagrant/commands.rb', line 137

def box_add(env, name, path)
  Box.add(env, name, path)
end

#box_list(env) ⇒ Object

Lists all added boxes



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/vagrant/commands.rb', line 121

def box_list(env)
  boxes = Box.all(env).sort

  wrap_output do
    if !boxes.empty?
      puts "Installed Vagrant Boxes:\n\n"
      boxes.each do |box|
        Kernel.puts box
      end
    else
      Kernel.puts "No Vagrant Boxes Added!"
    end
  end
end

#box_remove(env, name) ⇒ Object

Removes a box.



142
143
144
145
146
147
148
149
150
# File 'lib/vagrant/commands.rb', line 142

def box_remove(env, name)
  box = Box.find(env, name)
  if box.nil?
    error_and_exit(:box_remove_doesnt_exist)
    return # for tests
  end

  box.destroy
end

#downObject

Tear down a vagrant instance. This not only shuts down the instance (if its running), but also deletes it from the system, including the hard disks associated with it.

This command requires that an instance already be brought up with ‘vagrant up`.



38
39
40
41
# File 'lib/vagrant/commands.rb', line 38

def down
  env.require_persisted_vm
  env.vm.destroy
end

#haltObject

Halts a running vagrant instance. This forcibly halts the instance; it is the equivalent of pulling the power on a machine. The instance can be restarted again with #up.

This command requires than an instance already be brought up with ‘vagrant up`.



70
71
72
73
# File 'lib/vagrant/commands.rb', line 70

def halt
  env.require_persisted_vm
  env.vm.execute!(Actions::VM::Halt)
end

#package(out_path = nil, include_files = []) ⇒ Object

Export and package the current vm

This command requires that an instance be powered off



99
100
101
102
103
104
# File 'lib/vagrant/commands.rb', line 99

def package(out_path=nil, include_files=[])
  env.require_persisted_vm
  error_and_exit(:vm_power_off_to_package) unless env.vm.powered_off?

  env.vm.package(out_path, include_files)
end

#reloadObject

Reload the environment. This is almost equivalent to the #up command except that it doesn’t import the VM and do the initialize bootstrapping of the instance. Instead, it forces a shutdown (if its running) of the VM, updates the metadata (shared folders, forwarded ports), restarts the VM, and then reruns the provisioning if enabled.



48
49
50
51
# File 'lib/vagrant/commands.rb', line 48

def reload
  env.require_persisted_vm
  env.vm.execute!(Actions::VM::Reload)
end

#resumeObject

Resume a running vagrant instance. This resumes an already suspended instance (from #suspend).

This command requires that an instance already be brought up with ‘vagrant up`.



91
92
93
94
# File 'lib/vagrant/commands.rb', line 91

def resume
  env.require_persisted_vm
  env.vm.resume
end

#sshObject

SSH into the vagrant instance. This will setup an SSH connection into the vagrant instance, replacing the running ruby process with the SSH connection.

This command requires that an instance already be brought up with ‘vagrant up`.



59
60
61
62
# File 'lib/vagrant/commands.rb', line 59

def ssh
  env.require_persisted_vm
  env.ssh.connect
end

#statusObject

Outputs the status of the current environment. This command outputs useful information such as whether or not the environment is created and if its running, suspended, etc.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/vagrant/commands.rb', line 155

def status
  wrap_output do
    if !env.vm
      puts <<-msg
The environment has not yet been created. Run `vagrant up` to create the
environment.
msg
    else
      additional_msg = ""
      if env.vm.vm.running?
        additional_msg = <<-msg
To stop this VM, you can run `vagrant halt` to shut it down forcefully,
or you can run `vagrant suspend` to simply suspend the virtual machine.
In either case, to restart it again, simply run a `vagrant up`.
msg
      elsif env.vm.vm.saved?
        additional_msg = <<-msg
To resume this VM, simply run `vagrant up`.
msg
      elsif env.vm.vm.powered_off?
        additional_msg = <<-msg
To restart this VM, simply run `vagrant up`.
msg
      end

      if !additional_msg.empty?
        additional_msg.chomp!
        additional_msg = "\n\n#{additional_msg}"
      end

      puts <<-msg
The environment has been created. The status of the current environment's
virtual machine is: "#{env.vm.vm.state}."#{additional_msg}
msg
    end
  end
end

#suspendObject

Suspend a running vagrant instance. This suspends the instance, saving the state of the VM and “pausing” it. The instance can be resumed again with #resume.

This command requires that an instance already be brought up with ‘vagrant up`.



81
82
83
84
# File 'lib/vagrant/commands.rb', line 81

def suspend
  env.require_persisted_vm
  env.vm.suspend
end

#upObject

Bring up a vagrant instance. This handles everything from importing the base VM, setting up shared folders, forwarded ports, etc to provisioning the instance with chef. #up also starts the instance, running it in the background.



22
23
24
25
26
27
28
29
30
# File 'lib/vagrant/commands.rb', line 22

def up
  if env.vm
    logger.info "VM already created. Starting VM if its not already running..."
    env.vm.start
  else
    env.require_box
    env.create_vm.execute!(Actions::VM::Up)
  end
end