Amazon-Web-Services

廚師:mongodb::user_management 在 aws opsworks 中失敗,出現“NameError:未初始化的常量 Mongo::MongoClient”

  • October 5, 2015

幾個月來,我一直在成功使用 mongodb 食譜創建使用者帳戶,但最近發生了一些變化,我不知道它是什麼。預設配方完成且沒有錯誤,並且成功安裝了 mongodb 伺服器,但是 user_management 配方失敗並出現錯誤NameError: uninitialized constant Mongo::MongoClient

這是在具有 chef 版本 11.10 和 Berkshelf 版本 3.2.0 的 AWS Opsworks 環境中。

伯克斯文件

source "https://supermarket.chef.io"
cookbook 'mongodb', '~> 0.16.1', github: 'edelight/chef-mongodb'

這是我在層中使用的自定義 json 的 mongodb 部分:

"mongodb": {
   "dbconfig_file": "/etc/mongod.conf",
   "sysconfig_file": "/etc/default/mongod",
   "default_init_name": "mongod",
   "instance_name": "mongod",
   "admin": {
       "username": "admin",
       "password": "xxxxx"
   },
   "config": {
       "auth": true,
       "bind_ip": "0.0.0.0",
       "port": "27017",
       "logpath": "/var/log/mongo/mongod.log",
       "dbpath": "/var/lib/mongo"
   },
   "authentication": {
       "username": "admin",
       "password": "xxxxx"
   },
   "users": [
       {
           "username": "mongodb-dba1",
           "password": "xxxxx",
           "roles": [
               "root"
           ],
           "database": "admin"
       },
       {
           "username": "mongodb-rw1",
           "password": "xxxxx",
           "roles": [
               "readWrite"
           ],
           "database": "apis_cache"
       },
       {
           "username": "mongodb-r1",
           "password": "xxxxx",
           "roles": [
               "read"
           ],
           "database": "apis_cache"
       },
       {
           "username": "admin",
           "password": "xxxxx",
           "roles": [
               "userAdminAnyDatabase",
               "dbAdminAnyDatabase"
           ],
           "database": "admin"
       }
   ]
},

這是我執行 mongodb::user_management 配方時的 opsworks 日誌。

[2015-04-14T13:33:31+00:00] INFO: Processing chef_gem[mongo] action install (mongodb::user_management line 1)
[2015-04-14T13:33:32+00:00] WARN: Cloning resource attributes for mongodb_user[admin] from prior resource (CHEF-3694)
[2015-04-14T13:33:32+00:00] WARN: Previous mongodb_user[admin]: /var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/recipes/user_management.rb:14:in `block in from_file'
[2015-04-14T13:33:32+00:00] WARN: Current  mongodb_user[admin]: /var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/recipes/user_management.rb:14:in `block in from_file'
[2015-04-14T13:33:32+00:00] INFO: Processing template[/etc/hosts] action create (opsworks_stack_state_sync::hosts line 3)
[2015-04-14T13:33:32+00:00] INFO: Processing template[/etc/motd.opsworks-static] action create (opsworks_stack_state_sync::motd line 1)
[2015-04-14T13:33:32+00:00] INFO: Processing chef_gem[mongo] action install (mongodb::user_management line 1)
[2015-04-14T13:33:32+00:00] INFO: Processing mongodb_user[admin] action add (mongodb::user_management line 14)

================================================================================
Error executing action `add` on resource 'mongodb_user[admin]'
================================================================================

NameError
---------
uninitialized constant Mongo::MongoClient

Cookbook Trace:
---------------
/var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/providers/user.rb:62:in `retrieve_db'
/var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/providers/user.rb:9:in `add_user'
/var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/providers/user.rb:71:in `block in class_from_file'

Resource Declaration:
---------------------
# In /var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/recipes/user_management.rb

14:   mongodb_user user['username'] do
15:     password user['password']
16:     roles user['roles']
17:     database user['database']
18:     connection node['mongodb']
19:     action :add
20:   end
21: end


Compiled Resource:
------------------
# Declared in /var/lib/aws/opsworks/cache.stage2/cookbooks/mongod/recipes/user_management.rb:14:in `block in from_file'

