[chef] Re: Re: Re: Re: Fwd: Unable to use data bag


Chronological Thread 
  • From: Russell Bateman < >
  • To:
  • Subject: [chef] Re: Re: Re: Re: Fwd: Unable to use data bag
  • Date: Mon, 30 Sep 2013 12:47:44 -0600

It appears that
bag = search( :rollups, which ).first

returns the bag itself such that
replSet_name = bag[ :name ]

then returns exactly what I'm after, i.e.: "uas-replica".

Thanks, guys, this helps tremendously!

Russ


On 9/30/2013 12:22 PM, Russell Bateman wrote:
" type="cite"> Great stuff! This pp thing is very useful. However, as a Ruby neanderthal, I have to ask (and think the answer will bring instant light to my brain), when bag is:
[{"data_bag_item('rollups', 'uas-replicaset-1')"=>
   {"description"=>"Shard 1 replica set",
    "id"=>"uas-replicaset-1",
    "replica_1"=>
     {"hostname"=>"16.86.193.117", "port"=>37017, "node"=>"uas-dev-db01"},
    "replica_2"=>
     {"hostname"=>"16.86.193.118", "port"=>37018, "node"=>"uas-dev-db02"},
    "replica_3"=>
     {"hostname"=>"16.86.193.119", "port"=>37019, "node"=>"uas-dev-db03"},
    "name"=>"uas-replica",
    "chef_type"=>"data_bag_item",
    "data_bag"=>"rollups"}}]

...how I can get what I am (obviously, I hope) trying to get with the following statement:

replSet_name = bag[ :name ]

I think if I understood how to get my replica set name from bag, that all the other relationships would dawn on me.

Thanks for your forebearance.

Russ

On 9/30/2013 12:05 PM, Cassiano Leal wrote:
" type="cite">
Yep. So the catch here is that the search will always return an Array of all the items that matched the search criteria.

If the only item matching is your one data bag, it will return

[
  {
    “id”: “uas-replicaset-1”,
    …
  }
]

In which case calling .first() on it will have the desired result as it will return only the first (and in this case, the only one) item of the Array.

Problems might arise if your search returns more than one item, in which case you’d be discarding all but the first.

Daniel’s tip of using pp() to debug this kind of problem is very useful.

On Monday, September 30, 2013 at 14:56, Russell Bateman wrote:

Thank you for replying. What I want back, whether that's what I'm asking for or not, is the entire bag, data_bags/rollups/uas-replicaset-1.json:
{
    "id"          : "uas-replicaset-1",
    "name"        : "uas-replica",
    "description" : "Shard 1 replica set",
    "replica_1"   : { "hostname" : "16.86.193.117", "port" : 37017, "node" : "uas-dev-db01" },
    "replica_2"   : { "hostname" : "16.86.193.118", "port" : 37018, "node" : "uas-dev-db02" },
    "replica_3"   : { "hostname" : "16.86.193.119", "port" : 37019, "node" : "uas-dev-db03" }
}

I need all of this information during my recipe. What must I write in my recipe code to cause that to happen, that is, to cause bag to point to the Ruby construct created from the JSON above?



On 9/30/2013 11:43 AM, Cassiano Leal wrote:
Hey,

The error you’re getting is because when you do 

bag   = search( :rollups, which )  

the bag object is an Array, not a Hash.

When you try to do 
replSet_name = bag[ :name ]
That’s where the problem arises. You’re trying to reference an Array item (which is number-indexed) through a Symbol (:name).

If you’re sure that the search will only return one item, you can do instead: 

bag   = search( :rollups, which ).first

--
Cassiano Leal

On Monday, September 30, 2013 at 14:34, Russell Bateman wrote:

I'm just striking out when I try to use a data bag from my recipe. It looks very simple to me, so I'm guessing there's just something very fundamental I don't get. I'm unable to mimic the opscode/Chef examples in my code. I would greatly appreciate understanding what's wrong with how I'm trying to consume the data bag.

