Class: Chef::Knife::ReportResource

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/report-resource.rb

Constant Summary collapse

PROTOCOL_VERSION =
'0.1.0'
HEADERS =
{'X-Ops-Reporting-Protocol-Version' => PROTOCOL_VERSION}

Instance Method Summary collapse

Instance Method Details

#end_timeObject

Now is the end!



50
51
52
# File 'lib/chef/knife/report-resource.rb', line 50

def end_time
  Time.now.to_i
end

#restObject



54
55
56
# File 'lib/chef/knife/report-resource.rb', line 54

def rest
  @rest ||= Chef::REST.new(Chef::Config[:chef_server_url])
end

#rest_get(query) ⇒ Object



58
59
60
# File 'lib/chef/knife/report-resource.rb', line 58

def rest_get(query)
  rest.get(query, false, HEADERS)
end

#runObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/knife/report-resource.rb', line 62

def run
  rows = config[:reporting_rows]
  search_query = name_args[0]
  resource_type = name_args[1]
  resource_name = name_args[2]
  resource_updates = []
  node_list = []

  q = Chef::Search::Query.new
  q.search('node', search_query) do |nod|
    node_list << nod
  end

  runs = rest_get("reports/org/runs?from=#{start_time(config[:start_time]).to_i}&until=#{end_time}&rows=#{rows}")
  node_list.each do |n|
    nodes_run_data = runs['run_history'].map do |r|
      [r['run_id'], r['updated_res_count']] if r['node_name'] == n.name
    end.compact

    nodes_run_data.each do |nrd|
      query_string = "reports/org/runs/#{nrd.first}?start=0&rows=#{nrd.last}"

      run_data = rest_get(query_string)
      run_data['run_resources'].each do |rr|
        if rr['type'] == resource_type && rr['name'] == resource_name
          data =  {
            'host' => n['fqdn'],
            'run_id' => nrd.first,
            'end_time' => run_data['run_detail']['end_time']
          }
          resource_updates << data
        end
      end
    end
  end

  if resource_updates.length > 0
    ui.output("#{resource_type}[#{resource_name}] changed in the following runs:")
    resource_updates.each do |ru|
      ui.output "#{ru['host']} #{ru['run_id']} @ #{ru['end_time']}"
    end
    ui.output('Use `knife runs show` with the run UUID to get more info')
  end
end

#start_time(timestamp = nil) ⇒ Object

valid timestamps: 2014-08-19 13:33:36 (will be split and joined w/ ‘T’ to remove the space) 2014-08-19T13:33:36



45
46
47
# File 'lib/chef/knife/report-resource.rb', line 45

def start_time(timestamp = nil)
  timestamp.nil? ? (end_time - 3600).to_i : Time.at(DateTime.iso8601(timestamp.split.join("T")).to_time).to_i
end