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
    "# Automatically add user's public key from \#{key_path}\ncp /home/\#{user}/.ssh/authorized_keys{,.bak}\necho \#{public_key} >> /home/\#{user}/.ssh/authorized_keys\nchown \#{user}:\#{user} /home/\#{user}/.ssh/authorized_keys\n"
  else
    "# WARN: unable to find a ~/.ssh/id_rsa.pub locally on your machine.  user: \#{ENV['USER']}\n# Unable to automatically add the public key\n"
  end
end