knife-reporting 0.1.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,11 +12,24 @@ optional node name parameter.
12
12
  * `knife runs list` - returns a list of all runs within the organization
13
13
  * `knife runs list bobsnode` - return a list of all runs on "bobsnode"
14
14
 
15
- `knife runs show` returns a detailed list of runs. It has two parameters
16
- node name and run id. run id is optional. Calling `knife runs show` with only
17
- a node name will return a detailed list of runs within that node. Calling
18
- it with both node name and run id will return that specific run.
15
+ These commands default to returning the last 24 hours worth of data.
16
+
17
+ If more than 24 hours worth of data is desired, or if a different time frame
18
+ is desired, the --startime or -s option and --endtime or -e option can be given.
19
+ They specify a starting and ending time to returns runs from. They take an
20
+ argument in the date form of MM-DD-YYYY. If one of these options is specified
21
+ the other must also be specified.
22
+
23
+ The start and end time can be specified as a unix timestamp if the
24
+ --unixtimestamps or -u option is also specified.
25
+
26
+ Note that no more than three months worth of data can be requested at a time.
27
+ If more than three months of data is asked for an error will be returned.
28
+
29
+ User can also specify the number of rows that should be returned with
30
+ the result. This parameter is optional and defaults to 10.
31
+
32
+ `knife runs show` has one parameter run id. It will return that specific run details.
19
33
 
20
- * `knife runs show bobsnode` - returns a detailed list of all runs within that node
21
34
  * `knife runs show bobsnode 30077269-59d0-4283-81f6-8d23cbed3a7a` - returns details
22
35
  about that specific node run
@@ -0,0 +1,73 @@
1
+ module ReportingHelpers
2
+
3
+ # Need the unless guard b/c this code is dynamically loaded
4
+ # and can result in ruby warnings if it attempts to define the
5
+ # constant again
6
+ SECONDS_IN_24HOURS = 86400 unless const_defined?(:SECONDS_IN_24HOURS)
7
+ # Approximate, b/c of course the length of a month can vary
8
+ SECONDS_IN_3MONTHS = 7889230 unless const_defined?(:SECONDS_IN_3MONTHS)
9
+
10
+ def uuid?(run_id)
11
+ if run_id =~ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/
12
+ return false
13
+ else
14
+ return true
15
+ end
16
+ end
17
+
18
+ def apply_time_args()
19
+ if config[:start_time] && config[:end_time]
20
+ start_time, end_time = convert_to_unix_timestamps()
21
+ else
22
+ start_time, end_time = last_24hours_time_window()
23
+ end
24
+
25
+ return start_time, end_time
26
+ end
27
+
28
+ def last_24hours_time_window()
29
+ # Time is calculated as a unix timestamp
30
+ end_time = Time.now.to_i
31
+ start_time = end_time - SECONDS_IN_24HOURS
32
+ return start_time, end_time
33
+ end
34
+
35
+ def check_start_and_end_times_provided()
36
+ if config[:start_time] && !config[:end_time]
37
+ ui.error("The start_time option was provided, but the end_time option was not. If one is provided, the other is required.")
38
+ exit 1
39
+ elsif config[:end_time] && !config[:start_time]
40
+ ui.error("The end_time option was provided, but the start_time option was not. If one is provided, the other is required.")
41
+ exit 1
42
+ end
43
+ end
44
+
45
+ def convert_to_unix_timestamps()
46
+ if config[:unix_timestamps]
47
+ start_time = config[:start_time].to_i
48
+ end_time = config[:end_time].to_i
49
+ else
50
+ # Take user supplied input, assumes it is in a valid date format,
51
+ # convert to a date object to ensure we have the proper date format for
52
+ # passing to the time object (but converting is not a validation step,
53
+ # so bad user input will still be bad)
54
+ # then convert to a time object, and then convert to a unix timestamp
55
+ # An error could potentially be thrown if the conversions don't work
56
+ # This does work on windows - to_i on time even on windows still returns a unix timestamp
57
+ # Verified on ruby 1.9.3 on a windows 2000 ami on aws
58
+ start_time = Time.parse(Date.strptime(config[:start_time], '%m-%d-%Y').to_s).to_i
59
+ end_time = Time.parse(Date.strptime(config[:end_time], '%m-%d-%Y').to_s).to_i
60
+ end
61
+
62
+ return start_time, end_time
63
+ end
64
+
65
+ def check_3month_window(start_time, end_time)
66
+ # start_time and end_time are unix timestamps
67
+ if (end_time - start_time) > SECONDS_IN_3MONTHS
68
+ ui.error("Requesting information for more than three months at a time is disallowed. Please try a smaller timeframe.")
69
+ exit 1
70
+ end
71
+ end
72
+
73
+ end
@@ -1,41 +1,83 @@
1
1
  class Chef