mongodb_user("admin") do
action [:add]
retries 0
retry_delay 2
cookbook_name "mongodb"
recipe_name "user_management"
password "xxxxx"
roles ["userAdminAnyDatabase", "dbAdminAnyDatabase"]
database "admin"
connection {"client_roles"=>[], "cluster_name"=>nil, "shard_name"=>"default", "replica_arbiter_only"=>false, "replica_build_indexes"=>true, "replica_hidden"=>false, "replica_slave_delay"=>0, "replica_priority"=>1, "replica_tags"=>{}, "replica_votes"=>1, "auto_configure"=>{"replicaset"=>true, "sharding"=>true}, "configserver_url"=>nil, "root_group"=>"root", "user"=>"mongodb", "group"=>"mongodb", "init_dir"=>"/etc/init/", "init_script_template"=>"debian-mongodb.upstart.erb", "sysconfig_file"=>"/etc/default/mongod", "sysconfig_file_template"=>"mongodb.sysconfig.erb", "dbconfig_file_template"=>"mongodb.conf.erb", "dbconfig_file"=>"/etc/mongod.conf", "package_name"=>"mongodb", "package_version"=>nil, "default_init_name"=>"mongod", "instance_name"=>"mongod", "install_method"=>"distro", "is_replicaset"=>nil, "is_shard"=>nil, "is_configserver"=>nil, "reload_action"=>"restart", "apt_repo"=>"ubuntu-upstart", "template_cookbook"=>"mongodb", "key_file_content"=>nil, "ruby_gems"=>{"mongo"=>nil, "bson_ext"=>nil}, "config"=>{"port"=>"27017", "bind_ip"=>"0.0.0.0", "logpath"=>"/var/log/mongo/mongod.log", "logappend"=>true, "fork"=>false, "dbpath"=>"/var/lib/mongo", "nojournal"=>false, "rest"=>false, "smallfiles"=>false, "oplogSize"=>nil, "replSet"=>nil, "auth"=>true}, "mms_agent"=>{"api_key"=>nil, "package_url"=>"https://mms.mongodb.com/download/agent/%{agent_type}/mongodb-mms-%{agent_type}-agent", "monitoring"=>{"version"=>"2.2.0.70-1", "mmsApiKey"=>nil, "mmsBaseUrl"=>"https://mms.mongodb.com", "configCollectionsEnabled"=>true, "configDatabasesEnabled"=>true, "throttlePassesShardChunkCounts"=>10, "throttlePassesDbstats"=>20, "throttlePassesOplog"=>10, "disableProfileDataCollection"=>false, "disableGetLogsDataCollection"=>false, "disableLocksAndRecordStatsDataCollection"=>false, "enableMunin"=>true, "useSslForAllConnections"=>false, "sslRequireValidServerCertificates"=>false}, "backup"=>{"version"=>"2.1.0.106-1", "mmsApiKey"=>nil, "mothership"=>"api-backup.mongodb.com", "https"=>true, "sslRequireValidServerCertificates"=>false}, "user"=>"mongodb-mms-agent", "group"=>"mongodb-mms-agent"}, "sysconfig"=>{"DAEMON"=>"/usr/bin/$NAME", "DAEMON_USER"=>"mongodb", "DAEMON_OPTS"=>"--config /etc/mongod.conf", "CONFIGFILE"=>"/etc/mongod.conf", "ENABLE_MONGODB"=>"yes", "DAEMONUSER"=>"mongodb", "ENABLE_MONGOD"=>"yes", "ENABLE_MONGO"=>"yes"}, "ulimit"=>{"fsize"=>"unlimited", "cpu"=>"unlimited", "as"=>"unlimited", "nofile"=>64000, "rss"=>"unlimited", "nproc"=>32000}, "admin"=>{"username"=>"admin", "password"=>"xxxxx", "roles"=>["userAdminAnyDatabase", "dbAdminAnyDatabase"], "database"=>"admin"}, "users"=>[{"username"=>"mongodb-dba1", "password"=>"xxxxx", "roles"=>["root"], "database"=>"admin"}, {"username"=>"mongodb-rw1", "password"=>"xxxxx", "roles"=>["readWrite"], "database"=>"apis_cache"}, {"username"=>"mongodb-r1", "password"=>"xxxxx", "roles"=>["read"], "database"=>"apis_cache"}, {"username"=>"admin", "password"=>"xxxxx", "roles"=>["userAdminAnyDatabase", "dbAdminAnyDatabase"], "database"=>"admin"}], "save-state"=>"false", "restore-state"=>"false", "monit"=>"true", "pid_file"=>"/var/lib/mongo/mongod.lock", "start_command"=>"/usr/bin/env service mongod start", "stop_command"=>"/usr/bin/env service mongod stop", "authentication"=>{"username"=>"admin", "password"=>"xxxxx"}}
username "admin"
end

