Module: Chef::Goiardi::Reporting::KnifeHelpers

Included in:
Knife::GoiardiRunsList, Knife::GoiardiRunsShow
Defined in:
lib/chef/goiardi/reporting/knife_helpers.rb

Constant Summary collapse

SECONDS_IN_24HOURS =

Need the unless guard b/c this code is dynamically loaded and can result in ruby warnings if it attempts to define the constant again

86400
SECONDS_IN_3MONTHS =

Approximate, b/c of course the length of a month can vary

7889230

Instance Method Summary collapse

Instance Method Details

#apply_time_argsObject



38
39
40
41
42
43
44
45
46
# File 'lib/chef/goiardi/reporting/knife_helpers.rb', line 38

def apply_time_args()
  if config[:start_time] && config[:end_time]
    start_time, end_time = convert_to_unix_timestamps()
  else
    start_time, end_time = last_24hours_time_window()
  end

  return start_time, end_time
end

#check_3month_window(start_time, end_time) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/chef/goiardi/reporting/knife_helpers.rb', line 85

def check_3month_window(start_time, end_time)
  # start_time and end_time are unix timestamps
  if (end_time - start_time) > SECONDS_IN_3MONTHS
    ui.error("Requesting information for more than three months at a time is disallowed. Please try a smaller timeframe.")
    exit 1
  end
end

#check_start_and_end_times_providedObject



55
56
57
58
59
60
61
62
63
# File 'lib/chef/goiardi/reporting/knife_helpers.rb', line 55

def check_start_and_end_times_provided()
  if config[:start_time] && !config[:end_time]
    ui.error("The start_time option was provided, but the end_time option was not. If one is provided, the other is required.")
    exit 1
  elsif config[:end_time] && !config[:start_time]
    ui.error("The end_time option was provided, but the start_time option was not. If one is provided, the other is required.")
    exit 1
  end
end

#convert_to_unix_timestampsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chef/goiardi/reporting/knife_helpers.rb', line 65

def convert_to_unix_timestamps()
  if config[:unix_timestamps]
    start_time = config[:start_time].to_i
    end_time = config[:end_time].to_i
  else
    # Take user supplied input, assumes it is in a valid date format,
    # convert to a date object to ensure we have the proper date format for
    # passing to the time object (but converting is not a validation step,
    # so bad user input will still be bad)
    # then convert to a time object, and then convert to a unix timestamp
    # An error could potentially be thrown if the conversions don't work
    # This does work on windows - to_i on time even on windows still returns a unix timestamp
    # Verified on ruby 1.9.3 on a windows 2000 ami on aws
    start_time = Time.parse(Date.strptime(config[:start_time], '%m-%d-%Y').to_s).to_i
    end_time = Time.parse(Date.strptime(config[:end_time], '%m-%d-%Y').to_s).to_i
  end

  return start_time, end_time
end

#last_24hours_time_windowObject



48
49
50
51
52
53
# File 'lib/chef/goiardi/reporting/knife_helpers.rb', line 48

def last_24hours_time_window()
  # Time is calculated as a unix timestamp
  end_time = Time.now.to_i
  start_time = end_time - SECONDS_IN_24HOURS
  return start_time, end_time
end

#uuid?(run_id) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/chef/goiardi/reporting/knife_helpers.rb', line 30

def uuid?(run_id)
  if run_id =~ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/
    return false
  else
    return true
  end
end