简体   繁体   English

厨师食谱(红宝石)

[英]Chef Recipe (RUBY)

I'm working on a chef recipe that will pull down a program installer depending on the memory total in a linux server.我正在研究一个厨师食谱,它将根据 linux 服务器中的内存总量拉下程序安装程序。 If the memory total is 8GB or more install .... if the memory is less than 8GB then install ... .如果内存总量为 8GB 或更多,请安装 .... 如果内存小于 8GB,则安装 .... Does anybody happen to know how to script this?有没有人碰巧知道如何编写这个脚本? Chef is ruby based. Chef 是基于红宝石的。 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

The remote_file resource can written in a better way (once) if we can compute the value of source property beforehand.如果我们可以预先计算source属性的值, remote_file资源可以以更好的方式(一次)编写。 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.此外,由于您已经显示了node['platform_family']比较,看来您还需要匹配除debian以外的操作系统系列。

Below code will match for platform_family and ['memory']['total'] and set a value that we can use with the source property.下面的代码将匹配platform_family['memory']['total']并设置一个我们可以与source属性一起使用的值。

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.请注意,我在case语句的when条件中使用三元运算符作为if / else的替代方法。

If you're only ever going to use this recipe in Linux, then this method will work:如果你只打算在 Linux 中使用这个秘籍,那么这个方法会起作用:

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:因此,如果您想支持任何系统平台(以防万一这对不使用 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM