Class: VagrantPlugins::GuestMSYS2::Cap::InsertPublicKey

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-guest-msys2/cap/insert_public_key.rb

Class Method Summary collapse

Class Method Details

.insert_public_key(machine, contents) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vagrant-guest-msys2/cap/insert_public_key.rb', line 5

def self.insert_public_key(machine, contents)
  comm = machine.communicate
  contents = contents.chomp

  remote_path = "/tmp/vagrant-authorized-keys-#{Time.now.to_i}"
  Tempfile.open("vagrant-msys2-insert-public-key") do |f|
    f.binmode
    f.write(contents)
    f.fsync
    f.close
    comm.upload(f.path, remote_path)
  end

  comm.execute "    mkdir -p ~/.ssh\n    chmod 0700 ~/.ssh\n    cat '\#{remote_path}' >> ~/.ssh/authorized_keys\n    chmod 0600 ~/.ssh/authorized_keys\n\n    # Remove the temporary file\n    rm -f '\#{remote_path}'\n  EOH\nend\n".gsub(/^ {12}/, '')