【问题标题】:How can I upload more than one file with vagrant file provisioning?如何使用 vagrant 文件配置上传多个文件?
【发布时间】:2016-02-12 02:29:31
【问题描述】:

在 Vagrant 设置中,我必须在配置阶段将几个文件从主机上传到客户机。

https://docs.vagrantup.com/v2/provisioning/file.html 中,我可以看到如何将单个文件从源上传到目标,但如何上传多个文件或文件夹结构?

注意:我知道我可以使用 shell 配置器做到这一点,但在这种特殊情况下,一组文件上传会更合适。

【问题讨论】:

    标签: vagrant virtualbox vagrantfile


    【解决方案1】:

    每个文件都需要一个单独的config.vim.provision 部分。您可以将多个这些部分添加到您的 Vagrantfile,如下所示:

    config.vm.provision :file do |file|
      file.source = "/etc/file1.txt"
      file.destination = "/tmp/1.txt"
    end
    
    config.vm.provision :file do |file|
      file.source = "/etc/file2.txt"
      file.destination = "/tmp/2.txt"
    end
    

    输出:

    [default] Running provisioner: file...
    [default] Running provisioner: file...
    

    您会看到它正在执行两个配置操作。您可以验证虚拟机中的文件是否存在:

    vagrant ssh -- ls -al /tmp/{1,2}.txt
    -rw-r--r-- 1 vagrant vagrant 4 Aug 27 08:22 /tmp/1.txt
    -rw-rw-r-- 1 vagrant vagrant 4 Aug 27 08:22 /tmp/2.txt
    

    【讨论】:

    • 看起来正是我要找的东西...关于整个文件夹结构的任何提示?
    • @GiacomoTesio 如果您想使整个文件夹可访问,请使用config.vm.synced_folder
    • 这不是我需要的。例如,我有一些文件要从主机移动到 /etc/apt/source.list.d/ 中。我尝试在 shell 配置器中使用 tar(类似于 (cd root/ && tar cf - *) | (cd / && tar xf - --keep-old-files) ),但它不起作用。
    • 然后你需要用ruby遍历文件夹并动态创建config.vm.provision条目。
    【解决方案2】:

    文件夹内容上传似乎也能正常工作(我只尝试了文件,而不是嵌套文件夹):

    config.vm.provision :file, source: '../folder', destination: "/tmp/folder"
    

    【讨论】:

    • 是的,这似乎工作得很好。还有嵌套文件夹。
    • 我遇到了麻烦。 source:'../one/two', destination: '/destination' 第一次按预期工作,所有文件都在 /destination 中。再次运行 vagrant provision,你会得到 /destination/two 中的所有文件
    • 虽然这似乎可行,但问题是嵌套文件夹问题。在这种情况下,/tmp/ 会随着 Vagrant 框的每次重新启动而刷新,因此您将看不到它。但是,如果您将此命令与以下目标一起使用,则在重新启动之间保持不变:/home/vagrant/folder/。 Vagrant box 第一次启动时,你会得到/home/vagrant/folder/。下次启动 Vagrant 框时,您将收到 /home/vagrant/folder/folder/。那之后的下一个呢? /home/vagrant/folder/folder/folder/config.vm.synced_foldertype: "rsync" 效果更好。使用 Vagrant 1.8.1。
    【解决方案3】:

    如果其他人只需要快速而肮脏的 hack 将目录结构从主机复制到客户机,我最终就是这样做的:

    require 'pathname' # at the beginning of the Vagrantfile
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    
      # more configuration here
    
      r = Pathname.new 'root'
      Dir.glob(File.join("root", File.join("**","*"))) do |f|
        if !File.directory?(f) then
          s = Pathname.new f
          t = s.relative_path_from r
          config.vm.provision "file" do |fp|
            fp.source = s.to_s
            fp.destination = "/tmp/root/" + t.to_s
          end
        end
      end
    
      # more configuration here
    
    end
    

    感谢@hek2mgl 让我加入the right track

    【讨论】:

    【解决方案4】:

    这对我有用,cd 到你的 vagrant 然后运行

    vagrant up
    vagrant ssh
    
    vagrant@yourvagrant: sudo chown -R testuser:testuser /var/www/ or whatever folder you want to upload your files
    

    在你的 Vagrantfile 中,添加

    # we create add script to upload folder to a vagrant guest from local file
    config.vm.provision :file, source: '/wamp/www/ian_loans', destination:  "/var/www/solidbond"
    

    只需将文件目录替换为您的需要。退出并 并且不要忘记在你的 vagrant 目录中运行

    vagrant provision
    

    最后进入你的Ubuntu目录,看看你要上传的文件有没有。

    vagrant@yourvagrant: cd /var/your file directory
    

    【讨论】:

      【解决方案5】:

      您可以使用文件配置器,正如其他人所提到的,但仍然值得注意的是,可以使用类型设置为 rsync 的同步文件夹功能:http://docs.vagrantup.com/v2/synced-folders/rsync.html:

      Vagrant 可以使用 rsync 作为一种将文件夹同步到访客的机制 机器。这种同步的文件夹类型主要在某些情况下很有用 在其他同步文件夹机制不可用的情况下,例如何时 NFS 或 VirtualBox 共享文件夹在来宾中不可用 机器。

      rsync 同步文件夹从机器进行一次性单向同步 运行到 Vagrant 正在启动的机器上。

      第一次vagrant up 同步发生在配置程序执行之前。 Vagrantfile 中的示例配置如下所示:

      config.vm.synced_folder "upload/", "/home/vagrant", type: "rsync"
      

      【讨论】:

      • -1:如前所述,此配置非常危险,并且对基本的 Vagrant 功能具有破坏性。我在 Mac OS X 10.10.5 (Yosemite) 上使用 Vagrant 1.8.1。通过将源设置为upload/,将目标设置为/home/vagrant,Rsync 进程将覆盖/home/vagrant,这意味着Vagrant 用户的.ssh 目录将被清除,因此他们在执行此命令后无法再通过SSH 登录这台机器正在运行。目的地应该是/home/vagrant/upload/,所以配置应该是config.vm.synced_folder "upload/", "/home/vagrant/upload/", type: "rsync"
      【解决方案6】:

      这有点老了,但我会在这里为使用 PowerShell 的 Windows 用户的整个目录结构提供另一个答案。

      首先,创建一个从共享文件夹复制整个文件夹结构的 Powershell 脚本。

      #Check if the directory you're copying already exists on the VM
      $foldercheck = "path\to\folder\on\vm"
      if (test-path $foldercheck)
      {
          Write-Host "Folder 'path\to\folder\on\vm' already exists..."
      }
      else {
          #Create the new directory
          New-Item -ItemType Directory -Force -Path "path\to\folder\on\vm"
      
          #Copy over contents to the directory
          Copy-Item "\\VBOXSRV\<share_name>\path\to\folder\to\copy\*" -Destination "path\to\folder\on\vm" -recurse
      }
      

      然后,将 Vagrantfile 配置为自动挂载共享文件夹,复制 PowerShell 脚本,然后按指定的顺序运行它们。

      Vagrant.configure(2) do |config|
          config.vm.provider "virtualbox" do |vb|
              # Other settings
              # ...
              # Add shared folders
              vb.customize ["sharedfolder", "add", :id, "--name", "<share_name>", "--hostpath", "path/to/host/folder/location", "--automount"]
          end
          # =================================================================
          # PROVISIONING
          # =================================================================
          # Load up the files to the Guest environment for use by PowerShell
          config.vm.provision "file", source: "path/to/provisioning/files/powershellfile.ps1", destination: "path/to/temporary/location/powershellfile.ps1"
          # Rinse/repeat for additional provisioning files
      
          # Now provision files in the order they are specified
          config.vm.provision "shell", inline: "path/to/temporary/location/powershellfile.ps1"
          # Rinse/repeat to run PowerShell scripts
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-10
        • 1970-01-01
        • 1970-01-01
        • 2022-07-23
        • 2013-06-30
        • 2014-05-04
        • 2016-03-15
        相关资源
        最近更新 更多