Dir.glob没有在同一个厨师 - 客户端运行中显示新设备

时间:2015-07-16 15:27:49

标签: ruby chef

我正在尝试安装一个设备,后来在同一个厨师 - 客户端运行中,我需要Dir.glob('/dev/xvd?')所有设备,但我刚刚安装的设备的文件在下一个厨师运行之前不存在。

为了清楚起见,我的挂载确实发生了,我没有收到任何错误 - 我只是看不到Dir.glob的设备结果,直到下一个厨师 - 客户端运行。

在这种情况下,mount_point = /datadevice_id = /dev/xvdf

mount "#{mount_point}" do                                                        
    device device_id                                                             
    fstype 'ext4'                                                                
    options 'noatime,nobootwait'                                                 
    action [:enable, :mount]                                                     
end      

如果我在使用mount resource后尝试获取所有设备并将其注销,则/dev/xvdf不会出现在列表中。

devices = Dir.glob('/dev/xvd?')                                                                                                                  
devices.each do |device|                                                         
    log "devices available: #{device}"                                         
end 

我的日志输出如此显示。来自安装资源的一行,而我的日志输出中只有一行缺少新设备/dev/xvdf并显示现有设备/dev/xvda

- mount /dev/xvdf to /data
* log[devices available: /dev/xvda] action write

** 更新 **尝试重新加载ohai以获取可用设备。

ohai "reload_filesystem" do                                                      
  action :nothing                                                              
end                                                                              

# now we can enable and mount it and we're done!                                 
mount "#{mount_point}" do                                                        
  device device_id                                                             
  fstype 'ext4'                                                                
  options 'noatime,nobootwait'                                                 
  action [:enable, :mount]                                                     
  notifies :reload, "ohai[reload_filesystem]", :immediately                    
end                                                                              

log "***** testing ohai reload 1 ****"                                           
devices = Dir.glob('/dev/xvd?')                                                  
devcount = devices.count                                                         
log "devices count: #{devcount}"                                                 
devices.each do |d|                                                              
 log "devices available: #{d}"                                                   
end                                                                              

ohai "reload2" do                                                                
    action :reload                                                               
end                                                                              

log "***** testing ohai reload 2 ****"                                           
devices = Dir.glob('/dev/xvd?')                                                  
devcount = devices.count                                                         
log "devices count: #{devcount}"                                                 
devices.each do |d|                                                              
 log "devices available: #{d}"                                                   
end 

**输出**

xxx.xx.x.xxx   * mount[/data] action mount                                       
xxx.xx.x.xxx     - mount /dev/xvdf to /data                                      
xxx.xx.x.xxx   * ohai[reload_filesystem] action reload/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/ohai-8.5.0/lib/ohai/plugins/solaris2/network.rb:57: warning: already initialized constant ETHERNET_ENCAPS
xxx.xx.x.xxx /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/ohai-8.5.0/lib/ohai/plugins/solaris2/network.rb:57: warning: previous definition of ETHERNET_ENCAPS was here
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx     - re-run ohai and merge results into node attributes            
xxx.xx.x.xxx   * log[***** testing ohai reload 1 ****] action write              
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx   * log[devices count: 1] action write                              
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx   * log[devices available: /dev/xvda] action write                  
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx   * ohai[reload2] action reload/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/ohai-8.5.0/lib/ohai/plugins/solaris2/network.rb:57: warning: already initialized constant ETHERNET_ENCAPS
xxx.xx.x.xxx /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/ohai-8.5.0/lib/ohai/plugins/solaris2/network.rb:57: warning: previous definition of ETHERNET_ENCAPS was here
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx     - re-run ohai and merge results into node attributes            
xxx.xx.x.xxx   * log[***** testing ohai reload 2 ****] action write              
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx   * log[devices count: 1] action write                              
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx   * log[devices available: /dev/xvda] action write 

1 个答案:

答案 0 :(得分:1)

你被两次传递See this击中,也称为编译与收敛。

Chef按照特定顺序执行操作,首先编译配方以构建资源集合(编译时),一旦完成,它就会执行每个资源的代码以确保它在所需的位置国家(收敛时间)。

您的Dir.glob在配方中,当它被执行时(在编译时),挂载资源尚未运行且未安装任何内容。

您可以将现有代码嵌入到ruby_block资源中,这样它就会在收敛时和挂载后运行,如果它稍后会出现在配方代码中。

ruby_block 'List available devices' do 
  block do
    devices = Dir.glob('/dev/xvd?')                                                                                                                  
    devices.each do |device|                                                         
      Chef::Log.info("devices available: #{device}")                                        
    end
  end 
end

您无法直接使用ruby_block内的资源,这就是我使用Chef::Log.info的原因,但文档中有相关示例。

每条评论更新:

试试这个:

mount "/data" do 
  source "/dev/xvdf" # do whatever needs to be done there.
end

ohai "reload filsystem" do
  action :reload
  plugin "filesystem"
end

ruby_block "List filesystems" do
  block do
    node['filesystem'].each do |dev,properties|
      Chef::Log.warn("#{dev} is mounted on #{properties['mount']}")
    end
  end
end

这次我使用了警告级别,因此您会看到打印的内容,如果没有尝试使用chef-client -l info