1

我想存储一个构建版本号,以后可以在配方中使用它来解析“源”行中的 msi 路径。此版本特定于环境。我不确定是使用环境属性还是数据包,但无论哪种方式,我都希望能够从脚本更新属性或数据包项(我有一个调用各种 AWS 和刀功能的主 powershell 脚本) .

但是,基本的“刀环境编辑..”或“刀数据包编辑..”默认调用编辑器来手动编辑无法从脚本中使用的定义。

我已经搜索了解决方案并遇到了以下建议,但是是否有任何公认的“最佳实践”或替代方案?

顺便说一句,我发现从 powershell 脚本运行它很棘手:在尝试创建数据包的情况下,如果我通过 here-doc 将 ruby​​ 代码设置为变量并调用“knife exec -E $rubycode" 不报错但也不创建数据包?

$rubycode= @"
require 'net/http'
require 'chef/rest'
require 'chef/config'
require 'chef/data_bag'
require 'chef/data_bag_item'

bagname = 'myapps'
appname = 'foo'
version = '1.0.0'

#Use the same config as knife uses
Chef::Config.from_file(File.join(ENV['USERPROFILE'], '.chef', 'knife.rb'))

#Load data bag item, or create it if it doesn't exist yet
begin
  item = Chef::DataBagItem.load(bagname, appname)
rescue Net::HTTPServerException => e
  if e.response.code == "404" then
    puts("INFO: Creating a new data bag item")
    item = Chef::DataBagItem.new
    item.data_bag(bagname)
    item['id'] = appname
  else
    puts("ERROR: Received an HTTPException of type " + e.response.code)
    raise
  end
end

item['version'] = version
item.save
"@#

 knife exec -E $rubycode

例如,在我的 powershell 脚本中,为了创建一个我正在尝试的环境(我还没有更新现有环境中的属性):

# A POST call to /environments creates a new environment. The request body should contain the JSON representation of the environment.
$environment_desc = @"
{
  "name": "regtest",
  "description": "",
  "cookbook_versions": {},
  "json_class": "Chef::Environment",
  "chef_type": "environment",
  "override_attributes": {}
}
"@
knife exec -E "api.post('/environments',$environment_desc)"

我收到以下错误,我怀疑这可能与 powershell 解析有关

错误:语法错误:-E 参数:语法错误,意外 $end,期待 '}' api.post('/environments',{

我意识到一些错误很可能是由于 powershell 解析造成的,但我的问题是根据主题 - 是否有最佳实践方法来以非交互方式编辑环境和数据包,无论是来自上面的列表还是其他一些方法?

4

0 回答 0