Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
chef中的powershell脚本一直被认为是ruby代码_Ruby_Powershell_Chef Infra - Fatal编程技术网

chef中的powershell脚本一直被认为是ruby代码

chef中的powershell脚本一直被认为是ruby代码,ruby,powershell,chef-infra,Ruby,Powershell,Chef Infra,我试图解决的最初问题是,ruby的chefDK版本没有出现,因为它位于PATH环境变量的末尾,所以我创建了一个代码块来检查,如果找不到,则添加它 powershell_script "add_chefdk_bin_to_shell" do code '$oldPath=(Get-ItemProperty -Path \'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environme

我试图解决的最初问题是,ruby的chefDK版本没有出现,因为它位于
PATH
环境变量的末尾,所以我创建了一个代码块来检查,如果找不到,则添加它

powershell_script "add_chefdk_bin_to_shell" do
  code '$oldPath=(Get-ItemProperty -Path \'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment\' -Name PATH).Path
          $newPath= \'C:\opscode\chefdk\embedded\bin;\' + $oldPath
          Set-ItemProperty -Path \'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment\' -Name PATH –Value $newPath
       '
  only_if { `powershell $a = $Env:Path; $a -match 'C:\\opscode\\chefdk\\embedded\\bin'` == "False" }
end
除非运行berk shelf(将厨师烹饪书发布到我们的厨师服务器),否则它会抛出以下错误:

**E, [2014-10-24T11:33:55.771484 #2916] ERROR -- : Cookbook file recipes/install.rb has a ruby syntax error:
E, [2014-10-24T11:33:55.771484 #2916] ERROR -- : c:/path/to/cookbook/root/recipes/install.rb:25: invalid multibyte char (US-ASCII)
E, [2014-10-24T11:33:55.771484 #2916] ERROR -- : c:/path/to/cookbook/root/recipes/install.rb:23: invalid multibyte char (US-ASCII)
E, [2014-10-24T11:33:55.771484 #2916] ERROR -- : c:/path/to/cookbook/root/recipes/install.rb:23: syntax error, unexpected $end, expecting keyword_end
E, [2014-10-24T11:33:55.771484 #2916] ERROR -- : ...ger\Environment\' -Name PATH –Value $newPath
E, [2014-10-24T11:33:55.771484 #2916] ERROR -- : ...                               ^
E, [2014-10-24T11:33:55.771484 #2916] ERROR -- : Ridley::Errors::CookbookSyntaxError: Invalid ruby files in cookbook: gg-web-opsboard (1.0.177).**
这让我很沮丧,我只是在第一时间这么做,因为chefDK没有被用作默认的ruby(这会把其他东西搞砸)

还有一个次要问题,如果我查看路径,我可以清楚地看到末尾的
C:\opscode\chefdk\embedded\bin
,那么为什么:

powershell$a=$Env:Path$a-匹配“C:\\opscode\\chefdk\\embedded\\bin”

注册为假

编辑:

我尝试了@acro444所说的内容,但仍有错误:

E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : Cookbook file recipes/install.rb has a ruby syntax error:
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : c:/path/to/cookbook/root/recipes/install.rb:24: Invalid escape character syntax
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : ...ry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Sess...
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : ...                               ^
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : c:/path/to/cookbook/root/recipes/recipes/install.rb:24: Invalid escape character syntax
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : ...HINE\System\CurrentControlSet\Control\Session Manager\Enviro...
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : ...                               ^
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : c:/path/to/cookbook/root/recipes/recipes/install.rb:26: Invalid escape character syntax
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : ...ry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Sess...
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : ...                               ^
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : c:/path/to/cookbook/root/recipes/install.rb:26: Invalid escape character syntax
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : ...HINE\System\CurrentControlSet\Control\Session Manager\Enviro...
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : ...                               ^
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : c:/path/to/cookbook/root/recipes/install.rb:26: invalid multibyte char (US-ASCII)
E, [2014-10-24T12:36:15.642147 #1932] ERROR -- : c:/path/to/cookbook/root/recipes/install.rb:23: syntax error, unexpected $end, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END 
我做了四倍反斜杠转义,就像你在所有反斜杠上做的那样,这将错误减少到:

语法错误,意外$end,应为tSTRING\u内容或tSTRING\u DBEG或tSTRING\u DVAR或tSTRING\u end

我删除了所有尾随的空白,但没有修复它,当前我的herdoc看起来像:

code <<-EOF
$oldPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\\\System\\\\CurrentControlSet\\\\Control\\\\Session Manager\\\\Environment' -Name PATH).Path
$newPath='C:\\\\opscode\\\\chefdk\\\\embedded\\\\bin;' + $oldPath
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\\\System\\\\CurrentControlSet\\\\Control\\\\Session Manager\\\\Environment' -Name PATH –Value $newPath
EOF

code尝试按如下方式构造块:

powershell_script "add_chefdk_bin_to_shell" do
  code <<-EOF
$oldPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
$newPath='C:\opscode\chefdk\embedded\bin;' + $oldPath
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $newPath
EOF
  only_if { `powershell -command "$Env:Path -match 'C:\\\\opscode\\\\chefdk\\\\embedded\\\\bin'".strip }` == "False"
end
您甚至可以在编写时替换掉防护,并使用内置的解释器:

guard_interpreter :powershell_script
not_if "$Env:Path -match 'C:\\\\opscode\\\\chefdk\\\\embedded\\\\bin'"

仔细阅读。

如果需要,可以跳过保护块,并将部分操作移动到ruby,如下所示:

ruby_block "chefdk_path_check" do
  block do
    current_path = ENV['PATH'].split(';')
    chefdk_path = 'C:\\opscode\\chefdk\\embedded\\bin'
    current_path.delete_if { |path| path == chefdk_path }
    new_path = current_path.unshift(chefdk_path)
    `Powershell -Command Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment' -Name PATH –Value '#{new_path}'`
    end
end

这将每次检查,删除任何现有条目(如果它位于路径中的错误位置),并将所需条目添加到开头。没有重复,更不用担心逃跑了

这可能行得通,不过我必须为幂等性添加一个块。我决定按照我喜欢的方式来做,这就是使用ruby,我确实为幂等性添加了一个检查;它将检查数组中的前3个元素,如果它们与我要查找的路径不匹配,它将执行您编写的操作。
ruby_block "chefdk_path_check" do
  block do
    current_path = ENV['PATH'].split(';')
    chefdk_path = 'C:\\opscode\\chefdk\\embedded\\bin'
    current_path.delete_if { |path| path == chefdk_path }
    new_path = current_path.unshift(chefdk_path)
    `Powershell -Command Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment' -Name PATH –Value '#{new_path}'`
    end
end