Class: VagrantAWS::Action::CreateSSHKey

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-aws/action/create_sshkey.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CreateSSHKey

Returns a new instance of CreateSSHKey.



6
7
8
# File 'lib/vagrant-aws/action/create_sshkey.rb', line 6

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

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vagrant-aws/action/create_sshkey.rb', line 10

def call(env)
	@env = env
	
	if @env["config"].aws.key_name.nil?	
       # Do we have a previously created key available on AWS?
		key = @env["vm"].connection.key_pairs.all('key-name' => @env.env.ssh_keys).first
		if key.nil?
			# Create and save key
			key = @env["vm"].connection.key_pairs.create(:name => "vagrantaws_#{Mac.addr.gsub(':','')}")
			env.ui.info I18n.t("vagrant.plugins.aws.actions.create_ssh_key.created", :name => key.name)	
			File.open(local_key_path(key.name), File::WRONLY|File::TRUNC|File::CREAT, 0600) { |f| f.write(key.private_key) }	
		end
		
		@env["config"].aws.key_name = key.name
		@env["config"].aws.private_key_path = local_key_path(key.name) 
	end

	@app.call(env)
end

#local_key_path(name) ⇒ Object



30
31
32
# File 'lib/vagrant-aws/action/create_sshkey.rb', line 30

def local_key_path(name)
	@env.env.ssh_keys_path.join(name)
end