Class: Chopshop::Logreader::Executor
- Inherits:
-
Object
- Object
- Chopshop::Logreader::Executor
- Defined in:
- lib/chopshop/logreader/executor.rb
Constant Summary collapse
- REGEX =
The regex + time calculator takes the human readable output from the kubernetes CLI and calculates how long any given pod has been running. It then selects the most recently pod/shortest living pod
/(?<t1>\d+)(?<v1>[a-z]+)(?<t2>\d*)(?<v2>[a-z]*)/i- TIME_CALCULATOR =
{ "s" => 1, "m" => 60, "h" => 60 * 60, "d" => 60 * 60 * 24, "" => 0 # protect for the scenario where a value isn't present in the capture group. }
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#service_name ⇒ Object
readonly
Returns the value of attribute service_name.
-
#tenant ⇒ Object
readonly
Returns the value of attribute tenant.
Class Method Summary collapse
Instance Method Summary collapse
- #execute! ⇒ Object
-
#initialize ⇒ Executor
constructor
A new instance of Executor.
- #parse_options ⇒ Object
Constructor Details
#initialize ⇒ Executor
Returns a new instance of Executor.
25 26 27 |
# File 'lib/chopshop/logreader/executor.rb', line 25 def initialize @parser = Chopshop::Logreader::Parser.new end |
Instance Attribute Details
#container ⇒ Object (readonly)
Returns the value of attribute container.
6 7 8 |
# File 'lib/chopshop/logreader/executor.rb', line 6 def container @container end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
6 7 8 |
# File 'lib/chopshop/logreader/executor.rb', line 6 def namespace @namespace end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/chopshop/logreader/executor.rb', line 6 def end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
6 7 8 |
# File 'lib/chopshop/logreader/executor.rb', line 6 def parser @parser end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
6 7 8 |
# File 'lib/chopshop/logreader/executor.rb', line 6 def profile @profile end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
6 7 8 |
# File 'lib/chopshop/logreader/executor.rb', line 6 def region @region end |
#service_name ⇒ Object (readonly)
Returns the value of attribute service_name.
6 7 8 |
# File 'lib/chopshop/logreader/executor.rb', line 6 def service_name @service_name end |
#tenant ⇒ Object (readonly)
Returns the value of attribute tenant.
6 7 8 |
# File 'lib/chopshop/logreader/executor.rb', line 6 def tenant @tenant end |
Class Method Details
.execute! ⇒ Object
21 22 23 |
# File 'lib/chopshop/logreader/executor.rb', line 21 def self.execute! new.execute! end |
Instance Method Details
#execute! ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/chopshop/logreader/executor.rb', line 39 def execute! service = nil # log into the EKS cluster, may require 2FA authing here. `rally-kubectl -a #{region} -e #{profile} -t #{tenant}` puts "looking for valid service container" while !service services = `kubectl get pods --namespace #{namespace} | grep #{service_name}` service = services.split("\n").map {|line| line.split(" ") }.each do |line| # get the length of time the pod has been running from the kubernetes CLI output match_data = REGEX.match(line[4]) # calculate the human readable version of time into a single integer for comparison line[5] = TIME_CALCULATOR[match_data.captures[1].downcase] * match_data.captures[0].to_i + TIME_CALCULATOR[match_data.captures[3].downcase] * match_data.captures[2].to_i # select the most recent/shortest living pod end.select{|line| line[2] == .status }.sort_by {|line| line[5] }.first # if we have a pod we want logs from, then get logs and be done if service if container exec "kubectl logs --follow=#{options.follow} --tail=#{options.lines} --namespace #{namespace} --container=#{container} #{service[0]}" else exec "kubectl logs --follow=#{options.follow} --tail=#{options.lines} --namespace #{namespace} #{service[0]}" end end # no pod with logs, wait 1 second and try again. print "." sleep 1 end end |
#parse_options ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/chopshop/logreader/executor.rb', line 29 def = @parser.parse @profile = .profile || ENV["AWS_PROFILE"] || ENV["PROFILE"] @tenant = .tenant || ENV["DEFAULT_TENANT"] @region = .region || ENV["AWS_REGION"] || "us-east-1" @namespace = .namespace || ENV["K8_NAMESPACE"] || "connect" @container = .container @service_name = ARGV[0] end |