[2015-04-14T13:33:32+00:00] INFO: Running queued delayed notifications before re-raising exception
[2015-04-14T13:33:32+00:00] ERROR: Running exception handlers
[2015-04-14T13:33:32+00:00] ERROR: Exception handlers complete
[2015-04-14T13:33:32+00:00] FATAL: Stacktrace dumped to /var/lib/aws/opsworks/cache.stage2/chef-stacktrace.out
[2015-04-14T13:33:32+00:00] ERROR: mongodb_user[admin] (mongodb::user_management line 14) had an error: NameError: uninitialized constant Mongo::MongoClient
[2015-04-14T13:33:32+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

更新:

我在最新送出的 chef.io cookbook 更新為“悲觀地允許 1.X mongo gem”時遇到了同樣的錯誤:

伯克斯文件

source "https://supermarket.chef.io"
cookbook 'mongodb', git: 'https://github.com/bellpeterm/chef-mongodb.git', ref: '45cdb7177a5f6e9b59dea2ec8e057321114e8a3c'

Opsworks 日誌:

[2015-04-14T19:19:07+00:00] INFO: Processing chef_gem[mongo] action install (mongodb::mongo_gem line 20)
[2015-04-14T19:19:07+00:00] INFO: Installing chef-gem mongo ~> 1.12
[2015-04-14T19:19:16+00:00] INFO: Successfully installed bson-1.12.1
Successfully installed mongo-1.12.1
2 gems installed

[2015-04-14T19:19:16+00:00] INFO: Processing chef_gem[bson_ext] action install (mongodb::mongo_gem line 20)
[2015-04-14T19:19:19+00:00] INFO: Installing chef-gem bson_ext = 1.12.1
[2015-04-14T19:19:24+00:00] INFO: Building native extensions.  This could take a while...
Successfully installed bson_ext-1.12.1
1 gem installed

[2015-04-14T19:19:24+00:00] WARN: Cloning resource attributes for mongodb_user[admin] from prior resource (CHEF-3694)
[2015-04-14T19:19:24+00:00] WARN: Previous mongodb_user[admin]: /var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/recipes/user_management.rb:14:in `block in from_file'
[2015-04-14T19:19:24+00:00] WARN: Current  mongodb_user[admin]: /var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/recipes/user_management.rb:14:in `block in from_file'
[2015-04-14T19:19:24+00:00] INFO: Processing template[/etc/hosts] action create (opsworks_stack_state_sync::hosts line 3)
[2015-04-14T19:19:24+00:00] INFO: Processing template[/etc/motd.opsworks-static] action create (opsworks_stack_state_sync::motd line 1)
[2015-04-14T19:19:24+00:00] INFO: Processing package[gcc] action nothing (mongodb::mongo_gem line 3)
[2015-04-14T19:19:24+00:00] INFO: Processing package[libsasl2-dev] action nothing (mongodb::mongo_gem line 15)
[2015-04-14T19:19:24+00:00] INFO: Processing chef_gem[mongo] action install (mongodb::mongo_gem line 20)
[2015-04-14T19:19:24+00:00] INFO: Processing chef_gem[bson_ext] action install (mongodb::mongo_gem line 20)
[2015-04-14T19:19:24+00:00] INFO: Processing mongodb_user[admin] action add (mongodb::user_management line 14)

================================================================================
Error executing action `add` on resource 'mongodb_user[admin]'
================================================================================


NameError
---------
uninitialized constant Mongo::MongoClient


Cookbook Trace:
---------------
/var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/providers/user.rb:62:in `retrieve_db'
/var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/providers/user.rb:9:in `add_user'
/var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/providers/user.rb:71:in `block in class_from_file'


Resource Declaration:
---------------------
# In /var/lib/aws/opsworks/cache.stage2/cookbooks/mongodb/recipes/user_management.rb

14:   mongodb_user user['username'] do
15:     password user['password']
16:     roles user['roles']
17:     database user['database']
18:     connection node['mongodb']
19:     action :add
20:   end
21: end

謝謝你的幫助!

根據我在拉取請求384中看到的修改,我修改了食譜以將 ‘mongo’ 和 ‘bson_ext’ 版本都鎖定為 1.x,並為我修復了它(悲觀地允許 1.x 不起作用)。

屬性/default.rb

# install the mongo and bson_ext ruby gems at compile time to make them globally available
# TODO: remove bson_ext once mongo gem supports bson >= 2
default['mongodb']['ruby_gems'] = {
- :mongo => nil,
- :bson_ext => nil
+ :mongo => '1.12',
+ :bson_ext => '1.12'
}

食譜/user_management.rb

-chef_gem 'mongo'
+include_recipe 'mongodb::mongo_gem'
users = []
admin = node['mongodb']['admin']

修改後的食譜可在此處獲得。請注意,由於 gem 被鎖定在特定版本,這不是永久修復。

希望官方食譜的維護者能有時間趕上所有積壓的 PR。

更新(2015 年 10 月 5 日):chef-brigade 勇敢地維護了這本食譜,並開始解決 PR 的積壓問題。您可以從chef-brigade repo 獲取更新的食譜。

引用自:https://serverfault.com/questions/682599