2017-03-06 161 views
1

這是我的MongoDB的菜譜食譜:廚師:延遲服務通知

node.default['mongodb3']['version'] = '3.4.2' 
node.default['mongodb3']['repo'] = 'https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/' 

node.default['mongodb3']['config']['mongod']['net']['port'] = 30158 
node.default['mongodb3']['config']['mongod']['net']['bindIp'] = 'localhost' 
node.default['mongodb3']['config']['mongod']['security']['authorization'] = 'enabled' 

include_recipe 'mongodb3::default' 

cookbook_file "/tmp/setupUsers.js" do 
    source "mongo/setupUsers.js" 
    mode 0755 
end 

execute "Add Mongo Users" do 
    command "mongo localhost:30158 /tmp/setupUsers.js" 
end 

正如你可以看到:

  1. 我安裝蒙戈3.4和
  2. 我試圖執行mongo localhost:30158 /tmp/setupUsers.js

    ==> default: * execute[Add Mongo Users] action run 
    ==> default: 
    ==> default:  [execute] MongoDB shell version v3.4.2 
    ==> default:    connecting to: localhost:30158 
    ==> default:    2017-03-06T10:56:56.875+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:30158, in(checking socket 
    for error after poll), reason: Connection refused 
    ==> default:    2017-03-06T10:56:56.879+0000 E QUERY [thread1] Error: couldn't connect to server localhost:30158, connec 
    tion attempt failed : 
    ==> default:    [email protected]/mongo/shell/mongo.js:237:13 
    ==> default:    @(connect):1:6 
    ==> default:    exception: connect failed 
    ==> default: 
    ==> default:  ================================================================================ 
    ==> default:  Error executing action `run` on resource 'execute[Add Mongo Users]' 
    ==> default:  ================================================================================ 
    ==> default: 
    ==> default:  Mixlib::ShellOut::ShellCommandFailed 
    ==> default:  ------------------------------------ 
    ==> default:  Expected process to exit with [0], but received '1' 
    ==> default:  ---- Begin output of mongo localhost:30158 /tmp/setupUsers.js ---- 
    ==> default:  STDOUT: MongoDB shell version v3.4.2 
    ==> default:  connecting to: localhost:30158 
    ==> default:  2017-03-06T10:56:56.875+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:30158, in(checking socket for error 
    after poll), reason: Connection refused 
    ==> default:  2017-03-06T10:56:56.879+0000 E QUERY [thread1] Error: couldn't connect to server localhost:30158, connection attem 
    pt failed : 
    ==> default:  [email protected]/mongo/shell/mongo.js:237:13 
    ==> default:  @(connect):1:6 
    ==> default:  STDERR: exception: connect failed 
    ==> default:  ---- End output of mongo localhost:30158 /tmp/setupUsers.js ---- 
    ==> default:  Ran mongo localhost:30158 /tmp/setupUsers.js returned 1 
    ==> default: 
    ==> default:  Resource Declaration: 
    ==> default:  --------------------- 
    ==> default:  # In /var/chef/cache/cookbooks/berk/recipes/security.rb 
    ==> default: 
    ==> default:  6: execute "Add Mongo Users" do 
    ==> default:  7: command "mongo localhost:30158 /tmp/setupUsers.js" 
    ==> default:  8: end 
    ==> default: 
    ==> default:  Compiled Resource: 
    ==> default:  ------------------ 
    ==> default:  # Declared in /var/chef/cache/cookbooks/berk/recipes/security.rb:6:in `from_file' 
    ==> default: 
    ==> default:  execute("Add Mongo Users") do 
    ==> default:  action [:run] 
    ==> default:  retries 0 
    ==> default:  retry_delay 2 
    ==> default:  default_guard_interpreter :execute 
    ==> default:  command "mongo localhost:30158 /tmp/setupUsers.js" 
    ==> default:  backup 5 
    ==> default:  returns 0 
    ==> default:  user nil 
    ==> default:  declared_type :execute 
    ==> default:  cookbook_name "berk" 
    ==> default:  recipe_name "security" 
    ==> default:  end 
    ==> default: 
    ==> default:  Platform: 
    ==> default:  --------- 
    ==> default:  x86_64-linux 
    ==> default: 
    

    正如你所看到的,這似乎mongod的服務未在輸出信息的末尾還沒有運行,不過,師傅告訴:

不過,我從廚師收到此消息我試圖重新啓動延遲服務通知。

==> default: [2017-03-06T10:56:56+00:00] INFO: Running queued delayed notifications before re-raising exception 
==> default: [2017-03-06T10:56:56+00:00] INFO: template[/etc/mongod.conf] sending restart action to service[mongod] (delayed) 
==> default: Recipe: mongodb3::default 
==> default: * service[mongod] action restart 
==> default: [2017-03-06T10:56:57+00:00] INFO: service[mongod] restarted 
==> default: 
==> default:  - restart service service[mongod] 
==> default: [2017-03-06T10:56:57+00:00] INFO: template[/opt/wildfly/standalone/configuration/standalone-full.xml] sending restart act 
ion to service[wildfly] (delayed) 

爲什麼直到廚師配置結束後才能啓動此服務?我需要在mongod服務啓動後連接到mongo

我看了一下mongodb3的食譜。 According to this line(mongodb3配方的默認配方),服務應立即開始。

編輯

我使用mongodb3配方。 mongodb3默認食譜:

service 'mongod' do 
    case node['platform'] 
    when 'ubuntu' 
     if node['platform_version'].to_f >= 15.04 
     provider Chef::Provider::Service::Systemd 
     elsif node['platform_version'].to_f >= 14.04 
     provider Chef::Provider::Service::Upstart 
     end 
    end 
    supports :start => true, :stop => true, :restart => true, :status => true 
    action :enable 
    subscribes :restart, "template[#{node['mongodb3']['mongod']['config_file']}]", :delayed 
    subscribes :restart, "template[#{node['mongodb3']['config']['mongod']['security']['keyFile']}", :delayed 
end 

回答

2

我沒有看到service[mongod]在你的食譜任何地方,但我想你了以下行某處:

service 'mongod' do 
    action [:enable, :start] 
end 

此時廚師啓動該服務,但它可能需要一些時間才能完全運行並響應請求。廚師不會等待,並繼續運行食譜。

你可以嘗試execute[Add Mongo Users]幾次,直到成功爲止是這樣的:

execute "Add Mongo Users" do 
    command "mongo localhost:30158 /tmp/setupUsers.js" 
    retries 6 #times 
    retry_delay 10 #seconds 
end 

這會給你的服務一分鐘開始,前廚師將失敗。

重要提示:你也應該想出一些防範execute[Add Mongo Users],因爲否則它會運行在每個廚師跑。

編輯(後顯示service[mongod]資源。)

它不具備開始行動,這就是爲什麼它沒有啓動。所以加上

service 'mongod' do 
    action :start 
end 

execute[Add Mongo Users]之前的某處資源。

+0

謝謝@Draco。爲了添加一些細節,我在後面添加了'mongodb3 cookbook'默認配方執行的代碼。在服務開始之前,我覺得有點難以等待。有什麼方法可以訂閱,直到啓動過程結束?爲什麼mongod服務啓動過程延遲到Chef啓動結束? – Jordi

+0

的'服務[mongod的]'你引用,只有_enable_作用,所以它只是確保該服務在引導時啓動,但它不會立即啓動它。註釋(線137)https://github.com/sunggun-yu/chef-mongodb3/blob/master/recipes/default.rb#L137顯然是錯誤的。 –

+0

那麼,爲什麼mongod啓動? – Jordi