Thanks!

I have my own Chef server set up and a number of nodes. I have had a lot of success  so far, but data bags aren't working for me.

Here's the top of my recipe; I've put some print statements in:
which = "id:" + node[ :rollup ]    # i.e.: "uas-replicaset-1"
puts "which", which
bag   = search( :rollups, which )  # TROUBLE HERE!!!!!!

puts "bag", bag

port = node[ :port ]
replSet_name = bag[ :name ]

# Copy the upstart configuration file to /etc/init...
template "/etc/init/mongodb.conf" do
  source "replica-upstart.conf.erb"
  owner "root"
  group "root"
  mode 00644           # -rw-r--r--
end

template "/data/mongodb/mongodb.conf" do
  source "replica.conf.erb"
  owner "mongodb"
  group "mongodb"
  mode 00644           # -rw-r--r--
  variables( {
      :port => port,
      :replSet => replSet_name
      }
  )
  notifies :restart, "service[ $ip_address ]", :immediately
end

Here's what's in nodes/uas-dev-db01.json:
{
    "normal": { "port" : 27017, "rollup" : "uas-replicaset-1" },
    "name": "uas-dev-db01",
    "override": { },
    .
    .
    .

Here's the data bag, data_bags/rollups/uas-replicaset-1.json:
{
    "id"          : "uas-replicaset-1",
    "name"        : "uas-replica",
    "description" : "Shard 1 replica set",
    "replica_1"   : { "hostname" : "16.86.193.117", "port" : 37017, "node" : "uas-dev-db01" },
    "replica_2"   : { "hostname" : "16.86.193.118", "port" : 37018, "node" : "uas-dev-db02" },
    "replica_3"   : { "hostname" : "16.86.193.119", "port" : 37019, "node" : "uas-dev-db03" }
}


Here's the output running
chef-client.

.
.
.
Compiling Cookbooks...
16.86.192.117
[2013-09-30T17:04:53+00:00] INFO: AptPreference light-weight provider already initialized -- overriding!
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/provider/lwrp_base.rb:97: warning: already initialized constant AptPreference
[2013-09-30T17:04:53+00:00] INFO: AptRepository light-weight provider already initialized -- overriding!
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/provider/lwrp_base.rb:97: warning: already initialized constant AptRepository
[2013-09-30T17:04:53+00:00] INFO: AptPreference light-weight resource already initialized -- overriding!
[2013-09-30T17:04:53+00:00] INFO: AptRepository light-weight resource already initialized -- overriding!
which
id:uas-replicaset-1
bag
data_bag_item[uas-replicaset-1]     <-------- I don't understand this, but I guess I'll roll with it

================================================================================
Recipe Compile Error in /var/chef/cache/cookbooks/mongodb/recipes/replica.rb
================================================================================

TypeError
---------
can't convert Symbol into Integer

Cookbook Trace:
---------------
  /var/chef/cache/cookbooks/mongodb/recipes/replica.rb:15:in `[]'
  /var/chef/cache/cookbooks/mongodb/recipes/replica.rb:15:in `from_file'

Relevant File Content:
----------------------
/var/chef/cache/cookbooks/mongodb/recipes/replica.rb:

  8:  which = "id:" + node[ :rollup ]           # i.e.: "uas-replicaset-1"
  9:  puts "which", which
 10:  bag   = search( :rollups, which )
 11: 
 12:  puts "bag", bag
 13: 
 14:  port = node[ :port ]
 15>> replSet_name = bag[ :name ]
 16: 
 17:  # Copy the upstart configuration file to /etc/init...
 18:  template "/etc/init/mongodb.conf" do
 19:    source "replica-upstart.conf.erb"
 20:    owner "root"
 21:    group "root"
 22:    mode 00644           # -rw-r--r--
 23:  end
 24: 











Archive powered by MHonArc 2.6.16.

§