Class: VagrantAWS::AWSCommands

Inherits:
Vagrant::Command::GroupBase
  • Object
show all
Defined in:
lib/vagrant-aws/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ AWSCommands

Returns a new instance of AWSCommands.



8
9
10
11
# File 'lib/vagrant-aws/command.rb', line 8

def initialize(*args)
	super
	initialize_aws_environment(*args)
end

Instance Method Details

#box_add(name, uri) ⇒ Object



151
152
153
# File 'lib/vagrant-aws/command.rb', line 151

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

#box_create(name = nil) ⇒ Object

Raises:

  • (Vagrant::Errors::MultiVMTargetRequired)


136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/vagrant-aws/command.rb', line 136

def box_create(name=nil)
	raise Vagrant::Errors::MultiVMTargetRequired, :command => "box_create" if target_vms.length > 1
	
	ami_vm = target_vms.first		
	ami_vm.env.actions.run(:aws_create_image, {
		'package.output' => options[:image_name] || env.config.package.name,
		'image.register' => options[:register],
		'image.name' => options[:image_name] || "vagrantaws_#{rand(36**8).to_s(36)}",
		'image.desc' => options[:image_desc] || "Image created by vagrant-aws"
	})

end

#box_listObject



157
158
159
160
161
# File 'lib/vagrant-aws/command.rb', line 157

def box_list
	boxes = env.boxes.sort
	return env.ui.warn(I18n.t("vagrant.commands.box.no_installed_boxes"), :prefix => false) if boxes.empty?
	boxes.each { |b| env.ui.info(b.name, :prefix => false) }
end

#box_remove(name) ⇒ Object

Raises:

  • (Vagrant::Errors::BoxNotFound)


166
167
168
169
170
# File 'lib/vagrant-aws/command.rb', line 166

def box_remove(name)
			b = env.boxes.find(name)
  raise Vagrant::Errors::BoxNotFound, :name => name if !b
  b.remove({ 'image.deregister' => options[:deregister] })
end

#destroy(name = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/vagrant-aws/command.rb', line 28

def destroy(name=nil)
	target_vms(name).each do |vm|
		if vm.created?
			vm.env.actions.run(:aws_destroy)
		else
			vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
		end
	end
end

#provision(name = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/vagrant-aws/command.rb', line 78

def provision(name=nil)
			target_vms(name).each do |vm|
if vm.created? && vm.vm.state == 'running'
	vm.env.actions.run(:aws_provision)
else
	vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
end
  end
end

#resume(name = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/vagrant-aws/command.rb', line 102

def resume(name=nil)
	target_vms(name).each do |vm|
		if vm.created?
			vm.env.actions.run(:aws_resume)
		else
			vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
		end
	end	
end

#ssh(name = nil) ⇒ Object

Raises:

  • (Vagrant::Errors::MultiVMTargetRequired)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vagrant-aws/command.rb', line 56

def ssh(name=nil)
	raise Vagrant::Errors::MultiVMTargetRequired, :command => "ssh" if target_vms.length > 1
	
	ssh_vm = target_vms.first
	ssh_vm.env.actions.run(VagrantAWS::Action::PopulateSSH)

	if options[:execute]
		ssh_vm.ssh.execute do |ssh|
        ssh_vm.env.ui.info I18n.t("vagrant.commands.ssh.execute", :command => options[:execute])
        ssh.exec!(options[:execute]) do |channel, type, data|
          ssh_vm.env.ui.info "#{data}"
        end
      end
	else
		raise Vagrant::Errors::VMNotCreatedError if !ssh_vm.created?
      raise Vagrant::Errors::VMNotRunningError if !ssh_vm.vm.running?
      ssh_vm.ssh.connect 
	end	
end

#ssh_config(name = nil) ⇒ Object

Raises:

  • (Vagrant::Errors::MultiVMTargetRequired)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/vagrant-aws/command.rb', line 115

def ssh_config(name=nil)
	raise Vagrant::Errors::MultiVMTargetRequired, :command => "ssh_config" if target_vms.length > 1
	
	ssh_vm = target_vms.first
	raise Vagrant::Errors::VMNotCreatedError if !ssh_vm.created?
	ssh_vm.env.actions.run(VagrantAWS::Action::PopulateSSH)
	
	$stdout.puts(Vagrant::Util::TemplateRenderer.render("ssh_config", {
      :host_key => options[:host] || "vagrant",
      :ssh_host => ssh_vm.env.config.ssh.host,
		:ssh_user => ssh_vm.env.config.ssh.username,
		:ssh_port => ssh_vm.ssh.port,
		:private_key_path => ssh_vm.env.config.ssh.private_key_path
    }))
end

#statusObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vagrant-aws/command.rb', line 40

def status
	state = nil
	results = target_vms.collect do |vm|
		state = vm.created? ? vm.vm.state.to_s : 'not_created'
		"#{vm.name.to_s.ljust(25)}#{state.gsub("_", " ")}"	
	end
	state = target_vms.length == 1 ? state : "listing"
	@env.ui.info(I18n.t("vagrant.commands.status.output",
                  :states  => results.join("\n"),
                  :message => I18n.t("vagrant.plugins.aws.commands.status.#{state}")),
                  :prefix  => false)
end

#suspend(name = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/vagrant-aws/command.rb', line 90

def suspend(name=nil)
	target_vms(name).each do |vm|
		if vm.created?
			vm.env.actions.run(:aws_suspend)
		else
			vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
		end
	end	
end

#up(name = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/vagrant-aws/command.rb', line 16

def up(name=nil)	
	target_vms(name).each do |vm|				
		if vm.created?
			vm.env.ui.info I18n.t("vagrant.commands.up.vm_created")
		else
			vm.env.actions.run(:aws_up, "provision.enabled" => options[:provision])
		end
	end
end