<commit_before>require "tempfile"

require "vagrant/util/shell_quote"

module VagrantPlugins
  module GuestEsxi
    module Cap
      class PublicKey
      
        def self.insert_public_key(machine, contents)
          comm = machine.communicate
          contents = contents.strip << "\n"

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

          comm.execute <<-EOH.gsub(/^ {12}/, "")
            set -e
            SSH_DIR="$(grep -q '^AuthorizedKeysFile\s*\/etc\/ssh\/keys-%u\/authorized_keys$' /etc/ssh/sshd_config && echo -n /etc/ssh/keys-${USER} || echo -n ~/.ssh)"
            mkdir -p "${SSH_DIR}"
            chmod 0700 "${SSH_DIR}"
            cat '#{remote_path}' >> "${SSH_DIR}/authorized_keys"
            chmod 0600 "${SSH_DIR}/authorized_keys"
            rm -f '#{remote_path}'
          EOH
        end

        def self.remove_public_key(machine, contents)
          comm = machine.communicate
          contents = contents.strip << "\n"

          remote_path = "/tmp/vagrant-remove-pubkey-#{Time.now.to_i}"
          Tempfile.open("vagrant-esxi-remove-public-key") do |f|
            f.binmode
            f.write(contents)
            f.fsync
            f.close<commit_msg>esxi guest: do not fail when we cannot set the ssh directory permissions in esxi 7.0<commit_after>require "tempfile"

require "vagrant/util/shell_quote"

module VagrantPlugins
  module GuestEsxi
    module Cap
      class PublicKey
      
        def self.insert_public_key(machine, contents)
          comm = machine.communicate
          contents = contents.strip << "\n"

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

          comm.execute <<-EOH.gsub(/^ {12}/, "")
            set -e
            SSH_DIR="$(grep -q '^AuthorizedKeysFile\s*\/etc\/ssh\/keys-%u\/authorized_keys$' /etc/ssh/sshd_config && echo -n /etc/ssh/keys-${USER} || echo -n ~/.ssh)"
            mkdir -p "${SSH_DIR}"
            # NB in ESXi 7.0 we cannot change the /etc/ssh/keys-root directory
            #    permissions, so ignore any errors.
            chmod 0700 "${SSH_DIR}" || true
            cat '#{remote_path}' >> "${SSH_DIR}/authorized_keys"
            chmod 0600 "${SSH_DIR}/authorized_keys"
            rm -f '#{remote_path}'
          EOH
        end

        def self.remove_public_key(machine, contents)
          comm = machine.communicate
          contents = contents.strip << "\n"

          remote_path = "/tmp/vagrant-remove-pubkey-#{Time.now.to_i}"
          Tempfile.open("vagrant-esxi-remove-public-key") do |f|
            f.binmode
            f.write(contents)
            f.fsync
            f.close
{'fraction_non_alphanumeric': 0.1300259403372244, 'fraction_numerical': 0.007782101167315175, 'mean_word_length': 1.7868112014453479, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4424648', 'n_tokens_mistral': 1013, 'n_tokens_neox': 971, 'n_words': 194}