Module: AwsEc2::Template::Helper::SshKeyHelper

Defined in:
lib/aws_ec2/template/helper/ssh_key_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_ssh_key(user = "ec2-user") ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/aws_ec2/template/helper/ssh_key_helper.rb', line 2

def add_ssh_key(user="ec2-user")
  key_path = "#{ENV['HOME']}/.ssh/id_rsa.pub"
  if File.exist?(key_path)
    public_key = IO.read(key_path).strip
  end
  if public_key
    <<-SCRIPT
# Automatically add user's public key from #{key_path}
cp /home/#{user}/.ssh/authorized_keys{,.bak}
echo #{public_key} >> /home/#{user}/.ssh/authorized_keys
chown #{user}:#{user} /home/#{user}/.ssh/authorized_keys
SCRIPT
  else
    <<-SCRIPT
# WARN: unable to find a ~/.ssh/id_rsa.pub locally on your machine.  user: #{ENV['USER']}
# Unable to automatically add the public key
SCRIPT
  end
end