2
2
  class Knife
3
3
  class RunsList < Chef::Knife
4
+
5
+ deps do
6
+ # While Ruby automatically includes some data & time functions in the
7
+ # base class, more advanced data & time functions still required the
8
+ # modules be loaded.
9
+ require 'time'
10
+ require 'date'
11
+ require 'chef/knife/reporting_helpers'
12
+ end
13
+
14
+ include ReportingHelpers
15
+
4
16
  banner "knife runs list [<node name>]"
5
17
 
6
18
  PROTOCOL_VERSION = '0.1.0'
7
19
  HEADERS = {'X-Ops-Reporting-Protocol-Version' => PROTOCOL_VERSION}
8
20
 
21
+ option :start_time,
22
+ :long => '--starttime MM-DD-YYYY',
23
+ :short => '-s MM-DD-YYYY',
24
+ :required => false,
25
+ :description => 'Find runs with a start time great than or equal to the date provided. If the -u option is provided unix timestamps can be given instead.'
26
+
27
+ option :end_time,
28
+ :long => '--endtime MM-DD-YYYY',
29
+ :short => '-e MM-DD-YYYY',
30
+ :required => false,
31
+ :description => 'Find runs with an end time less than or equal to the date provided. If the -u option is provided unix timestamps can be given instead.'
32
+
33
+ option :unix_timestamps,
34
+ :long => '--unixtimestamps',
35
+ :short => '-u',
36
+ :required => false,
37
+ :boolean => true,
38
+ :description => 'Indicates start and end times are given as unix time stamps and not date formats.'
39
+
40
+ option :rows,
41
+ :long => '--rows N',
42
+ :short => '-r N',
43
+ :required => false,
44
+ :description => 'Specifies the rows to be returned from the database. The default is 10.'
45
+
9
46
  def run
10
47
  @rest = Chef::REST.new(Chef::Config[:chef_server_url])
11
48
 
12
49
  node_name = name_args[0]
13
50
 
14
- if node_name
15
- runs = node_history(node_name)
16
- else
17
- runs = org_history
18
- end
51
+ check_start_and_end_times_provided()
52
+ start_time, end_time = apply_time_args()
53
+ check_3month_window(start_time, end_time)
19
54
 
55
+ query_string = generate_query(start_time, end_time, node_name, config[:rows])
56
+ runs = history(query_string)
20
57
 
21
58
  output(runs)
22
59
  end
23
60
 
24
61
  private
25
62
 
26
- def org_history
27
- runs = @rest.get_rest("reports/org/runs", false, HEADERS)
28
-
29
- runs["run_history"].map do |run|
30
- { :run_id => run["run_id"],
31
- :node_name => run["node_name"],
32
- :status => run["status"],
33
- :start_time => run["start_time"] }
63
+ def generate_query(start_time, end_time, node_name = nil, rows = nil)
64
+ if node_name
65
+ if rows
66
+ "reports/nodes/#{node_name}/runs?from=#{start_time}&until=#{end_time}&rows=#{rows}"
67
+ else
68
+ "reports/nodes/#{node_name}/runs?from=#{start_time}&until=#{end_time}"
69
+ end
70
+ else
71
+ if rows
72
+ "reports/org/runs?from=#{start_time}&until=#{end_time}&rows=#{rows}"
73
+ else
74
+ "reports/org/runs?from=#{start_time}&until=#{end_time}"
75
+ end
34
76
  end
