berkshelf / ridley

A reliable Chef API client with a clean syntax
Other
231 stars 84 forks source link

How to retrieve node last checkin time? #368

Closed qubitrenegade closed 7 years ago

qubitrenegade commented 7 years ago

Hello,

I'm attempting to replicate the functionality of knife status with an API request as we have a need to ingest this information.

However, I can't for the life of me figure out how to obtain the last checkin time of a node.

The following seems to dump all the information about the node, except checkin time:

require 'ridley'
require 'pp'

ridley = Ridley.from_chef_config

pp ridley.node.all
pp ridley.node.all.map { |a| a._attributes_ }
pp ridley.client.all
pp ridley.client.all.map { |a| a._attributes_ }

I'm sure it's something simple I've just overlooked... Any help is greatly appreciated!

(also, could someone explain why my attributes are only accessible via the ._attributes_ method and not via .attributes ?)

qubitrenegade commented 7 years ago

Ah ha, ok, so according to jtimberman blog it looks like what I want is an ohai attribute :ohai_time. I also see there is a chef_attributes method as part of Ridley::NodeObject which appears it should return the node attributes:

A merged hash containing a deep merge of all of the attributes respecting the node attribute precedence level.

However, what I'm seeing is an empty Hashi::Mash returned:

#!/usr/bin/env ruby

require 'pp'
require 'ridley'

ridley = Ridley.from_chef_config
pp ridley.node.all.map { |n| n.chef_attributes }
pp ridley.node.all.map { |n| "attr: #{n.chef_attributes.class}, ohai: #{n.chef_attributes.ohai_time}, #{n.chef_attributes[:ohai_time]}" }

The output of which is:

#[] method.
W, [2017-04-19T15:49:35.386337 #19450]  WARN -- : You are setting a key that conflicts with a built-in method VariaModel::Attributes#default defined in Hash. This can cause unexpected behavior when accessing the key via as a property. You can still access the key via the #[] method.
W, [2017-04-19T15:49:35.386499 #19450]  WARN -- : You are setting a key that conflicts with a built-in method VariaModel::Attributes#default defined in Hash. This can cause unexpected behavior when accessing the key via as a property. You can still access the key via the #[] method.
# ... snip ...
[{},
 {},
# ... snip ...
 {},
 {}]
W, [2017-04-19T15:49:35.490379 #19450]  WARN -- : You are setting a key that conflicts with a built-in method VariaModel::Attributes#default defined in Hash. This can cause unexpected behavior when accessing the key via as a property. You can still access the key via the #[] method.
# ... snip ...
["attr: Hashie::Mash, ohai: , ",
 "attr: Hashie::Mash, ohai: , ",
# ... snip ...
 "attr: Hashie::Mash, ohai: , "]
W, [2017-04-19T15:49:35.709986 #19450]  WARN -- : Terminating task: type=:finalizer, meta={:method_name=>:__shutdown__}, status=:receiving
        Celluloid::TaskFiber backtrace unavailable. Please try `Celluloid.task_class = Celluloid::TaskThread` if you need backtraces here.
W, [2017-04-19T15:49:35.711265 #19450]  WARN -- : Terminating task: type=:finalizer, meta={:method_name=>:__shutdown__}, status=:receiving
        Celluloid::TaskFiber backtrace unavailable. Please try `Celluloid.task_class = Celluloid::TaskThread` if you need backtraces here.
W, [2017-04-19T15:49:35.712332 #19450]  WARN -- : Terminating task: type=:finalizer, meta={:method_name=>:__shutdown__}, status=:receiving
        Celluloid::TaskFiber backtrace unavailable. Please try `Celluloid.task_class = Celluloid::TaskThread` if you need backtraces here.

So I guess the question is am I on the right track here?

If no, how do I access node attributes via Ridley? If yes I'm on the right track, could my issues be related to #345/#365 or #366?

Finally, is the reason _attributes_ is a private method because chef_attributes merges all of the attributes?

Thanks!

thommay commented 7 years ago

Thanks for filing a GitHub Issue.

At Chef, we use GitHub Issues to track bugs and feature requests. Questions like "How do I .... with Chef?" are better answered by our awesome community on the Chef mailing lists, or on StackOverflow. (Please pick only one place to ask your question, however, as it's considered impolite to post the same question to multiple forums.)

You can find information about joining and asking questions on our mailing lists here. On StackOverflow, please tag your question with the "chef" label so that it shows up in this list.

In this instance:

ridley.node.all.map { |n| n.reload; n["automatic"]["ohai_time"] }
[
    [ 0] 1456399147.3776836,
    [ 1] nil,
    [ 2] 1432822847.0788348,
    [ 3] 1432823320.2941875,
    [ 4] nil,
    [ 5] 1435225293.7544727,
    [ 6] 1455812563.5615141,
    [ 7] 1470659613.6041462,
    [ 8] 1428502824.555589,
    [ 9] nil,
    [10] 1471010763.2676988
]

per the docs, you have to reload an object before it's fully populated: https://github.com/berkshelf/ridley#listing

qubitrenegade commented 7 years ago

Hi @thommay ,

Thanks for the reply! I knew it was something super simple I had overlooked.

However, I'll point out that this is buried under the overarching "CRUD" heading, I've probably been over this doc. a half dozen times and missed the "reload" comment. Perhaps calling that out could help others in the future.

And I do appreciate that there is an awesome community, however, when asked in slack this question was met with crickets, so while asking the community is good in theory, sometimes the best place for rather specific questions about a project is the project itself.

Additionally, there are a number of issues I pointed out above that, as someone who's never used Ridley before, you could see how I could conflate the two issues... I mean, the warning even says "This can cause unexpected behavior when accessing the key via as a property."