草庐IT

ruby-on-rails - ActiveRecord 如何将现有记录添加到 has_many :through relationship in rails? 中的关联

在我的Rails项目中,我有三个模型:classRecipe:recipe_categorizationsaccepts_nested_attributes_for:recipe_categories,allow_destroy::trueendclassCategory:recipe_categorizationsendclassRecipeCategorization通过这个简单的has_many:through设置,我怎样才能像这样获取给定的食谱:@recipe=Recipe.first并根据现有类别向此食谱添加类别,并在相应类别上对其进行更新。所以:@category=#Exi

ruby - 您如何模块化 Chef Recipe ?

这是一个工作配方示例,它循环访问网站名称数组并使用函数createIisWebsite()在IIS中创建它们。defcreateIisWebsite(websiteName)iis_sitewebsiteNamedoprotocol:httpport80path"#{node['iis']['docroot']}/#{websiteName}"host_header"#{websiteName}.test.kermit.a-aws.co.uk"action[:add,:start]endend在我们的实际解决方案中,这些数据存储在别处并通过WebAPI访问。websiteNames=[

ruby - 如何从 Chef 说明书中的库访问当前节点?

我正在尝试为ChefRecipe编写一个库,以简化一些常见的搜索。例如,我希望能够在cookbook/libraries/library.rb中执行类似的操作,然后从同一Recipe中的Recipe中使用它:moduleExampledefself.search_attribute(attribute_name)returnsearch(:nodes,node[attribute_name])endend问题是,在Chef库文件中,node对象或search函数都不可用。似乎可以使用Chef::Search::Query.new().search(...)进行搜索,但我找不到任何可以访

ruby - apt Recipe 不会安装在我的 Recipe 中

我正在尝试使用Vagrant创建我的第一个ChefRecipe,但在第一步就遇到了问题。我的Recipe的第一行是:include_recipe"apt"但是当我尝试vagrantprovision时,出现以下错误:==>default:[2014-09-21T07:15:42+00:00]WARN:MissingCookbookDependency:==>default:Recipe`apt`isnotintherun_list,andcookbook'apt'==>default:isnotadependencyofanycookbookintherun_list.Toloadth

ruby - Chef : Can a variable set within one ruby_block be used later in a recipe?

假设我有一个变量directory_list,我在名为get_directory_list的ruby​​_block中定义和设置了它。我可以稍后在我的Recipe中使用directory_list吗,或者编译/收敛过程会阻止这种情况吗?例子:ruby_block"get_file_list"doblockdotransferred_files=Dir['/some/dir/*']endendtransferred_files.eachdo|file|file"#{file}"dogroup"woohoo"user"woohoo"endend 最佳答案

ruby - 如何在 Chef Recipe 中使用 not_if

我是Chef的新手,所以我对条件not_if在执行资源中的工作方式有些困惑。我知道如果命令返回0或true,它会告诉Chef不要执行命令;但是,在我的代码中,它显然仍在运行命令。下面的代码应该创建一个用户(及其密码)和一个数据库;然而,如果用户和数据库已经存在,它不应该做任何事情。用户、数据库和密码在属性中定义。以下是我的代码:execute"create-user"docode=Chef给我以下错误:在资源“execute[create-user]”上执行操作run时出错...[2013-01-25T12:24:51-08:00]致命:Mixlib::ShellOut::ShellC

ruby - 在 vagrant rbenv NoMethodError loaded_recipe 上安装 ruby​​ 1.9.3?

我正在尝试vagrant安装并希望将1.9.3-p327作为默认的ruby​​版本。我正在使用chef-solo和librarian-chef来管理vagrant机器。我为chef-solo配置的vagrant文件看起来像这样config.vm.provision:chef_solodo|chef|chef.cookbooks_path="cookbooks"chef.add_recipe"apt"chef.add_recipe"build-essential"chef.add_recipe"git"chef.add_recipe"ruby_build"chef.add_recipe"

ruby - 如何在 Chef-repo 中跨 Chef Recipe 共享代码?

我想在ChefRepo中分享一些跨Recipe的方法。我知道在Recipe级别上,我可以将代码放入库目录中的模块中(参见relatedquestion)。我正在寻找的是类似的东西,但在我的Chef存储库中的所有Recipe中都可以找到。我可以想到几个解决方案:创建一个gem,将gem安装为chef运行的一部分。这似乎有点矫枉过正。将文件放在某个文件夹中,然后将该文件夹添加到配方文件中的$LOAD_PATH中。我感觉这不适用于实际部署,因为Chef服务器对repo一无所知。将文件放在某个文件夹中,并将其符号链接(symboliclink)到每本Recipe的库目录中。最后一个选项似乎是最

ruby - 如何将 linux 命令输出到 chef 属性

我想将命令输出到chef属性中。有人可以帮助我如何在执行资源或bash资源中设置它。ruby_block"something"doblockdo#trickywaytoloadthisChef::Mixin::ShellOututilitiesChef::Resource::RubyBlock.send(:include,Chef::Mixin::ShellOut)command='cat#{fileName}'command_out=shell_out(command)node.set['my_attribute']=command_out.stdoutendaction:creat

ruby - 是否可以从 vagrant box 调试 Chef Recipe ?

我正在为一个vagrantbox的ChefRecipe而苦苦挣扎。我正在使用chef-solo。我对Ruby不是很满意(我以前从未使用过),所以如果我必须这样做的话,编写我的Recipe的过程会很长vagrantprovision每次。有没有办法调试特定的配方?甚至来自虚拟机外壳。我看过chef-shell存在(http://docs.opscode.com/chef_shell.html),当我在虚拟机中时我可以启动它(它工作),但我不明白如何加载我的Recipe并测试它们。 最佳答案 在当前vagrantchefprovisio