File: provisioner.rb

package info (click to toggle)
vagrant 2.2.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,072 kB
  • sloc: ruby: 80,731; sh: 369; makefile: 9; lisp: 1
file content (60 lines) | stat: -rw-r--r-- 2,482 bytes parent folder | download
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
58
59
60
module VagrantPlugins
  module FileUpload
    class Provisioner < Vagrant.plugin("2", :provisioner)
      def provision
        @machine.communicate.tap do |comm|
          source = File.expand_path(config.source)
          destination = expand_guest_path(config.destination)

          # if source is a directory, make it then trim destination with dirname
          # Make sure the remote path exists
          if File.directory?(source)
            # We need to make sure the actual destination folder
            # also exists before uploading, otherwise
            # you will get nested folders
            #
            # https://serverfault.com/questions/538368/make-scp-always-overwrite-or-create-directory
            # https://unix.stackexchange.com/questions/292641/get-scp-path-behave-like-rsync-path/292732
            command = "mkdir -p \"%s\"" % destination
            if !destination.end_with?(File::SEPARATOR) &&
                !source.end_with?("#{File::SEPARATOR}.")
              # We also need to append a '/.' to the source folder so we copy
              # the contents rather than the folder itself, in case a users
              # destination folder differs from its source
              #
              # If source is set as `source/` it will lose the trailing
              # slash due to how `File.expand_path` works, so we don't need
              # a conditional for that case.
              if @machine.config.vm.communicator == :winrm
                # windows needs an array of paths because of the
                # winrm-fs function Vagrant is using to upload file/folder.
                source = Dir["#{source}#{File::SEPARATOR}*"]
              else
                source << "#{File::SEPARATOR}."
              end
            end
          else
            command = "mkdir -p \"%s\"" % File.dirname(destination)
          end
          comm.execute(command)

          @machine.ui.detail(I18n.t("vagrant.actions.vm.provision.file.locations",
                                   src: source, dst: destination))
          # now upload the file
          comm.upload(source, destination)
        end
      end

      private

      # Expand the guest path if the guest has the capability
      def expand_guest_path(destination)
        if machine.guest.capability?(:shell_expand_guest_path)
          machine.guest.capability(:shell_expand_guest_path, destination)
        else
          destination
        end
      end
    end
  end
end