厨师食谱(Ruby)

发布于 2025-02-02 08:55:26 字数 709 浏览 6 评论 0原文

我正在制作厨师食谱,该食谱将根据Linux服务器中的内存总数拉下程序安装程序。如果内存总数为8GB或更多。...如果内存小于8GB,则安装...。有人碰巧知道如何脚本吗? 厨师是基于红宝石的。 在我的厨师食谱中,我尝试了以下内容,但没有成功。

puts "***** Linux server node['platform_family']=#{node['platform_family']}"
puts "***** Linux server node['memory.total']=#{node['memory.total']}"
# -------------------------------------------------------------------------------
puts "*****#{node['platform_family']}"
puts "*****#{node['memory.total']}"

if node['platform_family'] == 'debian' && node['memory.total'] >= 7000000
   remote_file '/tmp'
        source 'local file'
        action :create
   end
elsif
  remote_file '/tmp'
       source 'external file'
       action :create
  end

I'm working on a chef recipe that will pull down a program installer depending on the memory total in a linux server. If the memory total is 8GB or more install .... if the memory is less than 8GB then install ... . Does anybody happen to know how to script this?
Chef is ruby based.
Within my chef recipes I have attempted the following, but had no success.

puts "***** Linux server node['platform_family']=#{node['platform_family']}"
puts "***** Linux server node['memory.total']=#{node['memory.total']}"
# -------------------------------------------------------------------------------
puts "*****#{node['platform_family']}"
puts "*****#{node['memory.total']}"

if node['platform_family'] == 'debian' && node['memory.total'] >= 7000000
   remote_file '/tmp'
        source 'local file'
        action :create
   end
elsif
  remote_file '/tmp'
       source 'external file'
       action :create
  end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪 2025-02-09 08:55:26

如果我们可以事先计算source属性的值,则remote_file资源可以以更好的方式(一次)编写。另外,由于您已经显示了node ['platform_family']的比较,因此您似乎还需要匹配debian以外的OS家族。

以下代码将匹配platform_family['MOMERY'] ['total'],并设置一个可以与source property一起使用的值。

mem = node['memory']['total'][/\d*/].to_i

remote_src = case node['platform_family']
when 'debian'
  mem >= 7000000 ? 'local file' : 'remote file'
when 'redhat'
  mem >= 7000000 ? 'some file' : 'some other file'
# when some other OS family if required
end

remote_file '/tmp/somefile' do
  source remote_src
  action :create
end

请注意,我正在使用 ternary oterator 代码>如果/elsecase> case语句的情况时。

The remote_file resource can written in a better way (once) if we can compute the value of source property beforehand. Also since you have shown comparison of node['platform_family'], it appears you would need to match for OS families other than debian as well.

Below code will match for platform_family and ['memory']['total'] and set a value that we can use with the source property.

mem = node['memory']['total'][/\d*/].to_i

remote_src = case node['platform_family']
when 'debian'
  mem >= 7000000 ? 'local file' : 'remote file'
when 'redhat'
  mem >= 7000000 ? 'some file' : 'some other file'
# when some other OS family if required
end

remote_file '/tmp/somefile' do
  source remote_src
  action :create
end

Note that I'm using ternary operator as an alternative to if / else within when condition of case statement.

少女净妖师 2025-02-09 08:55:26

如果您只想在Linux中使用此食谱,那么此方法将起作用:

memory_in_megabytes = node.memory.total[/\d*/].to_i / 1024

if memory_in_megabytes > 512
  ## Do your thing!
end

不同的OSS以不同的方式报告此值,但并非总是以相同的格式报告。因此,如果您想支持任何系统平台(以防万一使用不使用Linux的人使用),这将是一个更好的选择:

memory_in_megabytes = case node['os']
when /.*bsd/
  node.memory.total.to_i / 1024 / 1024
when 'linux'
  node.memory.total[/\d*/].to_i / 1024
when 'darwin'
  node.memory.total[/\d*/].to_i
when 'windows', 'solaris', 'hpux', 'aix'
  node.memory.total[/\d*/].to_i / 1024
end

if memory_in_megabytes > 512
  ## Do your thing!
end

If you're only ever going to use this recipe in Linux, then this method will work:

memory_in_megabytes = node.memory.total[/\d*/].to_i / 1024

if memory_in_megabytes > 512
  ## Do your thing!
end

Different OSes report this value differently and not always in the same format though. So if you want to support any system platform (just in case this has a use for someone not using Linux) this would be a far better option:

memory_in_megabytes = case node['os']
when /.*bsd/
  node.memory.total.to_i / 1024 / 1024
when 'linux'
  node.memory.total[/\d*/].to_i / 1024
when 'darwin'
  node.memory.total[/\d*/].to_i
when 'windows', 'solaris', 'hpux', 'aix'
  node.memory.total[/\d*/].to_i / 1024
end

if memory_in_megabytes > 512
  ## Do your thing!
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文