【发布时间】:2017-03-23 09:27:53
【问题描述】:
我正在尝试从我接受 URL 作为参数的 SVN 存储库中签出代码。我引用了如下所示的 URL,因为它包含空格。我还通过重定向文件中的$svn_url 来检查参数(如下所示)。如果我从文件中选择 URL 并在命令行上将其 按原样传递给给定的脚本,它可以正常工作,但是当从 Puppet 调用时,它不工作。
木偶清单:
repo_checkout.pp:
define infra::svn::repo_checkout ($svn_url_params) {
$svn_url = $svn_url_params[svn_url]
include infra::params
$repo_checkout_ps = $infra::params::repo_checkout_ps
file { $repo_checkout_ps:
ensure => file,
source => 'puppet:///modules/infra/repo_checkout.ps1',
}
util::executeps { 'Checking out repo':
pspath => $repo_checkout_ps,
argument => "\'\"$svn_url\"\'",
}
}
params.pp:
$repo_checkout_ps = 'c:/scripts/infra/repo_checkout.ps1',
site.pp:
$svn_url_ad = {
svn_url => 'https:\\\\some_repo.abc.com\svn\dir with space\util',
}
infra::svn::repo_checkout { "Checking out code in C:\build":
svn_url_params => $svn_url_ad
}
executeps.pp:
define util::executeps ($pspath, $argument) {
$powershell = 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -NoLogo -NonInteractive'
exec { "Executing PS file \"$pspath\" with argument \"$argument\"":
command => "$powershell -file $pspath $argument",
timeout => 900,
}
}
PowerShell 代码:
$svn_url = $args[0]
Set-Location C:\build
echo "svn co --username user --password xxx --non-interactive '$svn_url'" | Out-File c:\svn_url
svn co --username user --password xxx --non-interactive '$svn_url'
代理节点上的 Puppet 输出:
Util::Executeps[签出 repo]/Exec[使用参数“'”执行 PS 文件“c:/scripts/infra/repo_checkout.ps1”https:\\some_repo.abc.com\svn\dir with space\util” '"]/returns: 执行成功 注意:1.83秒内应用目录c:\svn_url的内容:
更新:很抱歉造成混乱,但我尝试了几种排列和组合,在此过程中,我忘记提及当 $svn_url 包含反斜杠 (\) 时,它确实不工作如果我从要重定向 echo 输出的文本文件中复制 SVN URL,也在命令行上。
根据@Ansgar 的建议,我在powershell 代码中将'$svn_url' 更改为"$svn_url",但文本文件中的输出随后在URL 周围包含' 引号两次。所以我将argument 参数从"\'\"$svn_url\"\'" 更改为"\"$svn_url\""。现在,输出文件的 URL 周围只有单引号。我只从输出文件中复制了 URL(连同它周围的单引号),并尝试将其传递给 powershell 脚本。我现在收到以下错误:
svn: E020024: Error resolving case of 'https:\\some_repo.abc.com\svn\dir with space\util'
要注意的另一件事是,如果我将 URL 中的反斜杠更改为正斜杠,它可以在命令行上运行很好。从 Puppet 调用仍然不起作用。
【问题讨论】:
-
在您的 PowerShell 代码中:
'$svn_url'->"$svn_url"。另外,为什么argument => "\'\"$svn_url\"\'"中的双嵌套引用? -
@AnsgarWiechers:请查看更新部分。
-
该错误可能是由backslashes in your URL 引起的。尝试将它们更改为正斜杠。
-
有时使用 Puppet 真的很令人沮丧和困惑。 ;)
-
我现在无法测试它,但我会将 URL 更改为正斜杠,因为这是 应该 工作的。如果不是,您将需要对此进行调查。当我需要对此类问题进行故障排除时,我通常会在清单中插入一些
notify语句,以便更好地了解代码中断的位置以及变量实际具有的值。我还建议仅在实际需要引号时添加引号,例如当您实际将变量放入命令行时。
标签: powershell svn puppet powershell-5.0