终端上的Vagrant argv输入抱怨机器名称

时间:2015-07-16 17:56:45

标签: ruby vagrant command-line-interface vagrantfile

我试图将参数(通过已知的ruby方法)传递给我的vagrant up命令行,但是我发现机器未找到错误。在Vagrant中这样做的正确方法是什么?

Vagrantfile

$scope.survey = {
        "chapters" : [
            {
                "questions" : [
                    {
                        "verbose" : "que1_verbose1",
                        "id": "que1_1"
                    },
                    {
                        "verbose" : "que1_verbose2",
                        "id": "que1_2"
                    }
                ]
            },
            {
                "questions" : [
                    {
                        "verbose" : "que2_verbose1",
                        "id": "que2_1"
                    },
                    {
                        "verbose" : "que2_verbose2",
                        "id": "que2_2"
                    }
                ]
            }
        ]
    };

$scope.sOptions = [];

angular.forEach($scope.survey.chapters, function(chapter) {
   angular.forEach(chapter.questions, function(question) {
      $scope.sOptions.push(question);
   });  
});

错误

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Parse options
options = {}
options[:keyfile] = ARGV[1] || false         # Your Github authentication keyfile
options[:boxtype] = ARGV[2] || 'virtualbox' # Type of virtual appliance to load
options[:version] = ARGV[3] || 'latest'     # Box version to load (not used currently)

ARGV.delete_at(1)
ARGV.delete_at(1)
ARGV.delete_at(1)


Vagrant.configure("2") do | config |

  puts ("==> [info] Looking for #{options[:keyfile]}")
  if File.file?(options[:keyfile])
    config.vm.provision :shell, :inline => "echo -e '#{File.read(options[:keyfile])}' > '/home/vagrant/.ssh/GitKey'"
  else
    puts ("==> [error] The require RSA key: #{options[:keyfile]} does not exist, exiting.")
    abort
  end
end

修改

尝试以不同的方式给我一些稍微有希望的结果

$ vagrant up ~/.ssh/github_rsa
The machine with the name '/Users/ehime/.ssh/github_rsa' was not found configured for
this Vagrant environment.

让我非常接近,但它需要以某种方式取消设置标志,这样它们就不会与流浪者发生冲突。至少帮助标志工作

require 'optparse'
require 'ostruct'

....

options = OpenStruct.new
OptionParser.new do | opt |
  opt.on('-v', '--version VERSION', 'Box version to load (not used currently)')  { | o | options.version = o }
  opt.on('-k', '--keyfile KEYFILE', 'Your Github authentication keyfile')        { | o | options.keyfile = o }
  opt.on('-b', '--boxfile BOXTYPE', 'Type of virtual appliance to load')         { | o | options.boxtype = o }
end.parse!

Vagrant.configure("2") do | config |

  puts ("==> [info] Looking for #{options.keyfile}")
  if File.file?(options.keyfile)
    config.vm.provision :shell, :inline => "echo -e '#{File.read(options.keyfile)}' > '/home/vagrant/.ssh/GitKey'"
  else
    puts ("==> [error] The require RSA key: #{options.keyfile} does not exist, exiting.")
    abort
  end

....

帮助

 $ vagrant up -k /Users/ehime/.ssh/github_rsa
 ==> [info] Looking for /Users/ehime/.ssh/github_rsa
 An invalid option was specified. The help for this command
 is available below.

 Usage: vagrant up [options] [name]

 Options:

         --[no-]provision             Enable or disable provisioning
         --provision-with x,y,z       Enable only certain provisioners, by type.
         --[no-]destroy-on-error      Destroy machine if any fatal error happens (default to true)
         --[no-]parallel              Enable or disable parallelism if provider supports it
         --provider PROVIDER          Back the machine with a specific provider
     -h, --help                       Print this help 

1 个答案:

答案 0 :(得分:0)

Vagrantfile不会直接执行,因此您不能像使用普通脚本那样传递参数。 vagrant在cwd()中查找文件并将其带入。
在运行vagrant之前,会使用env vars的路径或生成的模板文件。