35
77
  end
36
78
 
37
- def node_history(node_name)
38
- runs = @rest.get_rest( "reports/nodes/#{node_name}/runs", false, HEADERS )
79
+ def history(query_string)
80
+ runs = @rest.get_rest(query_string, false, HEADERS)
39
81
 
40
82
  runs["run_history"].map do |run|
41
83
  { :run_id => run["run_id"],
@@ -45,8 +87,7 @@ class Chef
45
87
  end
46
88
  end
47
89
 
48
- end
49
90
  end
50
91
  end
51
-
92
+ end
52
93
 
@@ -1,7 +1,19 @@
1
1
  class Chef
2
2
  class Knife
3
3
  class RunsShow < Chef::Knife
4
- banner "knife runs show <node name> [<run id>]"
4
+
5
+ deps do
6
+ # While Ruby automatically includes some data & time functions in the
7
+ # base class, more advanced data & time functions still required the
8
+ # modules be loaded.
9
+ require 'time'
10
+ require 'date'
11
+ require 'chef/knife/reporting_helpers'
12
+ end
13
+
14
+ include ReportingHelpers
15
+
16
+ banner "knife runs show <run id>"
5
17
 
6
18
  PROTOCOL_VERSION = '0.1.0'
7
19
  HEADERS = {'X-Ops-Reporting-Protocol-Version' => PROTOCOL_VERSION}
@@ -9,18 +21,23 @@ class Chef
9
21
  def run
10
22
  rest = Chef::REST.new(Chef::Config[:chef_server_url])
11
23
 
12
- node_name = name_args[0]
13
- run_id = name_args[1]
24
+ run_id = name_args[0]
14
25
 
15
- if node_name.nil?
26
+ if run_id.nil?
16
27
  show_usage
17
28
  exit 1
29
+ elsif uuid?(run_id)
30
+ puts "Run ID should be a Chef Client Run ID, e.g: 11111111-1111-1111-1111-111111111111"
31
+ exit 1
18
32
  end
19
33
 
20
- runs = rest.get_rest( "reports/nodes/#{node_name}/runs/#{run_id}", false, HEADERS )
34
+ query_string = "reports/org/runs/#{run_id}"
35
+
36
+ runs = rest.get(query_string, false, HEADERS)
21
37
 
22
38
  output(runs)
23
39
  end
40
+
24
41
  end
25
42
  end
26
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-reporting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-22 00:00:00.000000000 Z
12
+ date: 2013-12-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mixlib-cli
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &70220669244540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,13 +21,8 @@ dependencies:
21
21
  version: 1.2.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: 1.2.2
30
- description: Knife plugin for OpsCode Reporting
24
+ version_requirements: *70220669244540
25
+ description: Knife plugin for Opscode Reporting
31
26
  email: matthew@opscode.com
32
27
  executables: []
33
28
  extensions: []
@@ -38,6 +33,7 @@ files:
38
33
  - LICENSE
39
34
  - README.md
40
35
  - Rakefile
36
+ - lib/chef/knife/reporting_helpers.rb
41
37
  - lib/chef/knife/runs_list.rb
42
38
  - lib/chef/knife/runs_show.rb
43
39
  homepage: http://www.opscode.com
@@ -60,9 +56,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
56
  version: '0'
61
57
  requirements: []
62
58
  rubyforge_project:
63
- rubygems_version: 1.8.23
59
+ rubygems_version: 1.8.11
64
60
  signing_key:
65
61
  specification_version: 3
66
- summary: Knife plugin for OpsCode Reporting
62
+ summary: Knife plugin for Opscode Reporting
67
63
  test_files: []
68
64
  has_rdoc: false