File: mount_shared_folder.rb

package info (click to toggle)
vagrant 2.2.14%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,800 kB
  • sloc: ruby: 97,301; sh: 375; makefile: 16; lisp: 1
file content (57 lines) | stat: -rw-r--r-- 2,518 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require "vagrant/util/template_renderer"
require "base64"

module VagrantPlugins
  module GuestWindows
    module Cap
      class MountSharedFolder
        def self.mount_virtualbox_shared_folder(machine, name, guestpath, options)
          mount_shared_folder(machine, name, guestpath, "\\\\vboxsvr\\")
        end

        def self.mount_vmware_shared_folder(machine, name, guestpath, options)
          mount_shared_folder(machine, name, guestpath, "\\\\vmware-host\\Shared Folders\\")
        end

        def self.mount_parallels_shared_folder(machine, name, guestpath, options)
          mount_shared_folder(machine, name, guestpath, "\\\\psf\\")
        end

        def self.mount_smb_shared_folder(machine, name, guestpath, options)
          if !options[:smb_password].to_s.empty?
            # Ensure password is scrubbed
            Vagrant::Util::CredentialScrubber.sensitive(options[:smb_password])
          end
          machine.communicate.execute("cmdkey /add:#{options[:smb_host]} /user:#{options[:smb_username]} /pass:\"#{options[:smb_password]}\"", {shell: :powershell, elevated: true})
          mount_shared_folder(machine, name, guestpath, "\\\\#{options[:smb_host]}\\")
        end

        protected

        def self.mount_shared_folder(machine, name, guestpath, vm_provider_unc_base)
          name = name.gsub(/[\/\/]/,'_').sub(/^_/, '')

          path = File.expand_path("../../scripts/mount_volume.ps1", __FILE__)
          script = Vagrant::Util::TemplateRenderer.render(path, options: {
            mount_point: guestpath,
            share_name: name,
            vm_provider_unc_path: vm_provider_unc_base + name,
          })

          if machine.config.vm.communicator == :winrm || machine.config.vm.communicator == :winssh
            machine.communicate.execute(script, shell: :powershell)
          else
            # Convert script to double byte unicode string then base64 encode
            # just like PowerShell -EncodedCommand expects.
            # Suppress the progress stream from leaking to stderr.
            wrapped_encoded_command = Base64.strict_encode64(
              "$ProgressPreference='SilentlyContinue'; #{script}; exit $LASTEXITCODE".encode('UTF-16LE', 'UTF-8'))
            # Execute encoded PowerShell script via OpenSSH shell
            machine.communicate.execute("powershell.exe -noprofile -executionpolicy bypass " +
              "-encodedcommand '#{wrapped_encoded_command}'", shell: "sh")
          end
        end
      end
    end
  end
end