Module: VagrantPlugins::Rimu::Actions::SshUtils

Included in:
Create, Rebuild, SetupUser
Defined in:
lib/vagrant-rimu/actions/ssh_utils.rb

Instance Method Summary collapse

Instance Method Details

#public_key(private_key_path) ⇒ Object



35
36
37
38
39
# File 'lib/vagrant-rimu/actions/ssh_utils.rb', line 35

def public_key(private_key_path)
  File.read("#{private_key_path}.pub")
rescue
  raise Errors::PublicKeyError, :path => "#{private_key_path}.pub"
end

#upload_key(env, user = nil) ⇒ Object

rubocop:disable Metrics/AbcSize



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vagrant-rimu/actions/ssh_utils.rb', line 9

def upload_key(env, user=nil)
  path = env[:machine].config.ssh.private_key_path
  path = path[0] if path.is_a?(Array)
  path = File.expand_path(path, env[:machine].env.root_path)
  pub_key = public_key(path)
  if user.nil?
    env[:machine].communicate.execute(<<-BASH)
      if [ ! -d /root/.ssh ]; then
        mkdir /root/.ssh;
        chmod 0700 /root/.ssh;
      fi
      if ! grep '#{pub_key.chomp}' /root/.ssh/authorized_keys; then
        echo '#{pub_key}' >> /root/.ssh/authorized_keys;
      fi
    BASH
  else
    env[:machine].communicate.execute(<<-BASH)
      if ! grep '#{pub_key.chomp}' /home/#{user}/.ssh/authorized_keys; then
        echo '#{pub_key}' >> /home/#{user}/.ssh/authorized_keys;
      fi

      chown -R #{user} /home/#{user}/.ssh;
    BASH
  end
end